Skip to content

Instantly share code, notes, and snippets.

@mnemocron
Last active August 25, 2017 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnemocron/e6ec7ce9e6ec0fff6ef7f5147fa5dedc to your computer and use it in GitHub Desktop.
Save mnemocron/e6ec7ce9e6ec0fff6ef7f5147fa5dedc to your computer and use it in GitHub Desktop.
idea for a another overloading function for the Adafruit
#include <Adafruit_NeoPixel.h>
#include <string.h>
class NeoPixel : public Adafruit_NeoPixel
{
public:
NeoPixel(uint16_t pixels, uint8_t pin, uint8_t type):Adafruit_NeoPixel(pixels, pin, type){};
uint32_t Color( String col_str ){
// int to hex - 16 chars + terminator
char itoh_lower[18] = {"0123456789abcdef"};
char itoh_upper[18] = {"0123456789ABCDEF"};
int htoi_number = 0; // return value
for(int index = 0; index < col_str.length(); index ++){ // check each hex char in string
for(int val = 0; val < 16; val ++){ // against each available hex character
if ( col_str[col_str.length() -index -1] == itoh_lower[val] ||
col_str[col_str.length() -index -1] == itoh_upper[val] ){
if(val > 0){
htoi_number += (uint32_t)(val * (16 << index));
}
}
}
}
return (uint32_t)(htoi_number);
}
};
#include <Adafruit_NeoPixel.h>
#include <string.h>
class NeoPixel : public Adafruit_NeoPixel
{
private:
uint32_t power(uint8_t p_base, uint8_t p_exp){
uint32_t pow_ret = 1;
for(int p_ctr = 0; p_ctr < p_exp; p_ctr ++){
pow_ret *= p_base;
}
return pow_ret;
}
public:
NeoPixel(uint16_t pixels, uint8_t pin, uint8_t type):Adafruit_NeoPixel(pixels, pin, type){};
// Convert RGB / RGBW string into packed 32-bit WRGB color.
// string format: "RRGGBB" or "RRGGBBWW (no 0x)
uint32_t Color( String htoi_str ){
char itoh_lower[18] = {"0123456789abcdef"}; // 16 chars + null
char itoh_upper[18] = {"0123456789ABCDEF"}; // 16 chars + null
uint32_t htoi_number = 0;
for(uint8_t index = 0; index < htoi_str.length(); index ++){ // each hex number in String
for(uint8_t val = 0; val < 16; val ++){ // each possible value
if ( htoi_str[htoi_str.length() -index -1] == itoh_lower[val] ||
htoi_str[htoi_str.length() -index -1] == itoh_upper[val] ){
if(val > 0){
// htoi_number += val * power(16, index);
htoi_number += (uint32_t)( val * (power(16.0, index) + 0.01) );
}
}
}
}
return (uint32_t)(htoi_number);
}
};
#define PIN 6
#define NUM_LEDS 16
NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
// put your setup code here, to run once:
strip.begin();
strip.show();
Serial.begin(9600);
Serial.println("HEX to RGBW");
Serial.println("type in a HEX color string (RRGGBB or WWRRGBB - no '0x')");
}
void loop() {
// put your main code here, to run repeatedly:
String str;
if(Serial.available() > 0){
str = Serial.readString();
if(str.length() == 6){ // == 6 if RGB / == 8 if RGBW
uint32_t couleur = strip.Color(str);
for(int i=0; i<NUM_LEDS; i++){
strip.setPixelColor(i, couleur);
}
strip.show();
Serial.println(str);
Serial.print("W\t"); Serial.println( ((couleur >> 24) & 0xff));
Serial.print("R\t"); Serial.println( ((couleur >> 16) & 0xff));
Serial.print("G\t"); Serial.println( ((couleur >> 8 ) & 0xff));
Serial.print("B\t"); Serial.println( ((couleur ) &0xff));
Serial.println("----------");
}
}
}
@mnemocron
Copy link
Author

Serial To RGBW

This sketch converts a String received over Serial to a 32 bit color value to use with the Adafruit NeoPixel library.

colsole output:
HEX to RGBW
type in a HEX color string (RRGGBB or WWRRGBB - no '0x')
ff2005
W	0
R	255
G	32
B	5
----------
000008
W	0
R	0
G	0
B	8
----------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment