Skip to content

Commit b201503

Browse files
committed
Code cleanup
Bolje definirao header Promijenio iz big endian u little endian Smanjio broj globalnih varijabli
1 parent a881540 commit b201503

File tree

7 files changed

+104
-92
lines changed

7 files changed

+104
-92
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
badapple
33
halfframes
44
video
5+
prezentacija
56
*.txt
67
*.img
78
*.bin

arduino/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PROGRAMMER = arduino
44
PORT = /dev/ttyUSB0
55
BAUD = 57600
66
FILENAME = main
7-
COMPILE = avr-gcc -Wall -Wpedantic -Wshadow -Os -mmcu=$(DEVICE)
7+
COMPILE = avr-gcc -pipe -Wall -Wpedantic -Wshadow -Os -mmcu=$(DEVICE)
88

99

1010
all: build upload clean
@@ -20,7 +20,7 @@ build:
2020
avr-size --format=avr --mcu=$(DEVICE) $(FILENAME).elf
2121

2222
upload:
23-
avrdude -p $(DEVICE) -c $(PROGRAMMER) -P $(PORT) -b $(BAUD) -U flash:w:$(FILENAME).hex:i
23+
avrdude -p $(DEVICE) -c $(PROGRAMMER) -P $(PORT) -b $(BAUD) -D -U flash:w:$(FILENAME).hex:i
2424

2525
clean:
2626
rm -f *.elf

arduino/i2c.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <util/twi.h> // I2C library
2323

2424
extern volatile uint8_t i2c_address;
25-
extern volatile uint8_t received;
2625

2726
void i2c_init(uint8_t add);
2827

arduino/main.c

Lines changed: 91 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,40 @@
2121
#include <util/delay.h>
2222
#include <avr/interrupt.h>
2323
#include <avr/pgmspace.h>
24+
#include <string.h>
2425

2526
#include "serial.h"
2627
#include "sdimg.h"
2728
#include "i2c.h"
2829
#include "oled.h"
2930
#include "sd.h"
3031

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+
};
3754

3855
uint8_t res[5], token;
39-
uint8_t header_text[] = {0x62, 0x61, 0x64, 0x75, 0x69, 0x6E, 0x6F}; // "baduino"
4056

41-
void error_image()
57+
void error_image(const uint8_t fail_state)
4258
{
4359
if (fail_state == 0)
4460
{
@@ -58,76 +74,89 @@ void error_image()
5874
}
5975
}
6076

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)
6293
{
63-
if (done)
94+
if (videoInfo->v_done)
6495
{
6596
return;
6697
}
98+
6799
res[0] = SD_readSingleBlock(0, frame_buffer, &token); // read first 512 bytes
100+
68101
// parse header
69102
if (SD_R1_NO_ERROR(res[0]) && (token == 0xFE))
70103
{
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
72120
for (int i = 0; i < 7; i++)
73121
{
74-
75-
if (frame_buffer[i + 4] != header_text[i])
122+
if (v_header.h_header_text[i] != header_text[i])
76123
{
77-
//uart_putstr("Invalid file format\n");
78-
fail_state = 3;
124+
videoInfo->v_fail_state = 3;
79125
return;
80126
}
81127
}
82128

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)
85131
{
86-
//uart_putstr("Invalid file format\n");
87-
fail_state = 3;
132+
videoInfo->v_fail_state = 3;
88133
return;
89134
}
90135

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;
111140
}
112141
else
113142
{
114143
//UART_puts("Error reading sector\n");
115-
fail_state = 2;
144+
videoInfo->v_fail_state = 2;
116145
return;
117146
}
118147
}
119148

120-
void read_frames()
149+
void read_frames(playbackinfo *videoInfo)
121150
{
122-
if (done)
151+
if (videoInfo->v_done)
123152
{
124153
return;
125154
}
126155
clear_display();
127156
do
128157
{
129158
// SD v2 uses byte offset (za ovo sam propisao krv)
130-
if (sd_type == SD2)
159+
if (videoInfo->v_sd_type == SD2)
131160
{
132161
res[0] = SD_startMultiBlockRead(512);
133162
}
@@ -138,7 +167,7 @@ void read_frames()
138167
}
139168

140169
// 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++)
142171
{
143172
//res[0] = SD_readMultipleBlocks(frame_buffer, &token);
144173
SD_readMultipleBlocks(frame_buffer, &token);
@@ -150,77 +179,64 @@ void read_frames()
150179
else
151180
{
152181
UART_puts("Error multireading sector\n");
153-
fail_state = 2;
182+
videoInfo->v_fail_state = 2;
154183
return;
155184
}
156185
}
157186

158187
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);
175189
}
176190

177191
int main()
178192
{
179-
fail_state = 0;
180193
sei();
181194

182195
i2c_init(0x3C); // hex 3C je adresa OLED-a
183196
display_init();
184197
horizonal_mode();
185-
clear_display();
186-
send_command(0xaf);
198+
clear_display(); // Nuliraj oled vram
199+
send_command(0xaf); // Upali oled
187200
_delay_ms(50);
188201

189202
uart_init();
190203
// initialize SPI
191204
SPI_init();
192205
_delay_ms(10);
193206

207+
playbackinfo videoInfo;
208+
memset(&videoInfo, 0, sizeof(playbackinfo));
209+
194210
// hotswap yaaaaay
195211
while (1)
196212
{
197-
check_sd();
198-
if (fail_state)
213+
check_sd(&videoInfo);
214+
if (videoInfo.v_fail_state)
199215
{
200-
done = 0;
201-
error_image();
216+
videoInfo.v_done = 0;
217+
error_image(videoInfo.v_fail_state);
202218
_delay_ms(100);
203219
continue;
204220
}
205-
read_header();
221+
read_header(&videoInfo);
206222

207-
if (fail_state)
223+
if (videoInfo.v_fail_state)
208224
{
209-
error_image();
225+
error_image(videoInfo.v_fail_state);
210226
_delay_ms(100);
211227
continue;
212228
}
213-
read_frames();
229+
read_frames(&videoInfo);
214230

215-
if (fail_state)
231+
if (videoInfo.v_fail_state)
216232
{
217-
error_image();
233+
error_image(videoInfo.v_fail_state);
218234
_delay_ms(2000);
219235
continue;
220236
}
221237
else
222238
{
223-
done = 1;
239+
videoInfo.v_done = 1;
224240
}
225241
_delay_ms(200);
226242
}

arduino/sd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ uint8_t SD_sendOpCond()
215215
return res1;
216216
}
217217

218-
uint8_t SD_init()
218+
uint8_t SD_init(uint8_t *sd_type)
219219
{
220220
uint8_t res[5], cmdAttempts = 0;
221221

@@ -273,11 +273,11 @@ uint8_t SD_init()
273273

274274
if (CCS_VAL(res[1]))
275275
{
276-
sd_type = SDHC;
276+
*sd_type = SDHC;
277277
}
278278
else
279279
{
280-
sd_type = SD2;
280+
*sd_type = SD2;
281281
}
282282

283283
return SD_SUCCESS;

arduino/sd.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@
126126
#define SD_MAX_READ_ATTEMPTS 1563
127127
#define SD_R1_NO_ERROR(X) X < 0x02
128128

129-
extern volatile uint8_t sd_type;
130-
131129
// SPI functions
132130
void SPI_init(void);
133131
uint8_t SPI_transfer(uint8_t data);
@@ -154,7 +152,7 @@ uint8_t SD_sendApp();
154152

155153
uint8_t SD_sendOpCond();
156154

157-
uint8_t SD_init();
155+
uint8_t SD_init(uint8_t *sd_type);
158156

159157
uint8_t SD_readSingleBlock(uint32_t addr, uint8_t *buf, uint8_t *token);
160158

tools/enc.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,17 @@ def writeHeader(directory, filenames, outFileName, loopNum: int):
6565
getDimensions(f"{directory}/{filenames[0]}")
6666
writebuffer = [width, height // 8] # 2
6767
for byte in writebuffer:
68-
outFile.write(byte.to_bytes(1, byteorder='big'))
68+
outFile.write(byte.to_bytes(1, byteorder='little'))
6969
byte_num += 1
70-
outFile.write(len(filenames).to_bytes(2, 'big')) # 2
70+
outFile.write(len(filenames).to_bytes(2, 'little')) # 2
7171
byte_num += 2
7272
header_str = str.encode("baduino")
7373
outFile.write(header_str)
7474
byte_num += len(header_str)
75-
outFile.write(loopNum.to_bytes(1, 'big'))
75+
outFile.write(loopNum.to_bytes(1, 'little'))
7676
byte_num += 1
77-
for i in range(510 - byte_num):
78-
outFile.write((0).to_bytes(1, 'big'))
79-
outFile.write(0xF0.to_bytes(1, 'big'))
80-
outFile.write(0x0D.to_bytes(1, 'big'))
77+
for i in range(512 - byte_num):
78+
outFile.write((0).to_bytes(1, 'little'))
8179

8280
return
8381

@@ -151,7 +149,7 @@ def processFrames(directory, filenames, outFileName):
151149

152150
# Provjeravanje argumenata
153151
if len(argumenti) < 3:
154-
print("Argumenti nisu dobri")
152+
print("Argumenti nisu dobri\n ./enc.py <source directory> <output file name> [loop (1 ili 0, default 0)]")
155153
exit()
156154

157155
if len(argumenti) < 4:

0 commit comments

Comments
 (0)