-
Notifications
You must be signed in to change notification settings - Fork 0
/
ten20Scroller.ino
282 lines (228 loc) · 6.67 KB
/
ten20Scroller.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
#include <Adafruit_NeoPixel.h>
#define DEBUG
#ifdef __AVR__
#include <avr/power.h>
#endif
// time between display updates
#define WAIT 500
// Pixels across
#define WIDTH 20
// Pixels up/down
#define HEIGHT 6
// Arduino pin for display data
#define PIN 6
// Number of pixels connected
#define TOTALLED 120
// max # of letters to display
#define SENTENCELENGTH 20
// max # pixels wide in letter
#define LETTERWIDTH 5
// character in alphabet
typedef struct {
uint32_t pixelData;
byte width;
byte special;
char val; // for debug
} character;
/// CHANGE TO ARRAY
character charA, charB, charC, charD, charE, charH, charL, charO, charR, charT, charN, charM, charAMPER, charSPACE, charCOMMA, charPERIOD, charEXCLAIM, charEOL;
// character alphabet[70];
// A-Z 0-26
// a-z 27-52
// 0-9 53-62
// space, period, exclaim, comma, hashtag, dash, colon, slash, at? 63-70
// init the led display driver
Adafruit_NeoPixel strip = Adafruit_NeoPixel(TOTALLED, PIN, NEO_GRB + NEO_KHZ800);
// default for LED ON / off
uint32_t ON = strip.Color(200, 0, 0); // pixel on = red (eventually allow words to have different colors)
uint32_t OFF = strip.Color(0, 0, 0); // pixel off
// characters converted into on/off matrix
bool ledData[HEIGHT][(SENTENCELENGTH * LETTERWIDTH)];
// current index in ledData going left to right
uint8_t currentIndex;
// last piece of parsed data in array (array size likely larger than needs to be, so no reason for loop to look at "empty" data)
uint8_t maxIndex;
uint8_t lettersInSentence;
// holds characters to show in display
character sentence[SENTENCELENGTH];
void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
Serial.println("build alphabet");
buildChars();
Serial.println("load sentence");
sentence[0] = charSPACE;
sentence[1] = charH;
sentence[2] = charSPACE;
sentence[3] = charE;
sentence[4] = charSPACE;
sentence[5] = charL;
sentence[6] = charSPACE;
sentence[7] = charL;
sentence[8] = charSPACE;
sentence[9] = charO;
sentence[10] = charEOL;
lettersInSentence = 11;
Serial.println("wait 5 sec");
delay(5000);
Serial.println("Parse Sentence");
parseSentence();
Serial.println("start strip");
strip.begin();
strip.show(); // Initialize all pixels to 'off'
currentIndex = 0;
Serial.println("Ready to go");
Serial.print("WIDTH: ");
Serial.println(WIDTH);
Serial.print("HEIGHT: ");
Serial.println(HEIGHT);
Serial.print("Total leds: ");
Serial.println(TOTALLED);
delay(1000);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// flash each character in sentence
Serial.print("loop -> current: ");
Serial.print(currentIndex);
//Serial.print(" of ");
//Serial.println(maxIndex);
Serial.println("");
// while there's more data to show
if (currentIndex <= maxIndex) {
for (int r = 0; r < HEIGHT; r++)
{
int limit = currentIndex + WIDTH;
Serial.println("");
Serial.print("r: ");
Serial.print(r);
Serial.print(" ");
Serial.print(currentIndex);
Serial.print(" to ");
Serial.print(limit);
for (int c = currentIndex; c < limit; c++)
{
Serial.print(" : ");
Serial.print(c);
Serial.print("(");
if (ledData[r][c]) {
Serial.print(1);
strip.setPixelColor(((r * WIDTH) + c) - currentIndex, ON);
} else {
Serial.print(0);
strip.setPixelColor(((r * WIDTH) + c) - currentIndex, OFF);
}
Serial.print(")");
}
}
if (currentIndex % 2 == 0)
strip.setPixelColor(0, strip.Color(0, 0, 200));
else
strip.setPixelColor(0, strip.Color(0, 200, 0));
strip.show();
}
Serial.println("");
currentIndex++;
delay(WAIT);
}
/////////////////////////////////////////////////////////////////////
// Take the letters and build he pixel maps
void parseSentence() {
uint8_t idx = 0;
uint8_t row = HEIGHT;
uint8_t comp = 0;
Serial.println("..parse");
// skip the width of output to prepend blanks, so text will scroll in from the right
maxIndex = WIDTH;
// got through character by character until hit limit or the EOL
while (idx < SENTENCELENGTH && sentence[idx].special != 2) {
character cur = sentence[idx];
/*
Serial.println();
Serial.print(maxIndex);
Serial.print("....");
Serial.print(idx);
Serial.print(":");
Serial.println(cur.val);
*/
for (int r = 0; r < HEIGHT; r++)
{
/*
Serial.print("r: ");
Serial.print(r);
Serial.print(" ");
*/
for (int c = 0; c < cur.width ; c++)
{
/*
Serial.print(" ");
Serial.print("c: ");
Serial.print(c);
Serial.print(" (");
*/
uint32_t shift1 = (r * cur.width);
uint32_t shifted = shift1 + c;
unsigned long offset = (unsigned long)1 << shifted;
/*
Serial.print(shifted);
Serial.print(" << ");
Serial.print(offset);
Serial.print(")");
*/
// shift the number for comparison below to see if pixel is on or off
bool isOn = (cur.pixelData & offset) == offset;
// set LED bit to correspond to the character pixel status
if (isOn) {
int col = c + maxIndex;
/*Serial.print(" on (");
Serial.print(r);
Serial.print(":");
Serial.print(col);
Serial.print(") ");
*/
ledData[r][col] = true;
}
}
// Serial.println("");
}
maxIndex += cur.width;
idx++;
}
Serial.print("..max index = ");
Serial.println(maxIndex);
}
/////////////////////////////////////////////////////////////////////
void buildChars() {
charA.pixelData = 630678;
charA.width = 4;
charA.val = 'A';
charB.pixelData = 7642407;
charB.width = 4;
charB.val = 'B';
charC.pixelData = 14713902;
charC.width = 4;
charC.val = 'C';
charE.pixelData = 988959;
charE.width = 4;
charE.val = 'E';
charH.pixelData = 630681;
charH.width = 4;
charH.val = 'H';
charL.pixelData = 29257;
charL.width = 3;
charL.val = 'L';
charO.pixelData = 432534;
charO.width = 4;
charO.val = 'O';
charSPACE.pixelData = 0;
charSPACE.width = 1;
charSPACE.val = '_';
charEOL.pixelData = 0;
charEOL.val = '^';
charEOL.special = 1;
}
// code from here:
// https://stackoverflow.com/questions/3018313/algorithm-to-convert-rgb-to-hsv-and-hsv-to-rgb-in-range-0-255-for-both#_=_