summaryrefslogtreecommitdiffstats
path: root/mdb/device.py
diff options
context:
space:
mode:
authorSergiusz Bazanski <q3k@q3k.org>2020-02-23 10:57:44 +0100
committerSergiusz Bazanski <q3k@q3k.org>2020-02-23 10:59:01 +0100
commit9ea05b5c701ad662d4f5cc2ca27632e7e2ee3140 (patch)
treec25c4fd3e2cd57892f1ef59e347f9f5f147222d7 /mdb/device.py
parent445913037077a7eeb66aeb6c06b731655b6b4c59 (diff)
downloadbitvend-9ea05b5c701ad662d4f5cc2ca27632e7e2ee3140.tar.gz
bitvend-9ea05b5c701ad662d4f5cc2ca27632e7e2ee3140.tar.bz2
bitvend-9ea05b5c701ad662d4f5cc2ca27632e7e2ee3140.tar.xz
bitvend-9ea05b5c701ad662d4f5cc2ca27632e7e2ee3140.zip
hack: pull changes from old rpi
Diffstat (limited to 'mdb/device.py')
-rw-r--r--mdb/device.py33
1 files changed, 24 insertions, 9 deletions
diff --git a/mdb/device.py b/mdb/device.py
index 20ea546..9ea0e6e 100644
--- a/mdb/device.py
+++ b/mdb/device.py
@@ -7,6 +7,12 @@ try:
except ImportError:
import Queue as queue
+try:
+ import cygpio
+except ImportError:
+ raise
+ cygpio = None
+
from mdb.utils import compute_checksum, compute_chk, bcd_decode
from mdb.constants import *
from mdb.backend import RaspiBackend, DummyBackend, pigpio
@@ -18,6 +24,10 @@ class MDBRequest(object):
processed = False
def __init__(self, command):
+ self.reset(command)
+
+ def reset(self, command):
+ self.processed = False
self.timestamp = time.time()
self.command = command
self.data = bytearray()
@@ -41,7 +51,7 @@ class MDBRequest(object):
return False
@property
def ack(self):
- return self.data[-1] == 0x00
+ return len(self.data) and self.data[-1] == 0x00
def __repr__(self):
return '<MDBRequest 0x%02x [%s] chk:%r>' % (
@@ -60,31 +70,36 @@ class MDBDevice(object):
def __init__(self, app=None):
self.logger = logging.getLogger(type(self).__name__)
self.poll_queue = queue.Queue()
- if pigpio:
+ if cygpio:
+ self.backend = cygpio.CythonRaspiBackend()
+ self.logger.warning('Running with FAST CYTHON BACKEND')
+ elif pigpio:
self.backend = RaspiBackend()
else:
self.logger.warning('Running with dummy backend device')
self.backend = DummyBackend()
def initialize(self):
- self.logger.info('Initializing...')
+ self.logger.info('Initializing... %r backend', self.backend)
self.backend.open()
# here should IO / connection initizliation go
def run(self):
self.initialize()
+ self.current_request = MDBRequest(0)
+ self.current_request.processed = True
+
while True:
data = self.backend.read()
for b in range(0, len(data), 2):
if data[b+1]:
- if self.current_request: # and not self.current_request.processed:
- self.logger.debug(self.current_request)
- if self.current_request.processed and self.current_request.ack:
- self.logger.info('Got response: %d',self.current_request.data[-1])
- self.poll_msg = []
+ self.logger.debug(self.current_request)
+ if self.current_request.processed and self.current_request.ack:
+ self.logger.info('Got response: %d',self.current_request.data[-1])
+ self.poll_msg = []
- self.current_request = MDBRequest(data[b])
+ self.current_request.reset(data[b])
self.send_buffer = None
elif self.current_request:
if self.current_request.processed and data[b] == 0xaa and self.send_buffer: