basic button handler routines added, hard buttons tested
touch sensors were also preliminary tested but not all have been analyzed yet.
This commit is contained in:
@@ -3,4 +3,99 @@
|
||||
*/
|
||||
|
||||
#include "ch32x035_conf.h"
|
||||
#include "driver/adc.h"
|
||||
#include "btn.h"
|
||||
|
||||
#include "driver/adc.h"
|
||||
|
||||
|
||||
|
||||
BtnSub btn[BTN_COUNT];
|
||||
|
||||
uint16_t btn_pushed;
|
||||
uint16_t btn_held;
|
||||
|
||||
|
||||
|
||||
void btn_push_cb(uint8_t idx)
|
||||
{
|
||||
if (!(btn_pushed & (1 << idx))) {
|
||||
btn_pushed |= (1 << idx);
|
||||
if (btn[idx].cb_push) btn[idx].cb_push(idx);
|
||||
}
|
||||
}
|
||||
|
||||
void btn_hold_cb(uint8_t idx)
|
||||
{
|
||||
btn_held |= (1 << idx);
|
||||
if (btn[idx].cb_hold) btn[idx].cb_hold(idx);
|
||||
}
|
||||
|
||||
void btn_release_cb(uint8_t idx)
|
||||
{
|
||||
btn_pushed &= ~(1 << idx);
|
||||
btn_held &= ~(1 << idx);
|
||||
if (btn[idx].cb_release) btn[idx].cb_release(idx);
|
||||
}
|
||||
|
||||
void btn_process()
|
||||
{
|
||||
uint16_t i;
|
||||
uint32_t x;
|
||||
uint16_t btn_state[4] = {0};
|
||||
|
||||
// fill button bitfield
|
||||
for (i = 0; i < BTN_COUNT; i++) {
|
||||
if (i == 0) {
|
||||
// GPIOC pulldown button
|
||||
x = GPIOC->INDR & (1 << 10); // PC10 (or PC17)
|
||||
} else if (i < 3) {
|
||||
// GPIOB pullup buttons
|
||||
x = !(GPIOB->INDR & (1 << (i + 10))); // PB11, PB12
|
||||
} else {
|
||||
// touch sensors
|
||||
x = adc_get_tkey(i - 3);
|
||||
}
|
||||
|
||||
// is pushed?
|
||||
if (x) {
|
||||
if (btn[i].hold != 0xffff)
|
||||
btn[i].hold++;
|
||||
|
||||
if (btn[i].hold == DEBOUNCE) {
|
||||
btn_state[1] |= (1 << (i + 3));
|
||||
}
|
||||
}
|
||||
|
||||
// is held?
|
||||
if (btn[i].hold == HOLD_COUNTS) {
|
||||
btn_state[2] |= (1 << (i + 3));
|
||||
}
|
||||
|
||||
// is repeated?
|
||||
if (btn[i].repeat && (btn[i].hold == (HOLD_COUNTS + btn[i].repeat))) {
|
||||
btn_state[2] |= (1 << (i + 3));
|
||||
btn[i].hold = HOLD_COUNTS;
|
||||
}
|
||||
|
||||
// is released?
|
||||
if (!x) {
|
||||
if (btn[i].hold) {
|
||||
btn[i].hold = 0;
|
||||
btn_state[3] |= (1 << (i + 3));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// process callbacks for new events
|
||||
for (i = 0; i < BTN_COUNT; i++) {
|
||||
if (btn_state[1] & (1 << i)) {
|
||||
btn_push_cb(i);
|
||||
}
|
||||
if (btn_state[2] & (1 << i)) {
|
||||
btn_hold_cb(i);
|
||||
}
|
||||
if (btn_state[3] & (1 << i)) {
|
||||
btn_release_cb(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
43
firmware/app/ui/btn.h
Normal file
43
firmware/app/ui/btn.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* btn.h
|
||||
*
|
||||
* Created on: Oct 13, 2024
|
||||
* Author: true
|
||||
*/
|
||||
|
||||
#ifndef USER_UI_BTN_H_
|
||||
#define USER_UI_BTN_H_
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
#define HB_COUNT 3 // total hard buttons in system
|
||||
#define TS_COUNT 7 // total touch sensors in system
|
||||
#define BTN_COUNT (HB_COUNT + TS_COUNT) // total buttons in system
|
||||
|
||||
#define DEBOUNCE 12
|
||||
#define HOLD_COUNTS 600 // how long until a push-and-hold is detected
|
||||
|
||||
|
||||
|
||||
typedef struct BtnSub {
|
||||
uint16_t hold; // initial hold
|
||||
uint16_t repeat; // repeated hold
|
||||
void (*cb_push)(uint8_t);
|
||||
void (*cb_hold)(uint8_t);
|
||||
void (*cb_release)(uint8_t);
|
||||
} BtnSub;
|
||||
|
||||
|
||||
|
||||
extern BtnSub btn[BTN_COUNT];
|
||||
|
||||
|
||||
|
||||
void btn_process();
|
||||
|
||||
|
||||
|
||||
#endif /* USER_UI_BTN_H_ */
|
||||
Reference in New Issue
Block a user