Files
rate_music/pico/rate.py

80 lines
2.0 KiB
Python

from machine import Pin
import wifi_client
import time
RATE_VALUE = 0
BUTTON_SEND = None
RATE_BUTTONS = None
# initialize a specific Pin as Input with an pull-down resistor
def initialize_pin(pin_number:int):
button = Pin(pin_number, Pin.IN, Pin.PULL_DOWN)
return button
# activates send pin
def activate_sendPin() -> None:
BUTTON_SEND.irq(trigger=Pin.IRQ_RISING, handler=sendButton_pressed)
# deactivates send pin
def deactivate_sendPin() -> None:
BUTTON_SEND.irq(handler=None)
# rate button is pressed
def rate_button_pressed(pin:Pin) -> None:
global RATE_VALUE, RATE_BUTTONS, BUTTON_SEND
# get pin id
pin_id = str(pin).split('(')[1].split(',')[0]
pin_id = pin_id[len(pin_id)-1]
RATE_VALUE = pin_id
print(f"Song is rated with: {RATE_VALUE}")
# activate send pin, when rate pin was selected
activate_sendPin()
# Reset button ist pressed
def sendButton_pressed(pin) -> None:
global RATE_VALUE, BUTTON_SEND
# deactivate send pin to prevent multiple sends
deactivate_sendPin()
print('send button pressed')
# send a request to the access point with the selected rate value
wifi_client.send_request(RATE_VALUE)
# start the rating program
# initialize it with the pin list for the rate_buttoms and the pin for the send button
def start_rating(rate_button_pins:list, button_send_pin:int) -> None:
global RATE_BUTTONS, BUTTON_SEND
# create list for all rate buttons
RATE_BUTTONS = [None] * len(rate_button_pins)
# initialize all pins as buttons and add them to the rate_button list
for pin in rate_button_pins:
button = initialize_pin(pin)
button.irq(trigger=Pin.IRQ_RISING, handler=rate_button_pressed)
RATE_BUTTONS[pin] = button
# define pin 6 as send button
# not activate it, to not be able to send before a rate pin is selected
BUTTON_SEND = initialize_pin(button_send_pin)
print(RATE_BUTTONS)
print(BUTTON_SEND)
while True:
pass