Pogosim
Loading...
Searching...
No Matches
pogosim.h
Go to the documentation of this file.
1#ifndef POGOSIM_H
2#define POGOSIM_H
3
4#include <stdbool.h>
5
6#ifdef REAL_ROBOT
7#include "pogobot.h"
8#else
9#include "spogobot.h"
10#endif
11
12#include "colormaps.h"
13
14
16#if defined(__GNUC__) || defined(__clang__)
17// Static compile-time strcmp. Only available on GCC and CLANG
18#define STATIC_STRCMP(a,b) __builtin_strcmp(a,b)
19#else
20// Normal strcmp
21#define STATIC_STRCMP(a,b) strcmp(a,b)
22#endif
23#define STRINGIFY_RAW(x) #x
24#define STRINGIFY(x) STRINGIFY_RAW(x)
25
26
27// Macros to declare the mydata pointer
28#ifdef SIMULATOR // Compiling for the simulator
29#include <stddef.h>
30
31#define DECLARE_USERDATA(UDT) \
32 extern UDT *mydata;
33
34#define REGISTER_USERDATA(UDT) \
35 size_t UserdataSize = sizeof(UDT); \
36 UDT *mydata;
37
38#define SET_CALLBACK(CALLBACK_FN, FN) \
39 CALLBACK_FN = FN;
40
41extern void (*callback_create_data_schema)(void);
42extern void (*callback_export_data)(void);
43extern void (*callback_global_setup)(void);
44extern void (*callback_global_step)(void);
45extern void (*callback_robot_end)(void);
46extern void (*callback_robot_click)(void);
47
48#else // Compiling for real robots
49
50// It is possible to specify the category of robots to compile a binary for, using the ROBOT_CATEGORY variable
51// To compile for a given robot category (e.g. "robots2"), use a command like: make clean bin ROBOT_CATEGORY=robots2
52#ifndef ROBOT_CATEGORY
53#define ROBOT_CATEGORY robots // Default to "robots" category
54#endif
55
56// On real robots, declare an extern variable for a single shared instance.
57#ifdef __cplusplus
58 // C++ version - can use constexpr and noexcept for additional optimization
59 #define DECLARE_USERDATA(UDT) \
60 extern UDT myuserdata; \
61 static inline constexpr UDT * get_mydata(void) noexcept { return &myuserdata; }
62#else
63 // C version - uses restrict keyword for optimization
64 #define DECLARE_USERDATA(UDT) \
65 extern UDT myuserdata; \
66 static inline UDT * restrict get_mydata(void) { return &myuserdata; } // The accessor returns a restrict-qualified pointer.
67 // Here we use the keyword "restrict" to ensure that the compiler automatically transform mydata->foo statements into myuserdata.foo after optimization.
68 // This increases performance by removing pointer access operations.
69#endif
70
71/* Now, use mydata as an alias for the inline function result */
72#define mydata (get_mydata())
73
74#define REGISTER_USERDATA(UDT) \
75 UDT myuserdata = {0};
76
77#define SET_CALLBACK(CALLBACK_FN, FN)
78
79void user_init(void);
80void user_step(void);
81
82#endif // SIMULATOR
83
84
85#ifdef __cplusplus
86extern "C" {
87#endif
88
89extern uint32_t pogobot_ticks;
90
91extern uint8_t main_loop_hz;
93extern void (*msg_rx_fn)(message_t *);
94extern bool (*msg_tx_fn)(void);
95extern int8_t error_codes_led_idx;
98extern uint32_t _current_time_milliseconds;
99extern uint32_t _error_code_initial_time;
100
101extern uint8_t percent_msgs_sent_per_ticks;
102extern uint32_t nb_msgs_sent;
103extern uint32_t nb_msgs_recv;
104
109
110
111#define GET_MACRO_START(_1, _2, _3, NAME, ...) NAME
112
113#ifdef SIMULATOR // Compiling for the simulator
114const char* get_current_robot_category(void);
115bool current_robot_category_is(const char* category);
116
117void _pogobot_start(void (*user_init)(void), void (*user_step)(void), const char *object_category);
118#define pogobot_start_2(user_init, user_step) \
119 _pogobot_start((user_init), (user_step), "robots")
120
121#define pogobot_start_3(user_init, user_step, object_category) \
122 _pogobot_start((user_init), (user_step), (object_category))
123
124#define pogobot_start(...) GET_MACRO_START(__VA_ARGS__, pogobot_start_3, pogobot_start_2)(__VA_ARGS__)
125
126#else // Compiling for real robots
127#define get_current_robot_category() (STRINGIFY(ROBOT_CATEGORY))
128#define current_robot_category_is(category) (STATIC_STRCMP(get_current_robot_category(), category)==0)
129
130void _pogobot_start(void (*user_init)(void), void (*user_step)(void));
131#define pogobot_start_2(user_init, user_step) \
132 do { \
133 if (current_robot_category_is("robots")) { \
134 _pogobot_start((user_init), (user_step)); \
135 } \
136 } while (0)
137
138#define pogobot_start_3(user_init, user_step, object_category) \
139 do { \
140 if (current_robot_category_is(object_category)) { \
141 _pogobot_start((user_init), (user_step)); \
142 } \
143 } while (0)
144
145#define pogobot_start(...) GET_MACRO_START(__VA_ARGS__, pogobot_start_3, pogobot_start_2)(__VA_ARGS__)
146
147#endif
148
149void clear_IR_buffers(void);
150
151void pogo_main_loop_step(void (*user_step)(void));
152uint32_t current_time_milliseconds(void);
154
160void default_walls_user_init(void);
166void default_walls_user_step(void);
167
168#ifdef __cplusplus
169}
170#endif
171
172
173#endif // POGOSIM_H
174
175// MODELINE "{{{1
176// vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
177// vim:foldmethod=marker
void(* msg_rx_fn)(message_t *)
Definition pogosim.c:16
time_reference_t _global_timer
Definition pogosim.c:22
uint32_t nb_msgs_recv
Definition pogosim.c:27
uint8_t main_loop_hz
Definition pogosim.c:14
void(* callback_robot_end)(void)
Definition pogosim.c:10
void(* callback_robot_click)(void)
Definition pogosim.c:11
void(* callback_create_data_schema)(void)
Definition pogosim.c:6
uint32_t pogobot_ticks
Definition pogosim.c:19
void(* callback_global_step)(void)
Definition pogosim.c:9
uint8_t max_nb_processed_msg_per_tick
Definition pogosim.c:15
uint32_t _current_time_milliseconds
Definition pogosim.c:20
uint32_t nb_msgs_sent
Definition pogosim.c:26
bool(* msg_tx_fn)(void)
Definition pogosim.c:17
void(* callback_global_setup)(void)
Definition pogosim.c:8
uint8_t percent_msgs_sent_per_ticks
Definition pogosim.c:25
int8_t error_codes_led_idx
Definition pogosim.c:18
uint32_t _error_code_initial_time
Definition pogosim.c:21
time_reference_t timer_main_loop
Definition pogosim.c:23
void(* callback_export_data)(void)
Definition pogosim.c:7
const char * get_current_robot_category(void)
Definition pogosim_sim.cpp:17
error_code_t
Definition pogosim.h:105
@ error_code_t_last_entry
Definition pogosim.h:107
@ ERROR_TIME_OVERFLOW
Definition pogosim.h:106
uint32_t current_time_milliseconds(void)
Definition pogosim.c:80
void clear_IR_buffers(void)
Definition pogosim.c:30
void default_walls_user_step(void)
Main control loop for the Pogowalls.
Definition pogosim.c:191
void default_walls_user_init(void)
Initialization function for the pogowalls.
Definition pogosim.c:171
void display_led_error_code(error_code_t const c)
Definition pogosim.c:86
bool current_robot_category_is(const char *category)
Definition pogosim_sim.cpp:21
void pogo_main_loop_step(void(*user_step)(void))
Definition pogosim.c:98
void _pogobot_start(void(*user_init)(void), void(*user_step)(void), const char *object_category)
Definition pogosim_sim.cpp:7
Definition spogobot.h:205
Definition spogobot.h:877