-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpongwall_controller.ino
More file actions
122 lines (100 loc) · 2.3 KB
/
pongwall_controller.ino
File metadata and controls
122 lines (100 loc) · 2.3 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
/* Pongwall Controller
Globals:
CONTROL_BYTE: Control byte as per PongWall Protocol Specification
LITERAL_CONTROL_BYTE_MODIFIER: Byte to send a literal control byte in the data
portion of the packet as per PongWall Protocol Specification
START_MARKER: Control modifier for starting a packet as per PongWall Protocol
Specification
END_MARKER: Control modifier for ending a packet as per PongWall Protocol
Specification
*/
// FastLED stuff
#include <FastLED.h>
#define DATA_PIN 2
#define NUM_LEDS 100
CRGB leds[NUM_LEDS];
#define BYTES_PER_LED 3
// PongWall Serial Protocol stuff
#define CONTROL_BYTE 0x10
// Control Modifiers
#define LITERAL_CONTROL_BYTE_MODIFIER CONTROL_BYTE
#define START_MARKER 0x01
#define END_MARKER 0x02
void addByte(boolean newPacket, byte rb) {
static enum colors {
red,
green,
blue,
numColors,
} color = red;
static int ledNdx = 0;
if (newPacket == true) {
ledNdx = 0;
color = red;
}
else {
leds[ledNdx][color] = rb;
color = (color + 1) % numColors;
// if color is now red, it is time for a new LED
if (color == red) {
ledNdx++;
}
}
}
void displayFrame() {
FastLED.show();
Serial.println("ACK");
}
void processByte(byte rb) {
static enum packet_states {
CONTROL,
CONTROL_MODIFIER,
DATA,
} packet_state;
switch (packet_state) {
// waiting for a control byte
case CONTROL:
packet_state = (rb == CONTROL_BYTE) ? CONTROL_MODIFIER : CONTROL_BYTE;
break;
// take action according to control modifier
case CONTROL_MODIFIER:
switch (rb) {
case LITERAL_CONTROL_BYTE_MODIFIER:
addByte(false, rb);
packet_state = DATA;
break;
case START_MARKER:
packet_state = DATA;
addByte(true, 0x00);
break;
case END_MARKER:
displayFrame();
packet_state = CONTROL;
break;
}
break;
case DATA:
if (rb == CONTROL_BYTE) {
packet_state = CONTROL_MODIFIER;
}
else {
addByte(false, rb);
}
break;
}
}
void setup() {
Serial.begin(9600);
// initialize FastLED
FastLED.addLeds<WS2811, DATA_PIN>(leds, NUM_LEDS);
// blank out display
fill_solid(leds, 100, CRGB::Black);
FastLED.show();
}
void loop() {
byte rb; // read byte
while (Serial.available()) {
rb = Serial.read();
processByte(rb);
}
}