-
Notifications
You must be signed in to change notification settings - Fork 0
/
meeting.ino
273 lines (210 loc) · 5.94 KB
/
meeting.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// lamp for arduino lamp workshop in torino on 20/21.2.2015 in
// fabian frei
// include process library
#include <Process.h>
#include "MQTTclient.h"
#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
// define DEBUG for debug mode
# define DEBUG
namespace lamp {
typedef struct {
int r;
int g;
int b;
int hits;
} rgbColorAndHits;
#define GIF_AMOUNT 6
rgbColorAndHits hits[GIF_AMOUNT];
}
#define DELAY 5
#define MQTT_HOST "85.119.83.194" // test.mosquitto.org
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN 6
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 60
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int color[3] = {100, 100, 100}; //the Array that defines the color of the light
void setup() {
#ifdef DEBUG
//delay(5000);
Serial.println("Running in Debug mode");
#endif
pixels.begin(); // This initializes the NeoPixel library.
delay(1000);
// init all the hit structs
for(int i = 0; i > GIF_AMOUNT; i++) {
lamp::hits[i] = { 0, 0, 0, 0 };
}
#ifdef DEBUG
Serial.println("all hit containers initialized");
#endif
resetAllLeds();
#ifdef DEBUG
Serial.println("all leds reset");
#endif
pinMode(13, OUTPUT);
#ifdef DEBUG
Serial.println("Waiting for linux to boot");
#endif
Bridge.begin();
#ifdef DEBUG
Serial.println("Linux booted");
Serial.println("Waiting for Wifi to connect");
#endif
delay(10000);//because we wait for the wifi to connect!!
#ifdef DEBUG
Serial.println("Waited 10 sec");
#endif
mqtt.begin(MQTT_HOST, 1883);
mqtt.subscribe("oscola/23981479834/changeColor", registerCheck);
#ifdef DEBUG
registerCheckTest("#F01616");
delay(1000);
registerCheckTest("#0E6DEC");
delay(1000);
registerCheckTest("#17EC52");
#endif
}
void loop() {
mqtt.monitor();
#ifdef DEBUG
Process wifiCheck;
wifiCheck.runShellCommand("/usr/bin/pretty-wifi-info.lua");
while (wifiCheck.available() > 0) {
char c = wifiCheck.read();
Serial.print(c);
}
Serial.println();
delay(5000);
// registerCheckTest("#B83737");
#endif
}
void hexColorConverter(String c) {
String hexstring = c.substring(1);
#ifdef DEBUG
//Serial.print(hexstring);
//Serial.print("-");
#endif
long number = (long) strtol( &hexstring[0], NULL, 16);
color[0] = number >> 16;
color[1] = number >> 8 & 0xFF;
color[2] = number & 0xFF;
#ifdef DEBUG
Serial.print(color[0]);
Serial.print("-");
Serial.print(color[1]);
Serial.print("-");
Serial.println(color[2]);
#endif
}
// my stuff
void resetAllLeds() {
for(int i = 0; i > NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
delay(DELAY);
}
}
void registerCheck(const String& topic, const String& subtopic, const String& message) {
registerCheckTest(message);
}
void registerCheckTest(String message) {
#ifdef DEBUG
Serial.println(message);
Serial.println("checking all the hits");
hexColorConverter(message);
Serial.println(color[0]);
Serial.println(color[1]);
Serial.println(color[2]);
Serial.println("checking all the hits");
#endif
for(int i = 0; i < GIF_AMOUNT; i++) {
if(lamp::hits[i].r == color[0] && lamp::hits[i].g == color[1] && lamp::hits[i].b == color[2]) { // we have a hit
lamp::hits[i].hits++;
#ifdef DEBUG
Serial.print("we found something, with ");
Serial.println(lamp::hits[i].hits);
#endif
recalculateColors();
return;
}
}
// we did not find the color, lets add it
#ifdef DEBUG
Serial.println("we found nothing, adding new color");
#endif
for(int i = 0; i < GIF_AMOUNT; i++) {
if(lamp::hits[i].hits == 0) {
lamp::hits[i].hits = 1;
lamp::hits[i].r = color[0];
lamp::hits[i].g = color[1];
lamp::hits[i].b = color[2];
recalculateColors();
return; // we return early, we because only one new color
}
}
}
void recalculateColors() {
int total = 0;
int offset = 0;
for(int i = 0; i < GIF_AMOUNT; i++) {
if(lamp::hits[i].hits == 0) {
break;
}
total += lamp::hits[i].hits;
}
#ifdef DEBUG
Serial.print("total: ");
Serial.println(total);
#endif
for(int i = 0; i < GIF_AMOUNT; i++) {
int amount = floor(float(lamp::hits[i].hits) / float(total) * NUMPIXELS);
#ifdef DEBUG
Serial.print("amount: ");
Serial.println(amount);
Serial.print("offset: ");
Serial.println(offset);
#endif
for(int y = offset; y < offset + amount && y < NUMPIXELS; y++) {
#ifdef DEBUG
Serial.print("painting pixel ");
Serial.println(y);
Serial.print("with color: ");
Serial.print(lamp::hits[i].r);
Serial.print("-");
Serial.print(lamp::hits[i].g);
Serial.print("-");
Serial.println(lamp::hits[i].b);
#endif
pixels.setPixelColor(y, pixels.Color(lamp::hits[i].r, lamp::hits[i].g, lamp::hits[i].b));
pixels.show();
delay(DELAY);
}
offset += amount;
// we return early when we reach the maximum amount of pixels
if(offset == NUMPIXELS) {
#ifdef DEBUG
Serial.println("maximum offset reached");
#endif
return;
}
// we reached the last one, now we need to check if need to fill up
if(i == GIF_AMOUNT - 1 && offset < NUMPIXELS) {
#ifdef DEBUG
Serial.print("we need to fill up: ");
Serial.println(NUMPIXELS - offset);
#endif
for(;offset == NUMPIXELS; offset++) {
pixels.setPixelColor(offset, pixels.Color(lamp::hits[i].r, lamp::hits[i].g, lamp::hits[i].b));
pixels.show();
delay(DELAY);
}
}
}
}