Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/code cleanup #165

Merged
merged 4 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.15)

project(m8c LANGUAGES C)

set(CMAKE_C_FLAGS "-O2 -Wall -Wextra")

set(APP_NAME m8c)

find_package(PkgConfig REQUIRED)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DEPS = src/serial.h src/slip.h src/command.h src/render.h src/ini.h src/config.h
INCLUDES = $(shell pkg-config --libs sdl2 libserialport | sed 's/-mwindows//')

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

#Set the compiler you are using ( gcc for C or g++ for C++ )
CC = gcc
Expand Down
4 changes: 2 additions & 2 deletions src/SDL2_inprint.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ extern void kill_inline_font(void);

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

Expand Down
87 changes: 42 additions & 45 deletions src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static SDL_AudioDeviceID devid_out = 0;
static unsigned int audio_paused = 0;
static unsigned int audio_initialized = 0;

void toggle_audio(unsigned int audio_buffer_size, const char *output_device_name) {
void toggle_audio(const unsigned int audio_buffer_size, const char *output_device_name) {
if (!audio_initialized) {
audio_init(audio_buffer_size, output_device_name);
return;
Expand All @@ -22,66 +22,63 @@ void toggle_audio(unsigned int audio_buffer_size, const char *output_device_name
SDL_Log(audio_paused ? "Audio paused" : "Audio resumed");
}

void audio_cb_in(void *userdata, uint8_t *stream, int len) {
void audio_cb_in(void *, uint8_t *stream, int len) {
SDL_QueueAudio(devid_out, stream, len);
}

int audio_init(unsigned int audio_buffer_size, const char *output_device_name) {
int audio_init(const unsigned int audio_buffer_size, const char *output_device_name) {

int i = 0;
int m8_device_id = -1;
int devcount_in = 0; // audio input device count

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

devcount_in = SDL_GetNumAudioDevices(SDL_TRUE);
const int devcount_in = SDL_GetNumAudioDevices(SDL_TRUE);

if (devcount_in < 1) {
SDL_Log("No audio capture devices, SDL Error: %s", SDL_GetError());
return 0;
} else {
for (i = 0; i < devcount_in; i++) {
// Check if input device exists before doing anything else
SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", SDL_GetAudioDeviceName(i, SDL_TRUE));
if (SDL_strstr(SDL_GetAudioDeviceName(i, SDL_TRUE), "M8") != NULL) {
SDL_Log("M8 Audio Input device found: %s", SDL_GetAudioDeviceName(i, SDL_TRUE));
m8_device_id = i;
}
}
if (m8_device_id == -1) {
// forget about it
SDL_Log("Cannot find M8 audio input device");
return 0;
}
for (int i = 0; i < devcount_in; i++) {
// Check if input device exists before doing anything else
SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", SDL_GetAudioDeviceName(i, SDL_TRUE));
if (SDL_strstr(SDL_GetAudioDeviceName(i, SDL_TRUE), "M8") != NULL) {
SDL_Log("M8 Audio Input device found: %s", SDL_GetAudioDeviceName(i, SDL_TRUE));
m8_device_id = i;
}
}
if (m8_device_id == -1) {
// forget about it
SDL_Log("Cannot find M8 audio input device");
return 0;
}

SDL_AudioSpec want_in, have_in, want_out, have_out;

// Open output device first to avoid possible Directsound errors
SDL_zero(want_out);
want_out.freq = 44100;
want_out.format = AUDIO_S16;
want_out.channels = 2;
want_out.samples = audio_buffer_size;
devid_out = SDL_OpenAudioDevice(output_device_name, 0, &want_out, &have_out,
SDL_AUDIO_ALLOW_ANY_CHANGE);
if (devid_out == 0) {
SDL_Log("Failed to open output: %s", SDL_GetError());
return 0;
}
SDL_AudioSpec want_in, have_in, want_out, have_out;

// Open output device first to avoid possible Directsound errors
SDL_zero(want_out);
want_out.freq = 44100;
want_out.format = AUDIO_S16;
want_out.channels = 2;
want_out.samples = audio_buffer_size;
devid_out =
SDL_OpenAudioDevice(output_device_name, 0, &want_out, &have_out, SDL_AUDIO_ALLOW_ANY_CHANGE);
if (devid_out == 0) {
SDL_Log("Failed to open output: %s", SDL_GetError());
return 0;
}

SDL_zero(want_in);
want_in.freq = 44100;
want_in.format = AUDIO_S16;
want_in.channels = 2;
want_in.samples = audio_buffer_size;
want_in.callback = audio_cb_in;
devid_in = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(m8_device_id, SDL_TRUE), SDL_TRUE,
&want_in, &have_in, SDL_AUDIO_ALLOW_ANY_CHANGE);
if (devid_in == 0) {
SDL_Log("Failed to open M8 audio device, SDL Error: %s", SDL_GetError());
return 0;
}
SDL_zero(want_in);
want_in.freq = 44100;
want_in.format = AUDIO_S16;
want_in.channels = 2;
want_in.samples = audio_buffer_size;
want_in.callback = audio_cb_in;
devid_in = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(m8_device_id, SDL_TRUE), SDL_TRUE, &want_in,
&have_in, SDL_AUDIO_ALLOW_ANY_CHANGE);
if (devid_in == 0) {
SDL_Log("Failed to open M8 audio device, SDL Error: %s", SDL_GetError());
return 0;
}

// Start audio processing
Expand Down
135 changes: 58 additions & 77 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "render.h"

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

Expand All @@ -26,7 +26,7 @@ enum m8_command_bytes {
system_info_command_datalength = 6
};

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

switch (recv_buf[0]) {

case draw_rectangle_command:

case draw_rectangle_command: {
if (size < draw_rectangle_command_min_datalength ||
size > draw_rectangle_command_max_datalength) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR,
Expand All @@ -52,74 +51,64 @@ int process_command(uint8_t *data, uint32_t size) {
size);
dump_packet(size, recv_buf);
return 0;
break;
} else {

/* Support variable sized rectangle commands
If colors are omitted, the last drawn color should be used
If size is omitted, the size should be 1x1 pixels
So basically the command can be 5, 8, 9 or 12 bytes long */

static struct draw_rectangle_command rectcmd;

rectcmd.pos.x = decodeInt16(recv_buf, 1);
rectcmd.pos.y = decodeInt16(recv_buf, 3);

switch (size) {
case 5:
rectcmd.size.width = 1;
rectcmd.size.height = 1;
break;
case 8:
rectcmd.size.width = 1;
rectcmd.size.height = 1;
rectcmd.color.r = recv_buf[5];
rectcmd.color.g = recv_buf[6];
rectcmd.color.b = recv_buf[7];
break;
case 9:
rectcmd.size.width = decodeInt16(recv_buf, 5);
rectcmd.size.height = decodeInt16(recv_buf, 7);
break;
default:
rectcmd.size.width = decodeInt16(recv_buf, 5);
rectcmd.size.height = decodeInt16(recv_buf, 7);
rectcmd.color.r = recv_buf[9];
rectcmd.color.g = recv_buf[10];
rectcmd.color.b = recv_buf[11];
break;
}

draw_rectangle(&rectcmd);
return 1;
}
/* Support variable sized rectangle commands
If colors are omitted, the last drawn color should be used
If size is omitted, the size should be 1x1 pixels
So basically the command can be 5, 8, 9 or 12 bytes long */

break;
static struct draw_rectangle_command rectcmd;

case draw_character_command:
rectcmd.pos.x = decodeInt16(recv_buf, 1);
rectcmd.pos.y = decodeInt16(recv_buf, 3);

switch (size) {
case 5:
rectcmd.size.width = 1;
rectcmd.size.height = 1;
break;
case 8:
rectcmd.size.width = 1;
rectcmd.size.height = 1;
rectcmd.color.r = recv_buf[5];
rectcmd.color.g = recv_buf[6];
rectcmd.color.b = recv_buf[7];
break;
case 9:
rectcmd.size.width = decodeInt16(recv_buf, 5);
rectcmd.size.height = decodeInt16(recv_buf, 7);
break;
default:
rectcmd.size.width = decodeInt16(recv_buf, 5);
rectcmd.size.height = decodeInt16(recv_buf, 7);
rectcmd.color.r = recv_buf[9];
rectcmd.color.g = recv_buf[10];
rectcmd.color.b = recv_buf[11];
break;
}

draw_rectangle(&rectcmd);
return 1;
}

case draw_character_command: {
if (size != draw_character_command_datalength) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR,
"Invalid draw character packet: expected length %d, got %d",
draw_character_command_datalength, size);
dump_packet(size, recv_buf);
return 0;
break;
} else {

struct draw_character_command charcmd = {
recv_buf[1], // char
{decodeInt16(recv_buf, 2), decodeInt16(recv_buf, 4)}, // position x/y
{recv_buf[6], recv_buf[7], recv_buf[8]}, // foreground r/g/b
{recv_buf[9], recv_buf[10], recv_buf[11]}}; // background r/g/b
draw_character(&charcmd);
return 1;
}
struct draw_character_command charcmd = {
recv_buf[1], // char
{decodeInt16(recv_buf, 2), decodeInt16(recv_buf, 4)}, // position x/y
{recv_buf[6], recv_buf[7], recv_buf[8]}, // foreground r/g/b
{recv_buf[9], recv_buf[10], recv_buf[11]}}; // background r/g/b
draw_character(&charcmd);
return 1;
}

break;

case draw_oscilloscope_waveform_command:

case draw_oscilloscope_waveform_command: {
if (size < draw_oscilloscope_waveform_command_mindatalength ||
size > draw_oscilloscope_waveform_command_maxdatalength) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR,
Expand All @@ -128,21 +117,17 @@ int process_command(uint8_t *data, uint32_t size) {
draw_oscilloscope_waveform_command_maxdatalength, size);
dump_packet(size, recv_buf);
return 0;
break;
} else {

struct draw_oscilloscope_waveform_command osccmd;

osccmd.color = (struct color){recv_buf[1], recv_buf[2], recv_buf[3]}; // color r/g/b
memcpy(osccmd.waveform, &recv_buf[4], size - 4);
}
struct draw_oscilloscope_waveform_command osccmd;

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

draw_waveform(&osccmd);
return 1;
}
osccmd.waveform_size = size - 4;

break;
draw_waveform(&osccmd);
return 1;
}

case joypad_keypressedstate_command: {
if (size != joypad_keypressedstate_command_datalength) {
Expand All @@ -152,12 +137,10 @@ int process_command(uint8_t *data, uint32_t size) {
joypad_keypressedstate_command_datalength, size);
dump_packet(size, recv_buf);
return 0;
break;
}

// nothing is done with joypad key pressed packets for now
return 1;
break;
}

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

return 1;
break;
}

default:
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Invalid packet\n");
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Invalid packet");
dump_packet(size, recv_buf);
return 0;
break;
}
return 1;
}
Loading
Loading