Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Simple bluetooth connected notification LED
- // Written by Djhg2000 for http://boards.openpandora.org/
- // Connect serial port, then use with the following command:
- // zenity --color-selection | sed 's/#/$/' > /dev/rfcomm0
- #include <Adafruit_NeoPixel.h>
- #define LPIN 4 // Pin for NeoPixel communication
- #define PIXELS 8 // Number of LEDs (NeoPixels)
- Adafruit_NeoPixel leds = Adafruit_NeoPixel(PIXELS, LPIN,
- NEO_GRB + NEO_KHZ800);
- uint8_t color_red = 1;
- uint8_t color_green = 1;
- uint8_t color_blue = 1;
- long set_timer = millis();
- void setup()
- {
- // Initialize communications, nothing too fancy
- Serial1.begin(9600);
- leds.begin();
- }
- void loop()
- {
- // Generate color information and push it to the LED array
- for (int i = 0; i < PIXELS; i++)
- {
- leds.setPixelColor(i, leds.Color(color_red,
- color_green,
- color_blue));
- }
- leds.show();
- // Try to read the color header, as in the '#' in #ABCDEF
- // Returns -1 if there's no new character
- char header = Serial1.read();
- // I split this into two separate parsers since Zenity outputs
- // 48 bit colors
- // Run Zenity with a pipe to " sed 's/#/$/' " and the rest
- // should work just fine
- if (header == '#')
- {
- // This is for 24 bit color in HEX, like #ABCDEF
- char color_ascii24[6];
- for (int i = 0; i < 6; i++)
- {
- color_ascii24[i] = Serial1_readSafe();
- }
- // Now convert the HEX string into a format NeoPixels understand
- color_red = hex2integer(2, &color_ascii24[0]);
- color_green = hex2integer(2, &color_ascii24[2]);
- color_blue = hex2integer(2, &color_ascii24[4]);
- set_timer = millis();
- }
- if (header == '$')
- {
- // This is for 48 bit color in HEX, like $ABABCDCDEFEF
- // Basically this is for passing colors from Zenity as
- // explained above
- char color_ascii48[12];
- for (int i = 0; i < 12; i++)
- {
- color_ascii48[i] = Serial1_readSafe();
- }
- // Now convert the HEX string into a format NeoPixels understand
- color_red = hex2integer(2, &color_ascii48[0]);
- color_green = hex2integer(2, &color_ascii48[4]);
- color_blue = hex2integer(2, &color_ascii48[8]);
- set_timer = millis();
- }
- // Fade to black after 100 milliseconds since last change
- if (millis() > (set_timer + 100))
- {
- color_red /= 2;
- color_green /= 2;
- color_blue /= 2;
- set_timer = millis();
- }
- }
- // Does the waiting secuence for us, or put simply:
- // one call always returns one char of input
- char Serial1_readSafe()
- {
- while (Serial1.available() == 0);
- return Serial1.read();
- }
- // strtoul() seemed like a bit of overkill
- // No need to import the entire string facility for only one
- // relatively simple function
- int hex2integer(int characters, char *hex)
- {
- int integer = 0;
- for (int i = 0; i < characters; i++)
- {
- // Shift the veriable integer 4 bits to the left
- // This is to make room for the next hexadecimal
- // As a side effect, unrecognized characters are replaced with 0
- integer = integer << 4;
- // Match known good values and add those to our integer
- if ((hex[i] >= '0') && (hex[i] <= '9'))
- {
- integer += hex[i] - '0';
- }
- else if ((hex[i] >= 'a') && (hex[i] <= 'f'))
- {
- integer += 10 + (hex[i] - 'a');
- }
- else if ((hex[i] >= 'A') && (hex[i] <= 'F'))
- {
- integer += 10 + (hex[i] - 'A');
- }
- // If we get here without a match the character will be ignored
- // as explained above
- }
- return integer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement