Переглянути джерело

Consistent storage format.

No longer using one- and two-dimensional arrays combined.
Thomas Buck 12 роки тому
джерело
коміт
ba65d6c88d
4 змінених файлів з 71 додано та 60 видалено
  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 Переглянути файл

@@ -48,14 +48,14 @@ volatile uint8_t layer = 0;
48 48
 inline void delay_ns(int16_t ns);
49 49
 inline void isrCall(void);
50 50
 
51
-void setImage(uint8_t **img) {
51
+void setImage(uint8_t *img) {
52 52
 	uint8_t i, j;
53 53
 	ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
54 54
 		changedFlag = 1;
55 55
 		imgFlag = 0;
56 56
 		for (i = 0; i < 8; i++) {
57 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 Переглянути файл

@@ -29,7 +29,7 @@
29 29
 extern void initCube(void);
30 30
 
31 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 33
 extern uint8_t isFinished(void);
34 34
 extern void close(void);
35 35
 

+ 56
- 56
CubeFirmware/main.c Переглянути файл

@@ -31,7 +31,7 @@
31 31
 #ifdef DEBUG
32 32
 #define VERSION "v2 (Debug Build)\nNOT COMPATIBLE WITH CubeControl!\n"
33 33
 #else
34
-#define VERSION "v2\n"
34
+#define VERSION "v2 Release\n"
35 35
 #endif
36 36
 
37 37
 #include <avr/io.h>
@@ -56,7 +56,6 @@
56 56
 #define MEMORYERROR 2
57 57
 // Memory not writeable
58 58
 #define MEMORYWRITEERROR 4
59
-
60 59
 // x = errorcode, e = error definition, not NOERROR
61 60
 #define ISERROR(x, e) ((x) & (e))
62 61
 
@@ -66,10 +65,10 @@ void sendAudioData(void);
66 65
 void recieveAnimations(void);
67 66
 void transmitAnimations(void);
68 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 72
 #ifdef DEBUG
74 73
 void printErrors(uint8_t e);
75 74
 #endif
@@ -79,9 +78,9 @@ uint8_t lastButtonState = 0;
79 78
 char buffer[11];
80 79
 
81 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 84
 	uint16_t count;
86 85
 	uint64_t lastChecked;
87 86
 
@@ -96,25 +95,21 @@ int main(void) {
96 95
 	DDRC = 0xFF; // Latch Enable
97 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 103
 	i = selfTest();
100 104
 	if (i) {
101
-		// Something is not working
102
-#ifdef DEBUG
103 105
 		serialWriteString("Self-Test Error: 0b");
104 106
 		serialWriteString(itoa(i, buffer, 2));
105 107
 		serialWrite('\n');
106 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 112
 #ifdef DEBUG
116
-	refreshAnimationCount = 0; // Don't talk to FRAM yet...
117
-
118 113
 	serialWriteString("\n\nInitialized: ");
119 114
 	serialWriteString(VERSION);
120 115
 	serialWriteString("Took ");
@@ -125,35 +120,47 @@ int main(void) {
125 120
 	lastMode = audioModeSelected();
126 121
 	lastChecked = getSystemTime();
127 122
 
123
+	i = 0;
124
+	count = getAnimationCount();
128 125
 	while (1) {
129
-		//if(lastMode) {
126
+		if(lastMode) {
130 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 138
 				// Get animation count stored in FRAM via TWI, if needed
151 139
 				count = getAnimationCount();
152 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 164
 			lastMode = audioModeSelected();
158 165
 			lastChecked = getSystemTime();
159 166
 		} 
@@ -483,32 +490,25 @@ uint8_t audioModeSelected(void) {
483 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 502
 	uint8_t i = 0;
503 503
 	for (; i < height; i++) {
504 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 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 514
 	// 8 LEDs, Max Val 255:

+ 12
- 1
CubeFirmware/memLayer.c Переглянути файл

@@ -27,6 +27,12 @@
27 27
  #include "memLayer.h"
28 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 36
 // Free after usage!
31 37
 uint8_t *getFrame(uint16_t frameNumber) {
32 38
 	return memGetBytes(32 + (65 * frameNumber), 65);
@@ -48,7 +54,12 @@ uint16_t getAnimationCount() {
48 54
 	uint8_t lsb = memGetByte(0);
49 55
 	uint8_t msb = memGetByte(1);
50 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 65
 void setAnimationCount(uint16_t c) {

Завантаження…
Відмінити
Зберегти