py9b/py9b/transport/base.py

44 lines
872 B
Python
Raw Normal View History

2018-10-15 12:10:00 +00:00
"""Transport abstract class"""
def checksum(data):
s = 0
for c in data:
2019-10-18 14:11:14 +00:00
s += c
2018-10-15 12:10:00 +00:00
return (s & 0xFFFF) ^ 0xFFFF
2018-12-01 22:24:21 +00:00
2018-10-15 12:10:00 +00:00
class BaseTransport(object):
2018-12-01 22:24:21 +00:00
MOTOR = 0x01
2018-10-15 12:10:00 +00:00
ESC = 0x20
BLE = 0x21
BMS = 0x22
EXTBMS = 0x23
2018-10-15 12:10:00 +00:00
HOST = 0x3E
2018-12-01 22:24:21 +00:00
DeviceNames = { MOTOR : "MOTOR", ESC : "ESC", BLE : "BLE", BMS : "BMS", EXTBMS : "EXTBMS", HOST : "HOST" }
2018-10-15 12:10:00 +00:00
def __init__(self, link):
self.link = link
def recv(self):
raise NotImplementedError()
def send(self, src, dst, cmd, arg, data=""):
2018-10-15 12:10:00 +00:00
raise NotImplementedError()
def execute(self, command):
self.send(command.request)
if not command.has_response:
return True
#TODO: retry ?
rsp = self.recv()
return command.handle_response(rsp)
2018-12-01 22:24:21 +00:00
@staticmethod
def GetDeviceName(dev):
return BaseTransport.DeviceNames.get(dev, "%02X" % (dev))
2018-10-15 12:10:00 +00:00
__all__ = ["checksum", "BaseTransport"]