forked from Zidit/partyhat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
313 lines (229 loc) · 6.73 KB
/
main.cpp
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
/*
*/
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include "vectors.h"
#include "animation.h"
#include "serial.h"
#include "taskmanager.h"
#include "utils.h"
#include "config.h"
#include "gamma.h"
#include "battery.h"
#include "implementation.h"
#include "arduinowrapper.h"
#include "XBee.h"
#include "button.h"
#include "pgm_animations.h"
#include "animations.h"
#include "animation_tasks.h"
button b_change_animation(&PORTA, PIN2);
uart xbeeSerial(&USARTD0, 57600);
ISR (USARTD0_RXC_vect){ xbeeSerial.rxInterrupt(); }
ISR (USARTD0_DRE_vect){ xbeeSerial.txInterrupt(); }
uart debug(&USARTC1, 57600);
ISR (USARTC1_RXC_vect){ debug.rxInterrupt(); }
ISR (USARTC1_DRE_vect){ debug.txInterrupt(); }
uint16_t animationBuffer1[256];
uint16_t animationBuffer2[256];
uint16_t* currentAnimation = animationBuffer1;
uint16_t* nextAnimation = animationBuffer2;
vec vectors[NUMBER_OF_VECTORS];
void test_debug();
void gammaCorrection(led& currentLed);
void brightness(led& currentLed, uint8_t value);
void xbee_api_callback(ZBRxResponse &rx);
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
bool helsinkiMode = false;
int main(void)
{
srand(12);
// Set sys clock to 16 Mhz
// -if you change this, remember to change F_CUP define too
set32MHzClock(CLK_PSADIV_2_gc);
//Enable all interrupts
PMIC.CTRL = PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm;
sei();
userImplementationSetup();
batteryManager::initialize();
batteryManager::highCurrentCharging(false);
taskManager::initialize();
taskManager::registerTask(batteryManager::readLevel, 100, 0);
//taskManager::registerTask(&test_debug, 100, 0);
debug.sendStringPgm(PSTR("\n\n\nPartyhat version 0.1 \n"));
Stream xbeeStream(xbeeSerial);
xbee.begin(xbeeStream);
PORTD.DIRSET = PIN4_bm;
PORTD.OUTCLR = PIN4_bm;
_delay_ms(20);
PORTD.OUTSET = PIN4_bm;
loadPgmAnimation(currentAnimation, anim_spectrum);
uint32_t nextFrame = taskManager::getTimeMs() ;
load_nth_animation(1);
anim_runner.start_animation();
while(1){
xbee.readPacket();
if (xbee.getResponse().isAvailable())
{
// got something
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
{
// got a zb rx packet
xbee.getResponse().getZBRxResponse(rx);
xbee_api_callback(rx);
}
}
if( taskManager::getTimeMs() >= nextFrame)
{
uint32_t time = taskManager::getTimeMs();
if(!helsinkiMode)
{
nextFrame = time + 10 * runAnimationCode(currentAnimation);
}
else {
anim_runner.run(0);
nextFrame = anim_runner.getNextRunTime();
}
for (int ledIndex = 0; ledIndex < number_of_leds; ledIndex++)
{
updateLed(leds[ledIndex], vectors, NUMBER_OF_VECTORS);
brightness(leds[ledIndex], 255);
gammaCorrection(leds[ledIndex]);
}
userImplementationUpdate();
}
if(b_change_animation.isToggled() && b_change_animation.isDown())
{
helsinkiMode = false;
loadNextAnimation(nextAnimation);
//swap buffers
uint16_t* ptr16;
ptr16 = currentAnimation;
currentAnimation = nextAnimation;
nextAnimation = ptr16;
resetPC();
}
/// main loop:
/// -get data from xbee
/// -prosess data DONE
/// -update vectors in animation handler DONE
/// -get rgb values from vectors DONE
/// -gamma correction + ect. DONE
/// -set rgb values to all leds REFERENCE IMPLEMENTATION DONE
/// Backgound tasks:
/// -batterymonitor DONE
}
return 0;
}
void test_debug()
{
debug.sendInt(batteryManager::getBatteryLevel());
debug.sendString(" mV\n\r");
}
void gammaCorrection(led& currentLed)
{
currentLed.r = gamma256to256(currentLed.r);
currentLed.g = gamma256to256(currentLed.g);
currentLed.b = gamma256to256(currentLed.b);
}
void brightness(led& currentLed, uint8_t value)
{
currentLed.r = ((uint16_t)currentLed.r * (uint16_t)value) >> 8;
currentLed.g = ((uint16_t)currentLed.g * (uint16_t)value) >> 8;
currentLed.b = ((uint16_t)currentLed.b * (uint16_t)value) >> 8;
}
void xbeeEcho(ZBRxResponse &rx)
{
XBeeAddress64 address = rx.getRemoteAddress64();
// Create a TX Request
ZBTxRequest zbTx = ZBTxRequest(address, rx.getData(), rx.getDataLength());
// Send your request
xbee.send(zbTx);
}
void xbee_api_callback(ZBRxResponse &rx)
{
uint16_t* ptr16;
uint8_t* ptr8;
uint16_t k,i;
uint8_t offset;
// Check first byte
switch(rx.getData(0))
{
// TODO: Define a sane baseline protocol, for testing we have this dummy RGB setter
case 0x00:
debug.sendHex(rx.getData(1));
debug.sendHex(rx.getData(2));
debug.sendHex(rx.getData(3));
break;
case 0x2:
{
load_nth_animation(rx.getData(1));
break;
}
case 0x3:
{
helsinkiMode = true;
anim_runner.start_animation();
break;
}
case 0x4:
{
anim_runner.stop_animation();
break;
}
case 0x5:
{
helsinkiMode = true;
load_nth_animation(rx.getData(1));
anim_runner.start_animation();
break;
}
case 0x1:
break;
case 0x58: // Ascii X
// Your extended protocol goes here
break;
case 0x70:
xbeeEcho(rx);
break;
case 0x80:
helsinkiMode = false;
//swap buffers
ptr16 = currentAnimation;
currentAnimation = nextAnimation;
nextAnimation = ptr16;
resetPC();
break;
case 0x81:
offset = rx.getData(1) * 32;
ptr8 = rx.getData() + 2;
i = 0;
k = 0;
while(i < rx.getDataLength() - 2)
{
uint16_t val = ptr8[i++];
val <<= 8;
val += (uint16_t)ptr8[i++] & 0x00FF;
nextAnimation[k + offset] = val;
k++;
}
break;
case 0x82:
for(i = 0; i < 256; i++)
{
debug.sendHex(currentAnimation[i]);
debug.sendChar(' ');
}
break;
case 0x83:
for(i = 0; i < 256; i++)
{
nextAnimation[i] = 0;
}
break;
}
}