-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
299 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// тест утилит для парсинга | ||
#include <GParser.h> | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
|
||
// список. Данные разделены запятой | ||
char list[] = "123,456,789,abc,def"; | ||
|
||
// ===================== | ||
Serial.println("Split test"); | ||
char* p = list; | ||
GP_splitList(NULL); | ||
while ((p = GP_splitList(list)) != NULL) { | ||
Serial.println(p); | ||
} | ||
Serial.println(); | ||
|
||
// ===================== | ||
Serial.println("InList test"); | ||
Serial.println(GP_inList("abc", list)); | ||
Serial.println(GP_inList("kek", list)); | ||
Serial.println(); | ||
|
||
// ===================== | ||
Serial.println("Num list test"); | ||
Serial.println(GP_numFromList(list, 2)); | ||
Serial.println(); | ||
|
||
// ===================== | ||
Serial.println("Array list test"); | ||
int data[3]; | ||
GP_listToNum(list, data, 3); | ||
for (int i = 0; i < 3; i++) { | ||
Serial.print(data[i]); | ||
Serial.print(','); | ||
} | ||
Serial.println(); | ||
Serial.println(); | ||
|
||
// ===================== | ||
Serial.println("Num to char test"); | ||
char st[30]; | ||
|
||
GP_numToChar(0x123abc, st, HEX); | ||
Serial.println(st); | ||
|
||
GP_numToChar(123456, st, DEC); | ||
Serial.println(st); | ||
|
||
GP_numToChar(0b11100011, st, BIN); | ||
Serial.println(st); | ||
|
||
GP_floatToChar(3.1415, st, 3); | ||
Serial.println(st); | ||
Serial.println(); | ||
|
||
// ===================== | ||
Serial.println("Char to num test"); | ||
int32_t val; | ||
|
||
val = GP_charToNum("AAFF", HEX); | ||
Serial.println(val, HEX); | ||
|
||
val = GP_charToNum("123456", DEC); | ||
Serial.println(val, DEC); | ||
|
||
val = GP_charToNum("11000011", BIN); | ||
Serial.println(val, BIN); | ||
|
||
float valf = GP_charToFloat("3.14"); | ||
Serial.println(valf); | ||
Serial.println(); | ||
|
||
// ===================== | ||
Serial.println("Color test"); | ||
Serial.println(GP_decodeColor("0xFF22"), HEX); | ||
Serial.println(GP_decodeColor("#FF22"), HEX); | ||
Serial.println(GP_decodeColor("FF22"), HEX); | ||
} | ||
|
||
void loop() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#include "parseUtils.h" | ||
|
||
// разделить список list с разделителем div на подстроки | ||
char* GP_splitList(char* list, char div) { | ||
static uint8_t prev, end; | ||
if (list == NULL) prev = end = 0; | ||
else { | ||
if (prev) *(list + prev - 1) = div; | ||
char* cur = strchr(list + prev, div); | ||
if (cur) { | ||
*cur = '\0'; | ||
uint8_t b = prev; | ||
prev = cur - list + 1; | ||
return list + b; | ||
} else if (!end) { | ||
end = 1; | ||
return list + prev; | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
// получить номер, под которым name входит в list с разделителем div | ||
int8_t GP_inList(char* name, char* list, char div) { | ||
char* str = list; | ||
int8_t count = 0, pos = -1; | ||
GP_splitList(NULL); | ||
while ((str = GP_splitList(list, div)) != NULL) { | ||
if (!strcmp(str, name)) pos = count; | ||
count++; | ||
} | ||
return pos; | ||
} | ||
|
||
// преобразовать int в строку | ||
// DEC - 46, HEX/BIN - 17 (ltoa: BIN - 480, HEX - 125, DEC - 150) | ||
void GP_numToChar(int32_t n, char* buf, uint8_t base) { | ||
uint8_t l = 2; | ||
if (base != DEC) { | ||
int32_t nn = n; | ||
uint8_t b = (base == HEX) ? 4 : 1; | ||
while (nn >>= b) l++; | ||
char *str = &buf[l]; | ||
*--str = '\0'; | ||
do { | ||
char c = n & (base - 1); | ||
n >>= b; | ||
*--str = (c < 10) ? (c + '0') : (c + 'A' - 10); | ||
} while(n); | ||
} else { | ||
uint8_t s = 0; | ||
if (n < 0) n = -n, s = 1; | ||
int32_t nn = n; | ||
while (nn /= base) l++; | ||
char *str = &buf[l + s]; | ||
*--str = '\0'; | ||
do { | ||
_GP_div10 res(n); | ||
n = res.quot; | ||
*--str = (res.rem < 10) ? (res.rem + '0') : (res.rem + 'A' - 10); | ||
} while(n); | ||
if (s) *--str = '-'; | ||
} | ||
} | ||
|
||
// преобразовать float в строку | ||
void GP_floatToChar(double f, char *buf, uint8_t decimals) { | ||
dtostrf(f, decimals + 2, decimals, buf); | ||
} | ||
|
||
// преобразовать строку в число | ||
int32_t GP_charToNum(char* buf, uint8_t base) { | ||
if (base == DEC) return atol(buf); | ||
else return strtol(buf, NULL, base); | ||
} | ||
|
||
// преобразовать строку в float | ||
double GP_charToFloat(char* buf) { | ||
return atof(buf); | ||
} | ||
|
||
// получить число под индексом idx в списке list с разделителем div | ||
int GP_numFromList(char* list, int idx, char div) { | ||
uint8_t count = 0; | ||
char* p = list; | ||
while (1) { | ||
if (count++ == idx) return atoi(p); | ||
p = strchr(p, div); | ||
if (!p++) break; | ||
} | ||
return 0; | ||
} | ||
|
||
// переписать список list с разделителем div в массив чисел data размером size | ||
uint8_t GP_listToNum(char* list, int* data, uint8_t size, char div) { | ||
uint8_t count = 0; | ||
char* offset = list; | ||
while (1) { | ||
if (count >= size) break; | ||
data[count++] = atoi(offset); | ||
offset = strchr(offset, div); | ||
if (!offset++) break; | ||
} | ||
return count; | ||
} | ||
|
||
// преобразовать текстовый цвет (0x, #) в число | ||
uint32_t GP_decodeColor(char* hex) { | ||
uint32_t val = 0; | ||
uint8_t i = (hex[0] == '#') ? 1 : ((hex[1] == 'x') ? 2 : 0); | ||
for (; i < strlen(hex); i++) { | ||
val <<= 4; | ||
uint8_t d = hex[i]; | ||
d -= (d <= '9') ? 48 : ((d <= 'F') ? 55 : 87); | ||
val |= d; | ||
} | ||
return val; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
#include <Arduino.h> | ||
|
||
// разделить список list с разделителем div на подстроки (см. пример) | ||
char* GP_splitList(char* list, char div = ','); | ||
|
||
// получить номер, под которым name входит в list с разделителем div. -1 если не входит | ||
int8_t GP_inList(char* name, char* list, char div = ','); | ||
|
||
// преобразовать int в строку | ||
void GP_numToChar(int32_t n, char* buf, uint8_t base); | ||
|
||
// преобразовать float в строку | ||
void GP_floatToChar(double f, char *buf, uint8_t decimals); | ||
|
||
// преобразовать строку в число | ||
int32_t GP_charToNum(char* buf, uint8_t base); | ||
|
||
// преобразовать строку в float | ||
double GP_charToFloat(char* buf); | ||
|
||
// получить число под индексом idx в списке list с разделителем div | ||
int GP_numFromList(char* list, int idx, char div = ','); | ||
|
||
// переписать список list с разделителем div в массив чисел data размером size | ||
uint8_t GP_listToNum(char* list, int* data, uint8_t size, char div = ','); | ||
|
||
// преобразовать текстовый цвет (0x, #) в число | ||
uint32_t GP_decodeColor(char* hex); | ||
|
||
|
||
// http://we.easyelectronics.ru/Soft/preobrazuem-v-stroku-chast-1-celye-chisla.html | ||
struct _GP_div10 { | ||
_GP_div10(uint32_t n) { | ||
quot = n >> 1; | ||
quot += quot >> 1; | ||
quot += quot >> 4; | ||
quot += quot >> 8; | ||
quot += quot >> 16; | ||
uint32_t qq = quot; | ||
quot >>= 3; | ||
rem = uint8_t(n - ((quot << 1) + (qq & ~7ul))); | ||
if (rem > 9) rem -= 10, quot++; | ||
} | ||
uint32_t quot; | ||
uint8_t rem; | ||
}; |