Skip to content

Commit

Permalink
Merge pull request #112 from unaiur/namedcolors2
Browse files Browse the repository at this point in the history
AVR needs to use some macros to access PROGMEM
  • Loading branch information
Makuna authored Jun 19, 2016
2 parents c8938e0 + 4e7c534 commit e5c6a97
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/internal/HtmlColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ const HtmlColorName HtmlColor::ColorNames[] PROGMEM = {
#endif
};

#ifndef pgm_read_ptr
// ESP8266 doesn't define this macro, but it doesn't need it either.
#define pgm_read_ptr(addr) (*reinterpret_cast<const void* const *>(addr))
#endif

bool HtmlColor::Parse(const char* name, size_t namelen)
{
if (name[0] == '#')
Expand All @@ -368,8 +373,8 @@ bool HtmlColor::Parse(const char* name, size_t namelen)
}
else
{
// Convert a letter to lower case (only for ASCII letters)
// It's faster & smaller than tolower()
// Convert a letter to lower case (only for ASCII letters)
// It's faster & smaller than tolower()
c |= 32;
if (c >= 'a' && c <= 'f')
{
Expand All @@ -394,10 +399,11 @@ bool HtmlColor::Parse(const char* name, size_t namelen)

for (uint8_t i = 0; i < sizeof(ColorNames)/sizeof(ColorNames[0]); ++i)
{
const HtmlColorName* cn = &ColorNames[i];
if (strncasecmp_P(name, cn->Name, namelen) == 0 && strlen_P(cn->Name) == namelen)
const HtmlColorName* cptr = &ColorNames[i];
PGM_P cname = (PGM_P)pgm_read_ptr(&cptr->Name);
if (strncasecmp_P(name, cname, namelen) == 0 && strlen_P(cname) == namelen)
{
Color = cn->Color;
Color = pgm_read_dword(&cptr->Color);
return true;
}
}
Expand All @@ -413,14 +419,15 @@ size_t HtmlColor::ToString(char* buf, size_t buflen) const
{
for (uint8_t i = 0; i < sizeof(ColorNames) / sizeof(ColorNames[0]); ++i)
{
const HtmlColorName* cn = &ColorNames[i];
if (cn->Color == Color)
const HtmlColorName* cptr = &ColorNames[i];
if (pgm_read_dword(&cptr->Color) == Color)
{
size_t namelen = strlen_P(cn->Name);
PGM_P name = (PGM_P)pgm_read_ptr(&cptr->Name);
size_t namelen = strlen_P(name);
if (buflen-- > 0)
{
buflen = min(buflen, namelen);
memcpy_P(buf, cn->Name, buflen);
memcpy_P(buf, name, buflen);
buf[buflen] = 0;
}
return namelen;
Expand Down

0 comments on commit e5c6a97

Please sign in to comment.