~jkaivo/freeonebook

33b9c3fd557ce618f903d3473ab3df47496f3f4d — Jakob Kaivo 3 years ago b94d852
serialize button queue to avoid screen update races
1 files changed, 53 insertions(+), 35 deletions(-)

M freeonebook.c
M freeonebook.c => freeonebook.c +53 -35
@@ 1,4 1,5 @@
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


@@ 9,6 10,11 @@

#define SDPATH "/run/media/mmcblk0p1"

#define LOWBATTERY_L "/home/root/SplashScreen/epdc_lowbatL.pgm"
#define LOWBATTERY_R "/home/root/SplashScreen/epdc_lowbatR.pgm"

static int fifo[2];

void halt(void)
{
	fflush(NULL);


@@ 29,7 35,8 @@ void poweroff(int reason)
	printf("shutting down (%d)\n", reason);

	if (reason == GPIO_LOWBATTERY) {
		printf("low battery\n");
		fb_loadimage(RIGHT_SCREEN, LOWBATTERY_R);
		fb_loadimage(LEFT_SCREEN, LOWBATTERY_L);
	}

	exit(0);


@@ 37,37 44,10 @@ void poweroff(int reason)

void buttonpress(int button)
{
	switch (button) {
	case BUTTON_SPECIAL:
		printf("special\n");
		break;

	case BUTTON_NEXTCHAPTER:
		printf("next chapter\n");
		break;

	case BUTTON_PREVBOOK:
		printf("previous book\n");
		break;

	case BUTTON_NEXTBOOK:
		printf("next book\n");
		break;

	case BUTTON_PREVPAGE:
		printf("previous page\n");
		break;

	case BUTTON_NEXTPAGE:
		printf("next page\n");
		break;

	default:
		printf("button %d pressed\n", button);
	}

	fflush(NULL);
	sync();
	static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
	pthread_mutex_lock(&mutex);
	write(fifo[1], &button, sizeof(button));
	pthread_mutex_unlock(&mutex);
}

int main(int argc, char *argv[])


@@ 77,6 57,8 @@ int main(int argc, char *argv[])
	printf("registering atexit() handler\n");
	atexit(halt);

	pipe(fifo);

	printf("adding watchers\n");
	gpio_watch(GPIO_LOWBATTERY, poweroff);
	gpio_watch(GPIO_SHUTDOWN, poweroff);


@@ 89,11 71,47 @@ int main(int argc, char *argv[])

	printf("initializing framebuffer\n");
	fb_init();
	fb_loadimage(RIGHT_SCREEN, SDPATH "/right.gray");
	fb_loadimage(LEFT_SCREEN, SDPATH "/left.gray");
	system("ls -lA "SDPATH"/grid.png");
	//fb_loadimage(RIGHT_SCREEN, SDPATH "/image.gray");
	fb_loadimage(RIGHT_SCREEN, SDPATH "/grid.png");
	//fb_loadimage(RIGHT_SCREEN, SDPATH "/right.gray");
	//fb_loadimage(LEFT_SCREEN, SDPATH "/left.gray");
	
	for (;;) {
		sleep(INT_MAX);
		int button = 0;
		read(fifo[0], &button, sizeof(button));

		switch (button) {
		case BUTTON_SPECIAL:
			printf("special\n");
			break;

		case BUTTON_NEXTCHAPTER:
			printf("next chapter\n");
			break;

		case BUTTON_PREVBOOK:
			printf("previous book\n");
			break;

		case BUTTON_NEXTBOOK:
			printf("next book\n");
			break;

		case BUTTON_PREVPAGE:
			printf("previous page\n");
			break;

		case BUTTON_NEXTPAGE:
			printf("next page\n");
			break;

		default:
			printf("button %d pressed\n", button);
		}

		fflush(NULL);
		sync();
	}

	return 0;