forked from kyma-incubator/bullseye-showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slabs.ino
155 lines (132 loc) · 2.72 KB
/
slabs.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <Adafruit_NeoPixel.h>
#define LED_PIN 17
#define SENSOR_PIN A0
#define PIXELS 24
#define BRIGHTNESS 50
// Addresses
#define THRESHOLD 0
#define ID 1
#define Strip Adafruit_NeoPixel
#define rgb(r, g, b) strip.Color(r, g, b)
enum Command { cmd_id, cmd_on, cmd_off, cmd_animate,
cmd_sensor, cmd_raw_sensor,
cmd_set_brightness,
cmd_set_threshold, cmd_set_id};
Strip strip(PIXELS, LED_PIN);
int id = 0;
int threshold = 0;
uint32_t init_color = rgb(237, 17, 230);
uint32_t off_color = rgb(0, 0, 0);
uint32_t green = rgb(0, 255, 0);
uint32_t red = rgb(255, 0, 0);
void setup() {
Serial.begin(9600);
strip.begin();
strip.setBrightness(BRIGHTNESS);
turn_off();
circle_animate(init_color, 20);
delay(10);
circle_animate(off_color, 20);
while (!Serial) delay(10);
Serial.write(96);
flash(init_color, 100);
}
void loop() {
while (Serial.available() == 0) delay(5);
if (Serial.available() > 0) {
Command mode = static_cast<Command>(Serial.read());
int reading;
int input;
uint32_t color;
unsigned long ms;
switch (mode) {
case cmd_on:
color = read_color();
set_color(color);
break;
case cmd_off:
turn_off();
break;
case cmd_animate:
color = read_color();
ms = Serial.read();
circle_animate(color, ms);
break;
case cmd_id:
Serial.write(id);
break;
case cmd_sensor:
reading = read_sensor();
Serial.write(reading);
break;
case cmd_raw_sensor:
reading = analogRead(SENSOR_PIN);
Serial.write(reading);
break;
case cmd_set_brightness:
input = Serial.read();
set_brightness(input);
break;
case cmd_set_threshold:
input = Serial.read();
set_threshold(input);
break;
case cmd_set_id:
input = Serial.read();
set_id(input);
break;
}
}
}
void set_threshold(int value) {
if (threshold == value) return;
threshold = value;
// EEPROM.put(THRESHOLD, value);
}
int read_threshold() {
return threshold;
}
void set_id(int value) {
if (id == value) return;
id = value;
// EEPROM.put(ID, value);
}
int read_id() {
return id;
}
uint32_t read_color() {
char buf[3];
Serial.readBytes(buf, 3);
int r = buf[0];
int g = buf[1];
int b = buf[2];
return strip.Color(r, g, b);
}
void set_brightness(uint8_t value) {
strip.setBrightness(value);
}
void turn_off() {
strip.clear();
strip.show();
}
void set_color(uint32_t color) {
strip.fill(color);
strip.show();
}
void flash(uint32_t color, unsigned long ms) {
set_color(color);
delay(ms);
turn_off();
}
void circle_animate(uint32_t color, unsigned long ms) {
int pixels = strip.numPixels();
for (int i = 0; i < pixels; i++) {
strip.setPixelColor(i, color);
strip.show();
delay(ms);
}
}
int read_sensor() {
int value = analogRead(SENSOR_PIN);
return (value < threshold);
}