Skip to content

Instantly share code, notes, and snippets.

@jeamland
Created January 22, 2018 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeamland/09086479f68aad87ccbb45a1b31c362d to your computer and use it in GitHub Desktop.
Save jeamland/09086479f68aad87ccbb45a1b31c362d to your computer and use it in GitHub Desktop.
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