summaryrefslogtreecommitdiffstats
path: root/mdb/backend.py
diff options
context:
space:
mode:
Diffstat (limited to 'mdb/backend.py')
-rw-r--r--mdb/backend.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/mdb/backend.py b/mdb/backend.py
new file mode 100644
index 0000000..256c836
--- /dev/null
+++ b/mdb/backend.py
@@ -0,0 +1,70 @@
+import time
+
+try:
+ import pigpio
+except:
+ pigpio = None
+
+RX_PIN = 4
+TX_PIN = 17
+
+
+class Backend(object):
+ def __init__(self):
+ pass
+
+ def open(self):
+ pass
+
+ def close(self):
+ pass
+
+ def read(self):
+ return b''
+
+ def write(self, data):
+ pass
+
+
+class DummyBackend(Backend):
+ def read(self):
+ time.sleep(0.005)
+ return b''
+
+
+class RaspiBackend(Backend):
+ def __init__(self, rx_pin=RX_PIN, tx_pin=TX_PIN):
+ self.rx_pin = rx_pin
+ self.tx_pin = tx_pin
+
+ def open(self):
+ self.pi = pigpio.pi()
+ self.pi.wave_clear()
+ self.pi.set_mode(self.tx_pin, pigpio.INPUT)
+
+ try:
+ self.pi.bb_serial_read_close(self.rx_pin)
+ except:
+ pass
+
+ status = self.pi.bb_serial_read_open(self.rx_pin, 9600, 9)
+ if status:
+ raise Exception('Port open failed: %d', status)
+
+ def read(self):
+ _, data = self.pi.bb_serial_read(self.rx_pin)
+ return data
+
+ def write(self, data):
+ self.pi.wave_clear()
+ self.pi.wave_add_serial(self.tx_pin, 9600, data, bb_bits=9, bb_stop=6)
+ wid = self.pi.wave_create()
+
+ self.pi.set_mode(self.tx_pin, pigpio.OUTPUT)
+ self.pi.wave_send_once(wid) # transmit serial data
+
+ while self.pi.wave_tx_busy(): # wait until all data sent
+ pass
+
+ self.pi.wave_delete(wid)
+ self.pi.set_mode(self.tx_pin, pigpio.INPUT)