#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "multi_button.h"
#include "bsp_beep.h"
static struct button btn1;
static struct button btn2;
static struct button btn3;
#define KEY1_PIN GET_PIN(G, 11)
#define KEY2_PIN GET_PIN(G, 13)
#define KEY3_PIN GET_PIN(B, 3)
static uint8_t button1_read_pin(void) { return rt_pin_read(KEY1_PIN); }
static uint8_t button2_read_pin(void) { return rt_pin_read(KEY2_PIN); }
static uint8_t button3_read_pin(void) { return rt_pin_read(KEY3_PIN); }
void button1_callback(void *btn)
{
uint32_t btn_event_val;
btn_event_val = get_button_event((struct button *)btn);
switch (btn_event_val)
{
case PRESS_DOWN:
rt_kprintf("button1 press down\n");
break;
case PRESS_UP:
rt_kprintf("button1 press up\n");
break;
case PRESS_REPEAT:
rt_kprintf("button1 press repeat\n");
break;
case SINGLE_CLICK:
beep(20);
rt_kprintf("button1 single click\n");
break;
case DOUBLE_CLICK:
rt_kprintf("button1 double click\n");
break;
case LONG_PRESS_START:
rt_kprintf("button1 long press start\n");
break;
case LONG_PRESS_HOLD:
rt_kprintf("button1 long press hold\n");
break;
}
}
void button2_callback(void *btn)
{
uint32_t btn_event_val;
btn_event_val = get_button_event((struct button *)btn);
switch (btn_event_val)
{
case PRESS_DOWN:
rt_kprintf("button2 press down\n");
break;
case PRESS_UP:
rt_kprintf("button2 press up\n");
break;
case PRESS_REPEAT:
rt_kprintf("button2 press repeat\n");
break;
case SINGLE_CLICK:
beep(20);
rt_kprintf("button2 single click\n");
break;
case DOUBLE_CLICK:
rt_kprintf("button2 double click\n");
break;
case LONG_PRESS_START:
rt_kprintf("button2 long press start\n");
break;
case LONG_PRESS_HOLD:
rt_kprintf("button2 long press hold\n");
break;
}
}
void button3_callback(void *btn)
{
uint32_t btn_event_val;
btn_event_val = get_button_event((struct button *)btn);
switch (btn_event_val)
{
case PRESS_DOWN:
rt_kprintf("button3 press down\n");
break;
case PRESS_UP:
rt_kprintf("button3 press up\n");
break;
case PRESS_REPEAT:
rt_kprintf("button3 press repeat\n");
break;
case SINGLE_CLICK:
beep(20);
rt_kprintf("button3 single click\n");
break;
case DOUBLE_CLICK:
rt_kprintf("button3 double click\n");
break;
case LONG_PRESS_START:
rt_kprintf("button3 long press start\n");
break;
case LONG_PRESS_HOLD:
rt_kprintf("button3 long press hold\n");
break;
}
}
void btn_thread_entry(void *p)
{
while (1)
{
/* 5ms */
rt_thread_delay(RT_TICK_PER_SECOND / 200);
button_ticks();
}
}
int multi_button_test(void)
{
rt_thread_t thread = RT_NULL;
/* Create background ticks thread */
thread = rt_thread_create("btn", btn_thread_entry, RT_NULL, 2048, 20, 10);
if (thread == RT_NULL)
{
return RT_ERROR;
}
rt_thread_startup(thread);
/* low level drive */
rt_pin_mode(KEY1_PIN,
PIN_MODE_INPUT_PULLDOWN); /* set KEY pin mode to input */
button_init(&btn1, button1_read_pin, PIN_HIGH);
button_attach(&btn1, PRESS_DOWN, button1_callback);
button_attach(&btn1, PRESS_UP, button1_callback);
button_attach(&btn1, PRESS_REPEAT, button1_callback);
button_attach(&btn1, SINGLE_CLICK, button1_callback);
button_attach(&btn1, DOUBLE_CLICK, button1_callback);
button_attach(&btn1, LONG_PRESS_START, button1_callback);
button_attach(&btn1, LONG_PRESS_HOLD, button1_callback);
button_start(&btn1);
rt_pin_mode(KEY2_PIN,
PIN_MODE_INPUT_PULLDOWN); /* set KEY pin mode to input */
button_init(&btn2, button2_read_pin, PIN_HIGH);
button_attach(&btn2, PRESS_DOWN, button2_callback);
button_attach(&btn2, PRESS_UP, button2_callback);
button_attach(&btn2, PRESS_REPEAT, button2_callback);
button_attach(&btn2, SINGLE_CLICK, button2_callback);
button_attach(&btn2, DOUBLE_CLICK, button2_callback);
button_attach(&btn2, LONG_PRESS_START, button2_callback);
button_attach(&btn2, LONG_PRESS_HOLD, button2_callback);
button_start(&btn2);
rt_pin_mode(KEY3_PIN,
PIN_MODE_INPUT_PULLDOWN); /* set KEY pin mode to input */
button_init(&btn3, button3_read_pin, PIN_HIGH);
button_attach(&btn3, PRESS_DOWN, button3_callback);
button_attach(&btn3, PRESS_UP, button3_callback);
button_attach(&btn3, PRESS_REPEAT, button3_callback);
button_attach(&btn3, SINGLE_CLICK, button3_callback);
button_attach(&btn3, DOUBLE_CLICK, button3_callback);
button_attach(&btn3, LONG_PRESS_START, button3_callback);
button_attach(&btn3, LONG_PRESS_HOLD, button3_callback);
button_start(&btn3);
return RT_EOK;
}
INIT_APP_EXPORT(multi_button_test);