auto-generate code for mesh_msg.h
This commit is contained in:
parent
e0961c16c1
commit
e0c9f36c66
8 changed files with 325 additions and 45 deletions
|
@ -19,6 +19,23 @@ class SimpleFormat:
|
|||
value = value[0]
|
||||
return value, data[self.size:]
|
||||
|
||||
c_types = {
|
||||
"B": "uint8_t",
|
||||
"H": "uint16_t",
|
||||
"I": "uint32_t",
|
||||
"b": "int8_t",
|
||||
"h": "int16_t",
|
||||
"i": "int32_t",
|
||||
}
|
||||
|
||||
def get_c_struct(self, name):
|
||||
c_type = self.c_types[self.fmt[-1]]
|
||||
num = int(self.fmt[:-1]) if len(self.fmt) > 1 else 1
|
||||
if num == 1:
|
||||
return "%s %s;" % (c_type, name)
|
||||
else:
|
||||
return "%s %s[%d];" % (c_type, name, num)
|
||||
|
||||
|
||||
class FixedStrFormat:
|
||||
def __init__(self, num):
|
||||
|
@ -30,6 +47,12 @@ class FixedStrFormat:
|
|||
def decode(self, data: bytes):
|
||||
return struct.unpack('%ss' % self.num, data[:self.num])[0].rstrip(bytes((0, ))).decode(), data[self.num:]
|
||||
|
||||
def get_c_struct(self, name):
|
||||
return "char %(name)s[%(length)d];" % {
|
||||
"name": name,
|
||||
"length": self.num,
|
||||
}
|
||||
|
||||
|
||||
class BoolFormat:
|
||||
def encode(self, value):
|
||||
|
@ -38,6 +61,11 @@ class BoolFormat:
|
|||
def decode(self, data: bytes):
|
||||
return bool(struct.unpack('B', data[:1])[0]), data[1:]
|
||||
|
||||
def get_c_struct(self, name):
|
||||
return "uint8_t %(name)s;" % {
|
||||
"name": name,
|
||||
}
|
||||
|
||||
|
||||
class HexFormat:
|
||||
def __init__(self, num, sep=''):
|
||||
|
@ -53,14 +81,27 @@ class HexFormat:
|
|||
data[self.num:]
|
||||
)
|
||||
|
||||
def get_c_struct(self, name):
|
||||
return "uint8_t %(name)s[%(length)d];" % {
|
||||
"name": name,
|
||||
"length": self.num,
|
||||
}
|
||||
|
||||
|
||||
class VarStrFormat:
|
||||
var_num = 1
|
||||
|
||||
def encode(self, value: str) -> bytes:
|
||||
return bytes((len(value)+1, )) + value.encode() + bytes((0, ))
|
||||
|
||||
def decode(self, data: bytes):
|
||||
return data[1:data[0]].decode(), data[data[0]+1:]
|
||||
|
||||
def get_c_struct(self, name):
|
||||
return "uint8_t num;\nchar %(name)s[0];" % {
|
||||
"name": name,
|
||||
}
|
||||
|
||||
|
||||
class MacAddressFormat:
|
||||
def encode(self, value: str) -> bytes:
|
||||
|
@ -69,8 +110,15 @@ class MacAddressFormat:
|
|||
def decode(self, data: bytes):
|
||||
return (MAC_FMT % tuple(data[:6])), data[6:]
|
||||
|
||||
def get_c_struct(self, name):
|
||||
return "uint8_t %(name)s[6];" % {
|
||||
"name": name,
|
||||
}
|
||||
|
||||
|
||||
class MacAddressesListFormat:
|
||||
var_num = 6
|
||||
|
||||
def encode(self, value: list[str]) -> bytes:
|
||||
return bytes((len(value), )) + sum(
|
||||
(bytes((int(mac[i*3:i*3+2], 16) for i in range(6))) for mac in value),
|
||||
|
@ -80,6 +128,11 @@ class MacAddressesListFormat:
|
|||
def decode(self, data: bytes):
|
||||
return [MAC_FMT % tuple(data[1+6*i:1+6+6*i]) for i in range(data[0])], data[1+data[0]*6:]
|
||||
|
||||
def get_c_struct(self, name):
|
||||
return "uint8_t num;\nuint8_t %(name)s[6][0];" % {
|
||||
"name": name,
|
||||
}
|
||||
|
||||
|
||||
class LedType(IntEnum):
|
||||
SERIAL = 1
|
||||
|
@ -138,3 +191,20 @@ class LedConfigFormat:
|
|||
else:
|
||||
raise ValueError
|
||||
return value, data[4:]
|
||||
|
||||
def get_c_struct(self, name):
|
||||
return (
|
||||
"uint8_t type;\n"
|
||||
"union {\n"
|
||||
" struct {\n"
|
||||
" uint8_t gpio;\n"
|
||||
" uint8_t rmt;\n"
|
||||
" } serial;\n"
|
||||
" struct {\n"
|
||||
" uint8_t gpio_red;\n"
|
||||
" uint8_t gpio_green;\n"
|
||||
" uint8_t gpio_blue;\n"
|
||||
" } multipin;\n"
|
||||
" uint8_t bytes[3];\n"
|
||||
"};"
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue