-
Notifications
You must be signed in to change notification settings - Fork 0
/
IKEA-LED-Table ino
306 lines (257 loc) · 6.81 KB
/
IKEA-LED-Table 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* LedTable
*
* Written by: Klaas De Craemer, Ing. David Hrbaty
*
*
* Main file with common methods and defines, such as button reading from Bluetooth controller
* or setting pixels on the LED area
*/
#include <SoftwareSerial.h>
//LED field size
#define FIELD_WIDTH 10
#define FIELD_HEIGHT 10
#define ORIENTATION_HORIZONTAL //Rotation of table, uncomment to rotate field 90 degrees
#define USE_FAST_LED // FAST_LED as library to control the LED strips
#define BLUETOOTH_SPEED 38400
/*
* Some defines used by the FAST_LED library
*/
#define FAST_LED_CHIPSET WS2811
#define FAST_LED_DATA_PIN 5
#if defined(USE_OCTOWS2811) && defined(USE_FAST_LED)
#error "Only one of USE_OCTOWS2811 and USE_FAST_LED can be defined"
#endif
#define NUM_PIXELS FIELD_WIDTH*FIELD_HEIGHT
/* *** LED color table *** */
#define GREEN 0x00FF00
#define RED 0xFF00FF
#define BLUE 0x0000FF
#define YELLOW 0xFFFF00
#define LBLUE 0x00FFFF
#define PURPLE 0xFF00FF
#define WHITE 0XFFFFFF
unsigned int colorLib[3] = {YELLOW, BLUE, WHITE};
/* *** Game commonly used defines ** */
#define DIR_UP 1
#define DIR_DOWN 2
#define DIR_LEFT 3
#define DIR_RIGHT 4
/* *** Bluetooth controller button defines and input method *** */
#define BTN_NONE 0
#define BTN_UP 1
#define BTN_DOWN 2
#define BTN_LEFT 3
#define BTN_RIGHT 4
#define BTN_START 5
#define BTN_EXIT 6
uint8_t curControl = BTN_NONE;
SoftwareSerial bluetooth(10, 11);
void readInput(){
curControl = BTN_NONE;
if (bluetooth.available() > 0) {
// read the incoming byte:
uint8_t incomingByte = bluetooth.read();
Serial.println(incomingByte);
switch(incomingByte){
case 238:
curControl = BTN_LEFT;
break;
case 239:
curControl = BTN_RIGHT;
break;
case 236:
curControl = BTN_UP;
break;
case 237:
curControl = BTN_DOWN;
break;
case 224:
curControl = BTN_START;
break;
case 225:
curControl = BTN_EXIT;
break;
}
}
}
/*
* FAST_LED implementation
*/
#include "FastLED.h"
CRGB leds[NUM_PIXELS];
void initPixels(){
FastLED.addLeds<FAST_LED_CHIPSET, FAST_LED_DATA_PIN, RGB>(leds, NUM_PIXELS);
}
void setPixel(int n, int color){
leds[n] = CRGB(color);
}
void setPixelRGB(int n, int r,int g, int b){
leds[n] = CRGB(r,g,b);
}
void setDelay(int duration) {
FastLED.delay(duration);
}
int getPixel(int n){
return (leds[n].r << 16) + (leds[n].g << 8) + leds[n].b;
}
void showPixels(){
FastLED.show();
}
void setTablePixel(int x, int y, int color){
#ifdef ORIENTATION_HORIZONTAL
setPixel(y%2 ? y*FIELD_WIDTH + x : y*FIELD_WIDTH + ((FIELD_HEIGHT-1)-x),color);
#ifdef USE_CONSOLE_OUTPUT
setTablePixelConsole(y,x,color);
#endif
#else
setPixel(x%2 ? x*FIELD_WIDTH + ((FIELD_HEIGHT-1)-y) : x*FIELD_WIDTH + y,color);
#ifdef USE_CONSOLE_OUTPUT
setTablePixelConsole(x,y,color);
#endif
#endif
}
void setTablePixelRGB(int x, int y, int r,int g, int b){
#ifdef ORIENTATION_HORIZONTAL
setPixelRGB(y%2 ? y*FIELD_WIDTH + x : y*FIELD_WIDTH + ((FIELD_HEIGHT-1)-x),r,g,b);
#ifdef USE_CONSOLE_OUTPUT
setTablePixelConsole(y,x,color);
#endif
#else
setPixelRGB(x%2 ? x*FIELD_WIDTH + ((FIELD_HEIGHT-1)-y) : x*FIELD_WIDTH + y,r,g,b);
#ifdef USE_CONSOLE_OUTPUT
setTablePixelConsole(x,y,color);
#endif
#endif
}
void clearTablePixels(){
for (int n=0; n<FIELD_WIDTH*FIELD_HEIGHT; n++){
setPixel(n,0);
}
}
/* *** text helper methods *** */
#include "font.h"
uint8_t charBuffer[8][8];
void printText(char* text, unsigned int textLength, int xoffset, int yoffset, int color){
uint8_t curLetterWidth = 0;
int curX = xoffset;
clearTablePixels();
//Loop over all the letters in the string
for (int i=0; i<textLength; i++){
//Determine width of current letter and load its pixels in a buffer
curLetterWidth = loadCharInBuffer(text[i]);
//Loop until width of letter is reached
for (int lx=0; lx<curLetterWidth; lx++){
//Now copy column per column to field (as long as within the field
if (curX>=FIELD_WIDTH){//If we are to far to the right, stop loop entirely
break;
} else if (curX>=0){//Draw pixels as soon as we are "inside" the drawing area
for (int ly=0; ly<8; ly++){//Finally copy column
setTablePixel(curX,yoffset+ly,charBuffer[lx][ly]*color);
}
}
curX++;
}
}
showPixels();
}
//Load char in buffer and return width in pixels
uint8_t loadCharInBuffer(char letter){
uint8_t* tmpCharPix;
uint8_t tmpCharWidth;
int letterIdx = (letter-32)*8;
int x=0; int y=0;
for (int idx=letterIdx; idx<letterIdx+8; idx++){
for (int x=0; x<8; x++){
charBuffer[x][y] = ((font[idx]) & (1<<(7-x)))>0;
}
y++;
}
tmpCharWidth = 8;
return tmpCharWidth;
}
/* *********************************** */
void fadeOut(){
//Select random fadeout animation
int selection = random(3);
switch(selection){
case 0:
case 1:
{
//Fade out by dimming all pixels
for (int i=0; i<100; i++){
dimLeds(0.97);
showPixels();
delay(10);
}
break;
}
case 2:
{
//Fade out by swiping from left to right with ruler
const int ColumnDelay = 10;
int curColumn = 0;
for (int i=0; i<FIELD_WIDTH*ColumnDelay; i++){
dimLeds(0.97);
if (i%ColumnDelay==0){
//Draw vertical line
for (int y=0;y<FIELD_HEIGHT;y++){
setTablePixel(curColumn, y, GREEN);
}
curColumn++;
}
showPixels();
delay(5);
}
//Sweep complete, keep dimming leds for short time
for (int i=0; i<100; i++){
dimLeds(0.9);
showPixels();
delay(5);
}
break;
}
}
}
void dimLeds(float factor){
//Reduce brightness of all LEDs, typical factor is 0.97
for (int n=0; n<(FIELD_WIDTH*FIELD_HEIGHT); n++){
int curColor = getPixel(n);
//Derive the tree colors
int r = ((curColor & 0xFF0000)>>16);
int g = ((curColor & 0x00FF00)>>8);
int b = (curColor & 0x0000FF);
//Reduce brightness
r = r*factor;
g = g*factor;
b = b*factor;
//Pack into single variable again
curColor = (r<<16) + (g<<8) + b;
//Set led again
setPixel(n,curColor);
}
}
void testMatrix() {
setTablePixel(0, 0, WHITE);
showPixels();
delay(2000);
setTablePixel(0, 9, WHITE);
showPixels();
delay(2000);
setTablePixel(9, 0, WHITE);
showPixels();
delay(2000);
setTablePixel(9, 9, WHITE);
}
void setup(){
Serial.begin(115200);
//Wait for serial port to connect
bluetooth.begin(BLUETOOTH_SPEED);
//Initialise display
initPixels();
showPixels();
//Init random number generator
randomSeed(millis());
mainLoop();
}
void loop(){
}