Skip to content

Commit 8878fec

Browse files
authored
Patch color methods (#59)
Rewrite legacy color and neopixel helpers
1 parent 0c92c8f commit 8878fec

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit IO Arduino
2-
version=2.7.13
2+
version=2.7.14
33
author=Adafruit
44
maintainer=Adafruit <adafruitio@adafruit.com>
55
sentence=Arduino library to access Adafruit IO.

src/AdafruitIO_Data.cpp

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -337,46 +337,56 @@ unsigned long AdafruitIO_Data::toUnsignedLong()
337337

338338
int AdafruitIO_Data::toRed()
339339
{
340-
if(! _value)
341-
return 0;
342-
343-
char r[] = "0x";
344-
strncat(r, toChar() + 1, 2);
345-
346-
return (int)strtol(r, NULL, 0);
340+
// Convert 0xRRGGBB to red.
341+
if (! _value)
342+
{
343+
return 0;
344+
}
345+
char r[5];
346+
strcpy(r, "0x");
347+
strncpy(&r[2], toChar() + 1, 2);
348+
r[4] = '\x00';
349+
return (int)strtol(r, NULL, 0);
347350
}
348351

349352
int AdafruitIO_Data::toGreen()
350353
{
351-
if(! _value)
352-
return 0;
353-
354-
char g[] = "0x";
355-
strncat(g, toChar() + 3, 2);
356-
357-
return (int)strtol(g, NULL, 0);
354+
// Convert 0xRRGGBB to green.
355+
if (! _value)
356+
{
357+
return 0;
358+
}
359+
char g[5];
360+
strcpy(g, "0x");
361+
strncpy(&g[2], toChar() + 3, 2);
362+
g[4] = '\x00';
363+
return (int)strtol(g, NULL, 0);
358364
}
359365

360366
int AdafruitIO_Data::toBlue()
361367
{
362-
if(! _value)
363-
return 0;
364-
365-
char b[] = "0x";
366-
strncat(b, toChar() + 5, 2);
367-
368-
return (int)strtol(b, NULL, 0);
368+
// Convert 0xRRGGBB to blue.
369+
if (! _value)
370+
{
371+
return 0;
372+
}
373+
char b[5];
374+
strcpy(b, "0x");
375+
strncpy(&b[2], toChar() + 5, 2);
376+
b[4] = '\x00';
377+
return (int)strtol(b, NULL, 0);
369378
}
370379

371380
long AdafruitIO_Data::toNeoPixel()
372381
{
373-
if(! _value)
374-
return 0;
375-
376-
char rgb_string[9] = "0x";
377-
strncat(rgb_string, toChar() + 1, 6);
378-
379-
return strtol(rgb_string, NULL, 0);
382+
if (! _value)
383+
{
384+
return 0;
385+
}
386+
char rgb[9];
387+
strcpy(rgb, "0x");
388+
strncpy(&rgb[2], toChar() + 1, 6);
389+
return strtol(rgb, NULL, 0);
380390
}
381391

382392
char* AdafruitIO_Data::toCSV()

0 commit comments

Comments
 (0)