import machine
import time
from ssd1306 import SSD1306_I2C
# Pin Setup
BUTTON_PIN = 14
BUZZER_PIN = 15
# Configure Hardware
button = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_UP)
buzzer = machine.Pin(BUZZER_PIN, machine.Pin.OUT)
i2c = machine.I2C(1, scl=machine.Pin(27), sda=machine.Pin(26), freq=400000)
# Initialize 128x64 OLED Screen
oled = SSD1306_I2C(128, 64, i2c)
# Global Variables
queue_number = 0
last_press_time = 0
def update_display(num):
"""Refreshes text layout on the screen."""
oled.fill(0)
oled.text("QUEUE SYSTEM", 16, 5)
oled.text("----------------", 0, 18)
oled.text("NOW SERVING:", 16, 32)
oled.text(f"TICKET #{num:03d}", 24, 48)
oled.show()
def alert_next_patient(num):
"""Sounds the physical buzzer and sends high-level text for audio generation."""
# Print out Serial window to trigger web app audio engine
print(f"ANNOUNCE: Now serving ticket number {num}")
# Pulse the active buzzer twice
for _ in range(2):
buzzer.value(1)
time.sleep(0.15)
buzzer.value(0)
time.sleep(0.1)
# Display Baseline Initial State
update_display(queue_number)
print("System Active. Press the Button or use the Web Input to call patients.")
while True:
# Debounced button logic
if not button.value():
current_time = time.ticks_ms()
# 300ms debounce buffer time window
if time.ticks_diff(current_time, last_press_time) > 300:
queue_number += 1
update_display(queue_number)
alert_next_patient(queue_number)
last_press_time = current_time
time.sleep(0.05)