KETCube
ketCube_common.h
Go to the documentation of this file.
1 
46 /* Define to prevent recursive inclusion -------------------------------------*/
47 #ifndef __KETCUBE_COMMON_H
48 #define __KETCUBE_COMMON_H
49 
50 /* Includes ------------------------------------------------------------------*/
51 #ifndef DESKTOP_BUILD
52 #include "stm32l0xx_hal.h"
53 #include "stm32l0xx_hal_gpio.h"
54 #else
55 #include "stdint.h"
56 #endif
57 #include "stdlib.h"
58 #include "stdbool.h"
59 
69 #define TRUE 1
70 #define FALSE 0
71 
72 /*typedef enum {
73  TRUE = 1,
74  FALSE = 0
75 } bool;*/
76 
77 typedef uint8_t byte;
78 
79 extern void KETCube_ErrorHandler(void);
80 
81 #define KETCUBE_COMMON_BUFFER_LEN 256
82 extern char ketCube_common_buffer[];
84 char * ketCube_common_bytes2Str(uint8_t * byteArr, uint8_t len);
85 
92 static inline void ketCube_common_Byte2hex(uint8_t byte, char *str)
93 {
94  if ((byte & 0x0F) < 10) {
95  str[1] = (byte & 0x0F) + '0';
96  } else {
97  str[1] = (byte & 0x0F) - 10 + 'A';
98  }
99 
100  if (((byte & 0xF0) >> 4) < 10) {
101  str[0] = ((byte & 0xF0) >> 4) + '0';
102  } else {
103  str[0] = ((byte & 0xF0) >> 4) - 10 + 'A';
104  }
105 }
106 
114 static inline void ketCube_common_Int2dec(int32_t number, char *str,
115  int len)
116 {
117  int pos = 0, rem;
118  char tmp;
119 
120  if (number < 0) {
121  (*str++) = '-'; // a dirty trick to avoid reversing the minus sign
122  len--;
123  number *= -1;
124  }
125 
126  while (number != 0 && pos < len) {
127  rem = number % 10;
128  number /= 10;
129 
130  str[pos++] = '0' + rem;
131  }
132 
133  for (int rem = 0; rem < pos / 2; rem++) {
134  tmp = str[rem];
135  str[rem] = str[pos - 1 - rem];
136  str[pos - 1 - rem] = tmp;
137  }
138 
139  str[pos] = '\0';
140 }
141 
151 static inline bool ketCube_common_IsHexString(const char *str, uint8_t len)
152 {
153  uint8_t i = 0;
154 
155  for (i = 0; i < len; i++) {
156  if ((str[i] >= 'A') && (str[i] <= 'F')) {
157  continue;
158  } else if ((str[i] >= 'a') && (str[i] <= 'f')) {
159  continue;
160  } else if ((str[i] >= '0') && (str[i] <= '9')) {
161  continue;
162  } else {
163  return FALSE;
164  }
165  }
166 
167  return TRUE;
168 }
169 
179 static inline bool ketCube_common_IsDecString(const char *str, uint8_t len)
180 {
181  uint8_t i = 0;
182 
183  for (i = 0; i < len && str[i] != '\0'; i++) {
184  if ((str[i] < '0') || (str[i] > '9'))
185  return FALSE;
186  }
187 
188  return TRUE;
189 }
190 
191 
201 static inline uint8_t ketCube_common_Hex2Val(const char c) {
202 
203  if ((c >= '0') && (c <= '9')) {
204  return (c - '0') & 0x0F;
205  } else if ((c >= 'A') && (c <= 'F')) {
206  return (c - 'A' + 10) & 0x0F;
207  } else if ((c >= 'a') && (c <= 'f')) {
208  return (c - 'a' + 10) & 0x0F;
209  } else {
210  return 0xFF;
211  }
212 
213 }
214 
224 static inline void ketCube_common_Hex2Bytes(uint8_t * bytes, const char *str,
225  uint8_t len)
226 {
227  uint8_t i = 0, j = 0;
228  uint8_t conv0, conv1;
229 
230  if (len == 0) {
231  return;
232  }
233 
234  for (i = 0; i < (len - 1); i += 2, j++) {
235  conv0 = ketCube_common_Hex2Val(str[i]);
236  conv1 = ketCube_common_Hex2Val(str[i + 1]);
237 
238  if ((conv0 < 0x10) && ((conv1 < 0x10))) {
239  bytes[j] = (conv0 << 4) | conv1;
240  } else {
241  /* return earlier in case of error */
242  return;
243  }
244  }
245 
246  if (i == (len - 1)) {
247  conv0 = ketCube_common_Hex2Val(str[i]);
248 
249  if (conv0 < 0x10) {
250  bytes[j] = conv0 << 4;
251  } else {
252  /* return earlier in case of error */
253  return;
254  }
255  }
256 }
257 
266 static inline void ketCube_common_Dec2int(int32_t * output, const char *str,
267  uint8_t len)
268 {
269  uint8_t sign = FALSE;
270  int pos;
271 
272  if (len == 0) {
273  return;
274  }
275 
276  *output = 0;
277 
278  if (str[0] == '-') {
279  sign = TRUE;
280  str++;
281  len--;
282  }
283 
284  for (pos = 0; pos < len && str[pos] != '\0'; pos++) {
285  *output *= 10;
286  *output += str[pos] - '0';
287  }
288 
289  if (sign == TRUE)
290  *output *= -1;
291 }
292 
293 
303 static inline uint8_t ketCube_common_Min(uint8_t a, uint8_t b)
304 {
305  if (a < b)
306  return a;
307  else
308  return b;
309 }
310 
320 static inline uint8_t ketCube_common_Max(uint8_t a, uint8_t b)
321 {
322  if (a > b)
323  return a;
324  else
325  return b;
326 }
327 
328 
338 static inline uint16_t ketCube_common_Med(uint16_t * values, uint16_t size)
339 {
340  uint16_t i, j, tmp;
341 
342  // bubble-sort -- it's efficient for small arrays
343  for (i = 0; i < size - 1; i++) {
344  for (j = 0; j < size - 1 - i; j++) {
345  if (values[j] < values[j + 1]) {
346  tmp = values[j];
347  values[j] = values[j + 1];
348  values[j + 1] = tmp;
349  }
350  }
351  }
352 
353  return (uint16_t) values[size / 2];
354 }
355 
365 static inline uint32_t ketCube_common_Med32(uint32_t * values, uint32_t size)
366 {
367  uint32_t i, j, tmp;
368 
369  // bubble-sort -- it's efficient for small arrays
370  for (i = 0; i < size - 1; i++) {
371  for (j = 0; j < size - 1 - i; j++) {
372  if (values[j] < values[j + 1]) {
373  tmp = values[j];
374  values[j] = values[j + 1];
375  values[j + 1] = tmp;
376  }
377  }
378  }
379 
380  return (uint32_t) values[size / 2];
381 }
382 
392 static inline uint32_t ketCube_common_Max32(uint32_t * values, uint32_t size)
393 {
394  uint32_t i, max;
395 
396  max = values[0];
397 
398  for (i = 0; i < size; i++) {
399  if (values[i] > max) {
400  max = values[i];
401  }
402  }
403 
404  return max;
405 }
406 
416 static inline uint32_t ketCube_common_Min32(uint32_t * values, uint32_t size)
417 {
418  uint32_t i, min;
419 
420  min = values[0];
421 
422  for (i = 0; i < size; i++) {
423  if (values[i] < min) {
424  min = values[i];
425  }
426  }
427 
428  return min;
429 }
430 
440 static inline uint16_t ketCube_common_Avg(uint16_t * values, uint16_t size)
441 {
442  uint16_t i;
443  uint32_t result = 0;
444 
445  // average
446  for (i = 0; i < size; i++) {
447  result += values[i];
448  }
449 
450  result = result / size;
451 
452  return (uint16_t) result;
453 }
454 
455 
466 static inline uint16_t ketCube_common_swapW(uint16_t n)
467 {
468  return ((n >> 8) | (n << 8));
469 }
470 
475 {
476  while (TRUE) {
477  // do nothing
478  }
479 }
480 
481 
482 // Include project-specific common here
483 #ifndef DESKTOP_BUILD
484 #include "ketCube_projectCommon.h"
485 #endif
486 
487 
492 #endif /* __KETCUBE_COMMON_H */
TRUE
#define TRUE
boolean TRUE
Definition: ketCube_common.h:69
ketCube_common_Med
static uint16_t ketCube_common_Med(uint16_t *values, uint16_t size)
Return median from an array of shorts.
Definition: ketCube_common.h:338
ketCube_common_bytes2Str
char * ketCube_common_bytes2Str(uint8_t *byteArr, uint8_t len)
Definition: ketCube_common.c:60
FALSE
#define FALSE
boolean FALSE
Definition: ketCube_common.h:70
ketCube_common_Max32
static uint32_t ketCube_common_Max32(uint32_t *values, uint32_t size)
Return max from an array of ints.
Definition: ketCube_common.h:392
ketCube_common_IsHexString
static bool ketCube_common_IsHexString(const char *str, uint8_t len)
Test if the string is valid HEX string.
Definition: ketCube_common.h:151
ketCube_common_Hex2Val
static uint8_t ketCube_common_Hex2Val(const char c)
Convert HEX char to Int (Byte)
Definition: ketCube_common.h:201
ketCube_projectCommon.h
KETCube project-speciffic common definitions.
ketCube_common_Int2dec
static void ketCube_common_Int2dec(int32_t number, char *str, int len)
Convert an integer to DEC string (with defined length, counting zero terminator)
Definition: ketCube_common.h:114
ketCube_common_Med32
static uint32_t ketCube_common_Med32(uint32_t *values, uint32_t size)
Return median from an array of shorts.
Definition: ketCube_common.h:365
ketCube_common_Byte2hex
static void ketCube_common_Byte2hex(uint8_t byte, char *str)
Convert a single Byte to HEX string (two bytes)
Definition: ketCube_common.h:92
byte
uint8_t byte
Definition: ketCube_common.h:77
ketCube_common_Dec2int
static void ketCube_common_Dec2int(int32_t *output, const char *str, uint8_t len)
Convert DEC string to 4 byte signed integer.
Definition: ketCube_common.h:266
KETCube_ErrorHandler
void KETCube_ErrorHandler(void)
ketCube_common_Hex2Bytes
static void ketCube_common_Hex2Bytes(uint8_t *bytes, const char *str, uint8_t len)
Convert HEX string to Byte array.
Definition: ketCube_common.h:224
ketCube_common_IsDecString
static bool ketCube_common_IsDecString(const char *str, uint8_t len)
Test if the string is valid DEC string.
Definition: ketCube_common.h:179
ketCube_common_BasicErrorHandler
static void ketCube_common_BasicErrorHandler()
Basic Error Handler.
Definition: ketCube_common.h:474
ketCube_common_Avg
static uint16_t ketCube_common_Avg(uint16_t *values, uint16_t size)
Return median from an array of shorts.
Definition: ketCube_common.h:440
ketCube_common_swapW
static uint16_t ketCube_common_swapW(uint16_t n)
Swap word (16-bit) values.
Definition: ketCube_common.h:466
ketCube_common_Max
static uint8_t ketCube_common_Max(uint8_t a, uint8_t b)
Return maximum of two bytes.
Definition: ketCube_common.h:320
ketCube_common_buffer
char ketCube_common_buffer[]
Definition: ketCube_common.c:47
ketCube_common_Min
static uint8_t ketCube_common_Min(uint8_t a, uint8_t b)
Return minimum of two bytes.
Definition: ketCube_common.h:303
ketCube_common_Min32
static uint32_t ketCube_common_Min32(uint32_t *values, uint32_t size)
Return max from an array of ints.
Definition: ketCube_common.h:416