1 #include <twr_radio_pub.h>
3 #define _TWR_RADIO_PUB_BUFFER_SIZE_ACCELERATION (1 + sizeof(float) + sizeof(float) + sizeof(float))
5 __attribute__((weak))
void twr_radio_pub_on_event_count(uint64_t *
id, uint8_t event_id, uint16_t *event_count) { (void)
id; (void) event_id; (void) event_count; }
6 __attribute__((weak))
void twr_radio_pub_on_push_button(uint64_t *
id, uint16_t *event_count) { (void)
id; (void) event_count; }
7 __attribute__((weak))
void twr_radio_pub_on_temperature(uint64_t *
id, uint8_t channel,
float *celsius) { (void)
id; (void) channel; (void) celsius; }
8 __attribute__((weak))
void twr_radio_pub_on_humidity(uint64_t *
id, uint8_t channel,
float *percentage) { (void)
id; (void) channel; (void) percentage; }
9 __attribute__((weak))
void twr_radio_pub_on_lux_meter(uint64_t *
id, uint8_t channel,
float *illuminance) { (void)
id; (void) channel; (void) illuminance; }
10 __attribute__((weak))
void twr_radio_pub_on_barometer(uint64_t *
id, uint8_t channel,
float *pressure,
float *altitude) { (void)
id; (void) channel; (void) pressure; (void) altitude; }
11 __attribute__((weak))
void twr_radio_pub_on_co2(uint64_t *
id,
float *concentration) { (void)
id; (void) concentration; }
12 __attribute__((weak))
void twr_radio_pub_on_battery(uint64_t *
id,
float *voltage) { (void)
id; (void) voltage; }
13 __attribute__((weak))
void twr_radio_pub_on_acceleration(uint64_t *
id,
float *x_axis,
float *y_axis,
float *z_axis) { (void)
id; (void) x_axis; (void) y_axis; (void) z_axis; }
14 __attribute__((weak))
void twr_radio_pub_on_buffer(uint64_t *
id,
void *buffer,
size_t length) { (void)
id; (void) buffer; (void) length; }
15 __attribute__((weak))
void twr_radio_pub_on_state(uint64_t *
id, uint8_t state_id,
bool *state) { (void)
id; (void) state_id; (void) state; }
16 __attribute__((weak))
void twr_radio_pub_on_bool(uint64_t *
id,
char *subtopic,
bool *value) { (void)
id; (void) subtopic; (void) value; }
17 __attribute__((weak))
void twr_radio_pub_on_int(uint64_t *
id,
char *subtopic,
int *value) { (void)
id; (void) subtopic; (void) value; }
18 __attribute__((weak))
void twr_radio_pub_on_uint32(uint64_t *
id,
char *subtopic, uint32_t *value) { (void)
id; (void) subtopic; (void) value; }
19 __attribute__((weak))
void twr_radio_pub_on_float(uint64_t *
id,
char *subtopic,
float *value) { (void)
id; (void) subtopic; (void) value; }
20 __attribute__((weak))
void twr_radio_pub_on_string(uint64_t *
id,
char *subtopic,
char *value) { (void)
id; (void) subtopic; (void) value; }
21 __attribute__((weak))
void twr_radio_pub_on_value_int(uint64_t *
id, uint8_t value_id,
int *value) { (void)
id; (void) value_id; (void) value; }
26 uint8_t buffer[1 +
sizeof(event_id) +
sizeof(*event_count)];
28 buffer[0] = TWR_RADIO_HEADER_PUB_EVENT_COUNT;
31 twr_radio_uint16_to_buffer(event_count, buffer + 2);
33 return twr_radio_pub_queue_put(buffer,
sizeof(buffer));
43 uint8_t buffer[2 +
sizeof(*celsius)];
45 buffer[0] = TWR_RADIO_HEADER_PUB_TEMPERATURE;
48 twr_radio_float_to_buffer(celsius, buffer + 2);
50 return twr_radio_pub_queue_put(buffer,
sizeof(buffer));
55 uint8_t buffer[2 +
sizeof(*percentage)];
57 buffer[0] = TWR_RADIO_HEADER_PUB_HUMIDITY;
60 twr_radio_float_to_buffer(percentage, buffer + 2);
62 return twr_radio_pub_queue_put(buffer,
sizeof(buffer));
67 uint8_t buffer[2 +
sizeof(*lux)];
69 buffer[0] = TWR_RADIO_HEADER_PUB_LUX_METER;
72 twr_radio_float_to_buffer(lux, buffer + 2);
74 return twr_radio_pub_queue_put(buffer,
sizeof(buffer));
79 uint8_t buffer[2 +
sizeof(*pascal) +
sizeof(*meter)];
81 buffer[0] = TWR_RADIO_HEADER_PUB_BAROMETER;
84 uint8_t *pointer = twr_radio_float_to_buffer(pascal, buffer + 2);
85 twr_radio_float_to_buffer(meter, pointer);
87 return twr_radio_pub_queue_put(buffer,
sizeof(buffer));
92 uint8_t buffer[1 +
sizeof(*concentration)];
94 buffer[0] = TWR_RADIO_HEADER_PUB_CO2;
96 twr_radio_float_to_buffer(concentration, buffer + 1);
98 return twr_radio_pub_queue_put(buffer,
sizeof(buffer));
103 uint8_t buffer[1 +
sizeof(*voltage)];
105 buffer[0] = TWR_RADIO_HEADER_PUB_BATTERY;
107 twr_radio_float_to_buffer(voltage, buffer + 1);
109 return twr_radio_pub_queue_put(buffer,
sizeof(buffer));
114 uint8_t buffer[_TWR_RADIO_PUB_BUFFER_SIZE_ACCELERATION];
116 buffer[0] = TWR_RADIO_HEADER_PUB_ACCELERATION;
118 uint8_t *pointer = twr_radio_float_to_buffer(x_axis, buffer + 1);
120 pointer = twr_radio_float_to_buffer(y_axis, pointer);
122 pointer = twr_radio_float_to_buffer(z_axis, pointer);
124 return twr_radio_pub_queue_put(buffer,
sizeof(buffer));
129 uint8_t qbuffer[TWR_RADIO_MAX_BUFFER_SIZE];
131 if (length >
sizeof(qbuffer) - 1)
136 qbuffer[0] = TWR_RADIO_HEADER_PUB_BUFFER;
138 memcpy(&qbuffer[1], buffer, length);
140 return twr_radio_pub_queue_put(qbuffer, length + 1);
145 uint8_t buffer[1 +
sizeof(state_id) +
sizeof(*state)];
147 buffer[0] = TWR_RADIO_HEADER_PUB_STATE;
148 buffer[1] = state_id;
150 twr_radio_bool_to_buffer(state, buffer + 2);
152 return twr_radio_pub_queue_put(buffer,
sizeof(buffer));
157 uint8_t buffer[1 +
sizeof(uint8_t) +
sizeof(
int)];
159 buffer[0] = TWR_RADIO_HEADER_PUB_VALUE_INT;
160 buffer[1] = value_id;
162 twr_radio_int_to_buffer(value, buffer + 2);
164 return twr_radio_pub_queue_put(buffer,
sizeof(buffer));
169 size_t len = strlen(subtopic);
171 if (len > TWR_RADIO_MAX_TOPIC_LEN)
176 uint8_t buffer[TWR_RADIO_MAX_BUFFER_SIZE];
178 buffer[0] = TWR_RADIO_HEADER_PUB_TOPIC_BOOL;
180 twr_radio_bool_to_buffer(value, buffer + 1);
182 strcpy((
char *)buffer + 2, subtopic);
184 return twr_radio_pub_queue_put(buffer, len + 3);
189 size_t len = strlen(subtopic);
191 if (len > TWR_RADIO_MAX_TOPIC_LEN)
196 uint8_t buffer[TWR_RADIO_MAX_BUFFER_SIZE];
198 buffer[0] = TWR_RADIO_HEADER_PUB_TOPIC_INT;
200 twr_radio_int_to_buffer(value, buffer + 1);
202 strcpy((
char *)buffer + 5, subtopic);
204 return twr_radio_pub_queue_put(buffer, len + 6);
209 size_t len = strlen(subtopic);
211 if (len > TWR_RADIO_MAX_TOPIC_LEN)
216 uint8_t buffer[TWR_RADIO_MAX_BUFFER_SIZE];
218 buffer[0] = TWR_RADIO_HEADER_PUB_TOPIC_UINT32;
220 twr_radio_uint32_to_buffer(value, buffer + 1);
222 strcpy((
char *)buffer + 5, subtopic);
224 return twr_radio_pub_queue_put(buffer, len + 6);
229 size_t len = strlen(subtopic);
231 if (len > TWR_RADIO_MAX_TOPIC_LEN)
236 uint8_t buffer[TWR_RADIO_MAX_BUFFER_SIZE];
238 buffer[0] = TWR_RADIO_HEADER_PUB_TOPIC_FLOAT;
240 twr_radio_float_to_buffer(value, buffer + 1);
242 strcpy((
char *)buffer + 5, subtopic);
244 return twr_radio_pub_queue_put(buffer, len + 6);
249 size_t len = strlen(subtopic);
250 size_t len_value = strlen(value);
252 if (len + len_value > (TWR_RADIO_MAX_BUFFER_SIZE - 3))
257 uint8_t buffer[TWR_RADIO_MAX_BUFFER_SIZE];
259 buffer[0] = TWR_RADIO_HEADER_PUB_TOPIC_STRING;
261 strcpy((
char *)buffer + 1, subtopic);
262 strcpy((
char *)buffer + 1 + len + 1, value);
264 return twr_radio_pub_queue_put(buffer, len + len_value + 3);
270 if (buffer[0] == TWR_RADIO_HEADER_PUB_PUSH_BUTTON)
272 uint16_t event_count;
273 uint16_t *pevent_count;
275 twr_radio_uint16_from_buffer(buffer + 1, &event_count, &pevent_count);
277 twr_radio_pub_on_push_button(
id, &event_count);
279 twr_radio_pub_on_event_count(
id, TWR_RADIO_PUB_EVENT_PUSH_BUTTON, pevent_count);
281 else if (buffer[0] == TWR_RADIO_HEADER_PUB_EVENT_COUNT)
283 uint16_t event_count;
284 uint16_t *pevent_count;
286 if (length != (1 +
sizeof(uint8_t) +
sizeof(uint16_t)))
291 twr_radio_uint16_from_buffer(buffer + 2, &event_count, &pevent_count);
293 if (buffer[1] == TWR_RADIO_PUB_EVENT_PUSH_BUTTON)
295 twr_radio_pub_on_push_button(
id, pevent_count);
298 twr_radio_pub_on_event_count(
id, buffer[1], pevent_count);
300 else if (buffer[0] == TWR_RADIO_HEADER_PUB_TEMPERATURE)
305 if (length != (1 +
sizeof(uint8_t) +
sizeof(
float)))
310 twr_radio_float_from_buffer(buffer + 2, &celsius, &pcelsius);
312 twr_radio_pub_on_temperature(
id, buffer[1], pcelsius);
314 else if (buffer[0] == TWR_RADIO_HEADER_PUB_HUMIDITY)
319 if (length != (1 +
sizeof(uint8_t) +
sizeof(
float)))
324 twr_radio_float_from_buffer(buffer + 2, &percentage, &ppercentage);
326 twr_radio_pub_on_humidity(
id, buffer[1], ppercentage);
328 else if (buffer[0] == TWR_RADIO_HEADER_PUB_LUX_METER)
333 if (length != (1 +
sizeof(uint8_t) +
sizeof(
float)))
338 twr_radio_float_from_buffer(buffer + 2, &lux, &plux);
340 twr_radio_pub_on_lux_meter(
id, buffer[1], plux);
342 else if (buffer[0] == TWR_RADIO_HEADER_PUB_BAROMETER)
349 if (length != (1 +
sizeof(uint8_t) +
sizeof(
float) +
sizeof(
float)))
354 uint8_t *pointer = twr_radio_float_from_buffer(buffer + 2, &pascal, &ppascal);
356 twr_radio_float_from_buffer(pointer, &meter, &pmeter);
358 twr_radio_pub_on_barometer(
id, buffer[1], ppascal, pmeter);
360 else if (buffer[0] == TWR_RADIO_HEADER_PUB_CO2)
363 float *pconcentration;
365 if (length != (1 +
sizeof(
float)))
370 twr_radio_float_from_buffer(buffer + 1, &concentration, &pconcentration);
372 twr_radio_pub_on_co2(
id, pconcentration);
374 else if (buffer[0] == TWR_RADIO_HEADER_PUB_BATTERY)
379 if (length == (1 +
sizeof(
float)))
381 twr_radio_float_from_buffer(buffer + 1, &voltage, &pvoltage);
383 else if (length == (1 + 1 +
sizeof(
float)))
386 twr_radio_float_from_buffer(buffer + 2, &voltage, &pvoltage);
393 twr_radio_pub_on_battery(
id, pvoltage);
395 else if (buffer[0] == TWR_RADIO_HEADER_PUB_ACCELERATION)
397 if (length != _TWR_RADIO_PUB_BUFFER_SIZE_ACCELERATION)
409 buffer = twr_radio_float_from_buffer(buffer + 1, &x_axis, &px_axis);
411 buffer = twr_radio_float_from_buffer(buffer, &y_axis, &py_axis);
413 twr_radio_float_from_buffer(buffer, &z_axis, &pz_axis);
415 twr_radio_pub_on_acceleration(
id, px_axis, py_axis, pz_axis);
417 else if (buffer[0] == TWR_RADIO_HEADER_PUB_BUFFER)
419 twr_radio_pub_on_buffer(
id, buffer + 1, length - 1);
421 else if (buffer[0] == TWR_RADIO_HEADER_PUB_STATE)
426 if (length != (1 +
sizeof(uint8_t) +
sizeof(
bool)))
431 twr_radio_bool_from_buffer(buffer + 2, &state, &pstate);
433 twr_radio_pub_on_state(
id, buffer[1], pstate);
435 else if (buffer[0] == TWR_RADIO_HEADER_PUB_TOPIC_BOOL)
440 buffer = twr_radio_bool_from_buffer(buffer + 1, &value, &pvalue);
442 buffer[length - 1] = 0;
444 twr_radio_pub_on_bool(
id, (
char *) buffer, pvalue);
446 else if (buffer[0] == TWR_RADIO_HEADER_PUB_TOPIC_INT)
451 buffer = twr_radio_int_from_buffer(buffer + 1, &value, &pvalue);
453 buffer[length - 1] = 0;
455 twr_radio_pub_on_int(
id, (
char *) buffer, pvalue);
457 else if (buffer[0] == TWR_RADIO_HEADER_PUB_TOPIC_UINT32)
462 buffer = twr_radio_uint32_from_buffer(buffer + 1, &value, &pvalue);
464 buffer[length - 1] = 0;
466 twr_radio_pub_on_uint32(
id, (
char *) buffer, pvalue);
468 else if (buffer[0] == TWR_RADIO_HEADER_PUB_TOPIC_FLOAT)
473 buffer = twr_radio_float_from_buffer(buffer + 1, &value, &pvalue);
475 buffer[length - 1] = 0;
477 twr_radio_pub_on_float(
id, (
char *) buffer, pvalue);
479 else if (buffer[0] == TWR_RADIO_HEADER_PUB_TOPIC_STRING)
481 buffer[length - 1] = 0;
483 size_t len = strlen((
char *) buffer + 1);
485 twr_radio_pub_on_string(
id, (
char *) buffer + 1, (
char *) buffer + 2 + len);
487 else if (buffer[0] == TWR_RADIO_HEADER_PUB_VALUE_INT)
492 if (length != (1 +
sizeof(uint8_t) +
sizeof(
int)))
497 twr_radio_int_from_buffer(buffer + 2, &value, &pvalue);
499 twr_radio_pub_on_value_int(
id, buffer[1], pvalue);
bool twr_radio_pub_float(const char *subtopic, float *value)
Publish float value in custom topic.
bool twr_radio_pub_luminosity(uint8_t channel, float *lux)
Publish luminosity.
bool twr_radio_pub_value_int(uint8_t value_id, int *value)
Publish int value.
bool twr_radio_pub_state(uint8_t state_id, bool *state)
Publish battery.
bool twr_radio_pub_event_count(uint8_t event_id, uint16_t *event_count)
Publish event count.
bool twr_radio_pub_barometer(uint8_t channel, float *pascal, float *meter)
Publish barometer.
bool twr_radio_pub_acceleration(float *x_axis, float *y_axis, float *z_axis)
Publish acceleration.
bool twr_radio_pub_humidity(uint8_t channel, float *percentage)
Publish humidity.
bool twr_radio_pub_int(const char *subtopic, int *value)
Publish int value in custom topic.
bool twr_radio_pub_string(const char *subtopic, const char *value)
Publish string value in custom topic.
bool twr_radio_pub_temperature(uint8_t channel, float *celsius)
Publish temperature.
bool twr_radio_pub_uint32(const char *subtopic, uint32_t *value)
Publish uint32 value in custom topic.
bool twr_radio_pub_buffer(void *buffer, size_t length)
Publish buffer.
bool twr_radio_pub_battery(float *voltage)
Publish battery.
bool twr_radio_pub_push_button(uint16_t *event_count)
Publish push button event count, same as use twr_radio_pub_event_count with TWR_RADIO_PUB_EVENT_PUSH_...
bool twr_radio_pub_bool(const char *subtopic, bool *value)
Publish bool value in custom topic.
void twr_radio_pub_decode(uint64_t *id, uint8_t *buffer, size_t length)
Internal decode function for twr_radio.c.
bool twr_radio_pub_co2(float *concentration)
Publish co2.