Browse Source

Uploading animations with up to 4 frames works.

Larger animations get everything but the last 4 frames corrupted.
Thomas Buck 12 years ago
parent
commit
04d9bbf0d3

+ 9
- 56
CubeFirmware/animations.c View File

42
 // Prototypes for all animations
42
 // Prototypes for all animations
43
 void upWave(uint8_t i);
43
 void upWave(uint8_t i);
44
 void downWave(uint8_t i);
44
 void downWave(uint8_t i);
45
-void xWave1(uint8_t i);
46
-void xWave2(uint8_t i);
47
 void zWave1(uint8_t i);
45
 void zWave1(uint8_t i);
48
 void zWave2(uint8_t i);
46
 void zWave2(uint8_t i);
49
 
47
 
51
 #define NUMOFANIMATIONS 0
49
 #define NUMOFANIMATIONS 0
52
 uint8_t (*animations[NUMOFANIMATIONS])(void) = { };
50
 uint8_t (*animations[NUMOFANIMATIONS])(void) = { };
53
 
51
 
54
-#define WAVELENGTH 2
52
+#define WAVELENGTH 1
55
 
53
 
56
 uint8_t numOfAnimations(void) {
54
 uint8_t numOfAnimations(void) {
57
-	return NUMOFANIMATIONS + 24;
55
+	return NUMOFANIMATIONS + (4*4);
58
 }
56
 }
59
 
57
 
60
 uint8_t executeAnimation(uint8_t id) {
58
 uint8_t executeAnimation(uint8_t id) {
61
-	if (id < (6*4)) {
59
+	if (id < (4*4)) {
62
 		if (id < 4) {
60
 		if (id < 4) {
63
-			upWave(id);
61
+			zWave1(id);
64
 		} else if (id < 8) {
62
 		} else if (id < 8) {
65
-			downWave(id - 4);
63
+			zWave2(id - 4);
66
 		} else if (id < 12) {
64
 		} else if (id < 12) {
67
-			xWave1(id - 8);
65
+			upWave(id - 8);
68
 		} else if (id < 16) {
66
 		} else if (id < 16) {
69
-			xWave2(id - 12);
70
-		} else if (id < 20) {
71
-			zWave1(id - 16);
72
-		} else {
73
-			zWave2(id - 20);
67
+			downWave(id - 12);
74
 		}
68
 		}
75
 		return 1;
69
 		return 1;
76
-	} else if ((id - (6*4)) < NUMOFANIMATIONS) {
77
-		return animations[id - (6*4)](); // Call animation
70
+	} else if ((id - (4*4)) < NUMOFANIMATIONS) {
71
+		return animations[id - (4*4)](); // Call animation
78
 	}
72
 	}
79
 	return 1;
73
 	return 1;
80
 }
74
 }
121
 	free(buff);
115
 	free(buff);
122
 }
116
 }
123
 
117
 
124
-void xWave1(uint8_t i) {
125
-	uint8_t *buff;
126
-	int8_t x, y, z;
127
-
128
-	buff = buffNew();
129
-	// x-axis wave
130
-	for(x = (i * 2); x < ((i * 2) + 2); x++) {
131
-		for(y = 0; y < 8; y++) {
132
-			for(z = 0; z < 8; z++) {
133
-				buffSetPixel(buff, x, y, z);
134
-			}
135
-		}
136
-		setImage(buff);
137
-		while(isFinished() < WAVELENGTH) {
138
-			wdt_reset();
139
-		}
140
-		buffClearAllPixels(buff);
141
-	}
142
-	free(buff);
143
-}
144
-
145
-void xWave2(uint8_t i) {
146
-	uint8_t *buff;
147
-	int8_t x, y, z;
148
-
149
-	buff = buffNew();
150
-	for(x = (7 - (2 * i)); x >= ((7 - (2 * i)) - 1); x--) {
151
-		for(y = 0; y < 8; y++) {
152
-			for(z = 0; z < 8; z++) {
153
-				buffSetPixel(buff, x, y, z);
154
-			}
155
-		}
156
-		setImage(buff);
157
-		while(isFinished() < WAVELENGTH) {
158
-			wdt_reset();
159
-		}
160
-		buffClearAllPixels(buff);
161
-	}
162
-	free(buff);
163
-}
164
-
165
 void zWave1(uint8_t i) {
118
 void zWave1(uint8_t i) {
166
 	uint8_t *buff;
119
 	uint8_t *buff;
167
 	int8_t x, y, z;
120
 	int8_t x, y, z;

+ 67
- 83
CubeFirmware/cube.c View File

25
 #include <avr/interrupt.h>
25
 #include <avr/interrupt.h>
26
 #include <stdlib.h>
26
 #include <stdlib.h>
27
 #include <util/atomic.h>
27
 #include <util/atomic.h>
28
+#include <avr/pgmspace.h>
28
 
29
 
29
 #include "cube.h"
30
 #include "cube.h"
30
 
31
 
55
 volatile uint8_t layer = 0;
56
 volatile uint8_t layer = 0;
56
 volatile uint8_t toggleFlag = 0;
57
 volatile uint8_t toggleFlag = 0;
57
 
58
 
58
-inline void isrCall(void);
59
-
60
 volatile uint32_t timesTriggered = 0;
59
 volatile uint32_t timesTriggered = 0;
61
 volatile uint8_t warningDisplayed = 0;
60
 volatile uint8_t warningDisplayed = 0;
62
 
61
 
63
-uint8_t lookUp[] = { 	0,  64,  128,  192,  16,  80,  144,  208,  32,  96,  160,
64
-					224,  48,  112,  176,  240,  4,  68,  132,  196,  20,
65
-					84,  148,  212,  36,  100,  164,  228,  52,  116,  180,
66
-					244,  8,  72,  136,  200,  24,  88,  152,  216,  40,
67
-					104,  168,  232,  56,  120,  184,  248,  12,  76,  140,
68
-					204,  28,  92,  156,  220,  44,  108,  172,  236,  60,
69
-					124,  188,  252,  1,  65,  129,  193,  17,  81,  145,
70
-					209,  33,  97,  161,  225,  49,  113,  177,  241,  5,
71
-					69,  133,  197,  21,  85,  149,  213,  37,  101,  165,
72
-					229,  53,  117,  181,  245,  9,  73,  137,  201,  25,
73
-					89,  153,  217,  41,  105,  169,  233,  57,  121,  185,
74
-					249,  13,  77,  141,  205,  29,  93,  157,  221,  45,
75
-					109,  173,  237,  61,  125,  189,  253,  2,  66,  130,
76
-					194,  18,  82,  146,  210,  34,  98,  162,  226,  50,
77
-					114,  178,  242,  6,  70,  134,  198,  22,  86,  150,
78
-					214,  38,  102,  166,  230,  54,  118,  182,  246,  10,
79
-					74,  138,  202,  26,  90,  154,  218,  42,  106,  170,
80
-					234,  58,  122,  186,  250,  14,  78,  142,  206,  30,
81
-					94,  158,  222,  46,  110,  174,  238,  62,  126,  190,
82
-					254,  3,  67,  131,  195,  19,  83,  147,  211,  35,
83
-					99,  163,  227,  51,  115,  179,  243,  7,  71,  135,
84
-					199,  23,  87,  151,  215,  39,  103,  167,  231,  55,
85
-					119,  183,  247,  11,  75,  139,  203,  27,  91,  155,
86
-					219,  43,  107,  171,  235,  59,  123,  187,  251,  15,
87
-					79,  143,  207,  31,  95,  159,  223,  47,  111,  175,
88
-					239,  63,  127,  191,  255 };
62
+// A fix for our mirrored 64pin cable.
63
+uint8_t lookUp[256] PROGMEM = { 	0,  64,  128,  192,  16,  80,  144,  208,  32,  96,  160,
64
+									224,  48,  112,  176,  240,  4,  68,  132,  196,  20,
65
+									84,  148,  212,  36,  100,  164,  228,  52,  116,  180,
66
+									244,  8,  72,  136,  200,  24,  88,  152,  216,  40,
67
+									104,  168,  232,  56,  120,  184,  248,  12,  76,  140,
68
+									204,  28,  92,  156,  220,  44,  108,  172,  236,  60,
69
+									124,  188,  252,  1,  65,  129,  193,  17,  81,  145,
70
+									209,  33,  97,  161,  225,  49,  113,  177,  241,  5,
71
+									69,  133,  197,  21,  85,  149,  213,  37,  101,  165,
72
+									229,  53,  117,  181,  245,  9,  73,  137,  201,  25,
73
+									89,  153,  217,  41,  105,  169,  233,  57,  121,  185,
74
+									249,  13,  77,  141,  205,  29,  93,  157,  221,  45,
75
+									109,  173,  237,  61,  125,  189,  253,  2,  66,  130,
76
+									194,  18,  82,  146,  210,  34,  98,  162,  226,  50,
77
+									114,  178,  242,  6,  70,  134,  198,  22,  86,  150,
78
+									214,  38,  102,  166,  230,  54,  118,  182,  246,  10,
79
+									74,  138,  202,  26,  90,  154,  218,  42,  106,  170,
80
+									234,  58,  122,  186,  250,  14,  78,  142,  206,  30,
81
+									94,  158,  222,  46,  110,  174,  238,  62,  126,  190,
82
+									254,  3,  67,  131,  195,  19,  83,  147,  211,  35,
83
+									99,  163,  227,  51,  115,  179,  243,  7,  71,  135,
84
+									199,  23,  87,  151,  215,  39,  103,  167,  231,  55,
85
+									119,  183,  247,  11,  75,  139,  203,  27,  91,  155,
86
+									219,  43,  107,  171,  235,  59,  123,  187,  251,  15,
87
+									79,  143,  207,  31,  95,  159,  223,  47,  111,  175,
88
+									239,  63,  127,  191,  255 };
89
 
89
 
90
 ISR(TIMER1_COMPA_vect) {
90
 ISR(TIMER1_COMPA_vect) {
91
+	uint8_t latchCtr;
91
 	if (toggleFlag >= COUNT2) {
92
 	if (toggleFlag >= COUNT2) {
92
-		isrCall();
93
+
94
+		if (changedFlag != 0) {
95
+			// The picture changed. Restart!
96
+			layer = 0;
97
+			changedFlag = 0;
98
+		}
99
+
100
+		PORTD = 0;
101
+		PORTB &= ~(24); // Disable all Fets
102
+
103
+		for (latchCtr = 0; latchCtr < 8; latchCtr++) {
104
+			// Disable all latches
105
+			PORTC &= ~(0xFC); // 2 - 7
106
+			PORTB &= ~(6); // 0 & 1
107
+
108
+			PORTA = imgBuffer[layer][latchCtr]; // Put latch data
109
+
110
+			// Enable desired latch
111
+			if (latchCtr > 1) {
112
+				PORTC |= (1 << latchCtr);
113
+			} else {
114
+				PORTB |= (1 << (latchCtr + 1));
115
+			}
116
+		}
117
+
118
+		// Set FET
119
+		PORTD = (1 << layer); // PD2 to 7
120
+		PORTB = (PORTB & ~(24)) | (((1 << layer) << 3) & 24); // PB3 & 4
121
+		
122
+		// Select next layer
123
+		if (layer < 7) {
124
+			layer++;
125
+		} else {
126
+			layer = 0;
127
+			imgFlag++; // Finished
128
+		}
129
+
93
 		toggleFlag = 0;
130
 		toggleFlag = 0;
94
 		timesTriggered++;
131
 		timesTriggered++;
95
 	} else {
132
 	} else {
101
 	return timesTriggered;
138
 	return timesTriggered;
102
 }
139
 }
103
 
140
 
104
-uint8_t bitSwitch(uint8_t d) {
105
-	return lookUp[d];
106
-}
107
-
108
 void setImage(uint8_t *img) {
141
 void setImage(uint8_t *img) {
109
 	uint8_t i, j;
142
 	uint8_t i, j;
110
 	ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
143
 	ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
113
 		toggleFlag = 0;
146
 		toggleFlag = 0;
114
 		for (i = 0; i < 8; i++) {
147
 		for (i = 0; i < 8; i++) {
115
 			for (j = 0; j < 8; j++) {
148
 			for (j = 0; j < 8; j++) {
116
-				imgBuffer[j][7 - i] = ~(bitSwitch(img[j + (i * 8)]));
149
+				imgBuffer[j][7 - i] = ~(pgm_read_byte(&(lookUp[img[j + (i * 8)]])));
117
 			}
150
 			}
118
 		}
151
 		}
119
 	}
152
 	}
170
 void close(void) {
203
 void close(void) {
171
 	TIMSK &= ~(1 << OCIE1A); // Disable interrupt
204
 	TIMSK &= ~(1 << OCIE1A); // Disable interrupt
172
 }
205
 }
173
-
174
-// Data is sent to 8 Fet bits...
175
-inline void setFet(uint8_t data) {
176
-	PORTD = (data & ~(3)); // Doesn't interfere with serial communication...
177
-	PORTB = (PORTB & ~(24)) | ((data << 3) & 24);
178
-}
179
-
180
-// Give id of latch, 0 - 7
181
-inline void selectLatch(uint8_t latchNr) {
182
-	// Disable all latches
183
-	PORTC &= ~(0xFC); // 2 - 7
184
-	PORTB &= ~(6); // 0 & 1
185
-
186
-	// Enable desired latch
187
-	if (latchNr > 1) {
188
-		PORTC |= (1 << latchNr);
189
-	} else {
190
-		PORTB |= (1 << (latchNr + 1));
191
-	}
192
-}
193
-
194
-inline void setLatch(uint8_t latchNr, uint8_t data) {
195
-	selectLatch(latchNr); // Activate current latch
196
-	PORTA = data; // Put latch data
197
-	asm volatile("nop"::); // Wait for latch
198
-}
199
-
200
-inline void isrCall(void) {
201
-	uint8_t latchCtr = 0;
202
-
203
-	if (changedFlag != 0) {
204
-		// The picture changed. Restart!
205
-		layer = 0;
206
-		changedFlag = 0;
207
-	}
208
-	setFet(0);
209
-	for (; latchCtr < 8; latchCtr++) {
210
-		setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
211
-	}
212
-	setFet(1 << layer);
213
-	
214
-	// Select next layer
215
-	if (layer < 7) {
216
-		layer++;
217
-	} else {
218
-		layer = 0;
219
-		imgFlag++; // Finished
220
-	}
221
-}

+ 2
- 1
CubeFirmware/header/transmit.h View File

24
 void recieveAnimations(void);
24
 void recieveAnimations(void);
25
 void transmitAnimations(void);
25
 void transmitAnimations(void);
26
 void sendAudioData(void);
26
 void sendAudioData(void);
27
-void printTime(void);
27
+void printTime(void);
28
+void dumpFrame(uint8_t *f);

+ 31
- 5
CubeFirmware/main.c View File

155
 					if (isFinished() > duration) {
155
 					if (isFinished() > duration) {
156
 						// Last frame was displayed long enough
156
 						// Last frame was displayed long enough
157
 						imageIndex = (imageIndex < (imageCount - 1)) ? (imageIndex + 1) : 0;
157
 						imageIndex = (imageIndex < (imageCount - 1)) ? (imageIndex + 1) : 0;
158
-						imageData = getFrame(i);
158
+						imageData = getFrame(imageIndex);
159
 						if (imageData == NULL) {
159
 						if (imageData == NULL) {
160
 							duration = 24;
160
 							duration = 24;
161
 							setImage(defaultImageCube);
161
 							setImage(defaultImageCube);
166
 						}
166
 						}
167
 					}
167
 					}
168
 				} else {
168
 				} else {
169
-					// Built-In Frames
169
+					// Built-In Anims
170
 					if (isFinished() > duration) {
170
 					if (isFinished() > duration) {
171
 						idleIndex = (idleIndex < (idleCount - 1)) ? (idleIndex + 1) : 0;
171
 						idleIndex = (idleIndex < (idleCount - 1)) ? (idleIndex + 1) : 0;
172
 						duration = executeAnimation(idleIndex);
172
 						duration = executeAnimation(idleIndex);
307
 
307
 
308
 void serialHandler(char c) {
308
 void serialHandler(char c) {
309
 	// Used letters:
309
 	// Used letters:
310
-	// a, b, c, d, e, g, i, n, q, r, s, t, v, x, y, 0, 1, 2, 3, #
310
+	// a, b, c, d, e, f, g, h, i, n, q, r, s, t, u, v, x, y, 0, 1, 2, 3, #
311
 	uint8_t i, y, z;
311
 	uint8_t i, y, z;
312
+	uint8_t *tmp;
312
 
313
 
313
 	switch(c) {
314
 	switch(c) {
314
 	case OK:
315
 	case OK:
321
 		serialWriteString(getString(8));
322
 		serialWriteString(getString(8));
322
 		serialWriteString(getString(9));
323
 		serialWriteString(getString(9));
323
 		serialWriteString(getString(10));
324
 		serialWriteString(getString(10));
325
+		serialWriteString(getString(26));
326
+		serialWriteString(getString(34));
327
+
324
 		serialWriteString(getString(11));
328
 		serialWriteString(getString(11));
325
 		serialWriteString(getString(12));
329
 		serialWriteString(getString(12));
326
 		serialWriteString(getString(13));
330
 		serialWriteString(getString(13));
327
-		serialWriteString(getString(26));
328
 		break;
331
 		break;
329
 
332
 
330
 	case 'd': case 'D':
333
 	case 'd': case 'D':
331
-		// clearMem(); // Much too invasive
332
 		setAnimationCount(0);
334
 		setAnimationCount(0);
333
 		serialWrite(OK);
335
 		serialWrite(OK);
334
 		refreshAnimationCount = 1;
336
 		refreshAnimationCount = 1;
335
 		break;
337
 		break;
336
 
338
 
339
+	case 'f': case 'F':
340
+		serialWriteString(getString(32));
341
+		clearMem();
342
+		serialWriteString(getString(33));
343
+		refreshAnimationCount = 1;
344
+		break;
345
+
337
 	case 'g': case 'G':
346
 	case 'g': case 'G':
338
 		transmitAnimations();
347
 		transmitAnimations();
339
 		break;
348
 		break;
340
 
349
 
341
 	case 's': case 'S':
350
 	case 's': case 'S':
342
 		recieveAnimations();
351
 		recieveAnimations();
352
+		refreshAnimationCount = 1;
343
 		break;
353
 		break;
344
 
354
 
345
 	case 'v': case 'V':
355
 	case 'v': case 'V':
378
 
388
 
379
 	case 'c': case 'C':
389
 	case 'c': case 'C':
380
 		serialWriteString(itoa(getAnimationCount(), buffer, 10));
390
 		serialWriteString(itoa(getAnimationCount(), buffer, 10));
391
+		serialWriteString(" (");
392
+		serialWriteString(itoa(refreshAnimationCount, buffer, 10));
393
+		serialWriteString(")");
381
 		serialWriteString(getString(15));
394
 		serialWriteString(getString(15));
382
 		break;
395
 		break;
383
 
396
 
397
+	case 'u': case 'U':
398
+		serialWriteString(getString(31));
399
+		while(!serialHasChar()) {
400
+			wdt_reset();
401
+		}
402
+		i = serialGet() - '0';
403
+		serialWrite(i + '0');
404
+		serialWrite('\n');
405
+		tmp = getFrame(i);
406
+		dumpFrame(tmp);
407
+		free(tmp);
408
+		break;
409
+
384
 	case 'x': case 'X':
410
 	case 'x': case 'X':
385
 		// Get byte, store as animation count
411
 		// Get byte, store as animation count
386
 		serialWriteString(getString(16));
412
 		serialWriteString(getString(16));

+ 1278
- 1268
CubeFirmware/main.hex
File diff suppressed because it is too large
View File


+ 2
- 0
CubeFirmware/memLayer.c View File

23
  #include <stdint.h>
23
  #include <stdint.h>
24
  #include <avr/io.h>
24
  #include <avr/io.h>
25
  #include <stdlib.h>
25
  #include <stdlib.h>
26
+ #include <avr/wdt.h>
26
  #include "mem.h"
27
  #include "mem.h"
27
  #include "memLayer.h"
28
  #include "memLayer.h"
28
  #include "serial.h"
29
  #include "serial.h"
47
 	uint32_t i;
48
 	uint32_t i;
48
 	for (i = 0; i < MemLength; i++) {
49
 	for (i = 0; i < MemLength; i++) {
49
 		memWriteByte(i, 0);
50
 		memWriteByte(i, 0);
51
+		wdt_reset();
50
 	}
52
 	}
51
 }
53
 }
52
 
54
 

+ 6
- 6
CubeFirmware/strings.c View File

33
 const char stringHelp1[] PROGMEM = "(d)elete, (g)et anims, (s)et anims, (v)ersion\n"; // 6
33
 const char stringHelp1[] PROGMEM = "(d)elete, (g)et anims, (s)et anims, (v)ersion\n"; // 6
34
 const char stringHelp2[] PROGMEM = "(t)ime, (a)udio, (c)ount, (x)Custom count\n"; // 7
34
 const char stringHelp2[] PROGMEM = "(t)ime, (a)udio, (c)ount, (x)Custom count\n"; // 7
35
 const char stringHelp3[] PROGMEM = "(y)Set fixed animation count\n"; // 8
35
 const char stringHelp3[] PROGMEM = "(y)Set fixed animation count\n"; // 8
36
-const char stringHelp4[] PROGMEM = "S(e)lf Test, (m)ode\n"; // 9
36
+const char stringHelp4[] PROGMEM = "S(e)lf Test, (m)ode, d(u)mp\n"; // 9
37
 const char stringHelp5[] PROGMEM = "Play S(n)ake\n"; // 10
37
 const char stringHelp5[] PROGMEM = "Play S(n)ake\n"; // 10
38
 const char stringHelp6[] PROGMEM = "All LEDs Off/On (0/1)\n"; // 11
38
 const char stringHelp6[] PROGMEM = "All LEDs Off/On (0/1)\n"; // 11
39
 const char stringHelp7[] PROGMEM = "(2): Test Anim. 1\n"; // 12
39
 const char stringHelp7[] PROGMEM = "(2): Test Anim. 1\n"; // 12
55
 const char stringFrames2[] PROGMEM = " Frames per Second\n"; // 28
55
 const char stringFrames2[] PROGMEM = " Frames per Second\n"; // 28
56
 const char stringDeleted[] PROGMEM = "Memory deleted!\n"; // 29
56
 const char stringDeleted[] PROGMEM = "Memory deleted!\n"; // 29
57
 const char stringReset[] PROGMEM = "Reset in 500ms. Bye!\n"; // 30
57
 const char stringReset[] PROGMEM = "Reset in 500ms. Bye!\n"; // 30
58
-const char stringWatchdog[] PROGMEM = ""; // 31
59
-const char stringBrownout[] PROGMEM = ""; // 32
60
-const char stringNothing[] PROGMEM = ""; // 33
61
-const char stringExtern[] PROGMEM = ""; // 34
58
+const char stringWatchdog[] PROGMEM = "Enter frame to dump: "; // 31
59
+const char stringBrownout[] PROGMEM = "Formatting memory... "; // 32
60
+const char stringNothing[] PROGMEM = "Done!\n"; // 33
61
+const char stringExtern[] PROGMEM = "(f)ormat memory\n"; // 34
62
 const char stringJtag[] PROGMEM = ""; // 35
62
 const char stringJtag[] PROGMEM = ""; // 35
63
 const char stringPowerOn[] PROGMEM = ""; // 36
63
 const char stringPowerOn[] PROGMEM = ""; // 36
64
-const char stringMinute[] PROGMEM = "Yay! Another minute passed :)\n"; // 37
64
+const char stringMinute[] PROGMEM = ""; // 37
65
 const char stringAudioMode[] PROGMEM = ""; // 38
65
 const char stringAudioMode[] PROGMEM = ""; // 38
66
 const char stringCubeMode[] PROGMEM = ""; // 39
66
 const char stringCubeMode[] PROGMEM = ""; // 39
67
 const char stringModeChange[] PROGMEM = "Cube mode entered!\n"; // 40
67
 const char stringModeChange[] PROGMEM = "Cube mode entered!\n"; // 40

+ 31
- 79
CubeFirmware/transmit.c View File

34
 #define OK 0x42
34
 #define OK 0x42
35
 #define ERROR 0x23
35
 #define ERROR 0x23
36
 
36
 
37
-#define TRANSTIMEOUT 2500
37
+#define TIMEOUTPERFRAME 500
38
 
38
 
39
 // These are global variables from main.c
39
 // These are global variables from main.c
40
 extern char buffer[11];
40
 extern char buffer[11];
41
-extern uint8_t refreshAnimationCount;
42
 
41
 
43
 void recieveAnimations(void) {
42
 void recieveAnimations(void) {
44
 	uint8_t animCount, a, frameCount, f, i, c;
43
 	uint8_t animCount, a, frameCount, f, i, c;
45
 	uint16_t completeCount = 0;
44
 	uint16_t completeCount = 0;
46
 	uint8_t *frame = (uint8_t *)malloc(65);
45
 	uint8_t *frame = (uint8_t *)malloc(65);
47
 	uint64_t timestamp = getSystemTime();
46
 	uint64_t timestamp = getSystemTime();
47
+	uint64_t timeout = TIMEOUTPERFRAME;
48
 
48
 
49
 	serialWrite(OK); // We are ready...
49
 	serialWrite(OK); // We are ready...
50
 
50
 
51
 	while (!serialHasChar()) { // Wait for answer
51
 	while (!serialHasChar()) { // Wait for answer
52
-		if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
52
+		if ((getSystemTime() - timestamp) <= timeout) {
53
 			wdt_reset();
53
 			wdt_reset();
54
 		} else {
54
 		} else {
55
 			setAnimationCount(0);
55
 			setAnimationCount(0);
62
 
62
 
63
 	for (a = 0; a < animCount; a++) {
63
 	for (a = 0; a < animCount; a++) {
64
 		while (!serialHasChar()) { // Wait for answer
64
 		while (!serialHasChar()) { // Wait for answer
65
-			if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
65
+			if ((getSystemTime() - timestamp) <= timeout) {
66
 				wdt_reset();
66
 				wdt_reset();
67
 			} else {
67
 			} else {
68
 				setAnimationCount(0);
68
 				setAnimationCount(0);
74
 		serialWrite(OK);
74
 		serialWrite(OK);
75
 
75
 
76
 		for (f = 0; f < frameCount; f++) {
76
 		for (f = 0; f < frameCount; f++) {
77
+			timeout += TIMEOUTPERFRAME;
77
 			while (!serialHasChar()) { // Wait for answer
78
 			while (!serialHasChar()) { // Wait for answer
78
-				if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
79
+				if ((getSystemTime() - timestamp) <= timeout) {
79
 					wdt_reset();
80
 					wdt_reset();
80
 				} else {
81
 				} else {
81
 					setAnimationCount(0);
82
 					setAnimationCount(0);
88
 
89
 
89
 			for (i = 0; i < 64; i++) {
90
 			for (i = 0; i < 64; i++) {
90
 				while (!serialHasChar()) { // Wait for answer
91
 				while (!serialHasChar()) { // Wait for answer
91
-					if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
92
+					if ((getSystemTime() - timestamp) <= timeout) {
92
 						wdt_reset();
93
 						wdt_reset();
93
 					} else {
94
 					} else {
94
 						setAnimationCount(0);
95
 						setAnimationCount(0);
106
 	free(frame);
107
 	free(frame);
107
 
108
 
108
 	while (!serialHasChar()) { // Wait for answer
109
 	while (!serialHasChar()) { // Wait for answer
109
-		if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
110
+		if ((getSystemTime() - timestamp) <= timeout) {
110
 			wdt_reset();
111
 			wdt_reset();
111
 		} else {
112
 		} else {
112
 			setAnimationCount(0);
113
 			setAnimationCount(0);
115
 	}
116
 	}
116
 	c = serialGet();
117
 	c = serialGet();
117
 	while (!serialHasChar()) { // Wait for answer
118
 	while (!serialHasChar()) { // Wait for answer
118
-		if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
119
+		if ((getSystemTime() - timestamp) <= timeout) {
119
 			wdt_reset();
120
 			wdt_reset();
120
 		} else {
121
 		} else {
121
 			setAnimationCount(0);
122
 			setAnimationCount(0);
124
 	}
125
 	}
125
 	c = serialGet();
126
 	c = serialGet();
126
 	while (!serialHasChar()) { // Wait for answer
127
 	while (!serialHasChar()) { // Wait for answer
127
-		if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
128
+		if ((getSystemTime() - timestamp) <= timeout) {
128
 			wdt_reset();
129
 			wdt_reset();
129
 		} else {
130
 		} else {
130
 			setAnimationCount(0);
131
 			setAnimationCount(0);
133
 	}
134
 	}
134
 	c = serialGet();
135
 	c = serialGet();
135
 	while (!serialHasChar()) { // Wait for answer
136
 	while (!serialHasChar()) { // Wait for answer
136
-		if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
137
+		if ((getSystemTime() - timestamp) <= timeout) {
137
 			wdt_reset();
138
 			wdt_reset();
138
 		} else {
139
 		} else {
139
 			setAnimationCount(0);
140
 			setAnimationCount(0);
145
 	serialWrite(OK);
146
 	serialWrite(OK);
146
 
147
 
147
 	setAnimationCount(completeCount);
148
 	setAnimationCount(completeCount);
148
-	refreshAnimationCount = 1;
149
 }
149
 }
150
 
150
 
151
 // TODO: Rewrite for new Serial API
151
 // TODO: Rewrite for new Serial API
152
 void transmitAnimations(void) {
152
 void transmitAnimations(void) {
153
-	// We store no animation information in here
154
-	// So we have to place all frames in one or more
155
-	// animations... We need 8 animations max...
156
-	uint8_t animationsToGo;
157
-	uint16_t framesToGo = getAnimationCount();
158
-	uint16_t character;
159
-	uint8_t a;
160
-	uint8_t f, fMax, i;
161
-	uint8_t *frame;
162
-
163
-	if ((framesToGo % 255) == 0) {
164
-		animationsToGo = framesToGo / 255;
165
-	} else {
166
-		animationsToGo = (framesToGo / 255) + 1;
167
-	}
168
-
169
-	serialWrite(OK);
170
-	serialWrite(animationsToGo);
171
-	while ((character = serialGet()) & 0xFF00); // Wait for answer
172
-	if ((character & 0x00FF) != OK) { // Error code recieved
173
-		return;
174
-	}
175
-
176
-	for (a = 0; a < animationsToGo; a++) {
177
-		if (framesToGo > 255) {
178
-			fMax = 255;
179
-		} else {
180
-			fMax = framesToGo;
181
-		}
182
-
183
-		serialWrite(fMax); // Number of Frames in current animation
184
-		while ((character = serialGet()) & 0xFF00); // Wait for answer
185
-		if ((character & 0x00FF) != OK) { // Error code recieved
186
-			return;
187
-		}
188
-
189
-		for (f = 0; f < fMax; f++) {
190
-			frame = getFrame(f + (255 * a));
191
-
192
-			serialWrite(frame[64]); // frame duration
193
-			while ((character = serialGet()) & 0xFF00); // Wait for answer
194
-			if ((character & 0x00FF) != OK) { // Error code recieved
195
-				free(frame);
196
-				return;
197
-			}
198
-
199
-			for (i = 0; i < 64; i++) {
200
-				serialWrite(frame[i]);
201
-			}
202
-			while ((character = serialGet()) & 0xFF00); // Wait for answer
203
-			if ((character & 0x00FF) != OK) { // Error code recieved
204
-				free(frame);
205
-				return;
206
-			}
207
-
208
-			free(frame);
209
-		}
210
-		framesToGo -= fMax;
211
-	}
212
-
213
-	serialWrite(OK);
214
-	serialWrite(OK);
215
-	serialWrite(OK);
216
-	serialWrite(OK);
217
-
218
-	while ((character = serialGet()) & 0xFF00); // Wait for answer
219
-	// Error code ignored...
153
+	serialWrite(ERROR);
220
 }
154
 }
221
 
155
 
222
 void sendAudioData(void) {
156
 void sendAudioData(void) {
261
 	} else {
195
 	} else {
262
 		serialWrite('\n');
196
 		serialWrite('\n');
263
 	}
197
 	}
264
-}
198
+}
199
+
200
+void dumpFrame(uint8_t *f) {
201
+	uint8_t zeile, spalte;
202
+	for (zeile = 0; zeile < 8; zeile++) {
203
+		serialWrite(zeile + '0');
204
+		serialWriteString(": ");
205
+		for (spalte = 0; spalte < 8; spalte++) {
206
+			itoa(f[(8 * zeile) + spalte], buffer, 16);
207
+			serialWriteString(buffer);
208
+			serialWrite(' ');
209
+		}
210
+		serialWrite('\n');
211
+	}
212
+	serialWriteString("Duration: ");
213
+	itoa(f[64], buffer, 10);
214
+	serialWriteString(buffer);
215
+	serialWrite('\n');
216
+}

+ 106
- 6
UploadTest/main.c View File

13
 int serialReadTry(char *data, size_t length);
13
 int serialReadTry(char *data, size_t length);
14
 int serialWriteTry(char *data, size_t length);
14
 int serialWriteTry(char *data, size_t length);
15
 void intHandler(int dummy);
15
 void intHandler(int dummy);
16
+void transferFile(char *file);
16
 
17
 
17
 volatile int keepRunning = 1;
18
 volatile int keepRunning = 1;
19
+FILE *fp = NULL;
18
 
20
 
19
 #define suicide intHandler(0)
21
 #define suicide intHandler(0)
20
 
22
 
28
 	char command = -1;
30
 	char command = -1;
29
 
31
 
30
 	if (argc < 2) {
32
 	if (argc < 2) {
31
-		printf("Usage:\n%s /dev/port [-d 0xff]\n", argv[0]);
33
+		printf("Usage:\n%s /dev/port [-d 0xff] | [-f /file]\n", argv[0]);
32
 		return 0;
34
 		return 0;
33
 	}
35
 	}
34
 
36
 
37
 		return 0;
39
 		return 0;
38
 	}
40
 	}
39
 
41
 
40
-	if ((argc >= 4) && (strcmp(argv[2], "-d") == 0)) {
42
+	signal(SIGINT, intHandler);
43
+	signal(SIGQUIT, intHandler);
44
+
45
+	if ((argc == 4) && (strcmp(argv[2], "-d") == 0)) {
41
 		sscanf(argv[3], "%x", &data);
46
 		sscanf(argv[3], "%x", &data);
42
 		frameCount = 1;
47
 		frameCount = 1;
43
 	}
48
 	}
44
 
49
 
50
+	if ((argc == 4) && (strcmp(argv[2], "-f") == 0)) {
51
+		printf("Trying to send from file.\n");
52
+		transferFile(argv[3]);
53
+	}
54
+
45
 	if (argc == 3) {
55
 	if (argc == 3) {
46
 		command = argv[2][0];
56
 		command = argv[2][0];
47
 	}
57
 	}
48
 
58
 
49
-	signal(SIGINT, intHandler);
50
-	signal(SIGQUIT, intHandler);
51
-
52
 	printf("Port opened. Sending test data:\n");
59
 	printf("Port opened. Sending test data:\n");
53
 
60
 
54
-	printf("\tSending command 's'...");
55
 	if (command != -1) {
61
 	if (command != -1) {
56
 		c = command;
62
 		c = command;
57
 	} else {
63
 	} else {
58
 		c = 's';
64
 		c = 's';
59
 	}
65
 	}
66
+	printf("\tSending command '%c'...", c);
60
 	if (serialWriteTry(&c, 1) != 0) {
67
 	if (serialWriteTry(&c, 1) != 0) {
61
 		printf(" Could not send it!\n");
68
 		printf(" Could not send it!\n");
62
 		suicide;
69
 		suicide;
146
 	return 0;
153
 	return 0;
147
 }
154
 }
148
 
155
 
156
+void writeS(char c) {
157
+	if (serialWriteTry(&c, 1) != 0) {
158
+		printf("Error while sending!\n");
159
+		suicide;
160
+	}
161
+}
162
+
163
+int readFrame(char *frame) {
164
+	char lineBuffer[80];
165
+	char buff[3];
166
+	int lines, byte, i = 0, tmp;
167
+	buff[2] = '\0';
168
+
169
+	fgets(lineBuffer, 80, fp);// Frame name, ignore
170
+	for (lines = 0; lines < 8; lines++) {
171
+		fgets(lineBuffer, 80, fp);
172
+		for(byte = 0; byte < 8; byte++) {
173
+			buff[0] = lineBuffer[(2 * byte)];
174
+			buff[1] = lineBuffer[(2 * byte) + 1];
175
+			sscanf(buff, "%x", &tmp);
176
+			frame[i++] = (char)tmp;
177
+		}
178
+	}
179
+
180
+	fgets(lineBuffer, 80, fp);
181
+	if(lineBuffer[strlen(lineBuffer)-1] == '\n') {
182
+		lineBuffer[strlen(lineBuffer)-1] = '\0';
183
+	}
184
+	sscanf(lineBuffer, "%d", &lines);
185
+	return lines;
186
+}
187
+
188
+void transferFile(char *file) {
189
+	char lineBuffer[80];
190
+	int f, i;
191
+	int fMax = 0;
192
+	char frame[64];
193
+	int duration;
194
+
195
+	fp = fopen(file, "r");
196
+	if (fp != NULL) {
197
+		printf("File opened...\n");
198
+		fgets(lineBuffer, 80, fp);
199
+		if (lineBuffer[strlen(lineBuffer)-1] == '\n') {
200
+			lineBuffer[strlen(lineBuffer)-1] = '\0';
201
+		}
202
+		fMax = atoi(lineBuffer);
203
+		printf("Has %d frames.\n", fMax);
204
+		fgets(lineBuffer, 80, fp);
205
+		printf("Sendind command.\n");
206
+		writeS('s');
207
+		readAck();
208
+		printf("Sendind animation count.\n");
209
+		writeS(1); // Animation count
210
+		readAck();
211
+		printf("Sending frame count.\n");
212
+		writeS(fMax); // Frame count
213
+		readAck();
214
+		for (f = 0; f < fMax; f++) {
215
+			printf("Reading frame from file... ");
216
+			duration = readFrame(frame);
217
+			printf("Done!\nSending duration.\n");
218
+			writeS(duration);
219
+			readAck();
220
+			printf("Sending frame (");
221
+			for (i = 0; i < 64; i++) {
222
+				writeS(frame[i]);
223
+				printf("%x", frame[i]);
224
+				if (i < 63) {
225
+					printf(" ");
226
+				}
227
+			}
228
+			printf(") Done!\n");
229
+			readAck();
230
+			printf("Wrote frame %d\n\n", f);
231
+		}
232
+		printf("Sending end sequence!\n");
233
+		writeS(0x42);
234
+		writeS(0x42);
235
+		writeS(0x42);
236
+		writeS(0x42);
237
+		readAck();
238
+	} else {
239
+		printf("File not found\n");
240
+		suicide;
241
+	}
242
+	printf("Success!\n");
243
+	suicide;
244
+}
245
+
149
 void readAck() {
246
 void readAck() {
150
 	char c;
247
 	char c;
151
 	printf("Awaiting acknowledge...");
248
 	printf("Awaiting acknowledge...");
218
 
315
 
219
 void intHandler(int dummy) {
316
 void intHandler(int dummy) {
220
 	keepRunning = 0;
317
 	keepRunning = 0;
318
+	if (fp != NULL) {
319
+		fclose(fp);
320
+	}
221
 	serialClose();
321
 	serialClose();
222
 	exit(0);
322
 	exit(0);
223
 }
323
 }

Loading…
Cancel
Save