Add pico display feedback

This commit is contained in:
2026-03-28 12:53:28 +01:00
parent 4da779a125
commit 0bd11e7f3b
4 changed files with 245 additions and 31 deletions

View File

@@ -12,6 +12,48 @@ USER_ID = 0
RATING = 0
BUTTON_SEND = None
RATE_BUTTONS = []
DISPLAY = None
# Helper to update display based on state
def refresh_display(message=None):
if not DISPLAY:
return
DISPLAY.fill(0)
if message:
# Show temporary message (Success, Error, Confirmed)
DISPLAY.text(message, 10, 30)
elif CURRENT_STATE == STATE_USER_ID:
# 1. User ID Mode
DISPLAY.text("Inchometer", 25, 0)
# Display binary representation: _ _ _ _ _
# Bit 4 is leftmost button, Bit 0 is rightmost
binary_str = ""
for i in range(5):
bit_pos = 4 - i
if USER_ID & (1 << bit_pos):
binary_str += "X "
else:
binary_str += "_ "
DISPLAY.text(binary_str.strip(), 30, 30)
elif CURRENT_STATE == STATE_RATING:
# 3. Rating Mode
DISPLAY.text("Select Rating", 10, 0)
if RATING == 0:
rating_text = "_"
else:
rating_text = str(RATING)
# Draw "big" text (simulated with scale if supported, or just centered)
# Standard ssd1306 doesn't have big fonts, so we just center it
DISPLAY.text(rating_text, 60, 30)
DISPLAY.show()
# initialize a specific Pin as Input with a pull-down resistor
def initialize_pin(pin_number:int):
@@ -41,56 +83,58 @@ def rate_button_pressed(pin:Pin) -> None:
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)})")
print(f"Current User ID: {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
refresh_display()
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}")
# 2. Confirmed ID display
refresh_display(f"Confirmed: {USER_ID}")
time.sleep(1)
CURRENT_STATE = STATE_RATING
print("Please select rating (1-5) and press send.")
refresh_display()
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}")
# 4. Sending / Success / Error
refresh_display("Sending...")
# 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}")
success = wifi_client.send_request(USER_ID, RATING)
if success:
refresh_display("Success")
else:
refresh_display("Error")
time.sleep(5) # Display status for 5 seconds
# Reset for next rating
USER_ID = 0
RATING = 0
CURRENT_STATE = STATE_USER_ID
print("Done. Ready for next User ID input.")
refresh_display()
# start the rating program
def start_rating(rate_button_pins:list, button_send_pin:int) -> None:
global RATE_BUTTONS, BUTTON_SEND
def start_rating(rate_button_pins:list, button_send_pin:int, oled_display=None) -> None:
global RATE_BUTTONS, BUTTON_SEND, DISPLAY
DISPLAY = oled_display
# initialize all pins as buttons and add them to the list
RATE_BUTTONS = []
@@ -102,8 +146,8 @@ def start_rating(rate_button_pins:list, button_send_pin:int) -> None:
# 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.")
print("System started.")
refresh_display()
while True:
time.sleep(1)