Skip to content

Commit 752b98d

Browse files
committed
Update example generated codes
1 parent 62de84f commit 752b98d

File tree

2 files changed

+211
-45
lines changed

2 files changed

+211
-45
lines changed

example/C-optimization-mode/example_bp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
extern "C" {
1717
#endif
1818

19+
#define BITPROTO_OPTIMIZATION_MODE 1
20+
1921
typedef int64_t Timestamp; // 64bit
2022

2123
typedef int32_t TernaryInt32[3]; // 96bit

example/Python/example_bp.py

Lines changed: 209 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import json
1010
from dataclasses import dataclass, field
11-
from typing import ClassVar, Dict, List
11+
from typing import ClassVar, Dict, List, Union
12+
from enum import IntEnum, unique
1213

1314
from bitprotolib import bp
1415

@@ -31,79 +32,121 @@ def bp_default_factory_TernaryInt32() -> TernaryInt32:
3132
return [0 for _ in range(3)]
3233

3334

34-
DroneStatus = int # 3bit
35-
DRONE_STATUS_UNKNOWN: DroneStatus = 0
36-
DRONE_STATUS_STANDBY: DroneStatus = 1
37-
DRONE_STATUS_RISING: DroneStatus = 2
38-
DRONE_STATUS_LANDING: DroneStatus = 3
39-
DRONE_STATUS_FLYING: DroneStatus = 4
35+
@unique
36+
class DroneStatus(IntEnum): # 3bit
37+
DRONE_STATUS_UNKNOWN = 0
38+
DRONE_STATUS_STANDBY = 1
39+
DRONE_STATUS_RISING = 2
40+
DRONE_STATUS_LANDING = 3
41+
DRONE_STATUS_FLYING = 4
42+
43+
44+
# Aliases for backwards compatibility
45+
DRONE_STATUS_UNKNOWN: DroneStatus = DroneStatus.DRONE_STATUS_UNKNOWN
46+
DRONE_STATUS_STANDBY: DroneStatus = DroneStatus.DRONE_STATUS_STANDBY
47+
DRONE_STATUS_RISING: DroneStatus = DroneStatus.DRONE_STATUS_RISING
48+
DRONE_STATUS_LANDING: DroneStatus = DroneStatus.DRONE_STATUS_LANDING
49+
DRONE_STATUS_FLYING: DroneStatus = DroneStatus.DRONE_STATUS_FLYING
50+
4051

4152
_DRONESTATUS_VALUE_TO_NAME_MAP: Dict[DroneStatus, str] = {
42-
0: "DRONE_STATUS_UNKNOWN",
43-
1: "DRONE_STATUS_STANDBY",
44-
2: "DRONE_STATUS_RISING",
45-
3: "DRONE_STATUS_LANDING",
46-
4: "DRONE_STATUS_FLYING",
53+
DroneStatus.DRONE_STATUS_UNKNOWN: "DRONE_STATUS_UNKNOWN",
54+
DroneStatus.DRONE_STATUS_STANDBY: "DRONE_STATUS_STANDBY",
55+
DroneStatus.DRONE_STATUS_RISING: "DRONE_STATUS_RISING",
56+
DroneStatus.DRONE_STATUS_LANDING: "DRONE_STATUS_LANDING",
57+
DroneStatus.DRONE_STATUS_FLYING: "DRONE_STATUS_FLYING",
4758
}
4859

4960
def bp_processor_DroneStatus() -> bp.Processor:
5061
return bp.EnumProcessor(bp.Uint(3))
5162

5263

53-
PropellerStatus = int # 2bit
54-
PROPELLER_STATUS_UNKNOWN: PropellerStatus = 0
55-
PROPELLER_STATUS_IDLE: PropellerStatus = 1
56-
PROPELLER_STATUS_ROTATING: PropellerStatus = 2
64+
@unique
65+
class PropellerStatus(IntEnum): # 2bit
66+
PROPELLER_STATUS_UNKNOWN = 0
67+
PROPELLER_STATUS_IDLE = 1
68+
PROPELLER_STATUS_ROTATING = 2
69+
70+
71+
# Aliases for backwards compatibility
72+
PROPELLER_STATUS_UNKNOWN: PropellerStatus = PropellerStatus.PROPELLER_STATUS_UNKNOWN
73+
PROPELLER_STATUS_IDLE: PropellerStatus = PropellerStatus.PROPELLER_STATUS_IDLE
74+
PROPELLER_STATUS_ROTATING: PropellerStatus = PropellerStatus.PROPELLER_STATUS_ROTATING
75+
5776

5877
_PROPELLERSTATUS_VALUE_TO_NAME_MAP: Dict[PropellerStatus, str] = {
59-
0: "PROPELLER_STATUS_UNKNOWN",
60-
1: "PROPELLER_STATUS_IDLE",
61-
2: "PROPELLER_STATUS_ROTATING",
78+
PropellerStatus.PROPELLER_STATUS_UNKNOWN: "PROPELLER_STATUS_UNKNOWN",
79+
PropellerStatus.PROPELLER_STATUS_IDLE: "PROPELLER_STATUS_IDLE",
80+
PropellerStatus.PROPELLER_STATUS_ROTATING: "PROPELLER_STATUS_ROTATING",
6281
}
6382

6483
def bp_processor_PropellerStatus() -> bp.Processor:
6584
return bp.EnumProcessor(bp.Uint(2))
6685

6786

68-
RotatingDirection = int # 2bit
69-
ROTATING_DIRECTION_UNKNOWN: RotatingDirection = 0
70-
ROTATING_DIRECTION_CLOCK_WISE: RotatingDirection = 1
71-
ROTATING_DIRECTION_ANTI_CLOCK_WISE: RotatingDirection = 2
87+
@unique
88+
class RotatingDirection(IntEnum): # 2bit
89+
ROTATING_DIRECTION_UNKNOWN = 0
90+
ROTATING_DIRECTION_CLOCK_WISE = 1
91+
ROTATING_DIRECTION_ANTI_CLOCK_WISE = 2
92+
93+
94+
# Aliases for backwards compatibility
95+
ROTATING_DIRECTION_UNKNOWN: RotatingDirection = RotatingDirection.ROTATING_DIRECTION_UNKNOWN
96+
ROTATING_DIRECTION_CLOCK_WISE: RotatingDirection = RotatingDirection.ROTATING_DIRECTION_CLOCK_WISE
97+
ROTATING_DIRECTION_ANTI_CLOCK_WISE: RotatingDirection = RotatingDirection.ROTATING_DIRECTION_ANTI_CLOCK_WISE
98+
7299

73100
_ROTATINGDIRECTION_VALUE_TO_NAME_MAP: Dict[RotatingDirection, str] = {
74-
0: "ROTATING_DIRECTION_UNKNOWN",
75-
1: "ROTATING_DIRECTION_CLOCK_WISE",
76-
2: "ROTATING_DIRECTION_ANTI_CLOCK_WISE",
101+
RotatingDirection.ROTATING_DIRECTION_UNKNOWN: "ROTATING_DIRECTION_UNKNOWN",
102+
RotatingDirection.ROTATING_DIRECTION_CLOCK_WISE: "ROTATING_DIRECTION_CLOCK_WISE",
103+
RotatingDirection.ROTATING_DIRECTION_ANTI_CLOCK_WISE: "ROTATING_DIRECTION_ANTI_CLOCK_WISE",
77104
}
78105

79106
def bp_processor_RotatingDirection() -> bp.Processor:
80107
return bp.EnumProcessor(bp.Uint(2))
81108

82109

83-
PowerStatus = int # 2bit
84-
POWER_STATUS_UNKNOWN: PowerStatus = 0
85-
POWER_STATUS_OFF: PowerStatus = 1
86-
POWER_STATUS_ON: PowerStatus = 2
110+
@unique
111+
class PowerStatus(IntEnum): # 2bit
112+
POWER_STATUS_UNKNOWN = 0
113+
POWER_STATUS_OFF = 1
114+
POWER_STATUS_ON = 2
115+
116+
117+
# Aliases for backwards compatibility
118+
POWER_STATUS_UNKNOWN: PowerStatus = PowerStatus.POWER_STATUS_UNKNOWN
119+
POWER_STATUS_OFF: PowerStatus = PowerStatus.POWER_STATUS_OFF
120+
POWER_STATUS_ON: PowerStatus = PowerStatus.POWER_STATUS_ON
121+
87122

88123
_POWERSTATUS_VALUE_TO_NAME_MAP: Dict[PowerStatus, str] = {
89-
0: "POWER_STATUS_UNKNOWN",
90-
1: "POWER_STATUS_OFF",
91-
2: "POWER_STATUS_ON",
124+
PowerStatus.POWER_STATUS_UNKNOWN: "POWER_STATUS_UNKNOWN",
125+
PowerStatus.POWER_STATUS_OFF: "POWER_STATUS_OFF",
126+
PowerStatus.POWER_STATUS_ON: "POWER_STATUS_ON",
92127
}
93128

94129
def bp_processor_PowerStatus() -> bp.Processor:
95130
return bp.EnumProcessor(bp.Uint(2))
96131

97132

98-
LandingGearStatus = int # 2bit
99-
LANDING_GEAR_STATUS_UNKNOWN: LandingGearStatus = 0
100-
LANDING_GEAR_STATUS_UNFOLDED: LandingGearStatus = 1
101-
LANDING_GEAR_STATUS_FOLDED: LandingGearStatus = 2
133+
@unique
134+
class LandingGearStatus(IntEnum): # 2bit
135+
LANDING_GEAR_STATUS_UNKNOWN = 0
136+
LANDING_GEAR_STATUS_UNFOLDED = 1
137+
LANDING_GEAR_STATUS_FOLDED = 2
138+
139+
140+
# Aliases for backwards compatibility
141+
LANDING_GEAR_STATUS_UNKNOWN: LandingGearStatus = LandingGearStatus.LANDING_GEAR_STATUS_UNKNOWN
142+
LANDING_GEAR_STATUS_UNFOLDED: LandingGearStatus = LandingGearStatus.LANDING_GEAR_STATUS_UNFOLDED
143+
LANDING_GEAR_STATUS_FOLDED: LandingGearStatus = LandingGearStatus.LANDING_GEAR_STATUS_FOLDED
144+
102145

103146
_LANDINGGEARSTATUS_VALUE_TO_NAME_MAP: Dict[LandingGearStatus, str] = {
104-
0: "LANDING_GEAR_STATUS_UNKNOWN",
105-
1: "LANDING_GEAR_STATUS_UNFOLDED",
106-
2: "LANDING_GEAR_STATUS_FOLDED",
147+
LandingGearStatus.LANDING_GEAR_STATUS_UNKNOWN: "LANDING_GEAR_STATUS_UNKNOWN",
148+
LandingGearStatus.LANDING_GEAR_STATUS_UNFOLDED: "LANDING_GEAR_STATUS_UNFOLDED",
149+
LandingGearStatus.LANDING_GEAR_STATUS_FOLDED: "LANDING_GEAR_STATUS_FOLDED",
107150
}
108151

109152
def bp_processor_LandingGearStatus() -> bp.Processor:
@@ -116,8 +159,41 @@ class Propeller(bp.MessageBase):
116159
BYTES_LENGTH: ClassVar[int] = 2
117160

118161
id: int = 0 # 8bit
119-
status: PropellerStatus = 0 # 2bit
120-
direction: RotatingDirection = 0 # 2bit
162+
status: Union[int, PropellerStatus] = PropellerStatus.PROPELLER_STATUS_UNKNOWN
163+
# This field is a proxy to hold integer value of enum field 'status'
164+
_enum_field_proxy__status: int = field(init=False, repr=False) # 2bit
165+
direction: Union[int, RotatingDirection] = RotatingDirection.ROTATING_DIRECTION_UNKNOWN
166+
# This field is a proxy to hold integer value of enum field 'direction'
167+
_enum_field_proxy__direction: int = field(init=False, repr=False) # 2bit
168+
169+
def __post_init__(self):
170+
# initialize handling of enum field 'status' as `enum.IntEnum`
171+
if not isinstance(getattr(Propeller, "status", False), property):
172+
self._enum_field_proxy__status = self.status
173+
Propeller.status = property(Propeller._get_status, Propeller._set_status)
174+
# initialize handling of enum field 'direction' as `enum.IntEnum`
175+
if not isinstance(getattr(Propeller, "direction", False), property):
176+
self._enum_field_proxy__direction = self.direction
177+
Propeller.direction = property(Propeller._get_direction, Propeller._set_direction)
178+
179+
@staticmethod
180+
def dict_factory(kv_pairs):
181+
return {k: v for k, v in kv_pairs if not k.startswith('_enum_field_proxy__')}
182+
183+
def _get_status(self) -> PropellerStatus:
184+
"""property getter for enum proxy field"""
185+
return PropellerStatus(self._enum_field_proxy__status)
186+
187+
def _set_status(self, val):
188+
"""property setter for enum proxy field"""
189+
self._enum_field_proxy__status = val
190+
def _get_direction(self) -> RotatingDirection:
191+
"""property getter for enum proxy field"""
192+
return RotatingDirection(self._enum_field_proxy__direction)
193+
194+
def _set_direction(self, val):
195+
"""property setter for enum proxy field"""
196+
self._enum_field_proxy__direction = val
121197

122198
def bp_processor(self) -> bp.Processor:
123199
field_processors: List[bp.Processor] = [
@@ -173,9 +249,29 @@ class Power(bp.MessageBase):
173249
BYTES_LENGTH: ClassVar[int] = 2
174250

175251
battery: int = 0 # 8bit
176-
status: PowerStatus = 0 # 2bit
252+
status: Union[int, PowerStatus] = PowerStatus.POWER_STATUS_UNKNOWN
253+
# This field is a proxy to hold integer value of enum field 'status'
254+
_enum_field_proxy__status: int = field(init=False, repr=False) # 2bit
177255
is_charging: bool = False # 1bit
178256

257+
def __post_init__(self):
258+
# initialize handling of enum field 'status' as `enum.IntEnum`
259+
if not isinstance(getattr(Power, "status", False), property):
260+
self._enum_field_proxy__status = self.status
261+
Power.status = property(Power._get_status, Power._set_status)
262+
263+
@staticmethod
264+
def dict_factory(kv_pairs):
265+
return {k: v for k, v in kv_pairs if not k.startswith('_enum_field_proxy__')}
266+
267+
def _get_status(self) -> PowerStatus:
268+
"""property getter for enum proxy field"""
269+
return PowerStatus(self._enum_field_proxy__status)
270+
271+
def _set_status(self, val):
272+
"""property setter for enum proxy field"""
273+
self._enum_field_proxy__status = val
274+
179275
def bp_processor(self) -> bp.Processor:
180276
field_processors: List[bp.Processor] = [
181277
bp.MessageFieldProcessor(1, bp.Uint(8)),
@@ -234,6 +330,13 @@ class Network(bp.MessageBase):
234330
# The timestamp of the last time received heartbeat packet.
235331
heartbeat_at: Timestamp = field(default_factory=bp_default_factory_Timestamp) # 64bit
236332

333+
def __post_init__(self):
334+
pass
335+
336+
@staticmethod
337+
def dict_factory(kv_pairs):
338+
return {k: v for k, v in kv_pairs if not k.startswith('_enum_field_proxy__')}
339+
237340
def bp_processor(self) -> bp.Processor:
238341
field_processors: List[bp.Processor] = [
239342
bp.MessageFieldProcessor(1, bp.Uint(4)),
@@ -282,7 +385,27 @@ class LandingGear(bp.MessageBase):
282385
# Number of bytes to serialize class LandingGear
283386
BYTES_LENGTH: ClassVar[int] = 1
284387

285-
status: LandingGearStatus = 0 # 2bit
388+
status: Union[int, LandingGearStatus] = LandingGearStatus.LANDING_GEAR_STATUS_UNKNOWN
389+
# This field is a proxy to hold integer value of enum field 'status'
390+
_enum_field_proxy__status: int = field(init=False, repr=False) # 2bit
391+
392+
def __post_init__(self):
393+
# initialize handling of enum field 'status' as `enum.IntEnum`
394+
if not isinstance(getattr(LandingGear, "status", False), property):
395+
self._enum_field_proxy__status = self.status
396+
LandingGear.status = property(LandingGear._get_status, LandingGear._set_status)
397+
398+
@staticmethod
399+
def dict_factory(kv_pairs):
400+
return {k: v for k, v in kv_pairs if not k.startswith('_enum_field_proxy__')}
401+
402+
def _get_status(self) -> LandingGearStatus:
403+
"""property getter for enum proxy field"""
404+
return LandingGearStatus(self._enum_field_proxy__status)
405+
406+
def _set_status(self, val):
407+
"""property setter for enum proxy field"""
408+
self._enum_field_proxy__status = val
286409

287410
def bp_processor(self) -> bp.Processor:
288411
field_processors: List[bp.Processor] = [
@@ -331,6 +454,13 @@ class Position(bp.MessageBase):
331454
longitude: int = 0 # 32bit
332455
altitude: int = 0 # 32bit
333456

457+
def __post_init__(self):
458+
pass
459+
460+
@staticmethod
461+
def dict_factory(kv_pairs):
462+
return {k: v for k, v in kv_pairs if not k.startswith('_enum_field_proxy__')}
463+
334464
def bp_processor(self) -> bp.Processor:
335465
field_processors: List[bp.Processor] = [
336466
bp.MessageFieldProcessor(1, bp.Uint(32)),
@@ -391,6 +521,13 @@ class Pose(bp.MessageBase):
391521
pitch: int = 0 # 32bit
392522
roll: int = 0 # 32bit
393523

524+
def __post_init__(self):
525+
pass
526+
527+
@staticmethod
528+
def dict_factory(kv_pairs):
529+
return {k: v for k, v in kv_pairs if not k.startswith('_enum_field_proxy__')}
530+
394531
def bp_processor(self) -> bp.Processor:
395532
field_processors: List[bp.Processor] = [
396533
bp.MessageFieldProcessor(1, bp.Int(32)),
@@ -450,6 +587,13 @@ class Flight(bp.MessageBase):
450587
# Acceleration at X, Y, Z axis.
451588
acceleration: TernaryInt32 = field(default_factory=bp_default_factory_TernaryInt32) # 96bit
452589

590+
def __post_init__(self):
591+
pass
592+
593+
@staticmethod
594+
def dict_factory(kv_pairs):
595+
return {k: v for k, v in kv_pairs if not k.startswith('_enum_field_proxy__')}
596+
453597
def bp_processor(self) -> bp.Processor:
454598
field_processors: List[bp.Processor] = [
455599
bp.MessageFieldProcessor(1, Pose().bp_processor()),
@@ -501,14 +645,34 @@ class Drone(bp.MessageBase):
501645
# Number of bytes to serialize class Drone
502646
BYTES_LENGTH: ClassVar[int] = 65
503647

504-
status: DroneStatus = 0 # 3bit
648+
status: Union[int, DroneStatus] = DroneStatus.DRONE_STATUS_UNKNOWN
649+
# This field is a proxy to hold integer value of enum field 'status'
650+
_enum_field_proxy__status: int = field(init=False, repr=False) # 3bit
505651
position: Position = field(default_factory=Position) # 96bit
506652
flight: Flight = field(default_factory=Flight) # 288bit
507653
propellers: List[Propeller] = field(default_factory=lambda: [Propeller() for _ in range(4)]) # 48bit
508654
power: Power = field(default_factory=Power) # 11bit
509655
network: Network = field(default_factory=Network) # 68bit
510656
landing_gear: LandingGear = field(default_factory=LandingGear) # 2bit
511657

658+
def __post_init__(self):
659+
# initialize handling of enum field 'status' as `enum.IntEnum`
660+
if not isinstance(getattr(Drone, "status", False), property):
661+
self._enum_field_proxy__status = self.status
662+
Drone.status = property(Drone._get_status, Drone._set_status)
663+
664+
@staticmethod
665+
def dict_factory(kv_pairs):
666+
return {k: v for k, v in kv_pairs if not k.startswith('_enum_field_proxy__')}
667+
668+
def _get_status(self) -> DroneStatus:
669+
"""property getter for enum proxy field"""
670+
return DroneStatus(self._enum_field_proxy__status)
671+
672+
def _set_status(self, val):
673+
"""property setter for enum proxy field"""
674+
self._enum_field_proxy__status = val
675+
512676
def bp_processor(self) -> bp.Processor:
513677
field_processors: List[bp.Processor] = [
514678
bp.MessageFieldProcessor(1, bp_processor_DroneStatus()),

0 commit comments

Comments
 (0)