Browse Source

Consistent storage format.

No longer using one- and two-dimensional arrays combined.
Thomas Buck 12 years ago
parent
commit
ba65d6c88d
4 changed files with 71 additions and 60 deletions
  1. 2
    2
      CubeFirmware/cube.c
  2. 1
    1
      CubeFirmware/cube.h
  3. 56
    56
      CubeFirmware/main.c
  4. 12
    1
      CubeFirmware/memLayer.c

+ 2
- 2
CubeFirmware/cube.c View File

48
 inline void delay_ns(int16_t ns);
48
 inline void delay_ns(int16_t ns);
49
 inline void isrCall(void);
49
 inline void isrCall(void);
50
 
50
 
51
-void setImage(uint8_t **img) {
51
+void setImage(uint8_t *img) {
52
 	uint8_t i, j;
52
 	uint8_t i, j;
53
 	ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
53
 	ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
54
 		changedFlag = 1;
54
 		changedFlag = 1;
55
 		imgFlag = 0;
55
 		imgFlag = 0;
56
 		for (i = 0; i < 8; i++) {
56
 		for (i = 0; i < 8; i++) {
57
 			for (j = 0; j < 8; j++) {
57
 			for (j = 0; j < 8; j++) {
58
-				imgBuffer[i][j] = img[i][j];
58
+				imgBuffer[i][j] = img[j + (i * 8)];
59
 			}
59
 			}
60
 		}
60
 		}
61
 	}
61
 	}

+ 1
- 1
CubeFirmware/cube.h View File

29
 extern void initCube(void);
29
 extern void initCube(void);
30
 
30
 
31
 // Copies the data in img into its own buffer, so free img afterwards...
31
 // Copies the data in img into its own buffer, so free img afterwards...
32
-extern void setImage(uint8_t **img); // img[8][8]
32
+extern void setImage(uint8_t *img); // img[64]
33
 extern uint8_t isFinished(void);
33
 extern uint8_t isFinished(void);
34
 extern void close(void);
34
 extern void close(void);
35
 
35
 

+ 56
- 56
CubeFirmware/main.c View File

31
 #ifdef DEBUG
31
 #ifdef DEBUG
32
 #define VERSION "v2 (Debug Build)\nNOT COMPATIBLE WITH CubeControl!\n"
32
 #define VERSION "v2 (Debug Build)\nNOT COMPATIBLE WITH CubeControl!\n"
33
 #else
33
 #else
34
-#define VERSION "v2\n"
34
+#define VERSION "v2 Release\n"
35
 #endif
35
 #endif
36
 
36
 
37
 #include <avr/io.h>
37
 #include <avr/io.h>
56
 #define MEMORYERROR 2
56
 #define MEMORYERROR 2
57
 // Memory not writeable
57
 // Memory not writeable
58
 #define MEMORYWRITEERROR 4
58
 #define MEMORYWRITEERROR 4
59
-
60
 // x = errorcode, e = error definition, not NOERROR
59
 // x = errorcode, e = error definition, not NOERROR
61
 #define ISERROR(x, e) ((x) & (e))
60
 #define ISERROR(x, e) ((x) & (e))
62
 
61
 
66
 void recieveAnimations(void);
65
 void recieveAnimations(void);
67
 void transmitAnimations(void);
66
 void transmitAnimations(void);
68
 uint8_t audioModeSelected(void);
67
 uint8_t audioModeSelected(void);
69
-inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
70
-inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
71
-void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf);
72
-void visualizeAudioData(uint8_t *audioData, uint8_t **imageData);
68
+inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
69
+inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
70
+void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf);
71
+void visualizeAudioData(uint8_t *audioData, uint8_t *imageData);
73
 #ifdef DEBUG
72
 #ifdef DEBUG
74
 void printErrors(uint8_t e);
73
 void printErrors(uint8_t e);
75
 #endif
74
 #endif
79
 char buffer[11];
78
 char buffer[11];
80
 
79
 
81
 int main(void) {
80
 int main(void) {
82
-	uint8_t *audioData;
83
-	uint8_t **imageData;
84
-	uint8_t i, lastMode;
81
+	uint8_t *audioData = NULL;
82
+	uint8_t *imageData = NULL;
83
+	uint8_t i, length = 0, lastMode;
85
 	uint16_t count;
84
 	uint16_t count;
86
 	uint64_t lastChecked;
85
 	uint64_t lastChecked;
87
 
86
 
96
 	DDRC = 0xFF; // Latch Enable
95
 	DDRC = 0xFF; // Latch Enable
97
 	DDRA = 0xFF; // Latch Data
96
 	DDRA = 0xFF; // Latch Data
98
 
97
 
98
+#ifdef DEBUG
99
+	// Kill animation counter in debug mode
100
+	// => Don't preserve animations while power down
101
+	setAnimationCount(0);
102
+
99
 	i = selfTest();
103
 	i = selfTest();
100
 	if (i) {
104
 	if (i) {
101
-		// Something is not working
102
-#ifdef DEBUG
103
 		serialWriteString("Self-Test Error: 0b");
105
 		serialWriteString("Self-Test Error: 0b");
104
 		serialWriteString(itoa(i, buffer, 2));
106
 		serialWriteString(itoa(i, buffer, 2));
105
 		serialWrite('\n');
107
 		serialWrite('\n');
106
 		printErrors(i);
108
 		printErrors(i);
107
-#endif
108
-	}
109
-
110
-	imageData = (uint8_t **)malloc(8 * sizeof(uint8_t *));
111
-	for (i = 0; i < 8; i++) {
112
-		imageData[i] = (uint8_t *)malloc(8 * sizeof(uint8_t));
113
 	}
109
 	}
110
+#endif
114
 
111
 
115
 #ifdef DEBUG
112
 #ifdef DEBUG
116
-	refreshAnimationCount = 0; // Don't talk to FRAM yet...
117
-
118
 	serialWriteString("\n\nInitialized: ");
113
 	serialWriteString("\n\nInitialized: ");
119
 	serialWriteString(VERSION);
114
 	serialWriteString(VERSION);
120
 	serialWriteString("Took ");
115
 	serialWriteString("Took ");
125
 	lastMode = audioModeSelected();
120
 	lastMode = audioModeSelected();
126
 	lastChecked = getSystemTime();
121
 	lastChecked = getSystemTime();
127
 
122
 
123
+	i = 0;
124
+	count = getAnimationCount();
128
 	while (1) {
125
 	while (1) {
129
-		//if(lastMode) {
126
+		if(lastMode) {
130
 			// Get Audio Data and visualize it
127
 			// Get Audio Data and visualize it
131
-			/* audioData = getAudioData();
132
-			if (audioData != NULL) {
133
-				visualizeAudioData(audioData, imageData);
134
-				setImage(imageData);
135
-				free(audioData);
136
-			}
137
-			while(!isFinished()); // Wait for it to display */
138
-		//} else {
139
-			// Look for commands, play from fram
140
-			// We have 128*1024 bytes
141
-			// A Frame needs 65 bytes (64 data + duration)
142
-			// We place 2016 Frames in mem => 131040
143
-			// That gives us 32 bytes at the beginning, 0 -> 31
144
-			// The first frame starts at 32
145
-			if (serialHasChar()) {
146
-				serialHandler((char)(serialGet()));
128
+			if (isFinished()) {
129
+				audioData = getAudioData();
130
+				if (audioData != NULL) {
131
+					visualizeAudioData(audioData, imageData);
132
+					setImage(imageData);
133
+					free(audioData);
134
+				}
147
 			}
135
 			}
148
-
149
-			/* if (refreshAnimationCount) {
136
+		} else {
137
+			if (refreshAnimationCount) {
150
 				// Get animation count stored in FRAM via TWI, if needed
138
 				// Get animation count stored in FRAM via TWI, if needed
151
 				count = getAnimationCount();
139
 				count = getAnimationCount();
152
 				refreshAnimationCount = 0;
140
 				refreshAnimationCount = 0;
153
-			} */
154
-		//}
141
+				i = 0;
142
+			}
143
+
144
+			if (isFinished() > length) {
145
+				// Load next image
146
+				if (i < (count - 1)) {
147
+					i++;
148
+				} else {
149
+					i = 0;
150
+				}
151
+
152
+				imageData = getFrame(i);
153
+				length = imageData[64];
154
+				setImage(imageData);
155
+				free(imageData);
156
+			}
157
+		}
155
 
158
 
156
-		if ((getSystemTime() - lastChecked) > 100) {
159
+		if (serialHasChar()) {
160
+			serialHandler((char)(serialGet()));
161
+		}
162
+
163
+		if ((getSystemTime() - lastChecked) > 150) {
157
 			lastMode = audioModeSelected();
164
 			lastMode = audioModeSelected();
158
 			lastChecked = getSystemTime();
165
 			lastChecked = getSystemTime();
159
 		} 
166
 		} 
483
 	return lastButtonState;
490
 	return lastButtonState;
484
 }
491
 }
485
 
492
 
486
-inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
487
-	buf[z][y] |= (1 << x);
488
-}
489
-
490
-inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
491
-	buf[z][y] &= ~(1 << x);
493
+inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
494
+	buf[(8 * z) + y] |= (1 << x);
492
 }
495
 }
493
 
496
 
494
-void setBuffer(uint8_t d, uint8_t *buf, uint8_t length) {
495
-	uint8_t i;
496
-	for (i = 0; i < length; i++) {
497
-		buf[i] = d;
498
-	}
497
+inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
498
+	buf[(8 * z) + y] &= ~(1 << x);
499
 }
499
 }
500
 
500
 
501
-void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf) {
501
+void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf) {
502
 	uint8_t i = 0;
502
 	uint8_t i = 0;
503
 	for (; i < height; i++) {
503
 	for (; i < height; i++) {
504
 		setPixelBuffer(x, i, z, buf);
504
 		setPixelBuffer(x, i, z, buf);
505
 	}
505
 	}
506
 }
506
 }
507
 
507
 
508
-void visualizeAudioData(uint8_t *audioData, uint8_t **imageData) {
508
+void visualizeAudioData(uint8_t *audioData, uint8_t *imageData) {
509
 	uint8_t i;
509
 	uint8_t i;
510
-	for (i = 0; i < 8; i++) {
511
-		setBuffer(0, imageData[i], 8);
510
+	for (i = 0; i < 64; i++) {
511
+		imageData[i] = 0;
512
 	}
512
 	}
513
 
513
 
514
 	// 8 LEDs, Max Val 255:
514
 	// 8 LEDs, Max Val 255:

+ 12
- 1
CubeFirmware/memLayer.c View File

27
  #include "memLayer.h"
27
  #include "memLayer.h"
28
  #include "serial.h"
28
  #include "serial.h"
29
 
29
 
30
+// We have 128*1024 bytes
31
+// A Frame needs 65 bytes (64 data + duration)
32
+// We place 2016 Frames in mem => 131040
33
+// That gives us 32 bytes at the beginning, 0 -> 31
34
+// The first frame starts at 32
35
+
30
 // Free after usage!
36
 // Free after usage!
31
 uint8_t *getFrame(uint16_t frameNumber) {
37
 uint8_t *getFrame(uint16_t frameNumber) {
32
 	return memGetBytes(32 + (65 * frameNumber), 65);
38
 	return memGetBytes(32 + (65 * frameNumber), 65);
48
 	uint8_t lsb = memGetByte(0);
54
 	uint8_t lsb = memGetByte(0);
49
 	uint8_t msb = memGetByte(1);
55
 	uint8_t msb = memGetByte(1);
50
 	uint16_t animationCount = (uint16_t)lsb;
56
 	uint16_t animationCount = (uint16_t)lsb;
51
-	return (animationCount | (((uint16_t)(msb)) << 8));
57
+	animationCount |= (((uint16_t)(msb)) << 8);
58
+	if (animationCount <= 2016) {
59
+		return animationCount;
60
+	} else {
61
+		return 2016;
62
+	}
52
 }
63
 }
53
 
64
 
54
 void setAnimationCount(uint16_t c) {
65
 void setAnimationCount(uint16_t c) {

Loading…
Cancel
Save