21
21
#include <util/delay.h>
22
22
#include <avr/interrupt.h>
23
23
#include <avr/pgmspace.h>
24
+ #include <string.h>
24
25
25
26
#include "serial.h"
26
27
#include "sdimg.h"
27
28
#include "i2c.h"
28
29
#include "oled.h"
29
30
#include "sd.h"
30
31
31
- volatile uint8_t fail_state = 0 ;
32
- volatile uint16_t n_frames = 0 ;
33
- volatile uint16_t data_ptr = 0 ;
34
- volatile uint8_t sd_type = 0 ;
35
- volatile uint8_t done = 0 ;
36
- volatile uint8_t loop = 0 ;
32
+ // Header, prvi dio prvih 512 bajtova
33
+ typedef struct _header header ;
34
+ struct _header
35
+ {
36
+ uint8_t h_width ;
37
+ uint8_t h_height ;
38
+ uint16_t h_frames ;
39
+ uint8_t h_header_text [7 ];
40
+ uint8_t h_loop ;
41
+ };
42
+
43
+ typedef struct _playbackinfo playbackinfo ;
44
+ struct _playbackinfo
45
+ {
46
+ uint8_t v_width ;
47
+ uint8_t v_height ;
48
+ uint16_t v_frames ;
49
+ uint8_t v_loop ;
50
+ uint8_t v_sd_type ;
51
+ uint8_t v_done ;
52
+ uint8_t v_fail_state ;
53
+ };
37
54
38
55
uint8_t res [5 ], token ;
39
- uint8_t header_text [] = {0x62 , 0x61 , 0x64 , 0x75 , 0x69 , 0x6E , 0x6F }; // "baduino"
40
56
41
- void error_image ()
57
+ void error_image (const uint8_t fail_state )
42
58
{
43
59
if (fail_state == 0 )
44
60
{
@@ -58,76 +74,89 @@ void error_image()
58
74
}
59
75
}
60
76
61
- void read_header ()
77
+ void check_sd (playbackinfo * videoInfo )
78
+ {
79
+ videoInfo -> v_fail_state = 0 ;
80
+ if (SD_init (& (videoInfo -> v_sd_type )) == SD_SUCCESS )
81
+ {
82
+ //uart_putstr("Success!\n");
83
+ }
84
+ else
85
+ {
86
+ //uart_putstr("No SD card\n");
87
+ videoInfo -> v_fail_state = 1 ;
88
+ }
89
+ return ;
90
+ }
91
+
92
+ void read_header (playbackinfo * videoInfo )
62
93
{
63
- if (done )
94
+ if (videoInfo -> v_done )
64
95
{
65
96
return ;
66
97
}
98
+
67
99
res [0 ] = SD_readSingleBlock (0 , frame_buffer , & token ); // read first 512 bytes
100
+
68
101
// parse header
69
102
if (SD_R1_NO_ERROR (res [0 ]) && (token == 0xFE ))
70
103
{
71
- // "baduino" in hex from offset 4
104
+ header v_header ; // Inicijaliziraj header
105
+ memcpy (& v_header , & frame_buffer , sizeof (header ));
106
+
107
+ uart_putstr ("Looping: " );
108
+ uart_putint (v_header .h_loop );
109
+ uart_putstr ("\nResolution: " );
110
+ uart_putint (v_header .h_width );
111
+ uart_putchar ('x' );
112
+ uart_putint (v_header .h_height * 8 );
113
+ uart_putstr ("\nFrames: " );
114
+ uart_putint (v_header .h_frames );
115
+ uart_putstr ("\n\n" );
116
+
117
+ uint8_t header_text [] = {0x62 , 0x61 , 0x64 , 0x75 , 0x69 , 0x6E , 0x6F }; // "baduino"
118
+
119
+ // Invalid header string
72
120
for (int i = 0 ; i < 7 ; i ++ )
73
121
{
74
-
75
- if (frame_buffer [i + 4 ] != header_text [i ])
122
+ if (v_header .h_header_text [i ] != header_text [i ])
76
123
{
77
- //uart_putstr("Invalid file format\n");
78
- fail_state = 3 ;
124
+ videoInfo -> v_fail_state = 3 ;
79
125
return ;
80
126
}
81
127
}
82
128
83
- // 0xF00D on the last 2 bytes for shits and giggles
84
- if (frame_buffer [ 510 ] != 0xF0 || frame_buffer [ 511 ] != 0x0D )
129
+ // Invalid resolution
130
+ if (v_header . h_width != 128 || v_header . h_height != 8 )
85
131
{
86
- //uart_putstr("Invalid file format\n");
87
- fail_state = 3 ;
132
+ videoInfo -> v_fail_state = 3 ;
88
133
return ;
89
134
}
90
135
91
- // first 4 bytes - width in pixels, height in pages, no. of frames (2 bytes)
92
- data_ptr ++ ;
93
- if (frame_buffer [0 ] != 128 || frame_buffer [1 ] != 8 )
94
- {
95
- //uart_putstr("Invalid resolution\n");
96
- fail_state = 3 ;
97
- return ;
98
- }
99
- n_frames = 0 | (frame_buffer [2 ] << 8 );
100
- n_frames |= frame_buffer [3 ];
101
- loop = frame_buffer [11 ];
102
- uart_putstr ("Looping: " );
103
- uart_putint (loop );
104
- uart_putstr ("\nResolution: " );
105
- uart_putint (frame_buffer [0 ]);
106
- uart_putchar ('x' );
107
- uart_putint (frame_buffer [1 ] * 8 );
108
- uart_putstr ("\nFrames: " );
109
- uart_putint (n_frames );
110
- uart_putstr ("\n\n" );
136
+ videoInfo -> v_height = v_header .h_height ;
137
+ videoInfo -> v_width = v_header .h_width ;
138
+ videoInfo -> v_loop = v_header .h_loop ;
139
+ videoInfo -> v_frames = v_header .h_frames ;
111
140
}
112
141
else
113
142
{
114
143
//UART_puts("Error reading sector\n");
115
- fail_state = 2 ;
144
+ videoInfo -> v_fail_state = 2 ;
116
145
return ;
117
146
}
118
147
}
119
148
120
- void read_frames ()
149
+ void read_frames (playbackinfo * videoInfo )
121
150
{
122
- if (done )
151
+ if (videoInfo -> v_done )
123
152
{
124
153
return ;
125
154
}
126
155
clear_display ();
127
156
do
128
157
{
129
158
// SD v2 uses byte offset (za ovo sam propisao krv)
130
- if (sd_type == SD2 )
159
+ if (videoInfo -> v_sd_type == SD2 )
131
160
{
132
161
res [0 ] = SD_startMultiBlockRead (512 );
133
162
}
@@ -138,7 +167,7 @@ void read_frames()
138
167
}
139
168
140
169
// Reading block data directly into the frame buffer
141
- for (int i = 0 ; i < n_frames ; i ++ )
170
+ for (int i = 0 ; i < videoInfo -> v_frames ; i ++ )
142
171
{
143
172
//res[0] = SD_readMultipleBlocks(frame_buffer, &token);
144
173
SD_readMultipleBlocks (frame_buffer , & token );
@@ -150,77 +179,64 @@ void read_frames()
150
179
else
151
180
{
152
181
UART_puts ("Error multireading sector\n" );
153
- fail_state = 2 ;
182
+ videoInfo -> v_fail_state = 2 ;
154
183
return ;
155
184
}
156
185
}
157
186
158
187
SD_stopMultiBlockRead ();
159
- } while (loop );
160
- }
161
-
162
- void check_sd ()
163
- {
164
- fail_state = 0 ;
165
- if (SD_init () == SD_SUCCESS )
166
- {
167
- //uart_putstr("Success!\n");
168
- }
169
- else
170
- {
171
- //uart_putstr("No SD card\n");
172
- fail_state = 1 ;
173
- return ;
174
- }
188
+ } while (videoInfo -> v_loop );
175
189
}
176
190
177
191
int main ()
178
192
{
179
- fail_state = 0 ;
180
193
sei ();
181
194
182
195
i2c_init (0x3C ); // hex 3C je adresa OLED-a
183
196
display_init ();
184
197
horizonal_mode ();
185
- clear_display ();
186
- send_command (0xaf );
198
+ clear_display (); // Nuliraj oled vram
199
+ send_command (0xaf ); // Upali oled
187
200
_delay_ms (50 );
188
201
189
202
uart_init ();
190
203
// initialize SPI
191
204
SPI_init ();
192
205
_delay_ms (10 );
193
206
207
+ playbackinfo videoInfo ;
208
+ memset (& videoInfo , 0 , sizeof (playbackinfo ));
209
+
194
210
// hotswap yaaaaay
195
211
while (1 )
196
212
{
197
- check_sd ();
198
- if (fail_state )
213
+ check_sd (& videoInfo );
214
+ if (videoInfo . v_fail_state )
199
215
{
200
- done = 0 ;
201
- error_image ();
216
+ videoInfo . v_done = 0 ;
217
+ error_image (videoInfo . v_fail_state );
202
218
_delay_ms (100 );
203
219
continue ;
204
220
}
205
- read_header ();
221
+ read_header (& videoInfo );
206
222
207
- if (fail_state )
223
+ if (videoInfo . v_fail_state )
208
224
{
209
- error_image ();
225
+ error_image (videoInfo . v_fail_state );
210
226
_delay_ms (100 );
211
227
continue ;
212
228
}
213
- read_frames ();
229
+ read_frames (& videoInfo );
214
230
215
- if (fail_state )
231
+ if (videoInfo . v_fail_state )
216
232
{
217
- error_image ();
233
+ error_image (videoInfo . v_fail_state );
218
234
_delay_ms (2000 );
219
235
continue ;
220
236
}
221
237
else
222
238
{
223
- done = 1 ;
239
+ videoInfo . v_done = 1 ;
224
240
}
225
241
_delay_ms (200 );
226
242
}
0 commit comments