123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #include "iot_boardbutton.h"
- #include "cmsis_os2.h"
- #include "iot_gpio.h"
- #include "iot_gpio_ex.h"
- #include "ohos_init.h"
- typedef enum {
- BUTTON_ID_F1 = 0,
- BUTTON_ID_F2,
- BUTTON_ID_MAX,
- }ButtonID;
- typedef struct {
- ButtonPressedCallback callback;
- char *args;
- }ButtonCallBackController;
- typedef struct {
- ButtonCallBackController controller[BUTTON_ID_MAX];
- }ButtonController;
- static ButtonController g_buttonController;
- #define BUTTON_F1_GPIO 11
- #define BUTTON_F2_GPIO 12
- static void F1Pressed(char* arg)
- {
- (void)arg;
- printf("%s:pressed \r\n", __FUNCTION__);
- if (g_buttonController.controller[BUTTON_ID_F1].callback != NULL) {
- g_buttonController.controller[BUTTON_ID_F1].callback(arg);
- }
- }
- static void F2Pressed(char* arg)
- {
- printf("%s:pressed \r\n", __FUNCTION__);
- if (g_buttonController.controller[BUTTON_ID_F2].callback != NULL) {
- g_buttonController.controller[BUTTON_ID_F2].callback(arg);
- }
- }
- int Board_IsButtonPressedF2(void)
- {
- int value = 1;
- int tmcnt = 10;
- IoTGpioInit(BUTTON_F2_GPIO);
- IoTGpioSetFunc(BUTTON_F2_GPIO, IOT_GPIO_FUNC_GPIO_12_GPIO);
- IoTGpioSetDir(BUTTON_F2_GPIO, IOT_GPIO_DIR_IN);
- while (--tmcnt > 0) {
- osDelay(20);
- if (IoTGpioGetInputVal(BUTTON_F2_GPIO, &value) != 0) {
- printf("[ERROR][%s|%d] IoTGpioGetInputVal failed! \n", __func__, __LINE__);
- break;
- }
- if (value) {
- break;
- }
- }
- return value ? 0 : 1;
- }
- int Board_InitButtonF1(ButtonPressedCallback callback, char *arg)
- {
- g_buttonController.controller[BUTTON_ID_F1].callback = callback;
- g_buttonController.controller[BUTTON_ID_F1].args = arg;
-
- IoTGpioInit(BUTTON_F1_GPIO);
- IoTGpioSetFunc(BUTTON_F1_GPIO, IOT_GPIO_FUNC_GPIO_11_GPIO);
- IoTGpioSetDir(BUTTON_F1_GPIO, IOT_GPIO_DIR_IN);
- IoTGpioSetPull(BUTTON_F1_GPIO, IOT_GPIO_PULL_UP);
- IoTGpioRegisterIsrFunc(BUTTON_F1_GPIO, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, F1Pressed, NULL);
- return 0;
- }
- int Board_InitButtonF2(ButtonPressedCallback callback, char *arg)
- {
- g_buttonController.controller[BUTTON_ID_F2].callback = callback;
- g_buttonController.controller[BUTTON_ID_F2].args = arg;
-
- IoTGpioInit(BUTTON_F2_GPIO);
- IoTGpioSetFunc(BUTTON_F2_GPIO, IOT_GPIO_FUNC_GPIO_12_GPIO);
- IoTGpioSetDir(BUTTON_F2_GPIO, IOT_GPIO_DIR_IN);
- IoTGpioSetPull(BUTTON_F2_GPIO, IOT_GPIO_PULL_UP);
- IoTGpioRegisterIsrFunc(BUTTON_F2_GPIO, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, F2Pressed, NULL);
- return 0;
- }
|