Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: jrcutler/survive_dc_lights
base: 5a6163b640
...
head fork: jrcutler/survive_dc_lights
compare: 64d59bead2
  • 6 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 102 additions and 9 deletions.
  1. +53 −0 CIELAB.h
  2. +5 −0 colors.h
  3. +44 −9 survive_dc_lights.ino
53 CIELAB.h
View
@@ -0,0 +1,53 @@
+#ifndef CIELAB_H
+#define CIELAB_H
+/*
+ * International Commission on Illumination (CIE) color space corrections
+ */
+
+#include <avr/pgmspace.h>
+
+/* CIE lightness correction (from linear) */
+PROGMEM prog_uint8_t CIE_L_table[] = {
+ 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4,
+ 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7,
+ 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11,
+ 11, 12, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 16, 16, 17, 17,
+ 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25,
+ 25, 26, 26, 27, 28, 28, 29, 29, 30, 31, 31, 32, 32, 33, 34, 34,
+ 35, 36, 37, 37, 38, 39, 39, 40, 41, 42, 43, 43, 44, 45, 46, 47,
+ 47, 48, 49, 50, 51, 52, 53, 54, 54, 55, 56, 57, 58, 59, 60, 61,
+ 62, 63, 64, 65, 66, 67, 68, 70, 71, 72, 73, 74, 75, 76, 77, 79,
+ 80, 81, 82, 83, 85, 86, 87, 88, 90, 91, 92, 94, 95, 96, 98, 99,
+ 100,102,103,105,106,108,109,110,112,113,115,116,118,120,121,123,
+ 124,126,128,129,131,132,134,136,138,139,141,143,145,146,148,150,
+ 152,154,155,157,159,161,163,165,167,169,171,173,175,177,179,181,
+ 183,185,187,189,191,193,196,198,200,202,204,207,209,211,214,216,
+ 218,220,223,225,228,230,232,235,237,240,242,245,247,250,252,255
+};
+
+inline uint8_t CIE_L(uint8_t y)
+{
+ return pgm_read_byte(&CIE_L_table[y]);
+}
+
+uint32_t CIE_L(uint32_t rgb)
+{
+ uint8_t r, g, b;
+ // decompose color
+ b = rgb;
+ rgb >>= 8;
+ g = rgb;
+ rgb >>= 8;
+ r = rgb;
+ // rebuild color with CIE_L factor
+ rgb = CIE_L(r);
+ rgb <<= 8;
+ rgb |= CIE_L(g);
+ rgb <<= 8;
+ rgb |= CIE_L(b);
+ return rgb;
+}
+
+#endif
+
5 colors.h
View
@@ -1,3 +1,6 @@
+#ifndef COLORS_H
+#define COLORS_H
+
/* Standard color definitions */
enum {
// HTML 4 colors
@@ -23,3 +26,5 @@ enum {
safety_orange = 0xe87600
};
+#endif
+
53 survive_dc_lights.ino
View
@@ -3,6 +3,7 @@
#include <LPD8806.h> // patched for 8-bit color I/O
#include <SPI.h>
/* Values */
+#include "CIELAB.h"
#include "colors.h"
/*
@@ -19,7 +20,7 @@ const int dataPin = 2;
const int clockPin = 3;
#endif
-const uint32_t palette[] = { safety_orange, blue };
+uint32_t palette[] = { safety_orange, blue };
const size_t palette_count = sizeof(palette)/sizeof(palette[0]);
/*
@@ -36,7 +37,7 @@ LPD8806 lights(nLEDs, dataPin, clockPin);
* Utility Functions
*/
-void fill(LPD8806 &lpd, uint32_t color, uint16_t wait)
+void fill_up(LPD8806 &lpd, uint32_t color, uint16_t wait)
{
for (uint16_t i = 0; i < lpd.numPixels(); ++i)
{
@@ -46,6 +47,18 @@ void fill(LPD8806 &lpd, uint32_t color, uint16_t wait)
}
}
+void fill_down(LPD8806 &lpd, uint32_t color, uint16_t wait)
+{
+ uint16_t i = lpd.numPixels() - 1;
+ do
+ {
+ lpd.setPixelColor(i, color);
+ lpd.show();
+ delay(wait);
+ }
+ while (i-- != 0);
+}
+
// Lady Ada's "ordered dither" algorithm
void dither(LPD8806 &lpd, uint32_t color, uint16_t wait)
{
@@ -88,13 +101,14 @@ void shift_up(LPD8806 &lpd)
void shift_down(LPD8806 &lpd)
{
uint32_t previous = lpd.getPixelColor(0);
- for (uint16_t i = lpd.numPixels() - 1; i != 0; --i)
+ uint16_t i = lpd.numPixels() - 1;
+ do
{
uint32_t current = lpd.getPixelColor(i);
lpd.setPixelColor(i, previous);
previous = current;
}
- lpd.setPixelColor(0, previous);
+ while (i-- != 0);
}
void cycle_up(LPD8806 &lpd, uint16_t wait)
@@ -129,17 +143,27 @@ void fill_init(LPD8806 &lpd)
}
}
-void fill_repeat(LPD8806 &lpd)
+void fill_up_repeat(LPD8806 &lpd)
+{
+ // cycle through colors at roughly 2 Hz
+ unsigned ms = 500/lights.numPixels();
+ for (size_t i = 0; i < palette_count; ++i)
+ {
+ fill_up(lpd, palette[i], ms);
+ }
+}
+
+void fill_down_repeat(LPD8806 &lpd)
{
// cycle through colors at roughly 2 Hz
unsigned ms = 500/lights.numPixels();
for (size_t i = 0; i < palette_count; ++i)
{
- fill(lpd, palette[i], ms);
+ fill_down(lpd, palette[i], ms);
}
}
-void dither_repeat(LPD8806 &lpd)
+void fade_repeat(LPD8806 &lpd)
{
// switch colors at roughly 1 Hz
unsigned ms = 1000/lights.numPixels()/4;
@@ -187,8 +211,9 @@ typedef struct
const pattern_t pattern[] =
{
- { fill_init, fill_repeat },
- { fill_init, dither_repeat },
+ { fill_init, fade_repeat },
+ { fill_init, fill_up_repeat },
+ { fill_init, fill_down_repeat },
{ palette_init, cycle_up_repeat },
{ palette_init, cycle_down_repeat },
};
@@ -201,15 +226,25 @@ size_t pattern_index = 0;
void setup()
{
+#ifndef DEBUG
// read pattern_index from EEPROM
pattern_index = EEPROM.read(0);
+#endif
if (pattern_index >= pattern_count)
{
pattern_index = 0;
}
+#ifndef DEBUG
// write next pattern_index to EEPROM (after short delay)
delay(250);
EEPROM.write(0, pattern_index + 1);
+#endif
+
+ // color correct palette for human perception
+ for (size_t i = 0; i < palette_count; ++i)
+ {
+ palette[i] = CIE_L(palette[i]);
+ }
// initialize lights
lights.begin();

No commit comments for this range

Something went wrong with that request. Please try again.