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