2019-12-01 01:02:08 -07:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include "log.h"
|
|
|
|
#include "msg_queue.h"
|
|
|
|
#include "gpio.h"
|
|
|
|
|
2019-12-01 01:36:07 -07:00
|
|
|
PMSG_QUEUE Sys_Queue = NULL;
|
2019-12-01 01:02:08 -07:00
|
|
|
|
|
|
|
static pthread_t ithread;
|
|
|
|
static volatile sig_atomic_t running = 1;
|
|
|
|
static int last_bstate = 0;
|
|
|
|
|
|
|
|
static void *input_thread(void *arg)
|
|
|
|
{
|
2019-12-01 01:18:59 -07:00
|
|
|
int st, down, up, mask;
|
2019-12-01 01:02:08 -07:00
|
|
|
uintptr_t attr;
|
|
|
|
|
|
|
|
while (running)
|
|
|
|
{
|
|
|
|
/* poll hardware buttons */
|
|
|
|
st = Gpio_read_buttons();
|
|
|
|
if (st != last_bstate)
|
|
|
|
{
|
2019-12-01 01:18:59 -07:00
|
|
|
up = last_bstate & ~st;
|
|
|
|
down = st & ~last_bstate;
|
2019-12-01 15:43:05 -07:00
|
|
|
for (attr = 1, mask = GRB_STATE_BUTTON1; attr <= GPIO_BUTTON_COUNT; attr++, mask <<= 1)
|
2019-12-01 01:02:08 -07:00
|
|
|
{
|
2019-12-01 01:18:59 -07:00
|
|
|
if (up & mask)
|
2019-12-01 15:43:05 -07:00
|
|
|
Mq_post1(Sys_Queue, 0, WM_HWBUTTONUP, attr);
|
2019-12-01 01:18:59 -07:00
|
|
|
else if (down & mask)
|
2019-12-01 15:43:05 -07:00
|
|
|
Mq_post1(Sys_Queue, 0, WM_HWBUTTONDOWN, attr);
|
2019-12-01 01:02:08 -07:00
|
|
|
}
|
|
|
|
last_bstate = st;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* additional poll activity here */
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-12-01 15:43:05 -07:00
|
|
|
int Sys_enable_input(void)
|
2019-12-01 01:02:08 -07:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2019-12-01 15:43:05 -07:00
|
|
|
Sys_Queue = Mq_alloc(64);
|
2019-12-01 01:02:08 -07:00
|
|
|
if (!Sys_Queue)
|
|
|
|
{
|
|
|
|
Log(LFATAL, "Unable to allocate system message queue.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
running = 1;
|
|
|
|
rc = pthread_create(&ithread, NULL, input_thread, NULL);
|
|
|
|
if (rc != 0)
|
|
|
|
Log(LFATAL, "Unable to start system input thread (%d).", rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-12-01 15:43:05 -07:00
|
|
|
void Sys_disable_input(void)
|
2019-12-01 01:02:08 -07:00
|
|
|
{
|
|
|
|
running = 0;
|
|
|
|
pthread_join(ithread, NULL);
|
2019-12-01 15:43:05 -07:00
|
|
|
Mq_destroy(Sys_Queue);
|
2019-12-01 01:02:08 -07:00
|
|
|
Sys_Queue = NULL;
|
|
|
|
}
|