Browse Source

Cube FW Audio Interface done

Thomas Buck 12 years ago
parent
commit
3673a620ce
5 changed files with 249 additions and 15 deletions
  1. 1
    3
      CubeFirmware/cube.c
  2. 168
    12
      CubeFirmware/main.c
  3. 1
    0
      CubeFirmware/makefile
  4. 55
    0
      CubeFirmware/time.c
  5. 24
    0
      CubeFirmware/time.h

+ 1
- 3
CubeFirmware/cube.c View File

@@ -91,8 +91,6 @@ void init(void) {
91 91
 		// Same reasoning here...
92 92
 		imgBuffer[ctr] = malloc(8 * sizeof(uint8_t));
93 93
 	}
94
-
95
-	sei(); // Enable Interrupts
96 94
 }
97 95
 
98 96
 void close(void) {
@@ -148,7 +146,7 @@ inline void isrCall(void) {
148 146
 		layer++;
149 147
 	} else {
150 148
 		layer = 0;
151
-		imgFlag = 1; // Finished
149
+		imgFlag++; // Finished
152 150
 	}
153 151
 }
154 152
 

+ 168
- 12
CubeFirmware/main.c View File

@@ -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
+}

+ 1
- 0
CubeFirmware/makefile View File

@@ -41,6 +41,7 @@ SRC += cube.c
41 41
 SRC += twi.c
42 42
 SRC += mem.c
43 43
 SRC += audio.c
44
+SRC += time.c
44 45
 
45 46
 # List Assembler source files here.
46 47
 # Make them always end in a capital .S.  Files ending in a lowercase .s

+ 55
- 0
CubeFirmware/time.c View File

@@ -0,0 +1,55 @@
1
+/*
2
+ * time.c
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
6
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
7
+ *
8
+ * This file is part of LED-Cube.
9
+ *
10
+ * LED-Cube is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * LED-Cube is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+#include <stdlib.h>
24
+#include <stdint.h>
25
+#include <avr/io.h>
26
+#include <avr/interrupt.h>
27
+#include "time.h"
28
+
29
+// Interrupt:
30
+// Prescaler 256
31
+// Count to 208
32
+// Execute every 3 times
33
+// => 1 Interrupt per millisecond
34
+
35
+volatile uint64_t systemTime = 0; // Overflows in 500 million years... :)
36
+volatile uint8_t isrCounter = 0;
37
+
38
+void initSystemTimer() {
39
+	TCCR0 |= (1 << WGM01) | (1 << CS02); // Prescaler: 256, CTC Mode
40
+	OCR0 = 208;
41
+	TIMSK |= (1 << TOIE0); // Enable overflow interrupt
42
+}
43
+
44
+ISR(TIMER0_OVF_vect) {
45
+	if (isrCounter >= 2) {
46
+		isrCounter = 0;
47
+		systemTime++;
48
+	} else {
49
+		isrCounter++;
50
+	}
51
+}
52
+
53
+uint64_t getSystemTime() {
54
+	return systemTime;
55
+}

+ 24
- 0
CubeFirmware/time.h View File

@@ -0,0 +1,24 @@
1
+/*
2
+ * time.c
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
6
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
7
+ *
8
+ * This file is part of LED-Cube.
9
+ *
10
+ * LED-Cube is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * LED-Cube is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+void initSystemTimer(void);
24
+uint64_t getSystemTime(void);

Loading…
Cancel
Save