SDL UI fleshed out a lot.

This commit is contained in:
Solomon Peachy 2019-01-27 11:16:28 -05:00
parent 12b88b73c3
commit 60a19c1e67
4 changed files with 117 additions and 25 deletions

View File

@ -1,6 +1,6 @@
FILES := $(wildcard *.c)
#LDFLAGS := /usr/lib/libwiringPi.so.2
CFLAGS := -Wall -lSDL2 -lSDL2_ttf
LDFLAGS := -lSDL2 -lSDL2_ttf -lwiringPi
CFLAGS := -Wall
all: testpi

4
aldl.h
View File

@ -6,6 +6,9 @@ void delayfn(int ms);
extern int gotsync;
extern void (*aldl_callback)(uint8_t *data, int datalen);
int init_dash(void);
int render_dash(void);
/* Decoded data stream */
struct stream_common {
uint16_t heartbeat;
@ -52,4 +55,3 @@ extern struct stream_common decoded;
/* Examples */
void aldl_a179_callback(uint8_t *data, int datalen);

125
sdldash.c
View File

@ -12,16 +12,17 @@
#define SCREEN_W 320
#define SCREEN_H 240
#define FONT "/usr/share/fonts/levien-inconsolata/Inconsolata-Regular.ttf"
#define FONT_SZ 12
#define FONT_SZ 14
#define VIDEODRIVER NULL
#define WINDOW_NAME "ALDL Dashboard"
static const SDL_Color textcolor = {255,255,255};
static const SDL_Color errorcolor = {255,255,255};
/* State */
static SDL_bool videoinit = SDL_FALSE;
static SDL_Window *window = NULL;
static TTF_Font *font = NULL;
static SDL_Renderer *renderer = NULL;
static SDL_Surface *surface = NULL;;
/* Cleanup */
void OnQuit(void) {
@ -31,7 +32,6 @@ void OnQuit(void) {
SDL_Quit();
}
/* Initialize */
int init_dash(void) {
if (SDL_Init(0)) {
@ -71,14 +71,6 @@ int init_dash(void) {
return 5;
}
/* Set up drawing surface */
surface = SDL_CreateRGBSurface(0, SCREEN_W, SCREEN_H,
8, 0, 0, 0, 0);
if (!surface) {
printf("Fail to create drawing surface");
return 6;
}
/* Set up renderer */
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if (!renderer) {
@ -89,9 +81,114 @@ int init_dash(void) {
return 0;
}
int render_dash(void) {
// draw border, 1px around the edge.
// draw some text.
#define RENDER_TEXT_NOARG(__i, __x, __y, __color, __format) \
do { \
surf = TTF_RenderText_Solid(font, __format, __color); \
if (surf) { \
drawn_tex[__i] = SDL_CreateTextureFromSurface(renderer, surf); \
SDL_FreeSurface(surf); \
} \
DestR.x = __x; \
DestR.y = __y; \
SDL_QueryTexture(drawn_tex[__i], NULL, NULL, &DestR.w, &DestR.h); \
SDL_RenderCopy(renderer, drawn_tex[__i], NULL, &DestR); \
__i++; \
} while (0)
#define RENDER_TEXT_1ARG(__i, __x, __y, __color, __format, __arg) \
do { \
snprintf(txtbuf, sizeof(txtbuf), __format, __arg); \
surf = TTF_RenderText_Solid(font, txtbuf, __color); \
if (surf) { \
drawn_tex[__i] = SDL_CreateTextureFromSurface(renderer, surf); \
SDL_FreeSurface(surf); \
} \
DestR.x = __x; \
DestR.y = __y; \
SDL_QueryTexture(drawn_tex[__i], NULL, NULL, &DestR.w, &DestR.h); \
SDL_RenderCopy(renderer, drawn_tex[__i], NULL, &DestR); \
__i++; \
} while (0)
#define NUM_TEXTS_MAX 64
int render_dash(void) {
int i = 0;
SDL_Surface *surf;
SDL_Texture *drawn_tex[NUM_TEXTS_MAX] = { NULL };
SDL_Rect DestR;
char txtbuf[64];
/* Clear with black */
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
/* Set draw color */
if (decoded.mil)
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
else
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
/* Draw Border, 1px */
SDL_RenderDrawLine(renderer, 0, 0, SCREEN_W -1, 0);
SDL_RenderDrawLine(renderer, 0, 0, 0, SCREEN_H -1);
SDL_RenderDrawLine(renderer, SCREEN_W -1, 0, SCREEN_W -1, SCREEN_H);
SDL_RenderDrawLine(renderer, 0, SCREEN_H -1, SCREEN_W -1, SCREEN_H -1);
/* Draw fixed text */
RENDER_TEXT_NOARG(i, 100, 10, textcolor, "ALDL Dashboard");
if (!gotsync) {
/* No sync yet.. */
RENDER_TEXT_NOARG(i, 100, 100, errorcolor, "NO SYNC\n");
goto done;
}
RENDER_TEXT_1ARG(i, 10, 30, textcolor, "PROM %04x", decoded.prom_id);
/* Draw variable text */
RENDER_TEXT_1ARG(i, 10, 50, textcolor, "%03u MPH", decoded.mph);
RENDER_TEXT_1ARG(i, 10, 70, textcolor, "%04d RPM", decoded.rpm);
RENDER_TEXT_1ARG(i, 10, 90, textcolor, "%03d C", decoded.coolant_temp_c);
RENDER_TEXT_1ARG(i, 10, 110, textcolor, "%03u%% TP", decoded.tp);
if (decoded.idling) {
RENDER_TEXT_1ARG(i, 200, 10, textcolor, "IDLE @ %04d", decoded.idle_tgt);
if (decoded.closed_loop_idle)
RENDER_TEXT_NOARG(i, 200, 30, textcolor, "CLOSED LOOP");
else
RENDER_TEXT_NOARG(i, 200, 30, textcolor, "OPEN LOOP");
} else {
if (decoded.closed_loop)
RENDER_TEXT_NOARG(i, 200, 30, textcolor, "CLOSED LOOP");
else
RENDER_TEXT_NOARG(i, 200, 30, textcolor, "OPEN LOOP");
}
if (decoded.ac_request)
RENDER_TEXT_NOARG(i, 200, 50, textcolor, "AC REQ");
if (decoded.shift_req)
RENDER_TEXT_NOARG(i, 200, 70, textcolor, "SHIFT REQ");
else if (decoded.top_gear)
RENDER_TEXT_NOARG(i, 200, 70, textcolor, "TOP GEAR");
else if (decoded.neutral)
RENDER_TEXT_NOARG(i, 200, 70, textcolor, "NEUTRAL");
RENDER_TEXT_1ARG(i, 200, 90, textcolor, "BAT %02.01f v", decoded.battery_v);
RENDER_TEXT_1ARG(i, 310, 225, textcolor, "%c", decoded.heartbeat ? '^' : '*');
if (decoded.mil) {
/* Something wrong! */
RENDER_TEXT_1ARG(i, 10, 200, errorcolor, "CHECK ENGINE %06x\n", decoded.mil);
}
done:
/* Commit! */
SDL_RenderPresent(renderer);
/* Clean up afterwards */
for (i = 0; i < NUM_TEXTS_MAX ; i++) {
if (drawn_tex[i])
SDL_DestroyTexture(drawn_tex[i]);
}
return 0;
}

View File

@ -20,7 +20,6 @@ void delayfn(int ms)
int main (int argc, char **argv)
{
void *dash_ctx;
/* Set up RPi GPIO */
wiringPiSetup();
@ -35,14 +34,8 @@ int main (int argc, char **argv)
return -1;
while(1) {
sleep(1);
if (!gotsync) {
// No sync, we're off.
continue;
}
render_dash();
sleep(1);
}
return 0;