Thomas Buck 12 anni fa
parent
commit
1bf08451f6
6 ha cambiato i file con 165 aggiunte e 133 eliminazioni
  1. 9
    6
      CubeFirmware/audio.c
  2. 1
    1
      CubeFirmware/cube.c
  3. 1
    1
      CubeFirmware/cube.h
  4. 111
    85
      CubeFirmware/main.c
  5. 35
    25
      CubeFirmware/mem.c
  6. 8
    15
      CubeFirmware/time.c

+ 9
- 6
CubeFirmware/audio.c Vedi File

@@ -33,11 +33,14 @@ uint8_t *getAudioData(void) {
33 33
 	uint8_t i;
34 34
 	uint8_t *ret = (uint8_t *)malloc(7);
35 35
 
36
-	i2c_start(TWIADDRESSAUDIO | I2C_READ);
37
-	for (i = 0; i < 6; i++) {
38
-		ret[i] = i2c_readAck();
36
+	if (i2c_start(TWIADDRESSAUDIO | I2C_READ) == 0) {
37
+		for (i = 0; i < 6; i++) {
38
+			ret[i] = i2c_readAck();
39
+		}
40
+		ret[6] = i2c_readNak();
41
+		i2c_stop();
42
+		return ret;
43
+	} else {
44
+		return NULL;
39 45
 	}
40
-	ret[6] = i2c_readNak();
41
-	i2c_stop();
42
-	return ret;
43 46
 }

+ 1
- 1
CubeFirmware/cube.c Vedi File

@@ -76,7 +76,7 @@ uint8_t isFinished(void) {
76 76
 	return imgFlag;
77 77
 }
78 78
 
79
-void init(void) {
79
+void initCube(void) {
80 80
 	uint8_t ctr = 0;
81 81
 
82 82
 	TCCR1A |= (1 << WGM12); // CTC Mode

+ 1
- 1
CubeFirmware/cube.h Vedi File

@@ -26,7 +26,7 @@
26 26
  * After the last part is applied to the latches, isFinished() will change
27 27
  * to 1. Now you can place a new picture here.
28 28
  */
29
-extern void init(void);
29
+extern void initCube(void);
30 30
 
31 31
 // Copies the data in img into its own buffer, so free img afterwards...
32 32
 extern void setImage(uint8_t **img); // img[8][8]

+ 111
- 85
CubeFirmware/main.c Vedi File

@@ -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) {

+ 35
- 25
CubeFirmware/mem.c Vedi File

@@ -36,13 +36,16 @@ uint8_t memGetByte(uint32_t address) {
36 36
 	addA = memAddress & 0xFF00;
37 37
 	addB = memAddress & 0xFF;
38 38
 
39
-	i2c_start(memAddress | I2C_WRITE); // Send start, write address to read
40
-	i2c_write(addA);
41
-	i2c_write(addB); // Give address to memory
42
-	i2c_rep_start(memAddress | I2C_READ); // Start again, this time reading
43
-	ret = i2c_readNak(); // Read one byte from memory
44
-	i2c_stop();
45
-	return ret;
39
+	if (i2c_start(memAddress | I2C_WRITE) == 0) { // Send start, write address to read
40
+		i2c_write(addA);
41
+		i2c_write(addB); // Give address to memory
42
+		i2c_rep_start(memAddress | I2C_READ); // Start again, this time reading
43
+		ret = i2c_readNak(); // Read one byte from memory
44
+		i2c_stop();
45
+		return ret;
46
+	} else {
47
+		return 0;
48
+	}
46 49
 }
47 50
 
48 51
 // Free returned memory!
@@ -58,16 +61,19 @@ uint8_t *memGetBytes(uint32_t address, uint8_t length) {
58 61
 	addB = memAddress & 0xFF;
59 62
 	ret = (uint8_t *)malloc(length); // Allocate memory for values read
60 63
 
61
-	i2c_start(memAddress | I2C_WRITE);
62
-	i2c_write(addA);
63
-	i2c_write(addB);
64
-	i2c_rep_start(memAddress | I2C_READ);
65
-	for (i = 0; i < (length - 1); i++) {
66
-		ret[i] = i2c_readAck();
64
+	if (i2c_start(memAddress | I2C_WRITE) == 0) {
65
+		i2c_write(addA);
66
+		i2c_write(addB);
67
+		i2c_rep_start(memAddress | I2C_READ);
68
+		for (i = 0; i < (length - 1); i++) {
69
+			ret[i] = i2c_readAck();
70
+		}
71
+		ret[length - 1] = i2c_readNak();
72
+		i2c_stop();
73
+		return ret;
74
+	} else {
75
+		return NULL;
67 76
 	}
68
-	ret[length - 1] = i2c_readNak();
69
-	i2c_stop();
70
-	return ret;
71 77
 }
72 78
 
73 79
 void memWriteByte(uint32_t address, uint8_t data) {
@@ -78,10 +84,12 @@ void memWriteByte(uint32_t address, uint8_t data) {
78 84
 	}
79 85
 	addA = memAddress & 0xFF00;
80 86
 	addB = memAddress & 0xFF;
81
-	i2c_write(addA);
82
-	i2c_write(addB); // Give address to memory
83
-	i2c_write(data);
84
-	i2c_stop();
87
+	if (i2c_start(memAddress | I2C_WRITE) == 0) {
88
+		i2c_write(addA);
89
+		i2c_write(addB); // Give address to memory
90
+		i2c_write(data);
91
+		i2c_stop();
92
+	}
85 93
 }
86 94
 
87 95
 void memWriteBytes(uint32_t address, uint8_t *data, uint8_t length) {
@@ -92,10 +100,12 @@ void memWriteBytes(uint32_t address, uint8_t *data, uint8_t length) {
92 100
 	}
93 101
 	addA = memAddress & 0xFF00;
94 102
 	addB = memAddress & 0xFF;
95
-	i2c_write(addA);
96
-	i2c_write(addB); // Give address to memory
97
-	for (i = 0; i < length; i++) {
98
-		i2c_write(data[i]);
103
+	if (i2c_start(memAddress | I2C_WRITE) == 0) {
104
+		i2c_write(addA);
105
+		i2c_write(addB); // Give address to memory
106
+		for (i = 0; i < length; i++) {
107
+			i2c_write(data[i]);
108
+		}
109
+		i2c_stop();
99 110
 	}
100
-	i2c_stop();
101 111
 }

+ 8
- 15
CubeFirmware/time.c Vedi File

@@ -20,35 +20,28 @@
20 20
  * You should have received a copy of the GNU General Public License
21 21
  * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22 22
  */
23
-// Uses Timer 0!
24 23
 #include <stdlib.h>
25 24
 #include <stdint.h>
26 25
 #include <avr/io.h>
27 26
 #include <avr/interrupt.h>
28 27
 #include "time.h"
29 28
 
29
+// Uses Timer 0!
30 30
 // Interrupt:
31
-// Prescaler 256
32
-// Count to 208
33
-// Execute every 3 times
31
+// Prescaler 64
32
+// Count to 250
34 33
 // => 1 Interrupt per millisecond
35 34
 
36 35
 volatile uint64_t systemTime = 0; // Overflows in 500 million years... :)
37
-volatile uint8_t isrCounter = 0;
38 36
 
39 37
 void initSystemTimer() {
40
-	TCCR0 |= (1 << WGM01) | (1 << CS02); // Prescaler: 256, CTC Mode
41
-	OCR0 = 208;
42
-	TIMSK |= (1 << TOIE0); // Enable overflow interrupt
38
+	TCCR0 |= (1 << WGM01) | (1 << CS01) | (1 << CS00); // Prescaler: 256, CTC Mode
39
+	OCR0 = 250;
40
+	TIMSK |= (1 << OCIE0); // Enable overflow interrupt
43 41
 }
44 42
 
45
-ISR(TIMER0_OVF_vect) {
46
-	if (isrCounter >= 2) {
47
-		isrCounter = 0;
48
-		systemTime++;
49
-	} else {
50
-		isrCounter++;
51
-	}
43
+ISR(TIMER0_COMP_vect) {
44
+	systemTime++;
52 45
 }
53 46
 
54 47
 uint64_t getSystemTime() {

Loading…
Annulla
Salva