control_server.py 799 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/python3
  2. import json
  3. from .common import *
  4. from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
  5. pin_map = {
  6. 87: fwd_pin, # w
  7. 65: left_pin, # a
  8. 83: back_pin, # s
  9. 68: right_pin, # d
  10. 16: hard_pin # shift
  11. }
  12. GPIO.setup(list(pin_map.values()), GPIO.OUT)
  13. class SimpleServer(WebSocket):
  14. def handleMessage(self):
  15. keys = json.loads(self.data)
  16. for (key_code, pin) in pin_map.items():
  17. GPIO.output(pin, key_code in keys)
  18. def handleConnected(self):
  19. print(self.address, 'connected')
  20. def handleClose(self):
  21. print(self.address, 'closed')
  22. GPIO.output(pin_map.values(), GPIO.LOW)
  23. server = SimpleWebSocketServer('', 54321, SimpleServer)
  24. if __name__ == '__main__':
  25. server.serveforever()