Skip to content

Instantly share code, notes, and snippets.

@AndyA
Created July 14, 2014 08:24
Show Gist options
  • Select an option

  • Save AndyA/31f8f394772ce9443125 to your computer and use it in GitHub Desktop.

Select an option

Save AndyA/31f8f394772ce9443125 to your computer and use it in GitHub Desktop.
// 13 -> 9 CLOCK
// 12 -> 8 SERIAL OUT
// 11 -> 7 SERIAL IN
// 7 -> 4 STROBE
// 6 -> 3 OE
// 4 -> 2 LATCH
#define CLOCK 9
#define SEROUT 8
#define SERIN 7
#define STROBE 4
#define OE 3
#define LATCH 2
#define MAXX 18
#define MAXY 4
#define NLIGHT (MAXX * MAXY)
#define LEVELS 4
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
static unsigned char light[72];
static unsigned char key[72];
static unsigned keyOffset(int x, int y) {
static unsigned char flip[8] = { 7, 5, 6, 0, 1, 2, 3, 4 };
if (x < 0 || x >= MAXX || y < 0 || y >= MAXY) return 255;
unsigned raw = x * MAXY + y;
return (64 - (raw & 0xf8)) | flip[raw & 0x07];
}
static unsigned lightOffset(int x, int y) {
if (x < 0 || x >= MAXX || y < 0 || y >= MAXY) return 255;
unsigned raw = x * MAXY + y;
return (raw & 0xf8) | (7 - (raw & 0x07));
}
static void setLight(int x, int y, int bright) {
unsigned ofs = lightOffset(x, y);
if (ofs != 255) light[ofs] = MAX(0, MIN(bright, 255));
}
static unsigned getKey(int x, int y) {
unsigned ofs = keyOffset(x, y);
if (ofs == 255) return 0;
return key[ofs];
}
void setup() {
pinMode(CLOCK, OUTPUT);
pinMode(SEROUT, OUTPUT);
pinMode(SERIN, INPUT);
pinMode(STROBE, OUTPUT);
pinMode(OE, OUTPUT);
pinMode(LATCH, OUTPUT);
for (int x = 0; x < MAXX; x++) {
for (int y = 0; y < MAXY; y++) {
setLight(x, y, x * 255 / MAXX);
}
}
}
static void update() {
for (unsigned phase = 0; phase <= LEVELS; phase++) {
unsigned clip = phase * 256 / LEVELS;
digitalWrite(LATCH, LOW);
digitalWrite(LATCH, HIGH);
for (int l = 0; l < NLIGHT; l++) {
digitalWrite(SEROUT, light[l] > clip ? HIGH : LOW);
key[l] = digitalRead(SERIN) ? 0 : 1;
digitalWrite(CLOCK, HIGH);
digitalWrite(CLOCK, LOW);
}
digitalWrite(STROBE, HIGH);
digitalWrite(STROBE, LOW);
}
}
static int hot_x = 0, hot_y = 0, hot = 0;
void loop() {
update();
#if 1
if (hot++ > 3) {
setLight(hot_x, hot_y, 255);
setLight(MAXX - hot_x - 1, hot_y, 255);
setLight(MAXX - hot_x - 1, MAXY - hot_y - 1, 255);
setLight(hot_x, MAXY - hot_y - 1, 255);
hot_y++;
if (hot_y == MAXY) {
hot_y = 0;
hot_x++;
if (hot_x == MAXX)
hot_x = 0;
}
hot = 0;
}
#endif
for (int x = 0; x < MAXX; x++) {
for (int y = 0; y < MAXY; y++) {
if (getKey(x, y))
setLight(x, y, 255);
}
}
for (int i = 0; i < NLIGHT; i++) {
if (light[i] > 0) light[i] -= MIN(light[i], 5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment