You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.6 KiB
88 lines
2.6 KiB
7 years ago
|
import threading
|
||
|
|
||
7 years ago
|
import gex
|
||
7 years ago
|
from gex.Client import EventReport
|
||
|
|
||
7 years ago
|
|
||
|
class USART(gex.Unit):
|
||
|
"""
|
||
|
USART
|
||
|
"""
|
||
|
|
||
7 years ago
|
def _init(self):
|
||
|
self.handler_decode = None
|
||
|
self.handler = None
|
||
|
self.buffer = bytearray()
|
||
|
self.rxwaitnum = 0
|
||
|
self.rxdoneSem = threading.Semaphore()
|
||
|
|
||
7 years ago
|
def _type(self):
|
||
|
return 'USART'
|
||
|
|
||
|
def listen(self, handler, decode='utf-8'):
|
||
|
"""
|
||
|
Attach a Rx listener callback.
|
||
|
decode can be: None, 'utf-8', 'ascii' (any valid encoding for bytearray.decode())
|
||
|
None decoding returns bytearray
|
||
7 years ago
|
|
||
|
handler receives args: (bytes, timestamp)
|
||
7 years ago
|
"""
|
||
|
self.handler_decode = decode
|
||
|
self.handler = handler
|
||
|
|
||
|
def write(self, payload, sync=False, confirm=True):
|
||
|
"""
|
||
7 years ago
|
Write bytes. If 'sync' is True, wait for completion. sync implies confirm
|
||
7 years ago
|
"""
|
||
7 years ago
|
|
||
|
if type(payload) is str:
|
||
|
payload = payload.encode('utf-8')
|
||
|
|
||
7 years ago
|
pb = gex.PayloadBuilder()
|
||
|
pb.blob(payload) # payload to write
|
||
|
|
||
7 years ago
|
self._send(0x01 if sync else 0x00, pb.close(), confirm=confirm or sync)
|
||
7 years ago
|
|
||
7 years ago
|
def _on_event(self, evt:EventReport):
|
||
|
if evt.code == 0:
|
||
7 years ago
|
# Data received
|
||
|
if self.handler:
|
||
7 years ago
|
data = evt.payload if self.handler_decode is None \
|
||
|
else evt.payload.decode(self.handler_decode)
|
||
7 years ago
|
|
||
7 years ago
|
self.handler(data, evt.timestamp)
|
||
7 years ago
|
else:
|
||
|
self.buffer.extend(evt.payload)
|
||
|
if len(self.buffer) >= self.rxwaitnum:
|
||
|
self.rxdoneSem.release()
|
||
|
|
||
|
def clear_buffer(self):
|
||
|
self.buffer = bytearray()
|
||
|
|
||
|
def receive(self, nbytes, decode='utf-8', timeout=0.1):
|
||
|
if self.handler is not None:
|
||
|
raise Exception("Can't call .receive() with an async handler registered!")
|
||
|
if len(self.buffer) >= nbytes:
|
||
|
chunk = self.buffer[0:nbytes]
|
||
|
self.buffer = self.buffer[nbytes:] # put the rest back for later...
|
||
|
if decode is not None:
|
||
|
return chunk.decode(decode)
|
||
|
else:
|
||
|
return chunk
|
||
|
|
||
|
self.rxwaitnum = nbytes
|
||
|
self.rxdoneSem.acquire() # claim
|
||
|
|
||
|
# now the event handler releases the sem and we can take it again
|
||
|
suc = self.rxdoneSem.acquire(timeout=timeout)
|
||
|
# and release it back, to get into a defined state
|
||
|
self.rxdoneSem.release()
|
||
|
|
||
|
if not suc:
|
||
|
if len(self.buffer) < nbytes:
|
||
|
raise Exception("Data not Rx in timeout!")
|
||
|
|
||
|
# use the handling code above via recursion
|
||
|
return self.receive(nbytes, decode, timeout)
|
||
|
|