~jkaivo/freeonebook

freeonebook/freeonebook.c -rw-r--r-- 2.3 KiB
f1795ce9 — Jakob Kaivo remove debug output 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "gpio.h"
#include "fb.h"

#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);
	sync();

	char *halt[] = { "shutdown", "-h", "now", NULL };
	execvp(halt[0], halt);
}

void poweroff(int reason)
{
	static int halting = 0;
	if (halting) {
		return;
	}
	halting = 1;

	printf("shutting down (%d)\n", reason);

	if (reason == GPIO_LOWBATTERY) {
		fb_loadimage(RIGHT_SCREEN, LOWBATTERY_R);
		fb_loadimage(LEFT_SCREEN, LOWBATTERY_L);
	}

	exit(0);
}

void buttonpress(int button)
{
	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[])
{
	printf("starting freeonebook\n");
	(void)argc; (void)argv;
	printf("registering atexit() handler\n");
	atexit(halt);

	pipe(fifo);

	printf("adding watchers\n");
	gpio_watch(GPIO_LOWBATTERY, poweroff);
	gpio_watch(GPIO_SHUTDOWN, poweroff);
	gpio_watch(BUTTON_SPECIAL, buttonpress);
	gpio_watch(BUTTON_PREVBOOK, buttonpress);
	gpio_watch(BUTTON_PREVPAGE, buttonpress);
	gpio_watch(BUTTON_NEXTPAGE, buttonpress);
	gpio_watch(BUTTON_NEXTCHAPTER, buttonpress);
	gpio_watch(BUTTON_NEXTBOOK, buttonpress);

	printf("initializing framebuffer\n");
	fb_init();
	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 (;;) {
		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;
}