Skip to content

Commit

Permalink
split input.c to events.c and gamecontrollers.c, various cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
laamaa committed Aug 18, 2024
1 parent 934e8fc commit de61d0d
Show file tree
Hide file tree
Showing 21 changed files with 327 additions and 344 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#Set all your object files (the object files of all the .c files in your project, e.g. main.o my_sub_functions.o )
OBJ = src/main.o src/serial.o src/slip.o src/command.o src/render.o src/ini.o src/config.o src/input.o src/fx_cube.o src/usb.o src/audio.o src/usb_audio.o src/ringbuffer.o src/inprint2.o
OBJ = src/main.o src/serial.o src/slip.o src/command.o src/render.o src/ini.o src/config.o src/events.o src/gamecontrollers.o src/fx_cube.o src/usb.o src/audio.o src/usb_audio.o src/ringbuffer.o src/inprint2.o

#Set any dependant header files so that if they are edited they cause a complete re-compile (e.g. main.h some_subfunctions.h some_definitions_file.h ), or leave blank
DEPS = src/serial.h src/slip.h src/command.h src/render.h src/ini.h src/config.h src/input.h src/fx_cube.h src/audio.h src/ringbuffer.h src/inline_font.h
DEPS = src/serial.h src/slip.h src/command.h src/render.h src/ini.h src/config.h src/events.h src/gamecontrollers.h src/fx_cube.h src/audio.h src/ringbuffer.h src/inline_font.h

#Any special libraries you are using in your project (e.g. -lbcm2835 -lrt `pkg-config --libs gtk+-3.0` ), or leave blank
INCLUDES = $(shell pkg-config --libs sdl2 libserialport | sed 's/-mwindows//')
Expand Down Expand Up @@ -34,6 +34,8 @@ libusb: m8c
#Cleanup
.PHONY: clean

all: m8c

clean:
rm -f src/*.o *~ m8c

Expand Down
81 changes: 40 additions & 41 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 @@ -26,7 +26,7 @@ void audio_cb_in(void *userdata, 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;
Expand All @@ -40,48 +40,47 @@ int audio_init(unsigned int audio_buffer_size, const char *output_device_name) {
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 (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
127 changes: 57 additions & 70 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 @@ -44,6 +44,7 @@ int process_command(uint8_t *data, uint32_t size) {

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 +53,68 @@ 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;

/* 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;
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;
}

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

break;
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;
}

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 +123,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 +143,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 +177,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");
dump_packet(size, recv_buf);
return 0;
break;
}
return 1;
}
14 changes: 7 additions & 7 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/* Case insensitive string compare from ini.h library */
static int strcmpci(const char *a, const char *b) {
for (;;) {
int d = tolower(*a) - tolower(*b);
const int d = tolower(*a) - tolower(*b);
if (d != 0 || !*a) {
return d;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ config_params_s init_config() {
}

// Write config to file
void write_config(config_params_s *conf) {
void write_config(const config_params_s *conf) {

// Open the default config file for writing
char config_path[1024] = {0};
Expand Down Expand Up @@ -167,7 +167,7 @@ void write_config(config_params_s *conf) {
if (rw != NULL) {
// Write ini_values array to config file
for (int i = 0; i < INI_LINE_COUNT; i++) {
size_t len = SDL_strlen(ini_values[i]);
const size_t len = SDL_strlen(ini_values[i]);
if (SDL_RWwrite(rw, ini_values[i], 1, len) != len) {
SDL_LogDebug(SDL_LOG_CATEGORY_SYSTEM, "Couldn't write line into config file.");
} else {
Expand Down Expand Up @@ -205,7 +205,7 @@ void read_config(config_params_s *conf) {
write_config(conf);
}

void read_audio_config(ini_t *ini, config_params_s *conf) {
void read_audio_config(const ini_t *ini, config_params_s *conf) {
const char *param_audio_enabled = ini_get(ini, "audio", "audio_enabled");
const char *param_audio_buffer_size = ini_get(ini, "audio", "audio_buffer_size");
const char *param_audio_device_name = ini_get(ini, "audio", "audio_device_name");
Expand All @@ -227,7 +227,7 @@ void read_audio_config(ini_t *ini, config_params_s *conf) {
}
}

void read_graphics_config(ini_t *ini, config_params_s *conf) {
void read_graphics_config(const ini_t *ini, config_params_s *conf) {
const char *param_fs = ini_get(ini, "graphics", "fullscreen");
const char *param_gpu = ini_get(ini, "graphics", "use_gpu");
const char *idle_ms = ini_get(ini, "graphics", "idle_ms");
Expand Down Expand Up @@ -260,7 +260,7 @@ void read_graphics_config(ini_t *ini, config_params_s *conf) {
conf->wait_packets = SDL_atoi(wait_packets);
}

void read_key_config(ini_t *ini, config_params_s *conf) {
void read_key_config(const ini_t *ini, config_params_s *conf) {
// TODO: Some form of validation

const char *key_up = ini_get(ini, "keyboard", "key_up");
Expand Down Expand Up @@ -323,7 +323,7 @@ void read_key_config(ini_t *ini, config_params_s *conf) {
conf->key_jazz_dec_velocity = SDL_atoi(key_toggle_audio);
}

void read_gamepad_config(ini_t *ini, config_params_s *conf) {
void read_gamepad_config(const ini_t *ini, config_params_s *conf) {
// TODO: Some form of validation

const char *gamepad_up = ini_get(ini, "gamepad", "gamepad_up");
Expand Down
Loading

0 comments on commit de61d0d

Please sign in to comment.