View difference between Paste ID: A8BqKrgW and p0hFXzTX
SHOW: | | - or go back to the newest paste.
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,
12+
Adafruit_NeoPixel leds = Adafruit_NeoPixel(PIXELS, LPIN, NEO_GRB + NEO_KHZ800);
13-
                                            NEO_GRB + NEO_KHZ800);
13+
14
uint8_t color_green = 1;
15
uint8_t color_blue = 1;
16
uint8_t fade = 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,
31+
    leds.setPixelColor(i,leds.Color(map(color_red, 0, 255, 0, fade),
32-
                                      color_green, 
32+
                                    map(color_green, 0, 255, 0, fade), 
33-
                                      color_blue));
33+
                                    map(color_blue, 0, 255, 0, fade)));
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
39+
  // Serial1.read() 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
    fade = 255;
62
  }
63
  if (header == '$')
64
  {
65
    // This is for 48 bit color in HEX, like $ABABCDCDEFEF
66
    // Basically this is for passing colors from Zenity as
67
    // explained above
68
    char color_ascii48[12];
69
    for (int i = 0; i < 12; i++)
70
    {
71
      color_ascii48[i] = Serial1_readSafe();
72
    }
73
74
    // Now convert the HEX string into a format NeoPixels understand
75
    color_red = hex2integer(2, &color_ascii48[0]);
76
    color_green = hex2integer(2, &color_ascii48[4]);
77
    color_blue = hex2integer(2, &color_ascii48[8]);
78
79
    set_timer = millis();
80
    fade = 255;
81-
  // Fade to black after 100 milliseconds since last change
81+
82-
  if (millis() > (set_timer + 100))
82+
83
  // Fade to black after 5 milliseconds since last change
84-
    color_red /= 2;
84+
  if (millis() > (set_timer + 5))
85-
    color_green /= 2;
85+
86-
    color_blue /= 2;
86+
    if (fade > 0)
87
    {
88
      fade--;
89
    }
90
    set_timer = millis();
91
  }
92
}
93
94
// Does the waiting secuence for us, or put simply:
95
// one call always returns one char of input
96
char Serial1_readSafe()
97
{
98
  while (Serial1.available() == 0);
99
  return Serial1.read();
100
}
101
102
// strtoul() seemed like a bit of overkill
103
// No need to import the entire string facility for only one
104
// relatively simple function
105
int hex2integer(int characters, char *hex)
106
{
107
  int integer = 0;
108
109
  for (int i = 0; i < characters; i++)
110
  {
111
    // Shift the veriable integer 4 bits to the left
112
    // This is to make room for the next hexadecimal
113
    // As a side effect, unrecognized characters are replaced with 0
114
    integer = integer << 4;
115
116
    // Match known good values and add those to our integer
117
    if ((hex[i] >= '0') && (hex[i] <= '9'))
118
    {
119
      integer += hex[i] - '0';
120
    }
121
    else if ((hex[i] >= 'a') && (hex[i] <= 'f'))
122
    {
123
      integer += 10 + (hex[i] - 'a');
124
    }
125
    else if ((hex[i] >= 'A') && (hex[i] <= 'F'))
126
    {
127
      integer += 10 + (hex[i] - 'A');
128
    }
129
    // If we get here without a match the character will be ignored
130
    // as explained above
131
  }
132
133
  return integer;
134
}