key.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * ESPRSSIF MIT License
  3. *
  4. * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #include "ets_sys.h"
  25. #include "os_type.h"
  26. #include "osapi.h"
  27. #include "mem.h"
  28. #include "gpio.h"
  29. #include "user_interface.h"
  30. #include "driver/key.h"
  31. LOCAL void key_intr_handler(struct keys_param *keys);
  32. /******************************************************************************
  33. * FunctionName : key_init_single
  34. * Description : init single key's gpio and register function
  35. * Parameters : uint8 gpio_id - which gpio to use
  36. * uint32 gpio_name - gpio mux name
  37. * uint32 gpio_func - gpio function
  38. * key_function long_press - long press function, needed to install
  39. * key_function short_press - short press function, needed to install
  40. * Returns : single_key_param - single key parameter, needed by key init
  41. *******************************************************************************/
  42. struct single_key_param *ICACHE_FLASH_ATTR
  43. key_init_single(uint8 gpio_id, uint32 gpio_name, uint8 gpio_func, key_function long_press, key_function short_press)
  44. {
  45. struct single_key_param *single_key = (struct single_key_param *)os_zalloc(sizeof(struct single_key_param));
  46. single_key->gpio_id = gpio_id;
  47. single_key->gpio_name = gpio_name;
  48. single_key->gpio_func = gpio_func;
  49. single_key->long_press = long_press;
  50. single_key->short_press = short_press;
  51. return single_key;
  52. }
  53. /******************************************************************************
  54. * FunctionName : key_init
  55. * Description : init keys
  56. * Parameters : key_param *keys - keys parameter, which inited by key_init_single
  57. * Returns : none
  58. *******************************************************************************/
  59. void ICACHE_FLASH_ATTR
  60. key_init(struct keys_param *keys)
  61. {
  62. uint8 i;
  63. ETS_GPIO_INTR_ATTACH(key_intr_handler, keys);
  64. ETS_GPIO_INTR_DISABLE();
  65. for (i = 0; i < keys->key_num; i++) {
  66. keys->single_key[i]->key_level = 1;
  67. PIN_FUNC_SELECT(keys->single_key[i]->gpio_name, keys->single_key[i]->gpio_func);
  68. gpio_output_set(0, 0, 0, GPIO_ID_PIN(keys->single_key[i]->gpio_id));
  69. gpio_register_set(GPIO_PIN_ADDR(keys->single_key[i]->gpio_id), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE)
  70. | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE)
  71. | GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE));
  72. //clear gpio14 status
  73. GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(keys->single_key[i]->gpio_id));
  74. //enable interrupt
  75. gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_NEGEDGE);
  76. }
  77. ETS_GPIO_INTR_ENABLE();
  78. }
  79. /******************************************************************************
  80. * FunctionName : key_5s_cb
  81. * Description : long press 5s timer callback
  82. * Parameters : single_key_param *single_key - single key parameter
  83. * Returns : none
  84. *******************************************************************************/
  85. LOCAL void ICACHE_FLASH_ATTR
  86. key_5s_cb(struct single_key_param *single_key)
  87. {
  88. os_timer_disarm(&single_key->key_5s);
  89. // low, then restart
  90. if (0 == GPIO_INPUT_GET(GPIO_ID_PIN(single_key->gpio_id))) {
  91. if (single_key->long_press) {
  92. single_key->long_press();
  93. }
  94. }
  95. }
  96. /******************************************************************************
  97. * FunctionName : key_50ms_cb
  98. * Description : 50ms timer callback to check it's a real key push
  99. * Parameters : single_key_param *single_key - single key parameter
  100. * Returns : none
  101. *******************************************************************************/
  102. LOCAL void ICACHE_FLASH_ATTR
  103. key_50ms_cb(struct single_key_param *single_key)
  104. {
  105. os_timer_disarm(&single_key->key_50ms);
  106. // high, then key is up
  107. if (1 == GPIO_INPUT_GET(GPIO_ID_PIN(single_key->gpio_id))) {
  108. os_timer_disarm(&single_key->key_5s);
  109. single_key->key_level = 1;
  110. gpio_pin_intr_state_set(GPIO_ID_PIN(single_key->gpio_id), GPIO_PIN_INTR_NEGEDGE);
  111. if (single_key->short_press) {
  112. single_key->short_press();
  113. }
  114. } else {
  115. gpio_pin_intr_state_set(GPIO_ID_PIN(single_key->gpio_id), GPIO_PIN_INTR_POSEDGE);
  116. }
  117. }
  118. /******************************************************************************
  119. * FunctionName : key_intr_handler
  120. * Description : key interrupt handler
  121. * Parameters : key_param *keys - keys parameter, which inited by key_init_single
  122. * Returns : none
  123. *******************************************************************************/
  124. LOCAL void
  125. key_intr_handler(struct keys_param *keys)
  126. {
  127. uint8 i;
  128. uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
  129. for (i = 0; i < keys->key_num; i++) {
  130. if (gpio_status & BIT(keys->single_key[i]->gpio_id)) {
  131. //disable interrupt
  132. gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_DISABLE);
  133. //clear interrupt status
  134. GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(keys->single_key[i]->gpio_id));
  135. if (keys->single_key[i]->key_level == 1) {
  136. // 5s, restart & enter softap mode
  137. os_timer_disarm(&keys->single_key[i]->key_5s);
  138. os_timer_setfn(&keys->single_key[i]->key_5s, (os_timer_func_t *)key_5s_cb, keys->single_key[i]);
  139. os_timer_arm(&keys->single_key[i]->key_5s, 5000, 0);
  140. keys->single_key[i]->key_level = 0;
  141. gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_POSEDGE);
  142. } else {
  143. // 50ms, check if this is a real key up
  144. os_timer_disarm(&keys->single_key[i]->key_50ms);
  145. os_timer_setfn(&keys->single_key[i]->key_50ms, (os_timer_func_t *)key_50ms_cb, keys->single_key[i]);
  146. os_timer_arm(&keys->single_key[i]->key_50ms, 50, 0);
  147. }
  148. }
  149. }
  150. }