Advertisement
Guest User

Bluetooth notification LED for Dragonbox Pyra

a guest
Jul 23rd, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.61 KB | None | 0 0
  1. // Simple bluetooth connected notification LED
  2. // Written by Djhg2000 for http://boards.openpandora.org/
  3. // Connect serial port, then use with the following command:
  4. // zenity --color-selection | sed 's/#/$/' > /dev/rfcomm0
  5.  
  6. #include <Adafruit_NeoPixel.h>
  7.  
  8. #define LPIN 4    // Pin for NeoPixel communication
  9. #define PIXELS 8  // Number of LEDs (NeoPixels)
  10.  
  11.  
  12. Adafruit_NeoPixel leds = Adafruit_NeoPixel(PIXELS, LPIN,
  13.                                             NEO_GRB + NEO_KHZ800);
  14. uint8_t color_red = 1;
  15. uint8_t color_green = 1;
  16. uint8_t color_blue = 1;
  17. long set_timer = millis();
  18.  
  19. void setup()
  20. {
  21.   // Initialize communications, nothing too fancy
  22.   Serial1.begin(9600);
  23.   leds.begin();
  24. }
  25.  
  26. void loop()
  27. {
  28.   // Generate color information and push it to the LED array
  29.   for (int i = 0; i < PIXELS; i++)
  30.   {
  31.     leds.setPixelColor(i, leds.Color(color_red,
  32.                                       color_green,
  33.                                       color_blue));
  34.   }
  35.  
  36.   leds.show();
  37.  
  38.   // Try to read the color header, as in the '#' in #ABCDEF
  39.   // Returns -1 if there's no new character
  40.   char header = Serial1.read();
  41.  
  42.   // I split this into two separate parsers since Zenity outputs
  43.   // 48 bit colors
  44.   // Run Zenity with a pipe to " sed 's/#/$/' " and the rest
  45.   // should work just fine
  46.   if (header == '#')
  47.   {
  48.     // This is for 24 bit color in HEX, like #ABCDEF
  49.     char color_ascii24[6];
  50.     for (int i = 0; i < 6; i++)
  51.     {
  52.       color_ascii24[i] = Serial1_readSafe();
  53.     }
  54.  
  55.     // Now convert the HEX string into a format NeoPixels understand
  56.     color_red = hex2integer(2, &color_ascii24[0]);
  57.     color_green = hex2integer(2, &color_ascii24[2]);
  58.     color_blue = hex2integer(2, &color_ascii24[4]);
  59.  
  60.     set_timer = millis();
  61.   }
  62.   if (header == '$')
  63.   {
  64.     // This is for 48 bit color in HEX, like $ABABCDCDEFEF
  65.     // Basically this is for passing colors from Zenity as
  66.     // explained above
  67.     char color_ascii48[12];
  68.     for (int i = 0; i < 12; i++)
  69.     {
  70.       color_ascii48[i] = Serial1_readSafe();
  71.     }
  72.  
  73.     // Now convert the HEX string into a format NeoPixels understand
  74.     color_red = hex2integer(2, &color_ascii48[0]);
  75.     color_green = hex2integer(2, &color_ascii48[4]);
  76.     color_blue = hex2integer(2, &color_ascii48[8]);
  77.  
  78.     set_timer = millis();
  79.   }
  80.  
  81.   // Fade to black after 100 milliseconds since last change
  82.   if (millis() > (set_timer + 100))
  83.   {
  84.     color_red /= 2;
  85.     color_green /= 2;
  86.     color_blue /= 2;
  87.     set_timer = millis();
  88.   }
  89. }
  90.  
  91. // Does the waiting secuence for us, or put simply:
  92. // one call always returns one char of input
  93. char Serial1_readSafe()
  94. {
  95.   while (Serial1.available() == 0);
  96.   return Serial1.read();
  97. }
  98.  
  99. // strtoul() seemed like a bit of overkill
  100. // No need to import the entire string facility for only one
  101. // relatively simple function
  102. int hex2integer(int characters, char *hex)
  103. {
  104.   int integer = 0;
  105.  
  106.   for (int i = 0; i < characters; i++)
  107.   {
  108.     // Shift the veriable integer 4 bits to the left
  109.     // This is to make room for the next hexadecimal
  110.     // As a side effect, unrecognized characters are replaced with 0
  111.     integer = integer << 4;
  112.  
  113.     // Match known good values and add those to our integer
  114.     if ((hex[i] >= '0') && (hex[i] <= '9'))
  115.     {
  116.       integer += hex[i] - '0';
  117.     }
  118.     else if ((hex[i] >= 'a') && (hex[i] <= 'f'))
  119.     {
  120.       integer += 10 + (hex[i] - 'a');
  121.     }
  122.     else if ((hex[i] >= 'A') && (hex[i] <= 'F'))
  123.     {
  124.       integer += 10 + (hex[i] - 'A');
  125.     }
  126.     // If we get here without a match the character will be ignored
  127.     // as explained above
  128.   }
  129.  
  130.   return integer;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement