Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A noise monitor for the M5Stack Fire device.
from m5stack import *
from m5ui import *
from uiflow import *
import machine
from easyIO import *
import time
import utime
SAMPLE_PERIOD = 50
MIC_PIN = 34
MIN_VOL = 0
MAX_VOL = 1270
COLORS = [
0x008000,0x048300,0x078500,0x0b8800,0x0f8a00,0x138d00,0x188f00,0x1c9200,0x219400,0x259700,0x2a9900,0x2f9c00,0x349e00,0x3aa100,0x3fa300,0x45a600,0x4aa800,0x50ab00,0x56ae00,0x5cb000,0x62b300,0x69b500,0x6fb800,0x76ba00,0x7dbd00,0x84bf00,0x8bc200,0x92c400,0x99c700,0xa1c900,0xa8cc00,0xb0ce00,0xb8d100,0xc0d400,0xc8d600,0xd1d900,0xd9db00,0xded900,0xe0d600,0xe3d200,0xe5ce00,0xe8ca00,0xeac600,0xedc100,0xefbd00,0xf2b800,0xf4b300,0xf7ae00,0xf9a900,0xfca400,
0xfca100,0xfc9e00,0xfc9a00,0xfc9700,0xfc9400,0xfc9100,0xfc8d00,0xfc8a00,0xfc8700,0xfc8400,0xfc8100,0xfc7d00,0xfc7a00,0xfc7700,0xfc7400,0xfc7100,0xfc6d00,0xfc6a00,0xfc6700,0xfc6400,0xfc6000,0xfc5d00,0xfc5a00,0xfc5700,0xfc5400,0xfc5000,0xfc4d00,0xfc4a00,0xfc4700,0xfc4400,0xfc4000,0xfc3d00,0xfc3a00,0xfc3700,0xfc3300,0xfc3000,0xfc2d00,0xfc2a00,0xfc2700,0xfc2300,0xfc2000,0xfc1d00,0xfc1a00,0xfc1700,0xfc1300,0xfc1000,0xfc0d00,0xfc0a00,0xfc0600,0xfc0300,0xfc0000
]
def sample():
t1 = utime.ticks_ms()
min_val = MAX_VOL
max_val = MIN_VOL
while True:
t2 = utime.ticks_ms()
mic_val = analogRead(MIC_PIN)
max_val = max(mic_val, max_val)
min_val = min(mic_val, min_val)
if t2 - t1 > 50:
break
return max_val - min_val
def conv(val, minval, maxval, lower, upper):
return int((val - minval) * (upper - lower) / (maxval - minval) + lower)
def draw_screen(val, min_val, max_val):
"""
"""
global currentBackground
converted_volume = conv(val, MIN_VOL, MAX_VOL, 1, 100)
new_color = currentBackground
if converted_volume > 50:
new_color = lcd.RED
elif converted_volume > 30:
new_color = lcd.ORANGE
else:
new_color = 0x008000
if new_color != currentBackground:
setScreenColor(new_color)
currentBackground = new_color
label0.show()
rst.show()
ack.show()
rgb.setColorAll(COLORS[converted_volume])
curVal.setText(str(converted_volume))
lbl_minval.setText(str(min_val))
lbl_maxval.setText(str(max_val))
# RGB bar
########################################################################
# Disable the speaker
dac0 = machine.DAC(25)
dac0.write(0)
# Prepare
title = M5Title(title="Sind die Kinder zu laut?", fgcolor=lcd.RED, bgcolor=lcd.YELLOW)
setScreenColor(lcd.DARKGREEN)
# Screen is 320x140
#title = M5TextBox
# Get the window size
width, height = lcd.winsize()
# Initialize the label
label0 = M5TextBox(10, 40, "Sind die Kinder zu laut?", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0)
lbl_minval = M5TextBox(10, 100, "Min", lcd.FONT_Default, 0xFFFFFF, rotate=0)
min_val = MAX_VOL
lbl_maxval = M5TextBox(260, 100, "Max", lcd.FONT_Default, 0xFFFFFF, rotate=0)
max_val = MIN_VOL
# Put a reset button label on the bottom
rst = M5TextBox(30, height-30, "Reset", lcd.FONT_Default, 0xFFFFFF)
ack = M5TextBox(width-70, height-30, "OK", lcd.FONT_Default, 0xFFFFFF)
curVal = M5TextBox(int(width/2-40), int(height/2), "Current", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0)
currentBackground = lcd.DARKGREEN
rgb.setColorAll(currentBackground)
while True:
val = sample()
min_val = min(min_val, val)
max_val = max(max_val, val)
draw_screen(val, min_val, max_val)
#time.sleep(1)
machine.idle()
if btnA.wasPressed():
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment