from machine import Pin, PWM
from neopixel import Neopixel
from time import sleep,sleep_ms
# General settings
VOLUME = 100 # PWM duty cycle
# Define the LED matrix "display"
PIN_MATRIX = 3
N_ROWS = 8
N_COLUMNS = 16
N_MATRIX = N_ROWS*N_COLUMNS
matrix = Neopixel(N_MATRIX, 0, PIN_MATRIX, "GRB")
# Define the environment temperature "gauge"
PIN_GAUGE = 28
N_GAUGE = 8
gauge = Neopixel(N_GAUGE, 1, PIN_GAUGE, "GRB")
# Set up the button for pi pulse
btn = Pin(27, Pin.IN, Pin.PULL_DOWN)
# TODO: debounce with timeout or sth, e.g.
# https://www.elektronik-kompendium.de/sites/raspberry-pi/2612181.htm
def on_pressed(pin):
play_tone(300, 100)
btn.irq(trigger=Pin.IRQ_RISING, handler=on_pressed) # interrupt request when pressed
# Set up the buzzer for playing sound
pwm = PWM(Pin(16))
pwm.duty_u16(0) # turn off initially
def play_tone(freq, duration):
pwm.freq(freq)
pwm.duty_u16(VOLUME)
sleep_ms(duration)
pwm.duty_u16(0)
##
# Main Loop
##
while True:
# Light matrix leds one-by-one
for i in range(N_MATRIX):
matrix.set_pixel(i, (255, 0, 0))
matrix.show()
#play_tone(500 if (i%4)==0 else 300, 100)
sleep_ms(100)
# Increase the gauge every 2 columns of the matrix
if i%(N_ROWS*2) == 0:
gauge.set_pixel(i//(N_ROWS*2), (0, 0, 255))
gauge.show()
# Turn off all LEDs
for i in range(N_MATRIX):
matrix.set_pixel(i, (0, 0, 0))
for i in range(N_GAUGE):
gauge.set_pixel(i, (0, 0, 0))
matrix.show()
gauge.show()
sleep_ms(100)