Firmware SDK
twr_radio_node.c
1 #include <twr_radio_node.h>
2 
3 __attribute__((weak)) void twr_radio_node_on_state_set(uint64_t *id, uint8_t state_id, bool *state) { (void) id; (void) state_id; (void) state; }
4 __attribute__((weak)) void twr_radio_node_on_state_get(uint64_t *id, uint8_t state_id) { (void) id; (void) state_id; }
5 __attribute__((weak)) void twr_radio_node_on_buffer(uint64_t *id, void *buffer, size_t length) { (void) id; (void) buffer; (void) length; }
6 __attribute__((weak)) void twr_radio_node_on_led_strip_color_set(uint64_t *id, uint32_t *color) { (void) id; (void) color; }
7 __attribute__((weak)) void twr_radio_node_on_led_strip_brightness_set(uint64_t *id, uint8_t *brightness) { (void) id; (void) brightness; }
8 __attribute__((weak)) void twr_radio_node_on_led_strip_compound_set(uint64_t *id, uint8_t *compound, size_t length) { (void) id; (void) compound; (void) length; }
9 __attribute__((weak)) void twr_radio_node_on_led_strip_effect_set(uint64_t *id, twr_radio_node_led_strip_effect_t type, uint16_t wait, uint32_t *color) { (void) id; (void) type; (void) wait; (void) color; }
10 __attribute__((weak)) void twr_radio_node_on_led_strip_thermometer_set(uint64_t *id, float *temperature, int8_t *min, int8_t *max, uint8_t *white_dots, float *set_point, uint32_t *set_point_color) { (void) id; (void) temperature; (void) min; (void) max; (void) white_dots; (void) set_point; (void) set_point_color; }
11 
12 
13 bool twr_radio_node_state_set(uint64_t *id, uint8_t state_id, bool *state)
14 {
15  uint8_t buffer[1 + TWR_RADIO_ID_SIZE + sizeof(state_id) + sizeof(*state)];
16 
17  buffer[0] = TWR_RADIO_HEADER_NODE_STATE_SET;
18 
19  uint8_t *pbuffer = twr_radio_id_to_buffer(id, buffer + 1);
20 
21  pbuffer[0] = state_id;
22 
23  twr_radio_bool_to_buffer(state, pbuffer + 1);
24 
25  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
26 }
27 
28 bool twr_radio_node_state_get(uint64_t *id, uint8_t state_id)
29 {
30  uint8_t buffer[1 + TWR_RADIO_ID_SIZE + sizeof(state_id)];
31 
32  buffer[0] = TWR_RADIO_HEADER_NODE_STATE_GET;
33 
34  uint8_t *pbuffer = twr_radio_id_to_buffer(id, buffer + 1);
35 
36  pbuffer[0] = state_id;
37 
38  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
39 }
40 
41 bool twr_radio_node_buffer(uint64_t *id, void *buffer, size_t length)
42 {
43  uint8_t qbuffer[1 + TWR_RADIO_ID_SIZE + TWR_RADIO_NODE_MAX_BUFFER_SIZE];
44 
45  if (length > TWR_RADIO_NODE_MAX_BUFFER_SIZE)
46  {
47  return false;
48  }
49 
50  qbuffer[0] = TWR_RADIO_HEADER_NODE_BUFFER;
51  twr_radio_id_to_buffer(id, qbuffer + 1);
52 
53  memcpy(qbuffer + 1 + TWR_RADIO_ID_SIZE, buffer, length);
54 
55  return twr_radio_pub_queue_put(qbuffer, length + 1 + TWR_RADIO_ID_SIZE);
56 }
57 
58 bool twr_radio_node_led_strip_color_set(uint64_t *id, uint32_t color)
59 {
60  uint8_t buffer[1 + TWR_RADIO_ID_SIZE + sizeof(color)];
61 
62  buffer[0] = TWR_RADIO_HEADER_NODE_LED_STRIP_COLOR_SET;
63 
64  uint8_t *pbuffer = twr_radio_id_to_buffer(id, buffer + 1);
65 
66  twr_radio_data_to_buffer(&color, sizeof(color), pbuffer);
67 
68  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
69 }
70 
71 bool twr_radio_node_led_strip_brightness_set(uint64_t *id, uint8_t brightness)
72 {
73  uint8_t buffer[1 + TWR_RADIO_ID_SIZE + sizeof(brightness)];
74 
75  buffer[0] = TWR_RADIO_HEADER_NODE_LED_STRIP_BRIGHTNESS_SET;
76 
77  uint8_t *pbuffer = twr_radio_id_to_buffer(id, buffer + 1);
78 
79  pbuffer[0] = brightness;
80 
81  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
82 }
83 
84 bool twr_radio_node_led_strip_compound_set(uint64_t *id, uint8_t *compound, size_t length)
85 {
86  if ((length > TWR_RADIO_NODE_MAX_BUFFER_SIZE) || (length % 5 != 0))
87  {
88  return false;
89  }
90 
91  uint8_t buffer[1 + TWR_RADIO_ID_SIZE + TWR_RADIO_NODE_MAX_BUFFER_SIZE];
92 
93  buffer[0] = TWR_RADIO_HEADER_NODE_LED_STRIP_COMPOUND_SET;
94 
95  uint8_t *pbuffer = twr_radio_id_to_buffer(id, buffer + 1);
96 
97  twr_radio_data_to_buffer(compound, length, pbuffer);
98 
99  return twr_radio_pub_queue_put(buffer, 1 + TWR_RADIO_ID_SIZE + length);
100 }
101 
102 bool twr_radio_node_led_strip_effect_set(uint64_t *id, twr_radio_node_led_strip_effect_t type, uint16_t wait, uint32_t color)
103 {
104  uint8_t buffer[1 + TWR_RADIO_ID_SIZE + 1 + sizeof(uint16_t) + sizeof(uint32_t)];
105 
106  buffer[0] = TWR_RADIO_HEADER_NODE_LED_STRIP_EFFECT_SET;
107 
108  uint8_t *pbuffer = twr_radio_id_to_buffer(id, buffer + 1);
109 
110  *pbuffer++ = type;
111 
112  *pbuffer++ = (uint16_t) wait;
113  *pbuffer++ = (uint16_t) wait >> 8;
114 
115  twr_radio_data_to_buffer(&color, sizeof(color), pbuffer);
116 
117  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
118 }
119 
120 bool twr_radio_node_led_strip_thermometer_set(uint64_t *id, float temperature, int8_t min, int8_t max, uint8_t white_dots, float *set_point, uint32_t set_point_color)
121 {
122  uint8_t buffer[1 + TWR_RADIO_ID_SIZE + sizeof(float) + sizeof(int8_t) + sizeof(int8_t) + sizeof(uint8_t) + sizeof(float) + sizeof(uint32_t)];
123 
124  buffer[0] = TWR_RADIO_HEADER_NODE_LED_STRIP_THERMOMETER_SET;
125 
126  uint8_t *pbuffer = twr_radio_id_to_buffer(id, buffer + 1);
127 
128  pbuffer = twr_radio_float_to_buffer(&temperature, pbuffer);
129 
130  *pbuffer++ = min;
131 
132  *pbuffer++ = max;
133 
134  *pbuffer++ = white_dots;
135 
136  if ((set_point == NULL) || isnan(*set_point) || *set_point == TWR_RADIO_NULL_FLOAT)
137  {
138  return twr_radio_pub_queue_put(buffer, sizeof(buffer) - sizeof(float) - sizeof(uint32_t));
139  }
140 
141  pbuffer = twr_radio_float_to_buffer(set_point, pbuffer);
142 
143  pbuffer = twr_radio_data_to_buffer(&set_point_color, sizeof(uint32_t), pbuffer);
144 
145  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
146 }
147 
148 void twr_radio_node_decode(uint64_t *id, uint8_t *buffer, size_t length)
149 {
150  (void) id;
151 
152  uint64_t for_id;
153 
154  if (length < TWR_RADIO_ID_SIZE + 1)
155  {
156  return;
157  }
158 
159  uint8_t *pbuffer = buffer + 1 + TWR_RADIO_ID_SIZE;
160  length = length - 1 - TWR_RADIO_ID_SIZE;
161 
162  if (buffer[0] == TWR_RADIO_HEADER_NODE_STATE_SET)
163  {
164  bool state;
165  bool *pstate;
166 
167  twr_radio_bool_from_buffer(pbuffer + 1, &state, &pstate);
168  twr_radio_id_from_buffer(buffer + 1, &for_id);
169  twr_radio_node_on_state_set(&for_id, pbuffer[0], pstate);
170  }
171  else if (buffer[0] == TWR_RADIO_HEADER_NODE_STATE_GET)
172  {
173  twr_radio_id_from_buffer(buffer + 1, &for_id);
174  twr_radio_node_on_state_get(&for_id, pbuffer[0]);
175  }
176  else if (buffer[0] == TWR_RADIO_HEADER_NODE_BUFFER)
177  {
178  twr_radio_node_on_buffer(id, pbuffer, length);
179  }
180  else if (buffer[0] == TWR_RADIO_HEADER_NODE_LED_STRIP_COLOR_SET)
181  {
182  uint32_t color;
183 
184  twr_radio_data_from_buffer(pbuffer, &color, sizeof(color));
185 
186  twr_radio_node_on_led_strip_color_set(id, &color);
187  }
188  else if (buffer[0] == TWR_RADIO_HEADER_NODE_LED_STRIP_BRIGHTNESS_SET)
189  {
190  twr_radio_node_on_led_strip_brightness_set(id, pbuffer);
191  }
192  else if (buffer[0] == TWR_RADIO_HEADER_NODE_LED_STRIP_COMPOUND_SET)
193  {
194  twr_radio_node_on_led_strip_compound_set(id, pbuffer, length);
195  }
196  else if (buffer[0] == TWR_RADIO_HEADER_NODE_LED_STRIP_EFFECT_SET)
197  {
198  twr_radio_node_led_strip_effect_t type = (twr_radio_node_led_strip_effect_t) *pbuffer++;
199 
200  uint16_t wait = (uint16_t) *pbuffer++;
201  wait |= (uint16_t) *pbuffer++ >> 8;
202 
203  uint32_t color;
204 
205  twr_radio_data_from_buffer(pbuffer, &color, sizeof(color));
206 
207  twr_radio_node_on_led_strip_effect_set(id, type, wait, &color);
208  }
209  else if (buffer[0] == TWR_RADIO_HEADER_NODE_LED_STRIP_THERMOMETER_SET)
210  {
211  float temperature;
212  float *ptemperature;
213  float set_point = 0;
214  float *pset_point = NULL;
215  uint32_t color = 0;
216 
217  pbuffer = twr_radio_float_from_buffer(pbuffer, &temperature, &ptemperature);
218  int8_t *min = (int8_t *) pbuffer;
219  int8_t *max = (int8_t *) pbuffer + 1;
220  uint8_t *white_dots = (uint8_t *) pbuffer + 2;
221 
222  if (length == sizeof(float) + sizeof(int8_t) + sizeof(int8_t) + sizeof(uint8_t) + sizeof(float) + sizeof(uint32_t))
223  {
224  pbuffer = twr_radio_float_from_buffer(pbuffer + 3, &set_point, &pset_point);
225 
226  twr_radio_data_from_buffer(pbuffer, &color, sizeof(color));
227  }
228 
229  twr_radio_node_on_led_strip_thermometer_set(id, ptemperature, min, max, white_dots, pset_point, &color);
230  }
231 }
bool twr_radio_node_led_strip_brightness_set(uint64_t *id, uint8_t brightness)
Send data to node.
void twr_radio_node_decode(uint64_t *id, uint8_t *buffer, size_t length)
Internal decode function for twr_radio.c.
bool twr_radio_node_led_strip_effect_set(uint64_t *id, twr_radio_node_led_strip_effect_t type, uint16_t wait, uint32_t color)
Send data to node.
bool twr_radio_node_led_strip_thermometer_set(uint64_t *id, float temperature, int8_t min, int8_t max, uint8_t white_dots, float *set_point, uint32_t set_point_color)
Send data to node.
bool twr_radio_node_led_strip_color_set(uint64_t *id, uint32_t color)
Send data to node.
bool twr_radio_node_state_get(uint64_t *id, uint8_t state_id)
Send request for get actual state.
bool twr_radio_node_led_strip_compound_set(uint64_t *id, uint8_t *compound, size_t length)
Send data to node.
bool twr_radio_node_buffer(uint64_t *id, void *buffer, size_t length)
Send data to node.
bool twr_radio_node_state_set(uint64_t *id, uint8_t state_id, bool *state)
Send request for set new state.