Skip to content

Commit 9c5e4e4

Browse files
committed
[drv] Add button device driver
1 parent 17552f6 commit 9c5e4e4

File tree

5 files changed

+254
-2
lines changed

5 files changed

+254
-2
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=RT-Thread
2-
version=0.7.5
2+
version=0.7.6
33
author=onelife <[email protected]>, Bernard Xiong <[email protected]>
44
maintainer=onelife <[email protected]>
55
sentence=Real Time Operating System porting for Arduino SAM and SAMD boards
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/***************************************************************************//**
2+
* @file drv_button.cpp
3+
* @brief Arduino RT-Thread library button device driver
4+
* @author onelife <onelife.real[at]gmail.com>
5+
******************************************************************************/
6+
/* Includes ------------------------------------------------------------------*/
7+
extern "C" {
8+
9+
#include "include/rtthread.h"
10+
11+
#if defined(CONFIG_ARDUINO) && CONFIG_USING_BUTTON
12+
}
13+
14+
#include <Arduino.h>
15+
16+
#if CONFIG_USING_GUI
17+
# include <rttgui.h>
18+
#endif
19+
20+
extern "C" {
21+
22+
#include "drv_button.h"
23+
24+
/***************************************************************************//**
25+
* @addtogroup Arduino
26+
* @{
27+
******************************************************************************/
28+
29+
/* Private typedef -----------------------------------------------------------*/
30+
typedef void (*rx_indicator)(void);
31+
32+
/* Private define ------------------------------------------------------------*/
33+
#ifdef RT_USING_ULOG
34+
# ifdef BSP_BUTTON_DEBUG
35+
# define LOG_LVL LOG_LVL_DBG
36+
# else
37+
# define LOG_LVL LOG_LVL_INFO
38+
# endif
39+
# define LOG_TAG "BTN"
40+
# include "components/utilities/ulog/ulog.h"
41+
#else /* RT_USING_ULOG */
42+
# define LOG_E(format, args...) rt_kprintf(format "\n", ##args)
43+
# define LOG_W LOG_E
44+
# ifdef BSP_SPI_DEBUG
45+
# define LOG_D(format, args...) rt_kprintf(format "\n", ##args)
46+
# else
47+
# define LOG_D(format, args...)
48+
# endif
49+
# define LOG_I LOG_D
50+
# define LOG_HEX(format, args...)
51+
#endif /* RT_USING_ULOG */
52+
53+
#define BTN_IO_INIT(pin) pinMode(pin, INPUT_PULLUP)
54+
#define BTN_STATE(pin) digitalRead(pin)
55+
#define BTN_CTX() (&btn_ctx)
56+
57+
/* Private function prototypes -----------------------------------------------*/
58+
/* Private constants ---------------------------------------------------------*/
59+
/* Private variables ---------------------------------------------------------*/
60+
static struct bsp_btn_contex btn_ctx;
61+
static rx_indicator key_indicator = RT_NULL;
62+
static rt_uint32_t btn_pin[CONFIG_USING_BUTTON] = CONFIG_BUTTON_PIN;
63+
static rt_uint16_t btn_key[CONFIG_USING_BUTTON] = CONFIG_BUTTON_CODE;
64+
static rt_bool_t btn_state[CONFIG_USING_BUTTON];
65+
static rt_bool_t btn_change[CONFIG_USING_BUTTON];
66+
67+
/* Private functions ---------------------------------------------------------*/
68+
static void _btn_timeout(void *param) {
69+
struct bsp_btn_contex *ctx = BTN_CTX();
70+
rt_bool_t changed = RT_FALSE;
71+
rt_uint32_t i;
72+
(void)param;
73+
74+
for (i = 0; i < ctx->num; i++) {
75+
if (btn_state[i] != BTN_STATE(btn_pin[i])) {
76+
btn_state[i] = !btn_state[i];
77+
btn_change[i] = RT_TRUE;
78+
changed = RT_TRUE;
79+
}
80+
}
81+
if (key_indicator && changed) key_indicator();
82+
}
83+
84+
static rt_size_t bsp_button_read(rt_device_t dev, rt_off_t pos, void *buf,
85+
rt_size_t size) {
86+
struct bsp_btn_contex *ctx = (struct bsp_btn_contex *)(dev->user_data);
87+
rt_uint32_t i;
88+
(void)dev;
89+
(void)pos;
90+
(void)size;
91+
92+
for (i = 0; i < ctx->num; i++) {
93+
if (btn_change[i]) {
94+
#if CONFIG_USING_GUI
95+
rtgui_key_t *data = (rtgui_key_t *)buf;
96+
data->key = (rtgui_kbd_key_t)btn_key[i];
97+
data->mod = RTGUI_KMOD_NONE;
98+
data->type = btn_state[i] ? RTGUI_KEYUP : RTGUI_KEYDOWN;
99+
100+
#else
101+
rt_uint16_t *data = (rt_uint16_t *)buf;
102+
*data = btn_key[i];
103+
104+
#endif
105+
btn_change[i] = RT_FALSE;
106+
// LOG_D("[BTN] read %d %d", btn_key[i], btn_state[i]);
107+
return 1;
108+
}
109+
}
110+
return 0;
111+
}
112+
113+
static rt_err_t bsp_button_control(rt_device_t dev, rt_int32_t cmd, void *args) {
114+
rt_err_t ret = -RT_ERROR;
115+
(void)dev;
116+
117+
switch (cmd) {
118+
case RT_DEVICE_CTRL_SET_RX_INDICATOR:
119+
key_indicator = (rx_indicator)args;
120+
ret = RT_EOK;
121+
break;
122+
123+
default:
124+
break;
125+
}
126+
127+
return ret;
128+
}
129+
130+
/***************************************************************************//**
131+
* @brief Initialize FT6206 contex
132+
*
133+
* @param[in] struct bsp_btn_contex *ctx - Pointer to button contex
134+
*
135+
* @param[in] const char *name - Pointer to button device name
136+
*
137+
* @return rt_err_t - Error code
138+
*
139+
******************************************************************************/
140+
static rt_err_t bsp_button_contex_init(struct bsp_btn_contex *ctx,
141+
const char *name) {
142+
rt_err_t ret;
143+
144+
do {
145+
ctx->num = CONFIG_USING_BUTTON;
146+
rt_timer_init(&ctx->tmr, name, _btn_timeout, RT_NULL,
147+
BTN_CHECK_TICK, RT_TIMER_FLAG_PERIODIC);
148+
149+
/* register device */
150+
ctx->dev.type = RT_Device_Class_Miscellaneous;
151+
ctx->dev.rx_indicate = RT_NULL;
152+
ctx->dev.tx_complete = RT_NULL;
153+
ctx->dev.init = RT_NULL;
154+
ctx->dev.open = RT_NULL;
155+
ctx->dev.close = RT_NULL;
156+
ctx->dev.read = bsp_button_read;
157+
ctx->dev.write = RT_NULL;
158+
ctx->dev.control = bsp_button_control;
159+
ctx->dev.user_data = (void *)ctx;
160+
ret = rt_device_register(&ctx->dev, name,
161+
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
162+
if (RT_EOK != ret) break;
163+
ret = rt_timer_start(&ctx->tmr);
164+
} while (0);
165+
166+
return ret;
167+
}
168+
169+
/* Public functions ----------------------------------------------------------*/
170+
/***************************************************************************//**
171+
* @brief - initialize TFT6206 contex and hardware
172+
*
173+
* @return rt_err_t - Error code
174+
*
175+
******************************************************************************/
176+
rt_err_t bsp_hw_button_init(void) {
177+
rt_err_t ret;
178+
179+
do {
180+
rt_uint32_t i;
181+
182+
ret = bsp_button_contex_init(BTN_CTX(), BTN_NAME);
183+
if (RT_EOK != ret) break;
184+
185+
for (i = 0; i < CONFIG_USING_BUTTON; i++) {
186+
BTN_IO_INIT(btn_pin[i]);
187+
btn_state[i] = BTN_STATE(btn_pin[i]);
188+
btn_change[i] = RT_FALSE;
189+
}
190+
key_indicator = RT_NULL;
191+
LOG_D("[BTN] h/w init ok");
192+
} while (0);
193+
194+
if (RT_EOK != ret) {
195+
LOG_E("[BTN E] h/w init failed: %d", ret);
196+
}
197+
return ret;
198+
}
199+
200+
/***************************************************************************//**
201+
* @}
202+
******************************************************************************/
203+
204+
#endif /* defined(CONFIG_ARDUINO) && CONFIG_USING_BUTTON */
205+
206+
} /* extern "C" */
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/***************************************************************************//**
2+
* @file drv_button.h
3+
* @brief Arduino RT-Thread library FT6202 device driver header
4+
* @author onelife <onelife.real[at]gmail.com>
5+
******************************************************************************/
6+
#ifndef __DRV_BUTTON_H__
7+
#define __DRV_BUTTON_H__
8+
9+
/* Includes ------------------------------------------------------------------*/
10+
#include "drv_common.h"
11+
#if CONFIG_USING_GUI
12+
# include "include/rtgui.h"
13+
#endif
14+
15+
/* Exported defines ----------------------------------------------------------*/
16+
#define BTN_NAME "BTN"
17+
#define BTN_CHECK_TICK (4) /* 40ms */
18+
19+
/* Exported types ------------------------------------------------------------*/
20+
struct bsp_btn_contex {
21+
struct rt_device dev; /* RT device */
22+
rt_uint32_t num;
23+
struct rt_timer tmr;
24+
};
25+
26+
/* Exported constants --------------------------------------------------------*/
27+
/* Exported functions ------------------------------------------------------- */
28+
rt_err_t bsp_hw_button_init(void);
29+
30+
#endif /* __DRV_BUTTON_H__ */

src/rtconfig.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
# define CONFIG_SSD_PWR_PIN (27)
5555
# define CONFIG_SSD_SPI_CHANNEL 1
5656

57+
# define CONFIG_USING_BUTTON (6)
58+
/* UP, DOWN, LEFT, RIGHT, A, B */
59+
# define CONFIG_BUTTON_PIN { 42, 19, 25, 15, 45, 44 }
60+
# define CONFIG_BUTTON_CODE { 273, 274, 276, 275, 97, 98 }
61+
5762
# define CONFIG_USING_GUI (1)
5863
# define CONFIG_GUI_WIDTH (96)
5964
# define CONFIG_GUI_HIGH (64)
@@ -122,6 +127,10 @@
122127
# define CONFIG_USING_GUI (0)
123128
#endif
124129

130+
#ifndef CONFIG_USING_BUTTON
131+
# define CONFIG_USING_BUTTON (0)
132+
#endif
133+
125134
#ifndef CONFIG_USING_SPISD
126135
# define CONFIG_USING_SPISD (0)
127136
#endif
@@ -251,7 +260,7 @@
251260

252261
/* Arduino Thread Options */
253262
#ifndef CONFIG_ARDUINO_STACK_SIZE
254-
# define CONFIG_ARDUINO_STACK_SIZE (4 * 1024)
263+
# define CONFIG_ARDUINO_STACK_SIZE (2 * 1024)
255264
#endif
256265
#ifndef CONFIG_ARDUINO_PRIORITY
257266
# define CONFIG_ARDUINO_PRIORITY (RT_THREAD_PRIORITY_MAX >> 1)

src/rtt.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ extern "C" {
182182
#ifdef RT_USING_MODULE
183183
# include "components/libc/libdl/dlmodule.h"
184184
#endif
185+
#if CONFIG_USING_BUTTON
186+
# include "components/arduino/drv_button.h"
187+
#endif
185188
#if CONFIG_USING_SPISD
186189
# include "components/arduino/drv_spisd.h"
187190
#endif
@@ -226,6 +229,10 @@ void rt_driver_init(void) {
226229
void rt_high_driver_init(void) {
227230
rt_err_t ret;
228231

232+
#if CONFIG_USING_BUTTON
233+
ret = bsp_hw_button_init();
234+
RT_ASSERT(RT_EOK == ret);
235+
#endif
229236
#if CONFIG_USING_SPISD
230237
ret = bsp_hw_spiSd_init();
231238
RT_ASSERT(RT_EOK == ret);

0 commit comments

Comments
 (0)