|
@@ -24,6 +24,7 @@
|
24
|
24
|
#include <avr/interrupt.h>
|
25
|
25
|
#include <stdlib.h>
|
26
|
26
|
|
|
27
|
+#include "uart.h"
|
27
|
28
|
#include "cube.h"
|
28
|
29
|
|
29
|
30
|
#ifndef F_CPU
|
|
@@ -32,9 +33,62 @@
|
32
|
33
|
|
33
|
34
|
volatile uint8_t _isrCounter = 0;
|
34
|
35
|
volatile uint8_t **imgBuffer = NULL; // imgBuffer[8][8]
|
|
36
|
+volatile uint8_t changedFlag = 0;
|
35
|
37
|
volatile uint8_t imgFlag = 0;
|
|
38
|
+volatile uint8_t layer = 0;
|
36
|
39
|
|
37
|
|
-// Wir zählen 21 mal bis 3968
|
|
40
|
+inline void delay_ns(int16_t ns);
|
|
41
|
+inline void isrCall(void);
|
|
42
|
+
|
|
43
|
+void setImage(uint8_t **img) {
|
|
44
|
+ uint8_t i = 0, j;
|
|
45
|
+
|
|
46
|
+ changedFlag = 1;
|
|
47
|
+ for (; i < 8; i++) {
|
|
48
|
+ for (j = 0; j < 8; j++) {
|
|
49
|
+ imgBuffer[i][j] = img[i][j];
|
|
50
|
+ }
|
|
51
|
+ }
|
|
52
|
+}
|
|
53
|
+
|
|
54
|
+uint8_t isFinished(void) {
|
|
55
|
+ return imgFlag;
|
|
56
|
+}
|
|
57
|
+
|
|
58
|
+void init(void) {
|
|
59
|
+ uint8_t ctr = 0;
|
|
60
|
+ DDRD = 0xFF; // Mosfets as Output
|
|
61
|
+ DDRB = 0xFF;
|
|
62
|
+ DDRC = 0xFF; // Latch Enable
|
|
63
|
+ DDRA = 0xFF; // Latch Data
|
|
64
|
+
|
|
65
|
+ TCCR1A |= (1 << WGM12); // CTC Mode
|
|
66
|
+ TCCR1B |= (1 << CS10); // No prescaler
|
|
67
|
+ OCR1A = 3968;
|
|
68
|
+ TIMSK = (1 << OCIE1A); // Enable Overflow Interrupt
|
|
69
|
+
|
|
70
|
+ // We just assume this works, because after reset,
|
|
71
|
+ // enough Memory should be available...
|
|
72
|
+ imgBuffer = malloc(8 * sizeof(uint8_t*));
|
|
73
|
+
|
|
74
|
+ for(ctr = 0; ctr < 8; ctr++) {
|
|
75
|
+ // Same reasoning here...
|
|
76
|
+ imgBuffer[ctr] = malloc(8 * sizeof(uint8_t));
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ sei(); // Enable Interrupts
|
|
80
|
+}
|
|
81
|
+
|
|
82
|
+void close(void) {
|
|
83
|
+ uint8_t ctr = 0;
|
|
84
|
+ for (; ctr < 8; ctr++) {
|
|
85
|
+ free((uint8_t *)imgBuffer[ctr]);
|
|
86
|
+ }
|
|
87
|
+ free(imgBuffer);
|
|
88
|
+ TIMSK &= ~(1 << OCIE1A); // Disable interrupt
|
|
89
|
+}
|
|
90
|
+
|
|
91
|
+// Count to 3968 21 times...
|
38
|
92
|
ISR(TIMER1_COMPA_vect) {
|
39
|
93
|
if (_isrCounter < 20) {
|
40
|
94
|
_isrCounter++;
|
|
@@ -44,15 +98,14 @@ ISR(TIMER1_COMPA_vect) {
|
44
|
98
|
}
|
45
|
99
|
}
|
46
|
100
|
|
|
101
|
+// Data is sent to 8 Fet bits...
|
47
|
102
|
inline void setFet(uint8_t data) {
|
48
|
|
- data &= ~((1 << 1) | 1);
|
49
|
|
- PORTD = data;
|
50
|
|
- data &= ~(3);
|
51
|
|
- data = data << 3;
|
52
|
|
- PORTB |= data;
|
|
103
|
+ PORTD = (data & ~(3)); // Doesn't interfere with serial communication...
|
|
104
|
+ PORTB &= ~(24);
|
|
105
|
+ PORTB |= ((data << 3) & 24);
|
53
|
106
|
}
|
54
|
107
|
|
55
|
|
-
|
|
108
|
+// Give id of latch, 0 - 7
|
56
|
109
|
inline void selectLatch(uint8_t latchNr) {
|
57
|
110
|
PORTC = 0;
|
58
|
111
|
if (latchNr < 8) {
|
|
@@ -70,9 +123,15 @@ inline void setLatch(uint8_t latchNr, uint8_t data) {
|
70
|
123
|
}
|
71
|
124
|
|
72
|
125
|
inline void isrCall(void) {
|
73
|
|
- static uint8_t layer = 0;
|
74
|
126
|
uint8_t latchCtr = 0;
|
75
|
|
-
|
|
127
|
+
|
|
128
|
+ if (changedFlag != 0) {
|
|
129
|
+ // The picture changed. Restart!
|
|
130
|
+ layer = 0;
|
|
131
|
+ imgFlag = 0;
|
|
132
|
+ changedFlag = 0;
|
|
133
|
+ }
|
|
134
|
+
|
76
|
135
|
for (; latchCtr < 8; latchCtr++) {
|
77
|
136
|
setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
|
78
|
137
|
}
|
|
@@ -82,11 +141,12 @@ inline void isrCall(void) {
|
82
|
141
|
layer++;
|
83
|
142
|
} else {
|
84
|
143
|
layer = 0;
|
|
144
|
+ imgFlag = 1; // Finished
|
85
|
145
|
}
|
86
|
146
|
}
|
87
|
147
|
|
88
|
148
|
inline void delay_ns(int16_t ns) {
|
89
|
149
|
// minimum 63 nanoseconds (= 1 tick)
|
90
|
|
- for (ns > 0; ns -= 63)
|
|
150
|
+ for (;ns > 0; ns -= 63)
|
91
|
151
|
asm volatile("nop"::);
|
92
|
152
|
}
|