Sfoglia il codice sorgente

Initial working example using curses

Tankernn 7 anni fa
commit
16202ef70e
4 ha cambiato i file con 158 aggiunte e 0 eliminazioni
  1. 99 0
      server/.gitignore
  2. 0 0
      server/__init__.py
  3. 12 0
      server/common.py
  4. 47 0
      server/control.py

+ 99 - 0
server/.gitignore

@@ -0,0 +1,99 @@
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+.hypothesis/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# pyenv
+.python-version
+
+# celery beat schedule file
+celerybeat-schedule
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/

+ 0 - 0
server/__init__.py


+ 12 - 0
server/common.py

@@ -0,0 +1,12 @@
+try:
+    import RPi.GPIO as GPIO
+except RuntimeError:
+    print("Error importing RPi.GPIO!  This is probably because you need superuser privileges.  You can achieve this by using 'sudo' to run your script")
+
+GPIO.setmode(GPIO.BCM)
+
+right_pin = 25
+left_pin = 23
+hard_pin = 24
+fwd_pin = 17
+back_pin = 18

+ 47 - 0
server/control.py

@@ -0,0 +1,47 @@
+#!/usr/bin/python3
+
+import curses
+import time
+
+from .common import *
+
+pin_map = {
+    'w': fwd_pin,
+    'a': left_pin,
+    's': back_pin,
+    'd': right_pin
+}
+
+all_pins = list(pin_map.values())
+all_pins.append(hard_pin)
+
+def main(stdscr):
+    current_milli_time = lambda: int(round(time.time() * 1000))
+
+    stdscr.nodelay(1)
+
+    lastcommand = current_milli_time()
+    while True:
+        # get keyboard input, returns -1 if none available
+        c = stdscr.getch()
+        if c != -1:
+            # print numeric value
+            stdscr.addstr(str(c) + ' ')
+            stdscr.refresh()
+            # return curser to start position
+            stdscr.move(0, 0)
+            char = chr(c)
+            if char.lower() in pin_map:
+                GPIO.output(pin_map[char.lower()], GPIO.HIGH)
+            if char.isupper():
+                GPIO.output(hard_pin, GPIO.HIGH)
+            elif c == ord('q'):
+                return
+            lastcommand = current_milli_time()
+        elif current_milli_time() - lastcommand > 100:
+            GPIO.output(all_pins, GPIO.LOW)
+
+    GPIO.cleanup()
+
+if __name__ == '__main__':
+    curses.wrapper(main)