Created
January 22, 2018 04:30
-
-
Save jeamland/09086479f68aad87ccbb45a1b31c362d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import curses | |
import paho.mqtt.client as mqtt | |
MQTT_SERVER = '127.0.0.1' | |
MQTT_TOPIC = 'lolibot/esp32_4e8854/in' | |
client = mqtt.Client() | |
client.connect(MQTT_SERVER, 1883, 60) | |
client.publish(MQTT_TOPIC, '(led:write 255 0 0)') | |
screen = curses.initscr() | |
screen.addstr("THE FAT CONTROLLER") | |
screen.refresh() | |
curses.noecho() | |
buffer = [] | |
ARROWS = { | |
'A': 'forward', | |
'B': 'reverse', | |
'C': 'right', | |
'D': 'left', | |
} | |
try: | |
while True: | |
key = screen.getkey() | |
if key == 'q': | |
break | |
elif key == ' ': | |
screen.erase() | |
screen.addstr('ALL STOP') | |
screen.refresh() | |
client.publish(MQTT_TOPIC, payload='stop') | |
client.publish(MQTT_TOPIC, '(led:write 0 0 255)') | |
if not buffer and key == '\x1b': | |
buffer.append(key) | |
elif len(buffer) == 1 and key == '[': | |
buffer.append(key) | |
elif len(buffer) == 2: | |
if key in ARROWS: | |
label = ARROWS[key] | |
screen.erase() | |
screen.addstr(f"NOW GOING: {label}") | |
screen.refresh() | |
client.publish(MQTT_TOPIC, payload=label) | |
client.publish(MQTT_TOPIC, '(led:write 0 255 255)') | |
buffer = [] | |
else: | |
buffer = [] | |
except KeyboardInterrupt: | |
pass | |
client.publish(MQTT_TOPIC, payload='stop') | |
client.publish(MQTT_TOPIC, '(led:write 0 255 0)') | |
curses.endwin() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment