Files
rate_music/pico_2026/rate.py
structix 4da779a125 Add pico 2026 update
Vibecoded:
- User binary input of the user id
- confirms with send button
- User input of the rating
- Send button sends a get request to the server
2026-03-27 22:42:43 +01:00

110 lines
3.2 KiB
Python

from machine import Pin
import wifi_client
import time
# State Constants
STATE_USER_ID = 0
STATE_RATING = 1
# Global variables
CURRENT_STATE = STATE_USER_ID
USER_ID = 0
RATING = 0
BUTTON_SEND = None
RATE_BUTTONS = []
# initialize a specific Pin as Input with a 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 USER_ID, RATING, CURRENT_STATE, RATE_BUTTONS
# Find which button index was pressed
button_index = -1
for i, button in enumerate(RATE_BUTTONS):
if button == pin:
button_index = i
break
if button_index == -1:
return
if CURRENT_STATE == STATE_USER_ID:
# Binary input: each button toggles a bit
# Assume rightmost (highest index) is bit 0
bit_pos = len(RATE_BUTTONS) - 1 - button_index
USER_ID ^= (1 << bit_pos)
print(f"Current User ID: {USER_ID} (binary: {bin(USER_ID)})")
elif CURRENT_STATE == STATE_RATING:
# Rating selection (1 to 5/6)
# Assuming the button index + 1 is the rating
RATING = button_index + 1
print(f"Current Rating selected: {RATING}")
# Activate send pin to confirm selection
activate_sendPin()
# Send/Confirm button is pressed
def sendButton_pressed(pin) -> None:
global CURRENT_STATE, USER_ID, RATING
# deactivate send pin to prevent multiple sends
deactivate_sendPin()
if CURRENT_STATE == STATE_USER_ID:
print(f"Confirmed User ID: {USER_ID}")
CURRENT_STATE = STATE_RATING
print("Please select rating (1-5) and press send.")
elif CURRENT_STATE == STATE_RATING:
if RATING == 0:
print("Please select a rating first!")
activate_sendPin()
return
print(f"Confirmed Rating: {RATING}")
print(f"Sending request: User {USER_ID}, Rating {RATING}")
# send a request to the access point
try:
wifi_client.send_request(USER_ID, RATING)
except Exception as e:
print(f"Error sending request: {e}")
# Reset for next rating
USER_ID = 0
RATING = 0
CURRENT_STATE = STATE_USER_ID
print("Done. Ready for next User ID input.")
# start the rating program
def start_rating(rate_button_pins:list, button_send_pin:int) -> None:
global RATE_BUTTONS, BUTTON_SEND
# initialize all pins as buttons and add them to the list
RATE_BUTTONS = []
for pin_num in rate_button_pins:
button = initialize_pin(pin_num)
button.irq(trigger=Pin.IRQ_RISING, handler=rate_button_pressed)
RATE_BUTTONS.append(button)
# initialize send button
BUTTON_SEND = initialize_pin(button_send_pin)
print(f"System started. Pins: {rate_button_pins}, Send: {button_send_pin}")
print("Please enter User ID (binary) and press send.")
while True:
time.sleep(1)