Files
lwbtn/dev/main.c
T

86 lines
2.6 KiB
C

#include <stdio.h>
#include <string.h>
#include "lwbtn/lwbtn.h"
#include "windows.h"
static LARGE_INTEGER freq, sys_start_time;
static uint32_t get_tick(void);
/* User defined settings */
const int keys[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
/* List of buttons to process */
static lwbtn_btn_t btns[] = {{.arg = (void*)&keys[0]}, {.arg = (void*)&keys[1]}, {.arg = (void*)&keys[2]},
{.arg = (void*)&keys[3]}, {.arg = (void*)&keys[4]}, {.arg = (void*)&keys[5]},
{.arg = (void*)&keys[6]}, {.arg = (void*)&keys[7]}, {.arg = (void*)&keys[8]},
{.arg = (void*)&keys[9]}};
/**
* \brief Get input state callback
* \param lw: LwBTN instance
* \param btn: Button instance
* \return `1` if button active, `0` otherwise
*/
uint8_t
prv_btn_get_state(struct lwbtn* lw, struct lwbtn_btn* btn) {
(void)lw;
return GetAsyncKeyState(*(int*)btn->arg) < 0;
}
/**
* \brief Button event
*
* \param lw: LwBTN instance
* \param btn: Button instance
* \param evt: Button event
*/
void
prv_btn_event(struct lwbtn* lw, struct lwbtn_btn* btn, lwbtn_evt_t evt) {
const char* s = "unknown";
(void)lw;
/* Get event string */
s = ((evt == LWBTN_EVT_KEEPALIVE)
? "KEEPALIVE"
: ((evt == LWBTN_EVT_ONPRESS)
? " ONPRESS"
: ((evt == LWBTN_EVT_ONRELEASE) ? "ONRELEASE"
: ((evt == LWBTN_EVT_ONCLICK) ? " ONCLICK" : " UNKNOWN"))));
printf("[%7u] CH: %c, evt: %s, keep-alive cnt: %3u, click cnt: %3u\r\n", (unsigned)get_tick(), *(int*)btn->arg, s,
(unsigned)btn->keepalive.cnt, (unsigned)btn->click.cnt);
}
int
main(void) {
printf("Application running\r\n");
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&sys_start_time);
/* Define buttons */
lwbtn_init_ex(NULL, btns, sizeof(btns) / sizeof(btns[0]), prv_btn_get_state, prv_btn_event);
while (1) {
/* Process forever */
lwbtn_process_ex(NULL, get_tick());
/* Artificial sleep to offload win process */
Sleep(5);
}
}
/**
* \brief Get current tick in ms from start of program
* \return uint32_t: Tick in ms
*/
static uint32_t
get_tick(void) {
LONGLONG ret;
LARGE_INTEGER now;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&now);
ret = now.QuadPart - sys_start_time.QuadPart;
return (uint32_t)((ret * 1000) / freq.QuadPart);
}