Skip to content

Commit c5c5ea1

Browse files
authored
Merge pull request #66 from hit9/cpp-compile-example
Add a simple C++ (compile with C bp files) example
2 parents bb57ce4 + aa41306 commit c5c5ea1

18 files changed

Lines changed: 1338 additions & 5 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ dist
1313
spam
1414
.DS_Store
1515
compile_flags.txt
16+
test2
17+
*.o
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
example
2+
bench
3+
*.o
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
BITPROTO_LIB_PATH=../../lib/c
2+
3+
default: run
4+
5+
C_SOURCES = $(wildcard *.c)
6+
CPP_SOURCES = main.cpp
7+
OBJECTS = $(CPP_SOURCES:.cpp=.o) $(C_SOURCES:.c=.o)
8+
9+
%.o: %.cpp
10+
$(CXX) -c $< -o $@
11+
12+
%.o: %.c
13+
$(CC) -c $< -o $@
14+
15+
build: $(OBJECTS)
16+
$(CXX) $(OBJECTS) -o example
17+
18+
run: build
19+
./example
20+
21+
clean:
22+
rm example_bp.h example_bp.c example *.o || true
23+
24+
.PHONY: build run clean

example/CPP-optimization-mode/example_bp.c

Lines changed: 297 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Code generated by bitproto. DO NOT EDIT.
2+
3+
// Proto drone describes the structure of the drone.
4+
5+
#ifndef __BITPROTO__DRONE_H__
6+
#define __BITPROTO__DRONE_H__ 1
7+
8+
#include <inttypes.h>
9+
#include <stddef.h>
10+
#include <stdint.h>
11+
#ifndef __cplusplus
12+
#include <stdbool.h>
13+
#endif
14+
15+
#if defined(__cplusplus)
16+
extern "C" {
17+
#endif
18+
19+
#define BITPROTO_OPTIMIZATION_MODE 1
20+
21+
typedef int32_t Timestamp; // 32bit
22+
23+
typedef int32_t TernaryInt32[3]; // 96bit
24+
25+
typedef uint8_t DroneStatus; // 3bit
26+
27+
#define DRONE_STATUS_UNKNOWN 0
28+
#define DRONE_STATUS_STANDBY 1
29+
#define DRONE_STATUS_RISING 2
30+
#define DRONE_STATUS_LANDING 3
31+
#define DRONE_STATUS_FLYING 4
32+
33+
typedef uint8_t PropellerStatus; // 2bit
34+
35+
#define PROPELLER_STATUS_UNKNOWN 0
36+
#define PROPELLER_STATUS_IDLE 1
37+
#define PROPELLER_STATUS_ROTATING 2
38+
39+
typedef uint8_t RotatingDirection; // 2bit
40+
41+
#define ROTATING_DIRECTION_UNKNOWN 0
42+
#define ROTATING_DIRECTION_CLOCK_WISE 1
43+
#define ROTATING_DIRECTION_ANTI_CLOCK_WISE 2
44+
45+
typedef uint8_t PowerStatus; // 2bit
46+
47+
#define POWER_STATUS_UNKNOWN 0
48+
#define POWER_STATUS_OFF 1
49+
#define POWER_STATUS_ON 2
50+
51+
typedef uint8_t LandingGearStatus; // 2bit
52+
53+
#define LANDING_GEAR_STATUS_UNKNOWN 0
54+
#define LANDING_GEAR_STATUS_UNFOLDED 1
55+
#define LANDING_GEAR_STATUS_FOLDED 2
56+
57+
// Number of bytes to encode struct Propeller
58+
#define BYTES_LENGTH_PROPELLER 2
59+
60+
struct Propeller {
61+
uint8_t id; // 8bit
62+
PropellerStatus status; // 2bit
63+
RotatingDirection direction; // 2bit
64+
};
65+
66+
// Number of bytes to encode struct Power
67+
#define BYTES_LENGTH_POWER 2
68+
69+
struct Power {
70+
uint8_t battery; // 8bit
71+
PowerStatus status; // 2bit
72+
bool is_charging; // 1bit
73+
};
74+
75+
// Number of bytes to encode struct Network
76+
#define BYTES_LENGTH_NETWORK 5
77+
78+
struct Network {
79+
// Degree of signal, between 1~10.
80+
uint8_t signal; // 4bit
81+
// The timestamp of the last time received heartbeat packet.
82+
Timestamp heartbeat_at; // 32bit
83+
};
84+
85+
// Number of bytes to encode struct LandingGear
86+
#define BYTES_LENGTH_LANDING_GEAR 1
87+
88+
struct LandingGear {
89+
LandingGearStatus status; // 2bit
90+
};
91+
92+
// Number of bytes to encode struct Position
93+
#define BYTES_LENGTH_POSITION 12
94+
95+
struct Position {
96+
uint32_t latitude; // 32bit
97+
uint32_t longitude; // 32bit
98+
uint32_t altitude; // 32bit
99+
};
100+
101+
// Number of bytes to encode struct Pose
102+
#define BYTES_LENGTH_POSE 12
103+
104+
// Pose in flight. https://en.wikipedia.org/wiki/Aircraft_principal_axes
105+
struct Pose {
106+
int32_t yaw; // 32bit
107+
int32_t pitch; // 32bit
108+
int32_t roll; // 32bit
109+
};
110+
111+
// Number of bytes to encode struct Flight
112+
#define BYTES_LENGTH_FLIGHT 36
113+
114+
struct Flight {
115+
struct Pose pose; // 96bit
116+
// Velocity at X, Y, Z axis.
117+
TernaryInt32 velocity; // 96bit
118+
// Acceleration at X, Y, Z axis.
119+
TernaryInt32 acceleration; // 96bit
120+
};
121+
122+
// Number of bytes to encode struct PressureSensor
123+
#define BYTES_LENGTH_PRESSURE_SENSOR 6
124+
125+
struct PressureSensor {
126+
int32_t pressures[2]; // 48bit
127+
};
128+
129+
// Number of bytes to encode struct Drone
130+
#define BYTES_LENGTH_DRONE 67
131+
132+
struct Drone {
133+
DroneStatus status; // 3bit
134+
struct Position position; // 96bit
135+
struct Flight flight; // 288bit
136+
struct Propeller propellers[4]; // 48bit
137+
struct Power power; // 11bit
138+
struct Network network; // 36bit
139+
struct LandingGear landing_gear; // 2bit
140+
struct PressureSensor pressure_sensor; // 48bit
141+
};
142+
143+
// Encode struct Drone to given buffer s.
144+
int EncodeDrone(struct Drone *m, unsigned char *s);
145+
// Decode struct Drone from given buffer s.
146+
int DecodeDrone(struct Drone *m, unsigned char *s);
147+
148+
#if defined(__cplusplus)
149+
}
150+
#endif
151+
152+
#endif
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <cassert>
2+
#include <iostream>
3+
4+
#include "example_bp.h"
5+
6+
int main(void) {
7+
// Encode.
8+
struct Drone drone;
9+
10+
drone.status = DRONE_STATUS_RISING, drone.position.longitude = 2000;
11+
drone.position.latitude = 2000;
12+
drone.position.altitude = 1080;
13+
drone.flight.acceleration[0] = -1001;
14+
drone.power.is_charging = false;
15+
drone.propellers[0].direction = ROTATING_DIRECTION_CLOCK_WISE;
16+
drone.pressure_sensor.pressures[0] = -11;
17+
18+
unsigned char s[BYTES_LENGTH_DRONE] = {0};
19+
20+
EncodeDrone(&drone, s);
21+
22+
// Decode.
23+
struct Drone drone_new = {0};
24+
DecodeDrone(&drone_new, s);
25+
26+
assert(drone_new.status == drone.status);
27+
28+
// Json formatting is not supported in optimization mode.
29+
30+
return 0;
31+
}

example/CPP/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
example
2+
bench
3+
*.o

example/CPP/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
BITPROTO_LIB_PATH=../../lib/c
2+
3+
default: run
4+
5+
CFLAGS = -I$(BITPROTO_LIB_PATH)
6+
CXXFLAGS = -I$(BITPROTO_LIB_PATH)
7+
8+
C_SOURCES = $(wildcard *.c) $(BITPROTO_LIB_PATH)/bitproto.c
9+
CPP_SOURCES = main.cpp
10+
OBJECTS = $(CPP_SOURCES:.cpp=.o) $(C_SOURCES:.c=.o)
11+
12+
%.o: %.cpp
13+
$(CXX) $(CXXFLAGS) -c $< -o $@
14+
15+
%.o: %.c
16+
$(CC) $(CFLAGS) -c $< -o $@
17+
18+
build: $(OBJECTS)
19+
$(CXX) $(OBJECTS) $(CXXFLAGS) -o example
20+
21+
run: build
22+
./example
23+
24+
clean:
25+
rm example_bp.h example_bp.c example *.o || true
26+
27+
.PHONY: build run clean

0 commit comments

Comments
 (0)