Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_radio_node.c
1 #include <bc_radio_node.h>
2 
3 __attribute__((weak)) void bc_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 bc_radio_node_on_state_get(uint64_t *id, uint8_t state_id) { (void) id; (void) state_id; }
5 __attribute__((weak)) void bc_radio_node_on_buffer(uint64_t *id, void *buffer, size_t length) { (void) id; (void) buffer; (void) length; }
6 __attribute__((weak)) void bc_radio_node_on_led_strip_color_set(uint64_t *id, uint32_t *color) { (void) id; (void) color; }
7 __attribute__((weak)) void bc_radio_node_on_led_strip_brightness_set(uint64_t *id, uint8_t *brightness) { (void) id; (void) brightness; }
8 __attribute__((weak)) void bc_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 bc_radio_node_on_led_strip_effect_set(uint64_t *id, bc_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 bc_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 bc_radio_node_state_set(uint64_t *id, uint8_t state_id, bool *state)
14 {
15  uint8_t buffer[1 + BC_RADIO_ID_SIZE + sizeof(state_id) + sizeof(*state)];
16 
17  buffer[0] = BC_RADIO_HEADER_NODE_STATE_SET;
18 
19  uint8_t *pbuffer = bc_radio_id_to_buffer(id, buffer + 1);
20 
21  pbuffer[0] = state_id;
22 
23  bc_radio_bool_to_buffer(state, pbuffer + 1);
24 
25  return bc_radio_pub_queue_put(buffer, sizeof(buffer));
26 }
27 
28 bool bc_radio_node_state_get(uint64_t *id, uint8_t state_id)
29 {
30  uint8_t buffer[1 + BC_RADIO_ID_SIZE + sizeof(state_id)];
31 
32  buffer[0] = BC_RADIO_HEADER_NODE_STATE_GET;
33 
34  uint8_t *pbuffer = bc_radio_id_to_buffer(id, buffer + 1);
35 
36  pbuffer[0] = state_id;
37 
38  return bc_radio_pub_queue_put(buffer, sizeof(buffer));
39 }
40 
41 bool bc_radio_node_buffer(uint64_t *id, void *buffer, size_t length)
42 {
43  uint8_t qbuffer[1 + BC_RADIO_ID_SIZE + BC_RADIO_NODE_MAX_BUFFER_SIZE];
44 
45  if (length > BC_RADIO_NODE_MAX_BUFFER_SIZE)
46  {
47  return false;
48  }
49 
50  qbuffer[0] = BC_RADIO_HEADER_NODE_BUFFER;
51  bc_radio_id_to_buffer(id, qbuffer + 1);
52 
53  memcpy(qbuffer + 1 + BC_RADIO_ID_SIZE, buffer, length);
54 
55  return bc_radio_pub_queue_put(qbuffer, length + 1 + BC_RADIO_ID_SIZE);
56 }
57 
58 bool bc_radio_node_led_strip_color_set(uint64_t *id, uint32_t color)
59 {
60  uint8_t buffer[1 + BC_RADIO_ID_SIZE + sizeof(color)];
61 
62  buffer[0] = BC_RADIO_HEADER_NODE_LED_STRIP_COLOR_SET;
63 
64  uint8_t *pbuffer = bc_radio_id_to_buffer(id, buffer + 1);
65 
66  bc_radio_data_to_buffer(&color, sizeof(color), pbuffer);
67 
68  return bc_radio_pub_queue_put(buffer, sizeof(buffer));
69 }
70 
71 bool bc_radio_node_led_strip_brightness_set(uint64_t *id, uint8_t brightness)
72 {
73  uint8_t buffer[1 + BC_RADIO_ID_SIZE + sizeof(brightness)];
74 
75  buffer[0] = BC_RADIO_HEADER_NODE_LED_STRIP_BRIGHTNESS_SET;
76 
77  uint8_t *pbuffer = bc_radio_id_to_buffer(id, buffer + 1);
78 
79  pbuffer[0] = brightness;
80 
81  return bc_radio_pub_queue_put(buffer, sizeof(buffer));
82 }
83 
84 bool bc_radio_node_led_strip_compound_set(uint64_t *id, uint8_t *compound, size_t length)
85 {
86  if ((length > BC_RADIO_NODE_MAX_BUFFER_SIZE) || (length % 5 != 0))
87  {
88  return false;
89  }
90 
91  uint8_t buffer[1 + BC_RADIO_ID_SIZE + BC_RADIO_NODE_MAX_BUFFER_SIZE];
92 
93  buffer[0] = BC_RADIO_HEADER_NODE_LED_STRIP_COMPOUND_SET;
94 
95  uint8_t *pbuffer = bc_radio_id_to_buffer(id, buffer + 1);
96 
97  bc_radio_data_to_buffer(compound, length, pbuffer);
98 
99  return bc_radio_pub_queue_put(buffer, 1 + BC_RADIO_ID_SIZE + length);
100 }
101 
102 bool bc_radio_node_led_strip_effect_set(uint64_t *id, bc_radio_node_led_strip_effect_t type, uint16_t wait, uint32_t color)
103 {
104  uint8_t buffer[1 + BC_RADIO_ID_SIZE + 1 + sizeof(uint16_t) + sizeof(uint32_t)];
105 
106  buffer[0] = BC_RADIO_HEADER_NODE_LED_STRIP_EFFECT_SET;
107 
108  uint8_t *pbuffer = bc_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  bc_radio_data_to_buffer(&color, sizeof(color), pbuffer);
116 
117  return bc_radio_pub_queue_put(buffer, sizeof(buffer));
118 }
119 
120 bool bc_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 + BC_RADIO_ID_SIZE + sizeof(float) + sizeof(int8_t) + sizeof(int8_t) + sizeof(uint8_t) + sizeof(float) + sizeof(uint32_t)];
123 
124  buffer[0] = BC_RADIO_HEADER_NODE_LED_STRIP_THERMOMETER_SET;
125 
126  uint8_t *pbuffer = bc_radio_id_to_buffer(id, buffer + 1);
127 
128  pbuffer = bc_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 == BC_RADIO_NULL_FLOAT)
137  {
138  return bc_radio_pub_queue_put(buffer, sizeof(buffer) - sizeof(float) - sizeof(uint32_t));
139  }
140 
141  pbuffer = bc_radio_float_to_buffer(set_point, pbuffer);
142 
143  pbuffer = bc_radio_data_to_buffer(&set_point_color, sizeof(uint32_t), pbuffer);
144 
145  return bc_radio_pub_queue_put(buffer, sizeof(buffer));
146 }
147 
148 void bc_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 < BC_RADIO_ID_SIZE + 1)
155  {
156  return;
157  }
158 
159  uint8_t *pbuffer = buffer + 1 + BC_RADIO_ID_SIZE;
160  length = length - 1 - BC_RADIO_ID_SIZE;
161 
162  if (buffer[0] == BC_RADIO_HEADER_NODE_STATE_SET)
163  {
164  bool state;
165  bool *pstate;
166 
167  bc_radio_bool_from_buffer(pbuffer + 1, &state, &pstate);
168 
169  bc_radio_node_on_state_set(&for_id, pbuffer[0], pstate);
170  }
171  else if (buffer[0] == BC_RADIO_HEADER_NODE_STATE_GET)
172  {
173  bc_radio_node_on_state_get(&for_id, pbuffer[0]);
174  }
175  else if (buffer[0] == BC_RADIO_HEADER_NODE_BUFFER)
176  {
177  bc_radio_node_on_buffer(id, pbuffer, length);
178  }
179  else if (buffer[0] == BC_RADIO_HEADER_NODE_LED_STRIP_COLOR_SET)
180  {
181  uint32_t color;
182 
183  bc_radio_data_from_buffer(pbuffer, &color, sizeof(color));
184 
185  bc_radio_node_on_led_strip_color_set(id, &color);
186  }
187  else if (buffer[0] == BC_RADIO_HEADER_NODE_LED_STRIP_BRIGHTNESS_SET)
188  {
189  bc_radio_node_on_led_strip_brightness_set(id, pbuffer);
190  }
191  else if (buffer[0] == BC_RADIO_HEADER_NODE_LED_STRIP_COMPOUND_SET)
192  {
193  bc_radio_node_on_led_strip_compound_set(id, pbuffer, length);
194  }
195  else if (buffer[0] == BC_RADIO_HEADER_NODE_LED_STRIP_EFFECT_SET)
196  {
197  bc_radio_node_led_strip_effect_t type = (bc_radio_node_led_strip_effect_t) *pbuffer++;
198 
199  uint16_t wait = (uint16_t) *pbuffer++;
200  wait |= (uint16_t) *pbuffer++ >> 8;
201 
202  uint32_t color;
203 
204  bc_radio_data_from_buffer(pbuffer, &color, sizeof(color));
205 
206  bc_radio_node_on_led_strip_effect_set(id, type, wait, &color);
207  }
208  else if (buffer[0] == BC_RADIO_HEADER_NODE_LED_STRIP_THERMOMETER_SET)
209  {
210  float temperature;
211  float *ptemperature;
212  float set_point = 0;
213  float *pset_point = NULL;
214  uint32_t color = 0;
215 
216  pbuffer = bc_radio_float_from_buffer(pbuffer, &temperature, &ptemperature);
217  int8_t *min = (int8_t *) pbuffer;
218  int8_t *max = (int8_t *) pbuffer + 1;
219  uint8_t *white_dots = (uint8_t *) pbuffer + 2;
220 
221  if (length == sizeof(float) + sizeof(int8_t) + sizeof(int8_t) + sizeof(uint8_t) + sizeof(float) + sizeof(uint32_t))
222  {
223  pbuffer = bc_radio_float_from_buffer(pbuffer + 3, &set_point, &pset_point);
224 
225  bc_radio_data_from_buffer(pbuffer, &color, sizeof(color));
226  }
227 
228  bc_radio_node_on_led_strip_thermometer_set(id, ptemperature, min, max, white_dots, pset_point, &color);
229  }
230 }
bool bc_radio_node_led_strip_effect_set(uint64_t *id, bc_radio_node_led_strip_effect_t type, uint16_t wait, uint32_t color)
Send data to node.
bool bc_radio_node_state_get(uint64_t *id, uint8_t state_id)
Send request for get actual state.
Definition: bc_radio_node.c:28
bool bc_radio_node_buffer(uint64_t *id, void *buffer, size_t length)
Send data to node.
Definition: bc_radio_node.c:41
bool bc_radio_node_led_strip_compound_set(uint64_t *id, uint8_t *compound, size_t length)
Send data to node.
Definition: bc_radio_node.c:84
bool bc_radio_node_state_set(uint64_t *id, uint8_t state_id, bool *state)
Send request for set new state.
Definition: bc_radio_node.c:13
bool bc_radio_node_led_strip_color_set(uint64_t *id, uint32_t color)
Send data to node.
Definition: bc_radio_node.c:58
bool bc_radio_node_led_strip_brightness_set(uint64_t *id, uint8_t brightness)
Send data to node.
Definition: bc_radio_node.c:71
void bc_radio_node_decode(uint64_t *id, uint8_t *buffer, size_t length)
Internal decode function for bc_radio.c.
bool bc_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.