Merge pull request #33 from pimoroni/noise
[EVA-2020-02-2.git] / examples / noise-amps-at-freqs.py
CommitLineData
aa747a41
PH
1import ST7735
2from PIL import Image, ImageDraw
3from enviroplus.noise import Noise
4
18c9ae73 5print("""noise-amps-at-freqs.py - Measure amplitude from specific frequency bins
aa747a41 6
18c9ae73
PH
7This example retrieves the median amplitude from 3 user-specified frequency ranges and plots them in Blue, Green and Red on the Enviro+ display.
8
9As you play a continuous rising tone on your phone, you should notice peaks that correspond to the frequency entering each range.
10
11Press Ctrl+C to exit!
12
13""")
aa747a41
PH
14
15noise = Noise()
16
17disp = ST7735.ST7735(
18 port=0,
19 cs=ST7735.BG_SPI_CS_FRONT,
20 dc=9,
21 backlight=12,
22 rotation=90)
23
24disp.begin()
25
26img = Image.new('RGB', (disp.width, disp.height), color=(0, 0, 0))
27draw = ImageDraw.Draw(img)
28
29
30while True:
31 amps = noise.get_amplitudes_at_frequency_ranges([
18c9ae73
PH
32 (100, 200),
33 (500, 600),
34 (1000, 1200)
aa747a41
PH
35 ])
36 amps = [n * 32 for n in amps]
37 img2 = img.copy()
38 draw.rectangle((0, 0, disp.width, disp.height), (0, 0, 0))
39 img.paste(img2, (1, 0))
40 draw.line((0, 0, 0, amps[0]), fill=(0, 0, 255))
41 draw.line((0, 0, 0, amps[1]), fill=(0, 255, 0))
42 draw.line((0, 0, 0, amps[2]), fill=(255, 0, 0))
43
44 disp.display(img)