diff --git a/src/hasp/hasp_parser.cpp b/src/hasp/hasp_parser.cpp new file mode 100644 index 00000000..af46a4ec --- /dev/null +++ b/src/hasp/hasp_parser.cpp @@ -0,0 +1,90 @@ +/* MIT License - Copyright (c) 2020 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#include + +#ifdef ARDUINO +#include "pgmspace.h" +#endif + +#include "lvgl.h" +#include "hasp_conf.h" + +#include "hasp_parser.h" +#include "hasp_utilities.h" + +bool Parser::haspPayloadToColor(const char * payload, lv_color32_t & color) +{ + /* HEX format #rrggbb or #rgb */ + if(*payload == '#') { + if(strlen(payload) >= 8) return false; + + char * pEnd; + long color_int = strtol(payload + 1, &pEnd, HEX); + + if(pEnd - payload == 7) { // #rrbbgg + color.ch.red = color_int >> 16 & 0xff; + color.ch.green = color_int >> 8 & 0xff; + color.ch.blue = color_int & 0xff; + + } else if(pEnd - payload == 4) { // #rgb + color.ch.red = color_int >> 8 & 0xf; + color.ch.green = color_int >> 4 & 0xf; + color.ch.blue = color_int & 0xf; + + color.ch.red += color.ch.red * HEX; + color.ch.green += color.ch.green * HEX; + color.ch.blue += color.ch.blue * HEX; + + } else { + return false; /* Invalid hex length */ + } + + return true; /* Color found */ + } + + /* 16-bit RGB565 Color Scheme*/ + if(Utilities::is_only_digits(payload)) { + uint16_t c = atoi(payload); + + /* Initial colors */ + uint8_t R5 = ((c >> 11) & 0b11111); + uint8_t G6 = ((c >> 5) & 0b111111); + uint8_t B5 = (c & 0b11111); + + /* Remapped colors */ + color.ch.red = (R5 * 527 + 23) >> 6; + color.ch.green = (G6 * 259 + 33) >> 6; + color.ch.blue = (B5 * 527 + 23) >> 6; + + return true; /* Color found */ + } + + /* Named colors */ + size_t numColors = sizeof(haspNamedColors) / sizeof(haspNamedColors[0]); + uint16_t sdbm = Utilities::get_sdbm(payload); + +#ifdef ARDUINO + for(size_t i = 0; i < numColors; i++) { + if(sdbm == (uint16_t)pgm_read_word_near(&(haspNamedColors[i].hash))) { + color.ch.red = (uint16_t)pgm_read_byte_near(&(haspNamedColors[i].r)); + color.ch.green = (uint16_t)pgm_read_byte_near(&(haspNamedColors[i].g)); + color.ch.blue = (uint16_t)pgm_read_byte_near(&(haspNamedColors[i].b)); + + return true; /* Color found */ + } + } +#else + for(size_t i = 0; i < numColors; i++) { + if(sdbm == haspNamedColors[i].hash) { + color.ch.red = haspNamedColors[i].r; + color.ch.green = haspNamedColors[i].g; + color.ch.blue = haspNamedColors[i].b; + + return true; /* Color found */ + } + } +#endif + + return false; /* Color not found */ +} \ No newline at end of file diff --git a/src/hasp/hasp_parser.h b/src/hasp/hasp_parser.h new file mode 100644 index 00000000..2ffc058c --- /dev/null +++ b/src/hasp/hasp_parser.h @@ -0,0 +1,84 @@ +/* MIT License - Copyright (c) 2020 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#ifndef HASP_PARSER_H +#define HASP_PARSER_H + +#include "lvgl.h" +#include "hasp_conf.h" + +class Parser { + + public: + static bool haspPayloadToColor(const char * payload, lv_color32_t & color); +}; + +/* Named COLOR attributes */ +#define ATTR_RED 177 +#define ATTR_TAN 7873 +#define ATTR_AQUA 3452 +#define ATTR_BLUE 37050 +#define ATTR_CYAN 9763 +#define ATTR_GOLD 53440 +#define ATTR_GRAY 64675 +#define ATTR_GREY 64927 +#define ATTR_LIME 34741 +#define ATTR_NAVY 44918 +#define ATTR_PERU 36344 +#define ATTR_PINK 51958 +#define ATTR_PLUM 64308 +#define ATTR_SNOW 35587 +#define ATTR_TEAL 52412 +#define ATTR_AZURE 44239 +#define ATTR_BEIGE 12132 +#define ATTR_BLACK 26527 +#define ATTR_BLUSH 41376 +#define ATTR_BROWN 10774 +#define ATTR_CORAL 16369 +#define ATTR_GREEN 26019 +#define ATTR_IVORY 1257 +#define ATTR_KHAKI 32162 +#define ATTR_LINEN 30074 +#define ATTR_OLIVE 47963 +#define ATTR_WHEAT 11591 +#define ATTR_WHITE 28649 +#define ATTR_BISQUE 60533 +#define ATTR_INDIGO 46482 +#define ATTR_MAROON 12528 +#define ATTR_ORANGE 21582 +#define ATTR_ORCHID 39235 +#define ATTR_PURPLE 53116 +#define ATTR_SALMON 29934 +#define ATTR_SIENNA 50930 +#define ATTR_SILVER 62989 +#define ATTR_TOMATO 8234 +#define ATTR_VIOLET 61695 +#define ATTR_YELLOW 10484 +#define ATTR_FUCHSIA 5463 +#define ATTR_MAGENTA 49385 + +struct hasp_color_t +{ + uint16_t hash; + uint8_t r, g, b; +}; + +/* Named COLOR lookup table */ +const hasp_color_t haspNamedColors[] PROGMEM = { + {ATTR_RED, 0xFF, 0x00, 0x00}, {ATTR_TAN, 0xD2, 0xB4, 0x8C}, {ATTR_AQUA, 0x00, 0xFF, 0xFF}, + {ATTR_BLUE, 0x00, 0x00, 0xFF}, {ATTR_CYAN, 0x00, 0xFF, 0xFF}, {ATTR_GOLD, 0xFF, 0xD7, 0x00}, + {ATTR_GRAY, 0x80, 0x80, 0x80}, {ATTR_GREY, 0x80, 0x80, 0x80}, {ATTR_LIME, 0x00, 0xFF, 0x00}, + {ATTR_NAVY, 0x00, 0x00, 0x80}, {ATTR_PERU, 0xCD, 0x85, 0x3F}, {ATTR_PINK, 0xFF, 0xC0, 0xCB}, + {ATTR_PLUM, 0xDD, 0xA0, 0xDD}, {ATTR_SNOW, 0xFF, 0xFA, 0xFA}, {ATTR_TEAL, 0x00, 0x80, 0x80}, + {ATTR_AZURE, 0xF0, 0xFF, 0xFF}, {ATTR_BEIGE, 0xF5, 0xF5, 0xDC}, {ATTR_BLACK, 0x00, 0x00, 0x00}, + {ATTR_BLUSH, 0xB0, 0x00, 0x00}, {ATTR_BROWN, 0xA5, 0x2A, 0x2A}, {ATTR_CORAL, 0xFF, 0x7F, 0x50}, + {ATTR_GREEN, 0x00, 0x80, 0x00}, {ATTR_IVORY, 0xFF, 0xFF, 0xF0}, {ATTR_KHAKI, 0xF0, 0xE6, 0x8C}, + {ATTR_LINEN, 0xFA, 0xF0, 0xE6}, {ATTR_OLIVE, 0x80, 0x80, 0x00}, {ATTR_WHEAT, 0xF5, 0xDE, 0xB3}, + {ATTR_WHITE, 0xFF, 0xFF, 0xFF}, {ATTR_BISQUE, 0xFF, 0xE4, 0xC4}, {ATTR_INDIGO, 0x4B, 0x00, 0x82}, + {ATTR_MAROON, 0x80, 0x00, 0x00}, {ATTR_ORANGE, 0xFF, 0xA5, 0x00}, {ATTR_ORCHID, 0xDA, 0x70, 0xD6}, + {ATTR_PURPLE, 0x80, 0x00, 0x80}, {ATTR_SALMON, 0xFA, 0x80, 0x72}, {ATTR_SIENNA, 0xA0, 0x52, 0x2D}, + {ATTR_SILVER, 0xC0, 0xC0, 0xC0}, {ATTR_TOMATO, 0xFF, 0x63, 0x47}, {ATTR_VIOLET, 0xEE, 0x82, 0xEE}, + {ATTR_YELLOW, 0xFF, 0xFF, 0x00}, {ATTR_FUCHSIA, 0xFF, 0x00, 0xFF}, {ATTR_MAGENTA, 0xFF, 0x00, 0xFF}, +}; + +#endif \ No newline at end of file diff --git a/src/hasp/hasp_utilities.cpp b/src/hasp/hasp_utilities.cpp index 75ab5cd8..d99e6dca 100644 --- a/src/hasp/hasp_utilities.cpp +++ b/src/hasp/hasp_utilities.cpp @@ -1,11 +1,16 @@ #include -#include "Arduino.h" +#ifdef ARDUINO + #include "Arduino.h" +#endif + +#include "hasp_conf.h" + #include "hasp_utilities.h" /* 16-bit hashing function http://www.cse.yorku.ca/~oz/hash.html */ /* all possible attributes are hashed and checked if they are unique */ -uint16_t hasp_util_get_sdbm(const char * str) +uint16_t Utilities::get_sdbm(const char * str) { uint16_t hash = 0; char c; @@ -18,13 +23,13 @@ uint16_t hasp_util_get_sdbm(const char * str) return hash; } -bool hasp_util_is_true(const char * s) +bool Utilities::is_true(const char * s) { return (!strcasecmp_P(s, PSTR("true")) || !strcasecmp_P(s, PSTR("on")) || !strcasecmp_P(s, PSTR("yes")) || !strcmp_P(s, PSTR("1"))); } -bool hasp_util_is_only_digits(const char * s) +bool Utilities::is_only_digits(const char * s) { size_t digits = 0; while(*(s + digits) != '\0' && isdigit(*(s + digits))) { @@ -33,7 +38,7 @@ bool hasp_util_is_only_digits(const char * s) return strlen(s) == digits; } -int hasp_util_format_bytes(size_t filesize, char * buf, size_t len) +int Utilities::format_bytes(size_t filesize, char * buf, size_t len) { if(filesize < 1024) return snprintf_P(buf, len, PSTR("%d B"), filesize); @@ -47,4 +52,10 @@ int hasp_util_format_bytes(size_t filesize, char * buf, size_t len) } return snprintf_P(buf, len, PSTR("%d.%d %ciB"), filesize / 10, filesize % 10, labels[unit]); -} \ No newline at end of file +} + +#ifndef ARDUINO +long map(long x, long in_min, long in_max, long out_min, long out_max) { + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} +#endif \ No newline at end of file diff --git a/src/hasp/hasp_utilities.h b/src/hasp/hasp_utilities.h index eb164cd5..066d89e0 100644 --- a/src/hasp/hasp_utilities.h +++ b/src/hasp/hasp_utilities.h @@ -4,9 +4,17 @@ #ifndef HASP_UTILITIES_H #define HASP_UTILITIES_H -uint16_t hasp_util_get_sdbm(const char * str); -bool hasp_util_is_true(const char * s); -bool hasp_util_is_only_digits(const char * s); -int hasp_util_format_bytes(size_t filesize, char * buf, size_t len); +class Utilities { + + public: + static uint16_t get_sdbm(const char * str); + static bool is_true(const char * s); + static bool is_only_digits(const char * s); + static int format_bytes(size_t filesize, char * buf, size_t len); +}; + +#ifndef ARDUINO +long map(long x, long in_min, long in_max, long out_min, long out_max); +#endif #endif \ No newline at end of file