-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40h.cpp
More file actions
592 lines (510 loc) · 18.4 KB
/
Copy path40h.cpp
File metadata and controls
592 lines (510 loc) · 18.4 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
* Copyright (C) 2006, Brian Crabtree and Joe Lake, monome.org
*
* This file is part of 40h.
*
* 40h is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 40h is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with 40h; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* $Id: 40h.c,v. 1.1.1.1 2006/05/02 1:01:22
*/
// #define F_CPU 16000000UL // moved to project definition
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include "config.h"
#include "message.h"
#include "adc.h"
#include "button.h"
#include <NeoPixelBus.h>
struct io_pin_t
{
enum port_num { A, B, C, D } port;
uint8 pin;
};
inline void output_pin(io_pin_t iopin, bool state)
{
if (state) {
switch (iopin.port) {
case io_pin_t::A:
PORTA |= (1 << iopin.pin);
break;
case io_pin_t::B:
PORTB |= (1 << iopin.pin);
break;
case io_pin_t::C:
PORTC |= (1 << iopin.pin);
break;
case io_pin_t::D:
PORTD |= (1 << iopin.pin);
break;
}
} else {
switch (iopin.port) {
case io_pin_t::A:
PORTA &= ~(1 << iopin.pin);
break;
case io_pin_t::B:
PORTB &= ~(1 << iopin.pin);
break;
case io_pin_t::C:
PORTC &= ~(1 << iopin.pin);
break;
case io_pin_t::D:
PORTD &= ~(1 << iopin.pin);
break;
}
}
}
bool input_pin(io_pin_t pin)
{
switch (pin.port) {
case io_pin_t::A:
return (PINA & (1 << pin.pin));
case io_pin_t::B:
return (PINB & (1 << pin.pin));
case io_pin_t::C:
return (PINC & (1 << pin.pin));
case io_pin_t::D:
return (PIND & (1 << pin.pin));
}
return 0;
}
const uint16 kLedStripPixelCount = 64;
const uint8 kLedStripDataPin = 5; // MCU pin 6 / PB5 -> Arduino D5
// Hardware changes to support 3-wire RGB pixel leds:
// remove max7219 (it was used for SPI LEDs)
// 3 wire strip connections:
// data from max7219 socket pin 1
// ground from max7219 socket pin 4
// vcc from max7219 socket pin 19 (opposite side of 1, count back from 24)
const RgbColor kOffColor{ 0 };
constexpr uint8 kDimVal = 4;
constexpr uint8 kPresetCount = 32;
constexpr uint8 kPresetGroupSize = 16;
RgbColor gColorPresets[kPresetCount]
{
// Group 1 (2 groups so that slot numbers fit in 4 bits of message header)
{ 0, 0, 0x3f },
{ 0, 0x3f, 0 },
{ 0x3f, 0, 0 },
{ 0, 0x3f, 0x3f },
{ 0x3f, 0x3f, 0 },
{ 0x3f, 0, 0x3f },
{ 0, 0, 0x7f },
{ 0, 0x7f, 0 },
{ 0x7f, 0, 0 },
{ 0, 0x7f, 0x7f },
{ 0x7f, 0x7f, 0 },
{ 0x7f, 0, 0x7f },
{ 0, 0, kDimVal },
{ 0, kDimVal, 0 },
{ kDimVal, 0, 0 },
{ kDimVal, 0, kDimVal },
// Group 2
{ 0, 0, 0x1f },
{ 0, 0x1f, 0 },
{ 0x1f, 0, 0 },
{ 0, 0x1f, 0x1f },
{ 0x1f, 0x1f, 0 },
{ 0x1f, 0, 0x1f },
{ 0, 0, 0xf },
{ 0, 0xf, 0 },
{ 0xf, 0, 0 },
{ 0, 0xf, 0xf },
{ 0xf, 0xf, 0 },
{ 0xf, 0, 0xf },
{ 0, 0, kDimVal*2 },
{ 0, kDimVal*2, 0 },
{ kDimVal*2, 0, 0 },
{ kDimVal*2, 0, kDimVal*2 }
};
constexpr uint8 kMatrixRows = 8;
constexpr uint8 kMatrixCols = 8;
constexpr uint8 kInvalidPixel = -1;
// map of x/y coordinate to pixel strip ID (x == col, y == row).
// Not const because it is expected that the desktop application will
// update the entire matrix at runtime startup so that different
// configurations/layouts can be used without reflashing the firmware.
uint8 kLedMatrix[kMatrixRows][kMatrixCols]
{
{ 0, 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 }
};
using PixelStrip = NeoPixelBus<NeoRgbFeature, NeoAvr800KbpsMethod>;
void RunPixelTest(PixelStrip &strip, uint8 pattern);
struct SerialInputData
{
// first two data bytes are not in the dataEx array for compatibility with t_message unpacking macros
uint8 data0 = 0;
uint8 data1 = 0;
uint8 dataEx[3]{0};
uint8 expectedLen = 0;
uint8 readLen = 0;
uint8 rx_roll = 0;
static uint8 GetExpectedMessageLen(uint8 msgHeader)
{
switch (msgHeader >> 4)
{
case kMessageTypeUpdatePresetGroup1: return 4;
case kMessageTypeUpdatePresetGroup2: return 4;
case kMessageTypeLedRgbOn: return 5;
default: return 2;
}
}
void Read(uint8 b)
{
switch (readLen)
{
case 0:
data0 = b;
expectedLen = GetExpectedMessageLen(b);
break;
case 1:
data1 = b;
break;
default:
if (readLen > 1 && readLen < 5) // this condition should never fail...
dataEx[readLen - 2] = b;
}
++readLen;
}
bool PacketReady()
{
if (!readLen)
return false;
const bool ret = readLen == expectedLen;
if (ret)
{
// reset for read of next packet, current packet data still usable until next read
readLen = 0;
rx_roll = 0;
}
return ret;
}
void CheckRoll()
{
if (!readLen)
return;
// if single packet is "lost" trash it after a chance to get a match
// CheckRoll is called outside of the inner RXF loop
if (++rx_roll > 80)
{
rx_roll = 0;
readLen = 0;
}
}
};
int main(void)
{
uint8 i1, i2, i3;
SerialInputData serial_in;
t_message serial_out;
io_pin_t ioRXF{ io_pin_t::B, 1 }; // UMR245R RXF
io_pin_t ioRD{ io_pin_t::B, 2 }; // UMR245R RD
io_pin_t ioWR{ io_pin_t::B, 3 }; // UMR245R WR
io_pin_t ioLOAD{ io_pin_t::C, 6 }; // 74LS165 SH/LD
io_pin_t ioDATA{ io_pin_t::C, 7}; // 74LS165 QH
io_pin_t ioCLKSEL{ io_pin_t::A, 6 }; // 74LS164 CLK
io_pin_t ioCLKIN{ io_pin_t::A, 7 }; // 74LS165 CLK
io_pin_t ioSET{ io_pin_t::A, 5 }; // 74LS164 A
DDRB &= ~(1 << ioRXF.pin); PORTB |= (1 << ioRXF.pin);
DDRB |= (1 << ioRD.pin); PORTB |= (1 << ioRD.pin);
DDRB |= (1 << ioWR.pin); PORTB |= (1 << ioWR.pin);
DDRC |= (1 << ioLOAD.pin); PORTC |= (1 << ioLOAD.pin);
DDRC &= ~(1 << ioDATA.pin); PORTC |= (1 << ioDATA.pin);
DDRA |= (1 << ioCLKSEL.pin); PORTA |= (1 << ioCLKSEL.pin);
DDRA |= (1 << ioCLKIN.pin); PORTA |= (1 << ioCLKIN.pin);
DDRA |= (1 << ioSET.pin); PORTA |= (1 << ioSET.pin);
output_pin(ioSET, 1); // clear out row selector
for (i1 = 0; i1 < 8; i1++) {
output_pin(ioCLKSEL, 1); // clear out row selector
output_pin(ioCLKSEL, 0);
}
buttonInit();
init(); // for timer0 init, also calls sei() -- modified version of default Arduino code
PixelStrip strip(kLedStripPixelCount, kLedStripDataPin);
strip.Begin();
RunPixelTest(strip, 9);
// ******** main loop ********
while (1)
{
// read incoming serial **********************************************
PORTD = 0; // setup PORTD for input
DDRD = 0; // input w/ tristate
while (!(input_pin(ioRXF)))
{
output_pin(ioRD, 0);
serial_in.Read(PIND);
output_pin(ioRD, 1);
if (serial_in.PacketReady())
{
uint8 msg_data0;
// *********** process packet
const uint8 kMsgType = messageGetType(serial_in);
switch (kMsgType) {
case kMessageTypeLedTest:
msg_data0 = messageGetLedTestState(serial_in);
RunPixelTest(strip, msg_data0);
break;
case kMessageTypePixelIndexToXY:
// 4 bits message type : 6 bits pixel ID (0-63) : 3 bits X (0-7) : 3 bits Y (0-7)
// 6 bit pixel ID: 4 bits lsb in data0 and 2 bits msb in data1
i1 = (serial_in.data0 & 0xF) | ((serial_in.data1 >> 2) & 0x30); // index
if (i1 >= kLedStripPixelCount)
i1 = kInvalidPixel;
i2 = (serial_in.data1 >> 3) & 0x7; // X
i3 = serial_in.data1 & 0x7; // Y
if (i3 < kMatrixRows && i2 < kMatrixCols)
kLedMatrix[i3][i2] = i1;
break;
case kMessageTypeInvalidateAllPixels:
for (uint8 i1 = 0; i1 < kMatrixRows; ++i1)
for (uint8 i2 = 0; i2 < kMatrixCols; ++i2)
kLedMatrix[i1][i2] = kInvalidPixel;
break;
case kMessageTypeLedStateChange:
i1 = messageGetLedX(serial_in);
i2 = messageGetLedY(serial_in);
if (i2 < kMatrixRows && i1 < kMatrixCols)
{
if(messageGetLedState(serial_in) == 0)
strip.SetPixelColor(kLedMatrix[i2][i1], kOffColor);
else
strip.SetPixelColor(kLedMatrix[i2][i1], gColorPresets[0]);
}
break;
case kMessageTypeLedRgbOn:
i1 = messageGetLedX(serial_in);
i2 = messageGetLedY(serial_in);
if (i2 < kMatrixRows && i1 < kMatrixCols)
strip.SetPixelColor(kLedMatrix[i2][i1], RgbColor{serial_in.dataEx[0], serial_in.dataEx[1], serial_in.dataEx[2]});
break;
case kMessageTypeLedOnPresetGroup1:
case kMessageTypeLedOnPresetGroup2:
i1 = messageGetLedX(serial_in);
i2 = messageGetLedY(serial_in);
i3 = messageGetLedColorPreset(serial_in);
if (kMessageTypeLedOnPresetGroup2 == kMsgType)
i3 += kPresetGroupSize;
if (i3 < kPresetCount && i2 < kMatrixRows && i1 < kMatrixCols)
strip.SetPixelColor(kLedMatrix[i2][i1], gColorPresets[i3]);
break;
case kMessageTypeUpdatePresetGroup1:
case kMessageTypeUpdatePresetGroup2:
i1 = messageGetLedColorPreset(serial_in);
if (kMessageTypeUpdatePresetGroup2 == kMsgType)
i1 += kPresetGroupSize;
if (i1 < kPresetCount)
gColorPresets[i1] = RgbColor{serial_in.data1, serial_in.dataEx[0], serial_in.dataEx[1]};
break;
case kMessageTypeAdcEnable:
if (messageGetAdcEnableState(serial_in))
enableAdc(messageGetAdcEnablePort(serial_in));
else
disableAdc(messageGetAdcEnablePort(serial_in));
break;
case kMessageTypeShutdown:
strip.ClearTo(kOffColor);
strip.Show();
delay(500);
break;
case kMessageTypeLedSetRow:
i1 = (messageGetLedRowIndex(serial_in) & 0x7);
i2 = messageGetLedRowState(serial_in);
if (i1 < kMatrixRows)
{
for (i3 = 0; i3 < kMatrixCols; ++i3)
{
strip.SetPixelColor(kLedMatrix[i1][i3], (i2 & 0x1) ? gColorPresets[0] : kOffColor);
i2 >>= 1;
}
}
break;
case kMessageTypeLedSetColumn:
i1 = (messageGetLedColumnIndex(serial_in) & 0x7);
i2 = messageGetLedColumnState(serial_in);
if (i1 < kMatrixCols)
{
for (i3 = 0; i3 < kMatrixRows; ++i3)
{
strip.SetPixelColor(kLedMatrix[i3][i1], (i2 & 0x1) ? gColorPresets[0] : kOffColor);
i2 >>= 1;
}
}
break;
}
}
}
// called even if RXF has no data
serial_in.CheckRoll();
strip.Show();
// output serial data **********************************************
PORTD = 0; // setup PORTD for output
DDRD = 0xFF;
// process buttons
output_pin(ioSET,0);
for (i1 = 0; i1 < 8; i1++) {
output_pin(ioCLKSEL, 1);
output_pin(ioCLKSEL, 0);
output_pin(ioSET, 1);
button_last[i1] = button_current[i1];
output_pin(ioLOAD, 0); // set 165 to load
output_pin(ioLOAD, 1); // 165 shift
for (i2=0; i2 < 8; i2++) {
i3 = input_pin(ioDATA);
i3 = (i3 == 0);
if (i3)
button_current[i1] |= (1 << i2);
else
button_current[i1] &= ~(1 << i2);
buttonCheck(i1, i2);
if (button_event[i1] & (1 << i2)) {
button_event[i1] &= ~(1 << i2);
messagePackButtonPress(&serial_out, (button_state[i1] & (1 << i2)) ? kButtonDownEvent : kButtonUpEvent, i2, i1);
output_pin(ioWR, 1);
PORTD = serial_out.data0;
output_pin(ioWR, 0);
output_pin(ioWR, 1);
PORTD = serial_out.data1;
output_pin(ioWR, 0);
}
output_pin(ioCLKIN, 1);
output_pin(ioCLKIN, 0);
}
}
// process ADC
for (i1 = 0; i1 < kAdcFilterNumAdcs; i1++) {
if (gAdcFilters[i1].dirty == true) {
messagePackAdcVal(&serial_out, i1, gAdcFilters[i1].value);
output_pin(ioWR, 1);
PORTD = serial_out.data0;
output_pin(ioWR, 0);
output_pin(ioWR, 1);
PORTD = serial_out.data1;
output_pin(ioWR, 0);
gAdcFilters[i1].dirty = false;
}
}
}
return 0;
}
// recognized pattern values are:
// 09 : single RGB colors for all pixels; power up pattern (runs before pixel IDs have been mapped to XY)
// 10 : single RGB colors by row
// 11 : single RGB colors by row then by col
// 12 : show preset slots (limited to lesser of either 32 or actual LED pixels as determined by pixel ID of other than kInvalidPixel)
void
RunPixelTest(PixelStrip &strip, uint8 pattern)
{
strip.ClearTo(kOffColor);
if (9 == pattern)
{
// power on startup pattern; all LEDs at once
constexpr uint8 hiVal = 64;
for (uint8 i2 = 0; i2 < 3; ++i2) // iterate over each of the 3 color elements
{
uint8 r1 = 0, g1 = 0, b1 = 0;
switch (i2)
{
case 0:
r1 = hiVal;
break;
case 1:
g1 = hiVal;
break;
case 2:
b1 = hiVal;
break;
}
for (uint8 i1 = 0; i1 < strip.PixelCount(); ++i1)
{
strip.SetPixelColor(i1, RgbColor{ r1, g1, b1 });
}
strip.Show();
delay(750);
strip.ClearTo(kOffColor);
strip.Show();
delay(500);
}
}
else if (10 == pattern || 11 == pattern)
{
constexpr uint8 hiVal = 64;
for (uint8 i0 = 0; i0 < (11 == pattern ? 2 : 1); ++i0) // once by rows, once by columns
{
for (uint8 i2 = 0; i2 < 3; ++i2) // iterate over each of the 3 color elements
{
uint8 r1 = 0, g1 = 0, b1 = 0;
switch (i2)
{
case 0:
r1 = hiVal;
break;
case 1:
g1 = hiVal;
break;
case 2:
b1 = hiVal;
break;
}
for (uint8 i1 = 0; i1 < kMatrixRows; ++i1)
{
bool changed = false;
for (uint8 i3 = 0; i3 < kMatrixCols; ++i3)
{
const uint8 pixelId = (i0 == 0) ? kLedMatrix[i1][i3] : kLedMatrix[i3][i1];
if (kInvalidPixel != pixelId && pixelId < strip.PixelCount())
{
strip.SetPixelColor(pixelId, RgbColor{r1, g1, b1});
changed = true;
}
}
if (changed)
{
strip.Show();
delay(500);
strip.ClearTo(kOffColor);
}
}
}
}
}
else if (12 == pattern)
{
// display the preset slots (limited to lesser of either 32 or actual LED pixels
// as determined by pixel ID of other than kInvalidPixel)
uint8 slot = 0;
for (uint8 i1 = 0; i1 < kMatrixRows; ++i1)
{
for (uint8 i2 = 0; i2 < kMatrixCols && slot < kPresetCount; ++i2)
{
const uint8 pixelId = kLedMatrix[i1][i2];
if (kInvalidPixel != pixelId)
strip.SetPixelColor(pixelId, gColorPresets[slot++]);
}
}
strip.Show();
// leave displayed for user to clear
return;
}
strip.Show();
delay(250);
}