Skip to content

Commit 1d6616d

Browse files
committed
feat: singleton platform manager. Aggressive refactor
1 parent 5052963 commit 1d6616d

40 files changed

Lines changed: 2118 additions & 1816 deletions

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ file(GLOB SERIALIZATION_SOURCES CONFIGURE_DEPENDS
2121
)
2222
add_library(${PROJECT_NAME} STATIC
2323
${CMAKE_CURRENT_LIST_DIR}/Libs/libcanard_v0/canard.c
24-
${CMAKE_CURRENT_LIST_DIR}/src/dronecan.c
24+
${CMAKE_CURRENT_LIST_DIR}/src/dronecan.cpp
2525
${CMAKE_CURRENT_LIST_DIR}/src/logger.cpp
2626
${SERIALIZATION_SOURCES}
2727
)

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
7878

7979
**1. Initialize**
8080

81-
Include `dronecan.h` header and call `uavcanInitApplication` in the beginning of the application. Call `uavcanSpinOnce` periodically.
81+
Include `dronecan.hpp` header and call `uavcanInitApplication` in the beginning of the application. Call `uavcanSpinOnce` periodically.
8282

8383
```c++
84-
// Include dronecan.h header file
85-
#include "libdcnode/dronecan.h"
84+
// Include dronecan.hpp header file
85+
#include "libdcnode/dronecan.hpp"
8686

8787
// Initialize the library somewhere
8888
const uint8_t node_id = 42;
@@ -104,7 +104,7 @@ while (true) {
104104
Adding a publisher is very easy. Include `publisher.hpp` header, create an instance of the required publisher and just call `publish` when you need. Here is a BatteryInfo publisher example:
105105

106106
```c++
107-
#include "libdcnode/dronecan.h"
107+
#include "libdcnode/dronecan.hpp"
108108
#include "libdcnode/publisher.hpp"
109109

110110
// Create an instance of the publisher
@@ -136,7 +136,7 @@ Adding a subscriber is easy as well. Let's consider a RawCommand subscriber exam
136136
137137
```c++
138138
// Include necessary header files
139-
#include "libdcnode/dronecan.h"
139+
#include "libdcnode/dronecan.hpp"
140140
#include "libdcnode/subscriber.hpp"
141141
142142
// Add a callback handler function

examples/stm32_hal/hal.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@
77

88
#include <iostream>
99
#include <string.h>
10-
#include "libdcnode/dronecan.h"
10+
#include "libdcnode/dronecan.hpp"
1111

12-
void platformSpecificReadUniqueID(uint8_t out_uid[4]) {
12+
void platformSpecificReadUniqueID(uint8_t out_uid[4])
13+
{
1314
const uint32_t UNIQUE_ID_16_BYTES[4] = {
1415
HAL_GetUIDw0(),
1516
HAL_GetUIDw1(),
1617
HAL_GetUIDw2(),
17-
0
18-
};
18+
0};
1919
memset(out_uid, UNIQUE_ID_16_BYTES, 16);
2020
}
2121

22-
bool platformSpecificRequestRestart() {
22+
bool platformSpecificRequestRestart()
23+
{
2324
return false;
2425
}
2526

26-
uint32_t platformSpecificGetTimeMs() {
27+
uint32_t platformSpecificGetTimeMs()
28+
{
2729
return HAL_GetTick();
2830
}

examples/ubuntu/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <chrono>
1111
#include <string.h>
1212
#include "storage.h"
13-
#include "libdcnode/dronecan.h"
13+
#include "libdcnode/dronecan.hpp"
1414
#include "libdcnode/can_driver.h"
1515
// #include "libdcnode/subscriber.hpp"
1616
// #include "libdcnode/publisher.hpp"
@@ -139,6 +139,7 @@ void lights_callback(const uavcan_equipment_indication_LightsCommand &msg)
139139
*/
140140
int main()
141141
{
142+
142143
paramsInit(1, 1, -1, 1);
143144
paramsResetToDefault();
144145
ParamsApi params_api = {
@@ -212,7 +213,9 @@ int main()
212213
{
213214
circuit_status.msg.voltage = 5.0;
214215
battery_info.msg.voltage = 5.1;
216+
215217
circuit_status.spinOnce();
218+
216219
battery_info.spinOnce();
217220
uavcanSpinOnce();
218221
}
Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "libdcnode/legacy/uavcan/protocol/get_node_info.h"
1616
#include "params.h"
1717
#include "version.h"
18+
#include "platform.hpp"
1819

1920
#ifdef __cplusplus
2021
extern "C"
@@ -36,34 +37,6 @@ extern "C"
3637
uint8_t hw_version_minor;
3738
} AppInfo;
3839

39-
typedef uint32_t (*PlatformSpecificGetTimeMsFunc)(void);
40-
typedef bool (*PlatformSpecificRequestRestartFunc)(void);
41-
typedef void (*PlatformSpecificReadUniqueIDFunc)(uint8_t out_uid[16]);
42-
43-
typedef int16_t (*CanDriverInitFunc)(uint32_t can_speed, uint8_t can_driver_idx);
44-
typedef int16_t (*CanDriverReceiveFunc)(CanardCANFrame *const rx_frame, uint8_t can_driver_idx);
45-
typedef int16_t (*CanDriverTransmitFunc)(const CanardCANFrame *const tx_frame, uint8_t can_driver_idx);
46-
typedef uint64_t (*CanDriverGetRxOverflowCountFunc)(void);
47-
typedef uint64_t (*CanDriverGetErrorCountFunc)(void);
48-
49-
typedef struct
50-
{
51-
CanDriverInitFunc init;
52-
CanDriverReceiveFunc recv;
53-
CanDriverTransmitFunc send;
54-
CanDriverGetRxOverflowCountFunc getRxOverflowCount;
55-
CanDriverGetErrorCountFunc getErrorCount;
56-
} CanDriverApi;
57-
58-
typedef struct
59-
{
60-
PlatformSpecificGetTimeMsFunc getTimeMs;
61-
PlatformSpecificRequestRestartFunc requestRestart;
62-
PlatformSpecificReadUniqueIDFunc readUniqueId;
63-
64-
CanDriverApi can;
65-
} PlatformApi;
66-
6740
/**
6841
* @brief Initialize the node and minimal required services
6942
* @return 0 on success, otherwise negative error

include/libdcnode/legacy/dronecan/sensors/hygrometer/Hygrometer.h

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,67 +9,71 @@
99
#define DRONECAN_SENSORS_HYGROMETER_HYGROMETER_H_
1010

1111
#include "libdcnode/serialization_internal.h"
12-
#include "libdcnode/dronecan.h"
12+
#include "libdcnode/dronecan.hpp"
1313

14-
#define DRONECAN_SENSORS_HYGROMETER_HYGROMETER_ID 1032
15-
#define DRONECAN_SENSORS_HYGROMETER_HYGROMETER_SIGNATURE 0xCEB308892BF163E8ULL
16-
#define DRONECAN_SENSORS_HYGROMETER_HYGROMETER_MESSAGE_SIZE 5
17-
#define DRONECAN_SENSORS_HYGROMETER_HYGROMETER UAVCAN_EXPAND(DRONECAN_SENSORS_HYGROMETER_HYGROMETER)
14+
#define DRONECAN_SENSORS_HYGROMETER_HYGROMETER_ID 1032
15+
#define DRONECAN_SENSORS_HYGROMETER_HYGROMETER_SIGNATURE 0xCEB308892BF163E8ULL
16+
#define DRONECAN_SENSORS_HYGROMETER_HYGROMETER_MESSAGE_SIZE 5
17+
#define DRONECAN_SENSORS_HYGROMETER_HYGROMETER UAVCAN_EXPAND(DRONECAN_SENSORS_HYGROMETER_HYGROMETER)
1818

19-
typedef struct {
19+
typedef struct
20+
{
2021
float temperature;
2122
float humidity;
2223
uint8_t id;
2324
} Hygrometer;
2425

2526
#ifdef __cplusplus
26-
extern "C" {
27+
extern "C"
28+
{
2729
#endif
2830

29-
static inline int8_t dronecan_sensors_hygrometer_hygrometer_serialize(
30-
const Hygrometer* const obj,
31-
uint8_t* const buffer,
32-
size_t* const inout_buffer_size_bytes)
33-
{
34-
if ((obj == NULL) || (buffer == NULL) || (inout_buffer_size_bytes == NULL)) {
35-
return -2;
36-
}
31+
static inline int8_t dronecan_sensors_hygrometer_hygrometer_serialize(
32+
const Hygrometer *const obj,
33+
uint8_t *const buffer,
34+
size_t *const inout_buffer_size_bytes)
35+
{
36+
if ((obj == NULL) || (buffer == NULL) || (inout_buffer_size_bytes == NULL))
37+
{
38+
return -2;
39+
}
3740

38-
const size_t capacity_bytes = *inout_buffer_size_bytes;
39-
if (capacity_bytes < DRONECAN_SENSORS_HYGROMETER_HYGROMETER_MESSAGE_SIZE) {
40-
return -3;
41-
}
41+
const size_t capacity_bytes = *inout_buffer_size_bytes;
42+
if (capacity_bytes < DRONECAN_SENSORS_HYGROMETER_HYGROMETER_MESSAGE_SIZE)
43+
{
44+
return -3;
45+
}
4246

43-
uint16_t temperature = canardConvertNativeFloatToFloat16(obj->temperature);
44-
canardEncodeScalar(buffer, 0, 16, &temperature);
47+
uint16_t temperature = canardConvertNativeFloatToFloat16(obj->temperature);
48+
canardEncodeScalar(buffer, 0, 16, &temperature);
4549

46-
uint16_t humidity = canardConvertNativeFloatToFloat16(obj->humidity);
47-
canardEncodeScalar(buffer, 16, 16, &humidity);
50+
uint16_t humidity = canardConvertNativeFloatToFloat16(obj->humidity);
51+
canardEncodeScalar(buffer, 16, 16, &humidity);
4852

49-
canardEncodeScalar(buffer, 32, 8, &obj->id);
53+
canardEncodeScalar(buffer, 32, 8, &obj->id);
5054

51-
return 0;
52-
}
55+
return 0;
56+
}
5357

54-
static inline int8_t dronecan_sensors_hygrometer_hygrometer_publish(
55-
const Hygrometer* const obj,
56-
uint8_t* inout_transfer_id)
57-
{
58-
uint8_t buffer[DRONECAN_SENSORS_HYGROMETER_HYGROMETER_MESSAGE_SIZE];
59-
size_t inout_buffer_size = DRONECAN_SENSORS_HYGROMETER_HYGROMETER_MESSAGE_SIZE;
60-
dronecan_sensors_hygrometer_hygrometer_serialize(obj, buffer, &inout_buffer_size);
61-
uavcanPublish(DRONECAN_SENSORS_HYGROMETER_HYGROMETER_SIGNATURE,
62-
DRONECAN_SENSORS_HYGROMETER_HYGROMETER_ID,
63-
inout_transfer_id,
64-
CANARD_TRANSFER_PRIORITY_MEDIUM,
65-
buffer,
66-
DRONECAN_SENSORS_HYGROMETER_HYGROMETER_MESSAGE_SIZE);
58+
static inline int8_t dronecan_sensors_hygrometer_hygrometer_publish(
59+
const Hygrometer *const obj,
60+
uint8_t *inout_transfer_id)
61+
{
62+
uint8_t buffer[DRONECAN_SENSORS_HYGROMETER_HYGROMETER_MESSAGE_SIZE];
63+
size_t inout_buffer_size = DRONECAN_SENSORS_HYGROMETER_HYGROMETER_MESSAGE_SIZE;
64+
dronecan_sensors_hygrometer_hygrometer_serialize(obj, buffer, &inout_buffer_size);
65+
uavcanPublish(DRONECAN_SENSORS_HYGROMETER_HYGROMETER_SIGNATURE,
66+
DRONECAN_SENSORS_HYGROMETER_HYGROMETER_ID,
67+
inout_transfer_id,
68+
CANARD_TRANSFER_PRIORITY_MEDIUM,
69+
buffer,
70+
DRONECAN_SENSORS_HYGROMETER_HYGROMETER_MESSAGE_SIZE);
6771

68-
return 0;
69-
}
72+
return 0;
73+
}
7074

7175
#ifdef __cplusplus
7276
}
7377
#endif
7478

75-
#endif // DRONECAN_SENSORS_HYGROMETER_HYGROMETER_H_
79+
#endif // DRONECAN_SENSORS_HYGROMETER_HYGROMETER_H_
Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
/***
22
* Copyright (C) 2024 Anastasiia Stepanova <asiiapine@gmail.com>
33
* Distributed under the terms of the GPL v3 license, available in the file LICENSE.
4-
***/
5-
4+
***/
65

76
#ifndef UAVCAN_COARCE_ORIENTATION_H_
87
#define UAVCAN_COARCE_ORIENTATION_H_
98

109
#include "libdcnode/serialization_internal.h"
11-
#include "libdcnode/dronecan.h"
12-
#define UAVCAN_COARSE_ORIENTATION_MESSAGE_SIZE 2
10+
#include "libdcnode/dronecan.hpp"
11+
#define UAVCAN_COARSE_ORIENTATION_MESSAGE_SIZE 2
1312

1413
/**
1514
* @brief uavcan.CoarseOrientation beam_orientation_in_body_frame
@@ -23,42 +22,47 @@
2322
* - Allows to exactly represent the following angles:
2423
* 0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, and negatives
2524
*/
26-
typedef struct {
27-
int8_t fixed_axis_roll_pitch_yaw [3];
25+
typedef struct
26+
{
27+
int8_t fixed_axis_roll_pitch_yaw[3];
2828
bool orientation_defined;
2929
} CoarseOrientation_t;
3030

3131
#ifdef __cplusplus
32-
extern "C" {
32+
extern "C"
33+
{
3334
#endif
3435

35-
36-
static inline int8_t dronecan_coarse_orientation_serialize(
37-
const CoarseOrientation_t* const obj, uint8_t* const buffer,
38-
size_t* const inout_buffer_size_bytes, uint8_t buffer_offset) {
36+
static inline int8_t dronecan_coarse_orientation_serialize(
37+
const CoarseOrientation_t *const obj, uint8_t *const buffer,
38+
size_t *const inout_buffer_size_bytes, uint8_t buffer_offset)
39+
{
3940
if ((obj == NULL) || (buffer == NULL) ||
40-
(inout_buffer_size_bytes == NULL)) {
41-
return -2;
42-
}
41+
(inout_buffer_size_bytes == NULL))
42+
{
43+
return -2;
44+
}
4345

44-
const size_t capacity_bytes = *inout_buffer_size_bytes - buffer_offset;
45-
if (capacity_bytes < UAVCAN_COARSE_ORIENTATION_MESSAGE_SIZE) {
46-
return -3;
46+
const size_t capacity_bytes = *inout_buffer_size_bytes - buffer_offset;
47+
if (capacity_bytes < UAVCAN_COARSE_ORIENTATION_MESSAGE_SIZE)
48+
{
49+
return -3;
50+
}
51+
int8_t size = 0;
52+
for (int i = 0; i < 3; i++)
53+
{
54+
canardEncodeScalar(buffer, buffer_offset, 5, &obj->fixed_axis_roll_pitch_yaw[i]);
55+
buffer_offset += 5;
56+
size += 5;
57+
}
58+
bool orientation_defined = obj->orientation_defined;
59+
canardEncodeScalar(buffer, buffer_offset, 1, &orientation_defined);
60+
buffer_offset += 1;
61+
return buffer_offset;
4762
}
48-
int8_t size = 0;
49-
for (int i = 0; i < 3; i++) {
50-
canardEncodeScalar(buffer, buffer_offset, 5, &obj->fixed_axis_roll_pitch_yaw[i]);
51-
buffer_offset += 5;
52-
size += 5;
53-
}
54-
bool orientation_defined = obj->orientation_defined;
55-
canardEncodeScalar(buffer, buffer_offset, 1, &orientation_defined);
56-
buffer_offset += 1;
57-
return buffer_offset;
58-
}
5963

6064
#ifdef __cplusplus
6165
}
6266
#endif
6367

64-
#endif // UAVCAN_COARCE_ORIENTATION_H_
68+
#endif // UAVCAN_COARCE_ORIENTATION_H_

0 commit comments

Comments
 (0)