ButtonHandler.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. *
  3. * Copyright (c) 2022 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "ButtonHandler.h"
  19. #include "AppConfig.h"
  20. #include "AppTask.h"
  21. #include <FreeRTOS.h>
  22. #ifdef CFG_PLF_RV32
  23. #define duet_gpio_dev_t asr_gpio_dev_t
  24. #define duet_gpio_init asr_gpio_init
  25. #define duet_gpio_input_get asr_gpio_input_get
  26. #define DUET_INPUT_PULL_UP ASR_INPUT_PULL_UP
  27. #elif !defined CFG_PLF_DUET
  28. #define duet_gpio_dev_t lega_gpio_dev_t
  29. #define duet_gpio_init lega_gpio_init
  30. #define duet_gpio_input_get lega_gpio_input_get
  31. #define DUET_INPUT_PULL_UP LEGA_INPUT_PULL_UP
  32. #endif
  33. TaskHandle_t sGpioTaskHandle;
  34. static void GpioTaskMain(void * pvParameter);
  35. void ButtonHandler::Init(void)
  36. {
  37. GpioInit();
  38. xTaskCreate(GpioTaskMain, GPIO_TASK_NAME, GPIO_TASK_STACK_SIZE, 0, 2, &sGpioTaskHandle);
  39. }
  40. // port pin
  41. duet_gpio_dev_t switch1_btn;
  42. duet_gpio_dev_t switch2_btn;
  43. void ButtonHandler::GpioInit(void)
  44. {
  45. // light switch1 button
  46. switch1_btn.port = SWITCH1_BUTTON;
  47. switch1_btn.config = DUET_INPUT_PULL_UP;
  48. switch1_btn.priv = NULL;
  49. duet_gpio_init(&switch1_btn);
  50. // generic switch2 button
  51. switch2_btn.port = SWITCH2_BUTTON;
  52. switch2_btn.config = DUET_INPUT_PULL_UP;
  53. switch2_btn.priv = NULL;
  54. duet_gpio_init(&switch2_btn);
  55. }
  56. static uint32_t btn1Value = 1;
  57. static uint8_t btn1_trigger = 0;
  58. static uint32_t btn2Value = 1;
  59. static uint8_t btn2_trigger = 0;
  60. void GpioTaskMain(void * pvParameter)
  61. {
  62. ASR_LOG("GPIO Task started");
  63. uint32_t btnValue;
  64. uint8_t buttonevent = 0;
  65. while (true)
  66. {
  67. vTaskDelay(50 / portTICK_PERIOD_MS);
  68. // switch button 1
  69. duet_gpio_input_get(&switch1_btn, &btnValue);
  70. if (btnValue != btn1Value)
  71. {
  72. if (btn1_trigger)
  73. {
  74. btn1Value = btnValue;
  75. buttonevent = (uint8_t) btnValue;
  76. GetAppTask().ButtonEventHandler(SWITCH1_BUTTON, (buttonevent) ? BUTTON_RELEASED : BUTTON_PRESSED);
  77. btn1_trigger = 0;
  78. }
  79. else
  80. {
  81. btn1_trigger = 1;
  82. }
  83. }
  84. else
  85. {
  86. btn1_trigger = 0;
  87. }
  88. // switch button 2
  89. duet_gpio_input_get(&switch2_btn, &btnValue);
  90. if (btnValue != btn2Value)
  91. {
  92. if (btn2_trigger)
  93. {
  94. btn2Value = btnValue;
  95. buttonevent = (uint8_t) btnValue;
  96. GetAppTask().ButtonEventHandler(SWITCH2_BUTTON, (buttonevent) ? BUTTON_RELEASED : BUTTON_PRESSED);
  97. btn2_trigger = 0;
  98. }
  99. else
  100. {
  101. btn2_trigger = 1;
  102. }
  103. }
  104. else
  105. {
  106. btn2_trigger = 0;
  107. }
  108. }
  109. }