/*
 * $Id: btn.h 446 2019-05-29 00:35:24Z true $
 * begin 20190527 true
 *
 * button ticks are 1/1024 of a second
 */


#ifndef INC_UI_BTN_H_
#define INC_UI_BTN_H_



#include "efm8_config.h"



typedef struct BtnData {
	uint8_t state;			// what is the current handled state of this button
	uint8_t hold_time;		// hold time in 1/16 second increments
	uint16_t hold_retrig;	// hold retrigger in button ticks, 0 disables
	uint16_t hold_rt_ctr;	// hold retrigger counter
	uint16_t held;			// actual hold counts in button ticks
	void (*push_cb)();
	void (*held_cb)();
	void (*release_cb)();
} BtnData;



#define BTN_COUNT			4

#define BTN1				P2_B3
#define BTN2				P0_B6
#define BTN3				P0_B3
#define BTN4				P0_B1

#define BTN1_PIN			3
#define BTN2_PIN			6
#define BTN3_PIN			3
#define BTN4_PIN			1

#define BTN1_DIN			P2MDIN
#define BTN2_DIN			P0MDIN
#define BTN3_DIN			P0MDIN
#define BTN4_DIN			P0MDIN

#define BTN1_DOUT			P2MDOUT
#define BTN2_DOUT			P0MDOUT
#define BTN3_DOUT			P0MDOUT
#define BTN4_DOUT			P0MDOUT


#define BTN_PUSH			(1 << 0)
#define BTN_HELD			(1 << 1)
#define BTN_RELEASE			(1 << 2)

#define BTN_PUSH_CB			(1 << 4)
#define BTN_HELD_CB			(1 << 5)
#define BTN_RELEASE_CB		(1 << 6)


#define BTN_DEBOUNCE		11		// how many button ticks to wait before registering press
#define BTN_MAX_HOLD		30720	// longest reported / processed hold time (30s)
#define BTN_HOLD_SHIFT		6		// rshift value to get 1/16th sec hold time


#define BTN_HOLDTIME_0_25S	4
#define BTN_HOLDTIME_0_50S	8
#define BTN_HOLDTIME_1_00S	16
#define BTN_HOLDTIME_2_00S	(16 * 2)
#define BTN_HOLDTIME_3_00S	(16 * 3)
#define BTN_HOLDTIME_4_00S	(16 * 4)
#define BTN_HOLDTIME_5_00S	(16 * 5)



extern BtnData xdata btn[BTN_COUNT];



void btn_init();
void btn_tick();
void btn_callback();



#endif /* INC_UI_BTN_H_ */
