|
@@ -44,8 +44,10 @@
|
44
|
44
|
#include "time.h"
|
45
|
45
|
#include "audio.h"
|
46
|
46
|
#include "memLayer.h"
|
|
47
|
+#include "twi.h"
|
47
|
48
|
|
48
|
49
|
void serialHandler(char c);
|
|
50
|
+void sendAudioData(void);
|
49
|
51
|
void recieveAnimations(void);
|
50
|
52
|
void transmitAnimations(void);
|
51
|
53
|
uint8_t audioModeSelected(void);
|
|
@@ -61,10 +63,15 @@ char buffer[11];
|
61
|
63
|
int main(void) {
|
62
|
64
|
uint8_t *audioData;
|
63
|
65
|
uint8_t **imageData;
|
64
|
|
- uint8_t i;
|
65
|
|
- uint64_t lastTimeChecked;
|
66
|
|
- uint8_t audioMode;
|
|
66
|
+ uint8_t i, lastMode;
|
67
|
67
|
uint16_t count;
|
|
68
|
+ uint64_t lastChecked;
|
|
69
|
+
|
|
70
|
+ initCube();
|
|
71
|
+ serialInit(51, 8, NONE, 1);
|
|
72
|
+ i2c_init();
|
|
73
|
+ initSystemTimer();
|
|
74
|
+ sei(); // Enable Interrupts
|
68
|
75
|
|
69
|
76
|
DDRD = 0xFC; // Mosfets as Output
|
70
|
77
|
DDRB = 0xFE;
|
|
@@ -76,17 +83,7 @@ int main(void) {
|
76
|
83
|
imageData[i] = (uint8_t *)malloc(8 * sizeof(uint8_t));
|
77
|
84
|
}
|
78
|
85
|
|
79
|
|
- init(); // Initialize Cube Low-Level Code
|
80
|
|
- serialInit(51, 8, NONE, 1);
|
81
|
|
- initSystemTimer();
|
82
|
|
-
|
83
|
|
- sei(); // Enable Interrupts
|
84
|
|
-
|
85
|
|
- lastTimeChecked = getSystemTime();
|
86
|
|
- audioMode = audioModeSelected();
|
87
|
|
-
|
88
|
86
|
#ifdef DEBUG
|
89
|
|
-#warning NOT TALKING TO FRAM YET!
|
90
|
87
|
refreshAnimationCount = 0; // Don't talk to FRAM yet...
|
91
|
88
|
|
92
|
89
|
serialWriteString("Initialized: ");
|
|
@@ -96,14 +93,19 @@ int main(void) {
|
96
|
93
|
serialWriteString(" ms!\n");
|
97
|
94
|
#endif
|
98
|
95
|
|
|
96
|
+ lastMode = audioModeSelected();
|
|
97
|
+ lastChecked = getSystemTime();
|
|
98
|
+
|
99
|
99
|
while (1) {
|
100
|
|
- if(audioMode) {
|
|
100
|
+ if(lastMode) {
|
101
|
101
|
// Get Audio Data and visualize it
|
102
|
|
- audioData = getAudioData();
|
103
|
|
- visualizeAudioData(audioData, imageData);
|
104
|
|
- setImage(imageData);
|
105
|
|
- free(audioData);
|
106
|
|
- while(!isFinished()); // Wait for it to display
|
|
102
|
+ /* audioData = getAudioData();
|
|
103
|
+ if (audioData != NULL) {
|
|
104
|
+ visualizeAudioData(audioData, imageData);
|
|
105
|
+ setImage(imageData);
|
|
106
|
+ free(audioData);
|
|
107
|
+ }
|
|
108
|
+ while(!isFinished()); // Wait for it to display */
|
107
|
109
|
} else {
|
108
|
110
|
// Look for commands, play from fram
|
109
|
111
|
// We have 128*1024 bytes
|
|
@@ -115,18 +117,17 @@ int main(void) {
|
115
|
117
|
serialHandler((char)(serialGet()));
|
116
|
118
|
}
|
117
|
119
|
|
118
|
|
- if (refreshAnimationCount) {
|
|
120
|
+ /* if (refreshAnimationCount) {
|
119
|
121
|
// Get animation count stored in FRAM via TWI, if needed
|
120
|
122
|
count = getAnimationCount();
|
121
|
123
|
refreshAnimationCount = 0;
|
122
|
|
- }
|
|
124
|
+ } */
|
123
|
125
|
}
|
124
|
126
|
|
125
|
|
- if ((getSystemTime() - lastTimeChecked) > 1000) {
|
126
|
|
- // 1 second since we checked button position last time
|
127
|
|
- audioMode = audioModeSelected();
|
128
|
|
- lastTimeChecked = getSystemTime();
|
129
|
|
- }
|
|
127
|
+ if ((getSystemTime() - lastChecked) > 100) {
|
|
128
|
+ lastMode = audioModeSelected();
|
|
129
|
+ lastChecked = getSystemTime();
|
|
130
|
+ }
|
130
|
131
|
}
|
131
|
132
|
|
132
|
133
|
close();
|
|
@@ -134,44 +135,90 @@ int main(void) {
|
134
|
135
|
}
|
135
|
136
|
|
136
|
137
|
void serialHandler(char c) {
|
137
|
|
- // Got a char on serial line...
|
138
|
|
- // React accordingly
|
139
|
|
- // Set refreshAnimationCount if Animation Data in Ram was changed...
|
140
|
|
- // We do a complete transaction in one call of this routine...
|
141
|
|
-
|
|
138
|
+ // Used letters:
|
|
139
|
+ // a, c, d, g, s, t, v
|
142
|
140
|
switch(c) {
|
143
|
|
- case OK:
|
144
|
|
- // Send "Hello World"
|
145
|
|
- serialWrite(OK);
|
146
|
|
- break;
|
|
141
|
+ case OK:
|
|
142
|
+ serialWrite(OK);
|
|
143
|
+ break;
|
147
|
144
|
|
148
|
|
- case 'd': case 'D':
|
149
|
|
- clearMem();
|
150
|
|
- serialWrite(OK);
|
151
|
|
- break;
|
|
145
|
+ case 'd': case 'D':
|
|
146
|
+ clearMem();
|
|
147
|
+ serialWrite(OK);
|
|
148
|
+ break;
|
|
149
|
+
|
|
150
|
+ case 'g': case 'G':
|
|
151
|
+ transmitAnimations();
|
|
152
|
+ break;
|
|
153
|
+
|
|
154
|
+ case 's': case 'S':
|
|
155
|
+ recieveAnimations();
|
|
156
|
+ break;
|
|
157
|
+
|
|
158
|
+ case 'v': case 'V':
|
|
159
|
+ serialWriteString(VERSION);
|
|
160
|
+ break;
|
|
161
|
+
|
|
162
|
+ case 't': case 'T':
|
|
163
|
+ serialWriteString("System Time: ");
|
|
164
|
+ serialWriteString(ltoa(getSystemTime(), buffer, 10));
|
|
165
|
+ serialWriteString("ms");
|
|
166
|
+ if (getSystemTime() > 1000) {
|
|
167
|
+ serialWriteString(" (");
|
|
168
|
+ serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
|
|
169
|
+ itoa(getSystemTime() % 1000, buffer, 10);
|
|
170
|
+ if (buffer[0] != '\0')
|
|
171
|
+ serialWrite('.');
|
|
172
|
+ if (buffer[2] == '\0')
|
|
173
|
+ serialWrite('0');
|
|
174
|
+ if (buffer[1] == '\0')
|
|
175
|
+ serialWrite('0');
|
|
176
|
+ if (buffer[0] != '\0')
|
|
177
|
+ serialWriteString(buffer);
|
|
178
|
+ serialWriteString("s)\n");
|
|
179
|
+ } else {
|
|
180
|
+ serialWrite('\n');
|
|
181
|
+ }
|
|
182
|
+ break;
|
152
|
183
|
|
153
|
|
- case 'g': case 'G':
|
154
|
|
- transmitAnimations();
|
155
|
|
- break;
|
|
184
|
+ case 'a': case 'A':
|
|
185
|
+ sendAudioData();
|
|
186
|
+ break;
|
156
|
187
|
|
157
|
|
- case 's': case 'S':
|
158
|
|
- recieveAnimations();
|
159
|
|
- break;
|
|
188
|
+ case 'c': case 'C':
|
|
189
|
+ serialWriteString(itoa(getAnimationCount(), buffer, 10));
|
|
190
|
+ serialWriteString(" Frames stored\n");
|
|
191
|
+ break;
|
160
|
192
|
|
161
|
|
- case 'v': case 'V':
|
162
|
|
- serialWriteString(VERSION);
|
163
|
|
- break;
|
|
193
|
+ case '\n':
|
|
194
|
+ serialWriteString(VERSION);
|
|
195
|
+ serialWriteString("See xythobuz.org for more Infos!\n");
|
|
196
|
+ break;
|
164
|
197
|
|
165
|
|
- case 't': case 'T':
|
166
|
|
- serialWriteString("System Time: ");
|
167
|
|
- serialWriteString(itoa(getSystemTime(), buffer, 10));
|
168
|
|
- serialWrite('\n');
|
169
|
|
- break;
|
|
198
|
+ default:
|
|
199
|
+ serialWrite(ERROR);
|
|
200
|
+ break;
|
|
201
|
+ }
|
|
202
|
+}
|
170
|
203
|
|
171
|
|
- default:
|
172
|
|
- serialWrite(ERROR);
|
173
|
|
- break;
|
|
204
|
+void sendAudioData(void) {
|
|
205
|
+ uint8_t i;
|
|
206
|
+ uint8_t *audioData = getAudioData();
|
|
207
|
+ if (audioData == NULL) {
|
|
208
|
+ serialWriteString("Could not access device!\n");
|
|
209
|
+ return;
|
174
|
210
|
}
|
|
211
|
+
|
|
212
|
+ serialWriteString("Audio Data:\n");
|
|
213
|
+ for (i = 0; i < 7; i++) {
|
|
214
|
+ serialWrite(i + '0');
|
|
215
|
+ serialWriteString(": ");
|
|
216
|
+ itoa(audioData[i], buffer, 10);
|
|
217
|
+ serialWriteString(buffer);
|
|
218
|
+ serialWrite('\n');
|
|
219
|
+ }
|
|
220
|
+
|
|
221
|
+ free(audioData);
|
175
|
222
|
}
|
176
|
223
|
|
177
|
224
|
void recieveAnimations() {
|
|
@@ -242,10 +289,6 @@ void recieveAnimations() {
|
242
|
289
|
refreshAnimationCount = 1;
|
243
|
290
|
}
|
244
|
291
|
|
245
|
|
-#define CAF(x) (x % 256)
|
246
|
|
-#define CAS(x) (x / 256)
|
247
|
|
-#define CALCANIMATIONS(x) ((CAF(x) == 0) ? (CAS(x)) : (CAS(x) + 1))
|
248
|
|
-
|
249
|
292
|
void transmitAnimations() {
|
250
|
293
|
// We store no animation information in here
|
251
|
294
|
// So we have to place all frames in one or more
|
|
@@ -316,42 +359,25 @@ void transmitAnimations() {
|
316
|
359
|
// Error code ignored...
|
317
|
360
|
}
|
318
|
361
|
|
319
|
|
-// Blocks 10ms or more
|
320
|
362
|
uint8_t audioModeSelected(void) {
|
321
|
363
|
// Pushbutton: PB0, Low active
|
322
|
|
- uint8_t startState = PINB & (1 << PB0);
|
323
|
|
-
|
324
|
|
- _delay_ms(10);
|
325
|
364
|
|
326
|
|
- if ((PINB & (1 << PB0)) != startState) {
|
327
|
|
- // State changed
|
328
|
|
- // We can assume we have to toggle the state
|
329
|
|
-#ifdef DEBUG
|
330
|
|
- serialWriteString("New State!");
|
331
|
|
-#endif
|
|
365
|
+ if (!(PINB & (1 << PB0))) {
|
|
366
|
+ // Button pushed
|
332
|
367
|
if (lastButtonState == 0) {
|
333
|
368
|
lastButtonState = 1;
|
334
|
369
|
} else {
|
335
|
370
|
lastButtonState = 0;
|
336
|
371
|
}
|
337
|
|
- return lastButtonState;
|
338
|
|
- } else {
|
339
|
|
- if (!startState) {
|
340
|
|
- // Stayed the same, is pushed!
|
|
372
|
+
|
341
|
373
|
#ifdef DEBUG
|
342
|
|
- serialWriteString("New State!");
|
|
374
|
+ serialWriteString("New State (");
|
|
375
|
+ serialWriteString(itoa(lastButtonState, buffer, 10));
|
|
376
|
+ serialWriteString(")\n");
|
343
|
377
|
#endif
|
344
|
|
- if (lastButtonState == 0) {
|
345
|
|
- lastButtonState = 1;
|
346
|
|
- } else {
|
347
|
|
- lastButtonState = 0;
|
348
|
|
- }
|
349
|
|
- return lastButtonState;
|
350
|
|
- } else {
|
351
|
|
- // Stayed but is not pushed...
|
352
|
|
- return lastButtonState;
|
353
|
|
- }
|
|
378
|
+
|
354
|
379
|
}
|
|
380
|
+ return lastButtonState;
|
355
|
381
|
}
|
356
|
382
|
|
357
|
383
|
inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
|