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.
168 lines
3.8 KiB
168 lines
3.8 KiB
6 years ago
|
#!/usr/bin/env python3
|
||
|
|
||
6 years ago
|
import serial
|
||
|
import struct
|
||
|
|
||
|
PARITY_NONE = 0
|
||
|
PARITY_ODD = 1
|
||
|
PARITY_EVEN = 2
|
||
|
|
||
|
BAUD_TABLE = {
|
||
6 years ago
|
0: 1200,
|
||
|
1: 2400,
|
||
|
2: 4800,
|
||
|
3: 9600,
|
||
|
4: 19200,
|
||
|
5: 38400,
|
||
|
6: 57600,
|
||
|
7: 115200,
|
||
6 years ago
|
}
|
||
|
|
||
|
AIR_DATA_RATE_TABLE = {
|
||
6 years ago
|
0: 300,
|
||
|
1: 1200,
|
||
|
2: 2400,
|
||
|
3: 4800,
|
||
|
4: 9600,
|
||
|
5: 19200,
|
||
|
6: 19200,
|
||
|
7: 19200,
|
||
6 years ago
|
}
|
||
|
|
||
|
TX_MODE_TRANSPARENT = 0
|
||
|
TX_MODE_FIXED = 1
|
||
|
|
||
|
IO_MODE_OPEN_COLLECTOR = 0
|
||
|
IO_MODE_PUSH_PULL = 1
|
||
|
|
||
|
TX_POWER_TABLE = {
|
||
6 years ago
|
0: 30,
|
||
|
1: 27,
|
||
|
2: 24,
|
||
|
3: 21,
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
|
||
6 years ago
|
def least_gte(value, dic):
|
||
6 years ago
|
items = sorted(dic.items(), key=lambda item: item[1])
|
||
|
last_k = None
|
||
|
for k, v in items:
|
||
|
last_k = k
|
||
|
if v >= value:
|
||
|
break
|
||
|
return last_k
|
||
|
|
||
6 years ago
|
|
||
|
class E32_Params:
|
||
6 years ago
|
save: bool
|
||
|
address: int
|
||
|
parity: int
|
||
|
baud: int
|
||
|
air_data_rate: int
|
||
|
channel: int
|
||
|
tx_mode: int
|
||
|
io_mode: int
|
||
|
wake_up_time: int
|
||
|
fec_enabled: bool
|
||
|
tx_power: int
|
||
|
|
||
|
@staticmethod
|
||
|
def default():
|
||
|
p = E32_Params()
|
||
|
p.save = True
|
||
|
p.address = 0
|
||
|
p.parity = PARITY_NONE
|
||
|
p.baud = 9600
|
||
|
p.air_data_rate = 2400
|
||
|
p.channel = 0x17
|
||
|
p.tx_mode = TX_MODE_TRANSPARENT
|
||
|
p.io_mode = IO_MODE_PUSH_PULL
|
||
|
p.wake_up_time = 250
|
||
|
p.fec_enabled = True
|
||
|
p.tx_power = 30
|
||
|
return p
|
||
|
|
||
|
@staticmethod
|
||
|
def unpack(data):
|
||
|
p = E32_Params()
|
||
|
datab = bytes(data)
|
||
|
if len(datab) != 6:
|
||
|
raise Exception('invalid E32_Params data length')
|
||
|
if datab[0] == 0xC0:
|
||
|
p.save = True
|
||
|
elif datab[0] == 0xC2:
|
||
|
p.save = False
|
||
|
else:
|
||
|
raise Exception('invalid E32_Params data header')
|
||
|
p.address = (datab[1] << 8) | datab[2]
|
||
|
p.parity = (datab[3] >> 6) & 0b11
|
||
|
p.baud = BAUD_TABLE[(datab[3] >> 3) & 0b111]
|
||
|
p.air_data_rate = AIR_DATA_RATE_TABLE[datab[3] & 0b111]
|
||
|
p.channel = datab[4]
|
||
|
p.tx_mode = (datab[5] >> 7) & 1
|
||
|
p.io_mode = (datab[5] >> 6) & 1
|
||
|
p.wake_up_time = 250 * (((datab[5] >> 3) & 0b111) + 1)
|
||
|
p.fec_enabled = (datab[5] & (1 << 2)) != 0
|
||
|
p.tx_power = TX_POWER_TABLE[datab[5] & 0b11]
|
||
|
return p
|
||
|
|
||
|
def pack(self):
|
||
|
p = self
|
||
|
datab = bytearray(range(6))
|
||
|
if p.save:
|
||
|
datab[0] = 0xC0
|
||
|
else:
|
||
|
datab[0] = 0xC2
|
||
|
datab[1] = (p.address >> 8) & 0xFF
|
||
|
datab[2] = (p.address) & 0xFF
|
||
|
datab[3] = 0
|
||
|
datab[3] |= p.parity << 6
|
||
6 years ago
|
datab[3] |= (least_gte(p.baud, BAUD_TABLE) << 3)
|
||
|
datab[3] |= (least_gte(p.air_data_rate, AIR_DATA_RATE_TABLE))
|
||
6 years ago
|
datab[4] = p.channel
|
||
|
datab[5] = 0
|
||
|
datab[5] |= p.tx_mode << 7
|
||
|
datab[5] |= p.io_mode << 6
|
||
|
datab[5] |= (int((p.wake_up_time / 250) - 1) & 0b111) << 3
|
||
|
datab[5] |= p.fec_enabled << 2
|
||
6 years ago
|
datab[5] |= least_gte(p.tx_power, TX_POWER_TABLE)
|
||
6 years ago
|
return datab
|
||
6 years ago
|
|
||
|
|
||
|
class E32:
|
||
6 years ago
|
ser: serial.Serial
|
||
|
|
||
|
def __init__(self, serial_port: serial.Serial):
|
||
|
self.ser = serial_port
|
||
|
|
||
|
def close(self):
|
||
|
self.ser.close()
|
||
|
|
||
|
def read_version(self):
|
||
|
# self.ser.flush()
|
||
|
self.ser.write(b'\xC3\xC3\xC3')
|
||
|
version = self.ser.read(size=4)
|
||
|
print("version: ", version)
|
||
|
return version
|
||
|
|
||
|
def reset(self):
|
||
|
# self.ser.flush()
|
||
|
print("writing: ", b'\xC4\xC4\xC4')
|
||
|
self.ser.write(b'0xC40xC40xC4')
|
||
|
|
||
|
def read_params(self):
|
||
|
# self.ser.flush()
|
||
|
self.ser.write(b'\xC1\xC1\xC1')
|
||
|
param_bytes = self.ser.read(size=6)
|
||
|
print("param_bytes: ", param_bytes)
|
||
|
return E32_Params.unpack(param_bytes)
|
||
6 years ago
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
6 years ago
|
p = E32_Params.default()
|
||
|
print("params: ", p.__dict__)
|
||
|
data = p.pack()
|
||
|
print("packed data: ", ', '.join(format(x, '02x') for x in data))
|
||
|
p2 = E32_Params.unpack(data)
|
||
|
print("unpacked params: ", p2.__dict__)
|