ref: c0b5ec731c79872b5d92368adae3600d35071ad7
parent: d5dc8d481bb9dad21b46a4d300827d558ad178f0
parent: cf8fc7dffe2fb9cd0bc99478de55c92dd1fabe8b
author: Jonne Kokkonen <jonne.kokkonen@gmail.com>
date: Tue May 4 06:25:45 EDT 2021
Merge branch 'main' into gamecontroller
--- a/command.c
+++ b/command.c
@@ -1,8 +1,7 @@
// Copyright 2021 Jonne Kokkonen
// Released under the MIT licence, https://opensource.org/licenses/MIT
-#include <stdio.h>
-#include <string.h>
+#include <SDL2/SDL_log.h>
#include "command.h"
#include "render.h"
@@ -26,9 +25,9 @@
static inline void dump_packet(uint32_t size, uint8_t *recv_buf) { for (uint16_t a = 0; a < size; a++) {- fprintf(stderr, "0x%02X ", recv_buf[a]);
+ SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "0x%02X ", recv_buf[a]);
}
- fprintf(stderr, "\n");
+ SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "\n");
}
void process_command(uint8_t *data, uint32_t size) {@@ -43,9 +42,10 @@
case draw_rectangle_command:
if (size != draw_rectangle_command_datalength) {- fprintf(stderr,
- "Invalid draw rectangle packet: expected length %d, got %d\n",
- draw_rectangle_command_datalength, size);
+ SDL_LogError(
+ SDL_LOG_CATEGORY_ERROR,
+ "Invalid draw rectangle packet: expected length %d, got %d\n",
+ draw_rectangle_command_datalength, size);
dump_packet(size, recv_buf);
break;
}
@@ -62,9 +62,10 @@
case draw_character_command:
if (size != draw_character_command_datalength) {- fprintf(stderr,
- "Invalid draw character packet: expected length %d, got %d\n",
- draw_character_command_datalength, size);
+ SDL_LogError(
+ SDL_LOG_CATEGORY_ERROR,
+ "Invalid draw character packet: expected length %d, got %d\n",
+ draw_character_command_datalength, size);
dump_packet(size, recv_buf);
break;
}
@@ -82,12 +83,13 @@
if (size < draw_oscilloscope_waveform_command_mindatalength ||
size > draw_oscilloscope_waveform_command_maxdatalength) {- fprintf(stderr,
- "Invalid draw oscilloscope packet: expected length between %d "
- "and %d, got "
- "%d\n",
- draw_oscilloscope_waveform_command_mindatalength,
- draw_oscilloscope_waveform_command_maxdatalength, size);
+ SDL_LogError(
+ SDL_LOG_CATEGORY_ERROR,
+ "Invalid draw oscilloscope packet: expected length between %d "
+ "and %d, got "
+ "%d\n",
+ draw_oscilloscope_waveform_command_mindatalength,
+ draw_oscilloscope_waveform_command_maxdatalength, size);
dump_packet(size, recv_buf);
break;
}
@@ -107,7 +109,7 @@
case joypad_keypressedstate_command:
/*
if (size != joypad_keypressedstate_command_datalength) {- fprintf(stderr,
+ SDL_LogError(SDL_LOG_CATEGORY_ERROR,
"Invalid joypad keypressed state packet: expected length %d, "
"got %d\n",
joypad_keypressedstate_command_datalength, size);
@@ -116,12 +118,12 @@
} */
// nothing is done with joypad key pressed packets for now
-
+
break;
default:
- fprintf(stderr, "Invalid packet\n");
+ SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Invalid packet\n");
dump_packet(size, recv_buf);
break;
--- a/main.c
+++ b/main.c
@@ -4,7 +4,6 @@
#include <SDL2/SDL.h>
#include <libserialport.h>
#include <signal.h>
-#include <stdio.h>
#include <unistd.h>
#include "command.h"
@@ -64,7 +63,8 @@
// read serial port
size_t bytes_read = sp_nonblocking_read(port, serial_buf, serial_read_size);
if (bytes_read < 0) {- fprintf(stderr, "Error %zu reading serial. \n", bytes_read);
+ SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Error %zu reading serial. \n",
+ bytes_read);
run = 0;
}
if (bytes_read > 0) {@@ -73,9 +73,10 @@
// process the incoming bytes into commands and draw them
int n = slip_read_byte(&slip, rx);
if (n != SLIP_NO_ERROR) {- fprintf(stderr, "SLIP error %d\n", n);
+ SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SLIP error %d\n", n);
}
}
+ usleep(10);
} else {render_screen();
usleep(100);
@@ -112,7 +113,7 @@
}
// exit, clean up
- fprintf(stderr, "\nShutting down\n");
+ SDL_Log("\nShutting down\n");close_game_controllers();
close_renderer();
disconnect(port);
--- a/render.c
+++ b/render.c
@@ -4,6 +4,7 @@
#include "render.h"
#include <SDL2/SDL.h>
+#include <SDL2/SDL_log.h>
#include <stdio.h>
#include "SDL2_inprint.h"
@@ -28,9 +29,8 @@
const int window_width = 640; // SDL window width
const int window_height = 480; // SDL window height
- // retutns zero on success else non-zero
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {- fprintf(stderr, "SDL_Init: %s\n", SDL_GetError());
+ SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "SDL_Init: %s\n", SDL_GetError());
return -1;
}
@@ -49,6 +49,7 @@
SDL_SetRenderDrawColor(rend, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(rend);
+ // Initialize a texture for the font and read the inline font bitmap
inrenderer(rend);
prepare_inline_font();
@@ -176,7 +177,7 @@
if (SDL_GetTicks() - ticks_fps > 5000) {ticks_fps = SDL_GetTicks();
- fprintf(stderr, "%d fps\n", fps / 5);
+ SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "%d fps\n", fps / 5);
fps = 0;
}
#endif
--- a/serial.c
+++ b/serial.c
@@ -1,8 +1,10 @@
// Copyright 2021 Jonne Kokkonen
// Released under the MIT licence, https://opensource.org/licenses/MIT
-// Contains portions of code from libserialport's examples released to the public domain
+// Contains portions of code from libserialport's examples released to the
+// public domain
+#include <SDL2/SDL_log.h>
#include <libserialport.h>
#include <stdio.h>
#include <stdlib.h>
@@ -9,7 +11,6 @@
#include "serial.h"
-
// Helper function for error handling
static int check(enum sp_return result);
@@ -35,7 +36,7 @@
struct sp_port *m8_port = NULL;
struct sp_port **port_list;
- fprintf(stderr, "Looking for USB serial devices.\n");
+ SDL_Log("Looking for USB serial devices.\n");/* Call sp_list_ports() to get the ports. The port_list
* pointer will be updated to refer to the array created. */
@@ -42,7 +43,7 @@
enum sp_return result = sp_list_ports(&port_list);
if (result != SP_OK) {- fprintf(stderr, "sp_list_ports() failed!\n");
+ SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "sp_list_ports() failed!\n");
abort();
}
@@ -52,7 +53,7 @@
struct sp_port *port = port_list[i];
if (detect_m8_serial_device(port)) {- fprintf(stderr, "Found M8 in %s.\n", sp_get_port_name(port));
+ SDL_Log("Found M8 in %s.\n", sp_get_port_name(port));sp_copy_port(port, &m8_port);
}
}
@@ -61,7 +62,7 @@
if (m8_port != NULL) {// Open the serial port and configure it
- fprintf(stderr, "Opening port.\n");
+ SDL_Log("Opening port.\n");check(sp_open(m8_port, SP_MODE_READ_WRITE));
check(sp_set_baudrate(m8_port, 115200));
@@ -70,7 +71,7 @@
check(sp_set_stopbits(m8_port, 1));
check(sp_set_flowcontrol(m8_port, SP_FLOWCONTROL_NONE));
} else {- fprintf(stderr, "Cannot find a M8.\n");
+ SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "Cannot find a M8.\n");
}
return (m8_port);
@@ -78,23 +79,23 @@
// Helper function for error handling.
static int check(enum sp_return result) {-
+
char *error_message;
switch (result) {case SP_ERR_ARG:
- fprintf(stderr,"Error: Invalid argument.\n");
+ SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error: Invalid argument.\n");
abort();
case SP_ERR_FAIL:
error_message = sp_last_error_message();
- fprintf(stderr,"Error: Failed: %s\n", error_message);
+ SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error: Failed: %s\n", error_message);
sp_free_error_message(error_message);
abort();
case SP_ERR_SUPP:
- fprintf(stderr,"Error: Not supported.\n");
+ SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error: Not supported.\n");
abort();
case SP_ERR_MEM:
- fprintf(stderr,"Error: Couldn't allocate memory.\n");
+ SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error: Couldn't allocate memory.\n");
abort();
case SP_OK:
default:
--- a/write.c
+++ b/write.c
@@ -1,6 +1,7 @@
// Copyright 2021 Jonne Kokkonen
// Released under the MIT licence, https://opensource.org/licenses/MIT
+#include <SDL2/SDL_log.h>
#include <libserialport.h>
#include <stdint.h>
#include <stdio.h>
@@ -10,12 +11,12 @@
uint8_t buf[2];
int result;
- fprintf(stderr, "Enabling and resetting M8 display\n");
+ SDL_Log("Enabling and resetting M8 display\n");buf[0] = 0x44;
result = sp_blocking_write(port, buf, 1, 5);
if (result != 1) {- fprintf(stderr, "Error enabling M8 display, code %d", result);
+ SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error enabling M8 display, code %d", result);
}
usleep(500);
@@ -23,7 +24,7 @@
buf[1] = 0x52;
result = sp_blocking_write(port, buf, 2, 5);
if (result != 2) {- fprintf(stderr, "Error resetting M8 display, code %d", result);
+ SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error resetting M8 display, code %d", result);
}
sleep(1);
@@ -34,11 +35,11 @@
char buf[1] = {'D'};int result;
- fprintf(stderr, "Disconnecting M8\n");
+ SDL_Log("Disconnecting M8\n");result = sp_blocking_write(port, buf, 1, 5);
if (result != 1) {- fprintf(stderr, "Error sending disconnect, code %d", result);
+ SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending disconnect, code %d", result);
return -1;
}
return 1;
@@ -50,7 +51,7 @@
int result;
result = sp_blocking_write(port, buf, nbytes, 5);
if (result != nbytes) {- fprintf(stderr, "Error sending input, code %d", result);
+ SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending input, code %d", result);
return -1;
}
return 1;
@@ -65,7 +66,7 @@
int result;
result = sp_blocking_write(port, buf, nbytes,5);
if (result != nbytes) {- fprintf(stderr, "Error sending keyjazz, code %d", result);
+ SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending keyjazz, code %d", result);
return -1;
}
--
⑨