main_backstage
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
from machine import Pin
|
||||
import time
|
||||
import network
|
||||
import socket
|
||||
|
||||
# ============================================================
|
||||
# WLAN ZUR PUPPE
|
||||
# ============================================================
|
||||
SSID = "BABY_PUPPE"
|
||||
PASSWORD = "TheaterBaby2026"
|
||||
PUPPET_IP = "192.168.4.1"
|
||||
UDP_PORT = 4210
|
||||
SHARED_SECRET = "babysecret"
|
||||
|
||||
PING_INTERVAL_MS = 1000
|
||||
SEND_REPEAT_COUNT = 2
|
||||
SEND_REPEAT_DELAY_MS = 20
|
||||
|
||||
# ============================================================
|
||||
# BUTTONS
|
||||
# ============================================================
|
||||
btn_cry = Pin(2, Pin.IN, Pin.PULL_UP)
|
||||
btn_whim = Pin(3, Pin.IN, Pin.PULL_UP)
|
||||
btn_gig = Pin(4, Pin.IN, Pin.PULL_UP)
|
||||
btn_stop = Pin(5, Pin.IN, Pin.PULL_UP)
|
||||
btn_poop = Pin(6, Pin.IN, Pin.PULL_UP)
|
||||
|
||||
led = Pin("LED", Pin.OUT)
|
||||
|
||||
DEBOUNCE_MS = 80
|
||||
last_ms = {
|
||||
"cry": 0,
|
||||
"whim": 0,
|
||||
"gig": 0,
|
||||
"stop": 0,
|
||||
"poop": 0,
|
||||
}
|
||||
|
||||
seq = 0
|
||||
last_ping_ms = 0
|
||||
|
||||
def is_pressed(pin):
|
||||
return pin.value() == 0
|
||||
|
||||
def allow(key):
|
||||
now = time.ticks_ms()
|
||||
if time.ticks_diff(now, last_ms[key]) > DEBOUNCE_MS:
|
||||
last_ms[key] = now
|
||||
return True
|
||||
return False
|
||||
|
||||
# ============================================================
|
||||
# WLAN CONNECT
|
||||
# ============================================================
|
||||
wlan = network.WLAN(network.STA_IF)
|
||||
wlan.active(True)
|
||||
wlan.connect(SSID, PASSWORD)
|
||||
|
||||
timeout = time.ticks_add(time.ticks_ms(), 10000)
|
||||
while not wlan.isconnected():
|
||||
if time.ticks_diff(timeout, time.ticks_ms()) <= 0:
|
||||
break
|
||||
led.toggle()
|
||||
time.sleep_ms(200)
|
||||
|
||||
led.value(1 if wlan.isconnected() else 0)
|
||||
print("STA config:", wlan.ifconfig())
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
def next_seq():
|
||||
global seq
|
||||
seq += 1
|
||||
return seq
|
||||
|
||||
def send_packet(cmd, packet_seq=None):
|
||||
if packet_seq is None:
|
||||
packet_seq = next_seq()
|
||||
|
||||
payload = "{}|{}|{}".format(SHARED_SECRET, packet_seq, cmd)
|
||||
sock.sendto(payload.encode("utf-8"), (PUPPET_IP, UDP_PORT))
|
||||
|
||||
def send_command(cmd):
|
||||
packet_seq = next_seq()
|
||||
|
||||
for _ in range(SEND_REPEAT_COUNT):
|
||||
send_packet(cmd, packet_seq=packet_seq)
|
||||
time.sleep_ms(SEND_REPEAT_DELAY_MS)
|
||||
|
||||
# kurzes LED-Feedback
|
||||
led.off()
|
||||
time.sleep_ms(30)
|
||||
led.on()
|
||||
|
||||
def send_ping():
|
||||
packet_seq = next_seq()
|
||||
send_packet("PING", packet_seq=packet_seq)
|
||||
|
||||
# ============================================================
|
||||
# MAIN LOOP
|
||||
# ============================================================
|
||||
while True:
|
||||
if not wlan.isconnected():
|
||||
led.toggle()
|
||||
time.sleep_ms(200)
|
||||
continue
|
||||
|
||||
# Heartbeat
|
||||
now = time.ticks_ms()
|
||||
if time.ticks_diff(now, last_ping_ms) > PING_INTERVAL_MS:
|
||||
last_ping_ms = now
|
||||
send_ping()
|
||||
led.value(1)
|
||||
|
||||
# STOP zuerst
|
||||
if is_pressed(btn_stop) and allow("stop"):
|
||||
send_command("STOP")
|
||||
|
||||
if is_pressed(btn_cry) and allow("cry"):
|
||||
send_command("CRY")
|
||||
|
||||
if is_pressed(btn_whim) and allow("whim"):
|
||||
send_command("WHIM")
|
||||
|
||||
if is_pressed(btn_gig) and allow("gig"):
|
||||
send_command("GIG")
|
||||
|
||||
if is_pressed(btn_poop) and allow("poop"):
|
||||
send_command("POOP")
|
||||
|
||||
time.sleep_ms(5)
|
||||
Reference in New Issue
Block a user