iot_boardbutton.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2021 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "iot_boardbutton.h"
  16. #include "cmsis_os2.h"
  17. #include "iot_gpio.h"
  18. #include "iot_gpio_ex.h"
  19. #include "ohos_init.h"
  20. typedef enum {
  21. BUTTON_ID_F1 = 0,
  22. BUTTON_ID_F2,
  23. BUTTON_ID_MAX,
  24. }ButtonID;
  25. typedef struct {
  26. ButtonPressedCallback callback;
  27. char *args;
  28. }ButtonCallBackController;
  29. typedef struct {
  30. ButtonCallBackController controller[BUTTON_ID_MAX];
  31. }ButtonController;
  32. static ButtonController g_buttonController;
  33. #define BUTTON_F1_GPIO 11
  34. #define BUTTON_F2_GPIO 12
  35. /**
  36. * @brief Callback for F1 key
  37. *
  38. */
  39. static void F1Pressed(char* arg)
  40. {
  41. (void)arg;
  42. printf("%s:pressed \r\n", __FUNCTION__);
  43. if (g_buttonController.controller[BUTTON_ID_F1].callback != NULL) {
  44. g_buttonController.controller[BUTTON_ID_F1].callback(arg);
  45. }
  46. }
  47. /**
  48. * @brief Callback for F2 key
  49. *
  50. */
  51. static void F2Pressed(char* arg)
  52. {
  53. printf("%s:pressed \r\n", __FUNCTION__);
  54. if (g_buttonController.controller[BUTTON_ID_F2].callback != NULL) {
  55. g_buttonController.controller[BUTTON_ID_F2].callback(arg);
  56. }
  57. }
  58. int Board_IsButtonPressedF2(void)
  59. {
  60. int value = 1;
  61. int tmcnt = 10;
  62. IoTGpioInit(BUTTON_F2_GPIO);
  63. IoTGpioSetFunc(BUTTON_F2_GPIO, IOT_GPIO_FUNC_GPIO_12_GPIO);
  64. IoTGpioSetDir(BUTTON_F2_GPIO, IOT_GPIO_DIR_IN);
  65. while (--tmcnt > 0) {
  66. osDelay(20);
  67. if (IoTGpioGetInputVal(BUTTON_F2_GPIO, &value) != 0) {
  68. printf("[ERROR][%s|%d] IoTGpioGetInputVal failed! \n", __func__, __LINE__);
  69. break;
  70. }
  71. if (value) {
  72. break;
  73. }
  74. }
  75. return value ? 0 : 1;
  76. }
  77. int Board_InitButtonF1(ButtonPressedCallback callback, char *arg)
  78. {
  79. g_buttonController.controller[BUTTON_ID_F1].callback = callback;
  80. g_buttonController.controller[BUTTON_ID_F1].args = arg;
  81. // init gpio of F1 key and set it as the falling edge to trigger interrupt
  82. IoTGpioInit(BUTTON_F1_GPIO);
  83. IoTGpioSetFunc(BUTTON_F1_GPIO, IOT_GPIO_FUNC_GPIO_11_GPIO);
  84. IoTGpioSetDir(BUTTON_F1_GPIO, IOT_GPIO_DIR_IN);
  85. IoTGpioSetPull(BUTTON_F1_GPIO, IOT_GPIO_PULL_UP);
  86. IoTGpioRegisterIsrFunc(BUTTON_F1_GPIO, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, F1Pressed, NULL);
  87. return 0;
  88. }
  89. int Board_InitButtonF2(ButtonPressedCallback callback, char *arg)
  90. {
  91. g_buttonController.controller[BUTTON_ID_F2].callback = callback;
  92. g_buttonController.controller[BUTTON_ID_F2].args = arg;
  93. // init gpio of F2 key and set it as the falling edge to trigger interrupt
  94. IoTGpioInit(BUTTON_F2_GPIO);
  95. IoTGpioSetFunc(BUTTON_F2_GPIO, IOT_GPIO_FUNC_GPIO_12_GPIO);
  96. IoTGpioSetDir(BUTTON_F2_GPIO, IOT_GPIO_DIR_IN);
  97. IoTGpioSetPull(BUTTON_F2_GPIO, IOT_GPIO_PULL_UP);
  98. IoTGpioRegisterIsrFunc(BUTTON_F2_GPIO, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, F2Pressed, NULL);
  99. return 0;
  100. }