Skip to content

Commit 8fb6a86

Browse files
committed
Eyes.
1 parent 5e7160e commit 8fb6a86

File tree

4 files changed

+125
-4
lines changed

4 files changed

+125
-4
lines changed

Eyes.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
G35: An Arduino library for GE Color Effects G-35 holiday lights.
3+
Copyright © 2011 The G35 Authors. Use, modification, and distribution are
4+
subject to the BSD license as described in the accompanying LICENSE file.
5+
6+
By Mike Tsao <http://github.com/sowbug>.
7+
8+
See README for complete attributions.
9+
*/
10+
11+
#include <Eyes.h>
12+
13+
Eyes::Eyes(G35& g35) : LightProgram(g35), count_(0), next_eye_(0) {
14+
g35_.fill_color(0, light_count_, 255, COLOR_BLACK);
15+
16+
while (3 * EYE_COUNT >= light_count_) {
17+
// There isn't enough room on the light string for this many eyes.
18+
}
19+
20+
for (int i = 0; i < EYE_COUNT; ++i) {
21+
uint16_t new_position;
22+
do {
23+
new_position = rand() % (light_count_ - 1);
24+
for (int j = 0; j < i; ++j) {
25+
uint16_t occupied_position = eyes_[j].get_position();
26+
if (new_position >= occupied_position - 1 &&
27+
new_position <= occupied_position + 1) {
28+
new_position = light_count_;
29+
break;
30+
}
31+
}
32+
} while (new_position == light_count_);
33+
eyes_[i].set_position(new_position);
34+
}
35+
}
36+
37+
uint32_t Eyes::Do() {
38+
for (int i = 0; i < count_; ++i) {
39+
eyes_[i].Do(g35_);
40+
}
41+
if (count_ < EYE_COUNT && millis() > next_eye_) {
42+
++count_;
43+
next_eye_ = millis() + 2000 + 1000 * count_;
44+
}
45+
return bulb_frame_;
46+
}

Eyes.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
G35: An Arduino library for GE Color Effects G-35 holiday lights.
3+
Copyright © 2011 The G35 Authors. Use, modification, and distribution are
4+
subject to the BSD license as described in the accompanying LICENSE file.
5+
6+
By Mike Tsao <http://github.com/sowbug>.
7+
8+
See README for complete attributions.
9+
*/
10+
11+
#ifndef INCLUDE_G35_PROGRAMS_EYES_H
12+
#define INCLUDE_G35_PROGRAMS_EYES_H
13+
14+
#include <LightProgram.h>
15+
16+
class Eye {
17+
public:
18+
Eye()
19+
: color_(COLOR_BLACK), position_(0), next_blink_(0), state_(INIT) {
20+
switch (rand() % 4) {
21+
case 0: color_ = COLOR_ORANGE; break;
22+
case 1: color_ = COLOR_RED; break;
23+
case 2: color_ = COLOR_INDIGO; break;
24+
case 3: color_ = COLOR_GREEN; break;
25+
}
26+
}
27+
28+
uint16_t get_position() { return position_; }
29+
void set_position(uint16_t value) { position_ = value; }
30+
31+
void Do(G35& g35) {
32+
if (state_ == INIT) {
33+
state_ = WATCHING;
34+
}
35+
uint32_t now = millis();
36+
if (next_blink_ < now) {
37+
if (state_ == BLINKING) {
38+
g35.fill_color(position_, 2, G35::MAX_INTENSITY, color_);
39+
next_blink_ = now + 3000 + (rand() % 3000);
40+
state_ = WATCHING;
41+
} else {
42+
g35.fill_color(position_, 2, G35::MAX_INTENSITY, COLOR_BLACK);
43+
next_blink_ = now + 100;
44+
state_ = BLINKING;
45+
}
46+
}
47+
}
48+
49+
private:
50+
enum {
51+
INIT, WATCHING, BLINKING
52+
};
53+
54+
color_t color_;
55+
uint16_t position_;
56+
uint32_t next_blink_;
57+
uint8_t state_;
58+
};
59+
60+
class Eyes : public LightProgram {
61+
public:
62+
Eyes(G35& g35);
63+
uint32_t Do();
64+
65+
private:
66+
enum { EYE_COUNT = 10 };
67+
68+
uint8_t count_;
69+
uint32_t next_eye_;
70+
Eye eyes_[EYE_COUNT];
71+
};
72+
73+
#endif // INCLUDE_G35_PROGRAMS_EYES_H

HalloweenPrograms.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
LightProgram* HalloweenProgramGroup::CreateProgram(G35& lights,
1414
uint8_t program_index) {
1515
switch (program_index % ProgramCount) {
16-
case 0: return new Creepers(lights);
17-
case 1: return new PumpkinChase(lights);
18-
case 2: return new SpookySlow(lights);
16+
case 0: return new Eyes(lights);
17+
case 1: return new Creepers(lights);
18+
case 2: return new PumpkinChase(lights);
19+
case 3: return new SpookySlow(lights);
1920
}
2021

2122
// not reached

HalloweenPrograms.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
#include <Creepers.h>
1616
#include <PumpkinChase.h>
1717
#include <SpookySlow.h>
18+
#include <Eyes.h>
1819

1920
class HalloweenProgramGroup : public LightProgramGroup {
2021
public:
21-
enum { ProgramCount = 3 };
22+
enum { ProgramCount = 4 };
2223

2324
virtual LightProgram* CreateProgram(G35& lights, uint8_t program_index);
2425
};

0 commit comments

Comments
 (0)