|
@@ -24,37 +24,193 @@
|
24
|
24
|
#include <stdint.h>
|
25
|
25
|
#include <avr/io.h>
|
26
|
26
|
#include <util/delay.h>
|
|
27
|
+#include <avr/interrupt.h>
|
27
|
28
|
#include "uart.h"
|
28
|
29
|
#include "cube.h"
|
|
30
|
+#include "time.h"
|
|
31
|
+#include "audio.h"
|
29
|
32
|
|
30
|
33
|
#ifndef F_CPU
|
31
|
34
|
#define F_CPU 16000000L
|
32
|
35
|
#endif
|
33
|
36
|
|
|
37
|
+uint8_t audioModeSelected(void);
|
|
38
|
+inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
|
|
39
|
+inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
|
|
40
|
+void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf);
|
|
41
|
+void visualizeAudioData(uint8_t *audioData, uint8_t **imageData);
|
|
42
|
+
|
34
|
43
|
int main(void) {
|
35
|
|
- uint8_t i = 1, j = 1;
|
|
44
|
+ char c;
|
|
45
|
+ uint8_t *audioData;
|
|
46
|
+ uint8_t **imageData;
|
|
47
|
+ uint8_t i, j;
|
|
48
|
+ uint64_t lastTimeChecked;
|
|
49
|
+ uint8_t audioMode;
|
36
|
50
|
|
37
|
51
|
DDRD = 0xFF; // Mosfets as Output
|
38
|
|
- DDRB = 0xFF;
|
|
52
|
+ DDRB = 0xFE;
|
39
|
53
|
DDRC = 0xFF; // Latch Enable
|
40
|
54
|
DDRA = 0xFF; // Latch Data
|
41
|
55
|
|
|
56
|
+ imageData = (uint8_t **)malloc(8 * sizeof(uint8_t *));
|
|
57
|
+ for (i = 0; i < 8; i++) {
|
|
58
|
+ imageData[i] = (uint8_t *)malloc(8 * sizeof(uint8_t));
|
|
59
|
+ }
|
|
60
|
+
|
42
|
61
|
init(); // Initialize Cube Low-Level Code
|
43
|
62
|
uart_init(UART_BAUD_SELECT(19200, 16000000L)); // Initialize Serial
|
|
63
|
+ initSystemTimer();
|
|
64
|
+
|
|
65
|
+ sei(); // Enable Interrupts
|
|
66
|
+
|
|
67
|
+ audioMode = audioModeSelected();
|
|
68
|
+ lastTimeChecked = getSystemTime();
|
44
|
69
|
|
45
|
|
- //setFet(0x01);
|
46
|
|
- // Blink led :)
|
47
|
70
|
while (1) {
|
48
|
|
- fillBuffer(0x00);
|
49
|
|
- _delay_ms(500);
|
50
|
|
- fillBuffer(0xFF);
|
51
|
|
- _delay_ms(500);
|
52
|
|
- fillBuffer(0x00);
|
53
|
|
- _delay_ms(500);
|
54
|
|
- fillBuffer(0xFF);
|
55
|
|
- _delay_ms(500);
|
|
71
|
+ if(audioMode) {
|
|
72
|
+ // Get Audio Data and visualize it
|
|
73
|
+ audioData = getAudioData();
|
|
74
|
+ visualizeAudioData(audioData, imageData);
|
|
75
|
+ setImage(imageData);
|
|
76
|
+ free(audioData);
|
|
77
|
+ while(!isFinished()); // Wait for it to display
|
|
78
|
+ } else {
|
|
79
|
+ // Look for commands, play from fram
|
|
80
|
+
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ if ((getSystemTime() - lastTimeChecked) > 1000) {
|
|
84
|
+ // 1 second since we checked button position last time
|
|
85
|
+ audioMode = audioModeSelected();
|
|
86
|
+ lastTimeChecked = getSystemTime();
|
|
87
|
+ }
|
56
|
88
|
}
|
57
|
89
|
|
58
|
90
|
close();
|
59
|
91
|
return 0;
|
60
|
92
|
}
|
|
93
|
+
|
|
94
|
+// Blocks 10ms or more
|
|
95
|
+uint8_t audioModeSelected(void) {
|
|
96
|
+ // Switch: PB0, Low active
|
|
97
|
+ uint64_t startTime = getSystemTime();
|
|
98
|
+ uint8_t startState = PINB & (1 << PB0);
|
|
99
|
+
|
|
100
|
+ while((getSystemTime() - startTime) < 10); // Wait 10ms
|
|
101
|
+
|
|
102
|
+ if ((PINB & (1 << PB0)) != startState) {
|
|
103
|
+ return audioModeSelected();
|
|
104
|
+ } else {
|
|
105
|
+ return startState;
|
|
106
|
+ }
|
|
107
|
+}
|
|
108
|
+
|
|
109
|
+inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
|
|
110
|
+ buf[z][y] |= (1 << x);
|
|
111
|
+}
|
|
112
|
+
|
|
113
|
+inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
|
|
114
|
+ buf[z][y] &= ~(1 << x);
|
|
115
|
+}
|
|
116
|
+
|
|
117
|
+void setBuffer(uint8_t d, uint8_t *buf, uint8_t length) {
|
|
118
|
+ uint8_t i;
|
|
119
|
+ for (i = 0; i < length; i++) {
|
|
120
|
+ buf[i] = d;
|
|
121
|
+ }
|
|
122
|
+}
|
|
123
|
+
|
|
124
|
+void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf) {
|
|
125
|
+ uint8_t i = 0;
|
|
126
|
+ for (; i < height; i++) {
|
|
127
|
+ setPixelBuffer(x, i, z, buf);
|
|
128
|
+ }
|
|
129
|
+}
|
|
130
|
+
|
|
131
|
+void visualizeAudioData(uint8_t *audioData, uint8_t **imageData) {
|
|
132
|
+ uint8_t i;
|
|
133
|
+ for (i = 0; i < 8; i++) {
|
|
134
|
+ setBuffer(0, imageData[i], 8);
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ // 8 LEDs, Max Val 255:
|
|
138
|
+ // 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
|
|
139
|
+ // 255 / FACTOR = 8,...
|
|
140
|
+ // 127 / FACTOR = 4,...
|
|
141
|
+
|
|
142
|
+ #define FACTOR 31
|
|
143
|
+
|
|
144
|
+ // Could not figure out a way to represent this easily in a loop
|
|
145
|
+ // without using a shitload of 'if's...
|
|
146
|
+ setRow(0, 0, (audioData[0] / FACTOR), imageData);
|
|
147
|
+ setRow(0, 1, (audioData[0] / FACTOR), imageData);
|
|
148
|
+ setRow(1, 0, (audioData[0] / FACTOR), imageData);
|
|
149
|
+
|
|
150
|
+ setRow(0, 2, (audioData[1] / FACTOR), imageData);
|
|
151
|
+ setRow(0, 3, (audioData[1] / FACTOR), imageData);
|
|
152
|
+ setRow(1, 1, (audioData[1] / FACTOR), imageData);
|
|
153
|
+ setRow(1, 2, (audioData[1] / FACTOR), imageData);
|
|
154
|
+ setRow(2, 0, (audioData[1] / FACTOR), imageData);
|
|
155
|
+ setRow(2, 1, (audioData[1] / FACTOR), imageData);
|
|
156
|
+
|
|
157
|
+ setRow(0, 4, (audioData[2] / FACTOR), imageData);
|
|
158
|
+ setRow(0, 5, (audioData[2] / FACTOR), imageData);
|
|
159
|
+ setRow(1, 3, (audioData[2] / FACTOR), imageData);
|
|
160
|
+ setRow(1, 4, (audioData[2] / FACTOR), imageData);
|
|
161
|
+ setRow(2, 2, (audioData[2] / FACTOR), imageData);
|
|
162
|
+ setRow(2, 3, (audioData[2] / FACTOR), imageData);
|
|
163
|
+ setRow(3, 0, (audioData[2] / FACTOR), imageData);
|
|
164
|
+ setRow(3, 1, (audioData[2] / FACTOR), imageData);
|
|
165
|
+ setRow(3, 2, (audioData[2] / FACTOR), imageData);
|
|
166
|
+ setRow(4, 0, (audioData[2] / FACTOR), imageData);
|
|
167
|
+ setRow(4, 1, (audioData[2] / FACTOR), imageData);
|
|
168
|
+
|
|
169
|
+ setRow(0, 6, (audioData[3] / FACTOR), imageData);
|
|
170
|
+ setRow(0, 7, (audioData[3] / FACTOR), imageData);
|
|
171
|
+ setRow(1, 5, (audioData[3] / FACTOR), imageData);
|
|
172
|
+ setRow(1, 6, (audioData[3] / FACTOR), imageData);
|
|
173
|
+ setRow(2, 4, (audioData[3] / FACTOR), imageData);
|
|
174
|
+ setRow(2, 5, (audioData[3] / FACTOR), imageData);
|
|
175
|
+ setRow(3, 3, (audioData[3] / FACTOR), imageData);
|
|
176
|
+ setRow(3, 4, (audioData[3] / FACTOR), imageData);
|
|
177
|
+ setRow(4, 2, (audioData[3] / FACTOR), imageData);
|
|
178
|
+ setRow(4, 3, (audioData[3] / FACTOR), imageData);
|
|
179
|
+ setRow(5, 0, (audioData[3] / FACTOR), imageData);
|
|
180
|
+ setRow(5, 1, (audioData[3] / FACTOR), imageData);
|
|
181
|
+ setRow(5, 2, (audioData[3] / FACTOR), imageData);
|
|
182
|
+ setRow(6, 0, (audioData[3] / FACTOR), imageData);
|
|
183
|
+ setRow(6, 1, (audioData[3] / FACTOR), imageData);
|
|
184
|
+
|
|
185
|
+ setRow(1, 7, (audioData[4] / FACTOR), imageData);
|
|
186
|
+ setRow(2, 6, (audioData[4] / FACTOR), imageData);
|
|
187
|
+ setRow(2, 7, (audioData[4] / FACTOR), imageData);
|
|
188
|
+ setRow(3, 5, (audioData[4] / FACTOR), imageData);
|
|
189
|
+ setRow(3, 6, (audioData[4] / FACTOR), imageData);
|
|
190
|
+ setRow(4, 4, (audioData[4] / FACTOR), imageData);
|
|
191
|
+ setRow(4, 5, (audioData[4] / FACTOR), imageData);
|
|
192
|
+ setRow(5, 3, (audioData[4] / FACTOR), imageData);
|
|
193
|
+ setRow(5, 4, (audioData[4] / FACTOR), imageData);
|
|
194
|
+ setRow(6, 2, (audioData[4] / FACTOR), imageData);
|
|
195
|
+ setRow(6, 3, (audioData[4] / FACTOR), imageData);
|
|
196
|
+ setRow(7, 0, (audioData[4] / FACTOR), imageData);
|
|
197
|
+ setRow(7, 1, (audioData[4] / FACTOR), imageData);
|
|
198
|
+
|
|
199
|
+ setRow(3, 7, (audioData[5] / FACTOR), imageData);
|
|
200
|
+ setRow(4, 6, (audioData[5] / FACTOR), imageData);
|
|
201
|
+ setRow(4, 7, (audioData[5] / FACTOR), imageData);
|
|
202
|
+ setRow(5, 5, (audioData[5] / FACTOR), imageData);
|
|
203
|
+ setRow(5, 6, (audioData[5] / FACTOR), imageData);
|
|
204
|
+ setRow(6, 4, (audioData[5] / FACTOR), imageData);
|
|
205
|
+ setRow(6, 5, (audioData[5] / FACTOR), imageData);
|
|
206
|
+ setRow(7, 2, (audioData[5] / FACTOR), imageData);
|
|
207
|
+ setRow(7, 3, (audioData[5] / FACTOR), imageData);
|
|
208
|
+ setRow(7, 4, (audioData[5] / FACTOR), imageData);
|
|
209
|
+
|
|
210
|
+ setRow(5, 7, (audioData[6] / FACTOR), imageData);
|
|
211
|
+ setRow(6, 6, (audioData[6] / FACTOR), imageData);
|
|
212
|
+ setRow(6, 7, (audioData[6] / FACTOR), imageData);
|
|
213
|
+ setRow(7, 5, (audioData[6] / FACTOR), imageData);
|
|
214
|
+ setRow(7, 6, (audioData[6] / FACTOR), imageData);
|
|
215
|
+ setRow(7, 7, (audioData[6] / FACTOR), imageData);
|
|
216
|
+}
|