Skip to content

Commit 1b0b3e9

Browse files
authored
Merge pull request #165 from laamaa/refactor/code_cleanup
Refactor/code cleanup
2 parents 621a093 + 173be3c commit 1b0b3e9

27 files changed

+408
-421
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.15)
22

33
project(m8c LANGUAGES C)
44

5+
set(CMAKE_C_FLAGS "-O2 -Wall -Wextra")
6+
57
set(APP_NAME m8c)
68

79
find_package(PkgConfig REQUIRED)

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ DEPS = src/serial.h src/slip.h src/command.h src/render.h src/ini.h src/config.h
88
INCLUDES = $(shell pkg-config --libs sdl2 libserialport | sed 's/-mwindows//')
99

1010
#Set any compiler flags you want to use (e.g. -I/usr/include/somefolder `pkg-config --cflags gtk+-3.0` ), or leave blank
11-
local_CFLAGS = $(CFLAGS) $(shell pkg-config --cflags sdl2 libserialport) -Wall -O2 -pipe -I.
11+
local_CFLAGS = $(CFLAGS) $(shell pkg-config --cflags sdl2 libserialport) -Wall -Wextra -O2 -pipe -I.
1212

1313
#Set the compiler you are using ( gcc for C or g++ for C++ )
1414
CC = gcc

src/SDL2_inprint.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ extern void kill_inline_font(void);
1212

1313
extern void inrenderer(SDL_Renderer *renderer);
1414
extern void infont(SDL_Texture *font);
15-
extern void incolor1(SDL_Color *color);
16-
extern void incolor(Uint32 color, Uint32 unused); /* Color must be in 0x00RRGGBB format ! */
15+
extern void incolor1(const SDL_Color *color);
16+
extern void incolor(Uint32 color); /* Color must be in 0x00RRGGBB format ! */
1717
extern void inprint(SDL_Renderer *dst, const char *str, Uint32 x, Uint32 y, Uint32 fgcolor,
1818
Uint32 bgcolor);
1919

src/audio.c

+42-45
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static SDL_AudioDeviceID devid_out = 0;
1111
static unsigned int audio_paused = 0;
1212
static unsigned int audio_initialized = 0;
1313

14-
void toggle_audio(unsigned int audio_buffer_size, const char *output_device_name) {
14+
void toggle_audio(const unsigned int audio_buffer_size, const char *output_device_name) {
1515
if (!audio_initialized) {
1616
audio_init(audio_buffer_size, output_device_name);
1717
return;
@@ -22,66 +22,63 @@ void toggle_audio(unsigned int audio_buffer_size, const char *output_device_name
2222
SDL_Log(audio_paused ? "Audio paused" : "Audio resumed");
2323
}
2424

25-
void audio_cb_in(void *userdata, uint8_t *stream, int len) {
25+
void audio_cb_in(void *, uint8_t *stream, int len) {
2626
SDL_QueueAudio(devid_out, stream, len);
2727
}
2828

29-
int audio_init(unsigned int audio_buffer_size, const char *output_device_name) {
29+
int audio_init(const unsigned int audio_buffer_size, const char *output_device_name) {
3030

31-
int i = 0;
3231
int m8_device_id = -1;
33-
int devcount_in = 0; // audio input device count
3432

3533
// wait for system to initialize possible new audio devices
3634
SDL_Delay(500);
3735

38-
devcount_in = SDL_GetNumAudioDevices(SDL_TRUE);
36+
const int devcount_in = SDL_GetNumAudioDevices(SDL_TRUE);
3937

4038
if (devcount_in < 1) {
4139
SDL_Log("No audio capture devices, SDL Error: %s", SDL_GetError());
4240
return 0;
43-
} else {
44-
for (i = 0; i < devcount_in; i++) {
45-
// Check if input device exists before doing anything else
46-
SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", SDL_GetAudioDeviceName(i, SDL_TRUE));
47-
if (SDL_strstr(SDL_GetAudioDeviceName(i, SDL_TRUE), "M8") != NULL) {
48-
SDL_Log("M8 Audio Input device found: %s", SDL_GetAudioDeviceName(i, SDL_TRUE));
49-
m8_device_id = i;
50-
}
51-
}
52-
if (m8_device_id == -1) {
53-
// forget about it
54-
SDL_Log("Cannot find M8 audio input device");
55-
return 0;
41+
}
42+
for (int i = 0; i < devcount_in; i++) {
43+
// Check if input device exists before doing anything else
44+
SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", SDL_GetAudioDeviceName(i, SDL_TRUE));
45+
if (SDL_strstr(SDL_GetAudioDeviceName(i, SDL_TRUE), "M8") != NULL) {
46+
SDL_Log("M8 Audio Input device found: %s", SDL_GetAudioDeviceName(i, SDL_TRUE));
47+
m8_device_id = i;
5648
}
49+
}
50+
if (m8_device_id == -1) {
51+
// forget about it
52+
SDL_Log("Cannot find M8 audio input device");
53+
return 0;
54+
}
5755

58-
SDL_AudioSpec want_in, have_in, want_out, have_out;
59-
60-
// Open output device first to avoid possible Directsound errors
61-
SDL_zero(want_out);
62-
want_out.freq = 44100;
63-
want_out.format = AUDIO_S16;
64-
want_out.channels = 2;
65-
want_out.samples = audio_buffer_size;
66-
devid_out = SDL_OpenAudioDevice(output_device_name, 0, &want_out, &have_out,
67-
SDL_AUDIO_ALLOW_ANY_CHANGE);
68-
if (devid_out == 0) {
69-
SDL_Log("Failed to open output: %s", SDL_GetError());
70-
return 0;
71-
}
56+
SDL_AudioSpec want_in, have_in, want_out, have_out;
57+
58+
// Open output device first to avoid possible Directsound errors
59+
SDL_zero(want_out);
60+
want_out.freq = 44100;
61+
want_out.format = AUDIO_S16;
62+
want_out.channels = 2;
63+
want_out.samples = audio_buffer_size;
64+
devid_out =
65+
SDL_OpenAudioDevice(output_device_name, 0, &want_out, &have_out, SDL_AUDIO_ALLOW_ANY_CHANGE);
66+
if (devid_out == 0) {
67+
SDL_Log("Failed to open output: %s", SDL_GetError());
68+
return 0;
69+
}
7270

73-
SDL_zero(want_in);
74-
want_in.freq = 44100;
75-
want_in.format = AUDIO_S16;
76-
want_in.channels = 2;
77-
want_in.samples = audio_buffer_size;
78-
want_in.callback = audio_cb_in;
79-
devid_in = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(m8_device_id, SDL_TRUE), SDL_TRUE,
80-
&want_in, &have_in, SDL_AUDIO_ALLOW_ANY_CHANGE);
81-
if (devid_in == 0) {
82-
SDL_Log("Failed to open M8 audio device, SDL Error: %s", SDL_GetError());
83-
return 0;
84-
}
71+
SDL_zero(want_in);
72+
want_in.freq = 44100;
73+
want_in.format = AUDIO_S16;
74+
want_in.channels = 2;
75+
want_in.samples = audio_buffer_size;
76+
want_in.callback = audio_cb_in;
77+
devid_in = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(m8_device_id, SDL_TRUE), SDL_TRUE, &want_in,
78+
&have_in, SDL_AUDIO_ALLOW_ANY_CHANGE);
79+
if (devid_in == 0) {
80+
SDL_Log("Failed to open M8 audio device, SDL Error: %s", SDL_GetError());
81+
return 0;
8582
}
8683

8784
// Start audio processing

src/command.c

+58-77
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "render.h"
88

99
// Convert 2 little-endian 8bit bytes to a 16bit integer
10-
static uint16_t decodeInt16(uint8_t *data, uint8_t start) {
10+
static uint16_t decodeInt16(const uint8_t *data, const uint8_t start) {
1111
return data[start] | (uint16_t)data[start + 1] << 8;
1212
}
1313

@@ -26,7 +26,7 @@ enum m8_command_bytes {
2626
system_info_command_datalength = 6
2727
};
2828

29-
static inline void dump_packet(uint32_t size, uint8_t *recv_buf) {
29+
static void dump_packet(const uint32_t size, const uint8_t *recv_buf) {
3030
for (uint16_t a = 0; a < size; a++) {
3131
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "0x%02X ", recv_buf[a]);
3232
}
@@ -42,8 +42,7 @@ int process_command(uint8_t *data, uint32_t size) {
4242

4343
switch (recv_buf[0]) {
4444

45-
case draw_rectangle_command:
46-
45+
case draw_rectangle_command: {
4746
if (size < draw_rectangle_command_min_datalength ||
4847
size > draw_rectangle_command_max_datalength) {
4948
SDL_LogError(SDL_LOG_CATEGORY_ERROR,
@@ -52,74 +51,64 @@ int process_command(uint8_t *data, uint32_t size) {
5251
size);
5352
dump_packet(size, recv_buf);
5453
return 0;
55-
break;
56-
} else {
57-
58-
/* Support variable sized rectangle commands
59-
If colors are omitted, the last drawn color should be used
60-
If size is omitted, the size should be 1x1 pixels
61-
So basically the command can be 5, 8, 9 or 12 bytes long */
62-
63-
static struct draw_rectangle_command rectcmd;
64-
65-
rectcmd.pos.x = decodeInt16(recv_buf, 1);
66-
rectcmd.pos.y = decodeInt16(recv_buf, 3);
67-
68-
switch (size) {
69-
case 5:
70-
rectcmd.size.width = 1;
71-
rectcmd.size.height = 1;
72-
break;
73-
case 8:
74-
rectcmd.size.width = 1;
75-
rectcmd.size.height = 1;
76-
rectcmd.color.r = recv_buf[5];
77-
rectcmd.color.g = recv_buf[6];
78-
rectcmd.color.b = recv_buf[7];
79-
break;
80-
case 9:
81-
rectcmd.size.width = decodeInt16(recv_buf, 5);
82-
rectcmd.size.height = decodeInt16(recv_buf, 7);
83-
break;
84-
default:
85-
rectcmd.size.width = decodeInt16(recv_buf, 5);
86-
rectcmd.size.height = decodeInt16(recv_buf, 7);
87-
rectcmd.color.r = recv_buf[9];
88-
rectcmd.color.g = recv_buf[10];
89-
rectcmd.color.b = recv_buf[11];
90-
break;
91-
}
92-
93-
draw_rectangle(&rectcmd);
94-
return 1;
9554
}
55+
/* Support variable sized rectangle commands
56+
If colors are omitted, the last drawn color should be used
57+
If size is omitted, the size should be 1x1 pixels
58+
So basically the command can be 5, 8, 9 or 12 bytes long */
9659

97-
break;
60+
static struct draw_rectangle_command rectcmd;
9861

99-
case draw_character_command:
62+
rectcmd.pos.x = decodeInt16(recv_buf, 1);
63+
rectcmd.pos.y = decodeInt16(recv_buf, 3);
10064

65+
switch (size) {
66+
case 5:
67+
rectcmd.size.width = 1;
68+
rectcmd.size.height = 1;
69+
break;
70+
case 8:
71+
rectcmd.size.width = 1;
72+
rectcmd.size.height = 1;
73+
rectcmd.color.r = recv_buf[5];
74+
rectcmd.color.g = recv_buf[6];
75+
rectcmd.color.b = recv_buf[7];
76+
break;
77+
case 9:
78+
rectcmd.size.width = decodeInt16(recv_buf, 5);
79+
rectcmd.size.height = decodeInt16(recv_buf, 7);
80+
break;
81+
default:
82+
rectcmd.size.width = decodeInt16(recv_buf, 5);
83+
rectcmd.size.height = decodeInt16(recv_buf, 7);
84+
rectcmd.color.r = recv_buf[9];
85+
rectcmd.color.g = recv_buf[10];
86+
rectcmd.color.b = recv_buf[11];
87+
break;
88+
}
89+
90+
draw_rectangle(&rectcmd);
91+
return 1;
92+
}
93+
94+
case draw_character_command: {
10195
if (size != draw_character_command_datalength) {
10296
SDL_LogError(SDL_LOG_CATEGORY_ERROR,
10397
"Invalid draw character packet: expected length %d, got %d",
10498
draw_character_command_datalength, size);
10599
dump_packet(size, recv_buf);
106100
return 0;
107-
break;
108-
} else {
109-
110-
struct draw_character_command charcmd = {
111-
recv_buf[1], // char
112-
{decodeInt16(recv_buf, 2), decodeInt16(recv_buf, 4)}, // position x/y
113-
{recv_buf[6], recv_buf[7], recv_buf[8]}, // foreground r/g/b
114-
{recv_buf[9], recv_buf[10], recv_buf[11]}}; // background r/g/b
115-
draw_character(&charcmd);
116-
return 1;
117101
}
102+
struct draw_character_command charcmd = {
103+
recv_buf[1], // char
104+
{decodeInt16(recv_buf, 2), decodeInt16(recv_buf, 4)}, // position x/y
105+
{recv_buf[6], recv_buf[7], recv_buf[8]}, // foreground r/g/b
106+
{recv_buf[9], recv_buf[10], recv_buf[11]}}; // background r/g/b
107+
draw_character(&charcmd);
108+
return 1;
109+
}
118110

119-
break;
120-
121-
case draw_oscilloscope_waveform_command:
122-
111+
case draw_oscilloscope_waveform_command: {
123112
if (size < draw_oscilloscope_waveform_command_mindatalength ||
124113
size > draw_oscilloscope_waveform_command_maxdatalength) {
125114
SDL_LogError(SDL_LOG_CATEGORY_ERROR,
@@ -128,21 +117,17 @@ int process_command(uint8_t *data, uint32_t size) {
128117
draw_oscilloscope_waveform_command_maxdatalength, size);
129118
dump_packet(size, recv_buf);
130119
return 0;
131-
break;
132-
} else {
133-
134-
struct draw_oscilloscope_waveform_command osccmd;
135-
136-
osccmd.color = (struct color){recv_buf[1], recv_buf[2], recv_buf[3]}; // color r/g/b
137-
memcpy(osccmd.waveform, &recv_buf[4], size - 4);
120+
}
121+
struct draw_oscilloscope_waveform_command osccmd;
138122

139-
osccmd.waveform_size = size - 4;
123+
osccmd.color = (struct color){recv_buf[1], recv_buf[2], recv_buf[3]}; // color r/g/b
124+
memcpy(osccmd.waveform, &recv_buf[4], size - 4);
140125

141-
draw_waveform(&osccmd);
142-
return 1;
143-
}
126+
osccmd.waveform_size = size - 4;
144127

145-
break;
128+
draw_waveform(&osccmd);
129+
return 1;
130+
}
146131

147132
case joypad_keypressedstate_command: {
148133
if (size != joypad_keypressedstate_command_datalength) {
@@ -152,12 +137,10 @@ int process_command(uint8_t *data, uint32_t size) {
152137
joypad_keypressedstate_command_datalength, size);
153138
dump_packet(size, recv_buf);
154139
return 0;
155-
break;
156140
}
157141

158142
// nothing is done with joypad key pressed packets for now
159143
return 1;
160-
break;
161144
}
162145

163146
case system_info_command: {
@@ -188,14 +171,12 @@ int process_command(uint8_t *data, uint32_t size) {
188171
set_font_mode(recv_buf[5]);
189172

190173
return 1;
191-
break;
192174
}
193175

194176
default:
195-
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Invalid packet\n");
177+
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Invalid packet");
196178
dump_packet(size, recv_buf);
197179
return 0;
198-
break;
199180
}
200181
return 1;
201182
}

0 commit comments

Comments
 (0)