Jump to content
Sign in to follow this  
StefanWxx

How to parse string like "FF33BB" to 3 bytes ? ..for LEDStripe

Recommended Posts

Hi,

 

in my code i want so set the color for a rgb led stripe. I use the WS2811Driver library.

 

I tried this but it doesn't work:

// blink_strip - ws2811 led strip blink example
// 11-15-2012 Initial Version 0.1

#include <WS2811Driver.h>
byte leds0[30];
byte leds1[30];


// 24 bits of color data stored GGRRBB
//const uint8_t leds0[] = { 0x00,0xff,0x00, 0xff,0x00,0x00, 0x00,0x00,0xff, 0xff,0xff,0xff }; // R,G,B,W
//const uint8_t leds1[] = { 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00 }; // 0,0,0,0,

WS2811Driver ledStrip; // uses P1_7 as datapin connect to DIN on strip

void setup(void)
{
    ledStrip.setLEDCount(10); // setup for 4 leds on a strip
    ledStrip.begin();        // configure P1.7 for output
}

void loop() {
    //ledStrip.write(leds0);               // set leds to red,green,blue,white
    //delay(500);
    //ledStrip.write(leds1,sizeof(leds1)); // turn off all 4 leds
    //delay(500);
    String color = "0000FF";
    
    for(byte x = 0; x < 30 ; x++){
      leds0[x] = byte('F');
      //leds0[x+1] = byte(color.substring(2,3));
      //leds0[x+2] = byte(color.substring(4,5));
      }  
    ledStrip.write(leds0); 
    delay(5000);
    for(byte x = 0; x < 30 ; x++){
      leds1[x] = 0x00;
      //leds0[x+1] = byte(color.substring(2,3));
      //leds0[x+2] = byte(color.substring(4,5));
      }  
     ledStrip.write(leds1); 
     delay(5000);
}

How can I convert the string to 3 bytes?

 

 

Share this post


Link to post
Share on other sites


#include <cstdlib>

 

const char* s = "ff33bb";

unsigned long a = strtoul (s, NULL, 16);

// a is equal to 0xff33bb is equal to 996283

 

// to break the 32-bit integer into three separate 8-bit integers

unsigned char b[3];

b[0] = (a >> 16) & 0xFF;

b[1] = (a >> 8) & 0xFF;

b[2] = a & 0xFF;

// b[0] is equal to 0xff is equal to 255

// b[1] is equal to 0x33 is equal to 51

// b[2] is equal to 0xbb is equal to 187

 

Share this post


Link to post
Share on other sites

Okay,

just another Question about this..

 

How can ich make this with a loop:

void loop() {
    
    const char* color = "34acef";  //GGRRBB
    unsigned long a = strtoul (color, NULL, 16);
    
    

    // want to do this in a loop:   
    leds0[0] = (a >> 16) & 0xFF;
    leds0[1] = (a >> 8) & 0xFF;
    leds0[2] = a & 0xFF;
    
    leds0[3] = (a >> 16) & 0xFF;
    leds0[4] = (a >> 8) & 0xFF;
    leds0[5] = a & 0xFF;
    
    leds0[6] = (a >> 16) & 0xFF;
    leds0[7] = (a >> 8) & 0xFF;
    leds0[8] = a & 0xFF;
    
    leds0[9] = (a >> 16) & 0xFF;
    leds0[10] = (a >> 8) & 0xFF;
    leds0[11] = a & 0xFF;
    
    leds0[12] = (a >> 16) & 0xFF;
    leds0[13] = (a >> 8) & 0xFF;
    leds0[14] = a & 0xFF;
    
    leds0[15] = (a >> 16) & 0xFF;
    leds0[16] = (a >> 8) & 0xFF;
    leds0[17] = a & 0xFF;
    
   
   
      
      
    ledStrip.write(leds0); 
    
    delay(5000);
      
    
    
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...