adkey.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "key_driver.h"
  2. #include "adkey.h"
  3. #include "gpio.h"
  4. #include "system/event.h"
  5. #include "app_config.h"
  6. #if TCFG_ADKEY_ENABLE
  7. static const struct adkey_platform_data *__this = NULL;
  8. u8 ad_get_key_value(void);
  9. //按键驱动扫描参数列表
  10. struct key_driver_para adkey_scan_para = {
  11. .scan_time = 10, //按键扫描频率, 单位: ms
  12. .last_key = NO_KEY, //上一次get_value按键值, 初始化为NO_KEY;
  13. .filter_time = 2, //按键消抖延时;
  14. .long_time = 75, //按键判定长按数量
  15. .hold_time = (75 + 15), //按键判定HOLD数量
  16. .click_delay_time = 20, //按键被抬起后等待连击延时数量
  17. .key_type = KEY_DRIVER_TYPE_AD,
  18. .get_value = ad_get_key_value,
  19. };
  20. u8 ad_get_key_value(void)
  21. {
  22. u8 i;
  23. u16 ad_data;
  24. if (!__this->enable) {
  25. return NO_KEY;
  26. }
  27. /* ad_data = adc_get_voltage(__this->ad_channel); */
  28. ad_data = adc_get_value(__this->ad_channel);
  29. /* printf("ad_value = %d \n", ad_data); */
  30. for (i = 0; i < ADKEY_MAX_NUM; i++) {
  31. if ((ad_data <= __this->ad_value[i]) && (__this->ad_value[i] < 0x3ffL)) {
  32. return __this->key_value[i];
  33. }
  34. }
  35. return NO_KEY;
  36. }
  37. int adkey_init(const struct adkey_platform_data *adkey_data)
  38. {
  39. __this = adkey_data;
  40. if (!__this) {
  41. return -EINVAL;
  42. }
  43. if (!__this->enable) {
  44. return KEY_NOT_SUPPORT;
  45. }
  46. adc_add_sample_ch(__this->ad_channel); //注意:初始化AD_KEY之前,先初始化ADC
  47. #if (TCFG_ADKEY_LED_IO_REUSE || TCFG_ADKEY_IR_IO_REUSE || TCFG_ADKEY_LED_SPI_IO_REUSE)
  48. #else
  49. gpio_set_die(__this->adkey_pin, 0);
  50. gpio_set_direction(__this->adkey_pin, 1);
  51. gpio_set_pull_down(__this->adkey_pin, 0);
  52. if (__this->extern_up_en) {
  53. gpio_set_pull_up(__this->adkey_pin, 0);
  54. } else {
  55. gpio_set_pull_up(__this->adkey_pin, 1);
  56. }
  57. #endif
  58. return 0;
  59. }
  60. #if (TCFG_ADKEY_LED_IO_REUSE || TCFG_ADKEY_IR_IO_REUSE || TCFG_ADKEY_LED_SPI_IO_REUSE)
  61. #if TCFG_ADKEY_IR_IO_REUSE
  62. static u8 ir_io_sus = 0;
  63. extern u8 ir_io_suspend(void);
  64. extern u8 ir_io_resume(void);
  65. #endif
  66. #if TCFG_ADKEY_LED_IO_REUSE
  67. static u8 led_io_sus = 0;
  68. extern u8 led_io_suspend(void);
  69. extern u8 led_io_resume(void);
  70. #endif
  71. #if TCFG_ADKEY_LED_SPI_IO_REUSE
  72. static u8 led_spi_sus = 0;
  73. extern u8 led_spi_suspend(void);
  74. extern u8 led_spi_resume(void);
  75. #endif
  76. u8 adc_io_reuse_enter(u32 ch)
  77. {
  78. if (ch == __this->ad_channel) {
  79. #if TCFG_ADKEY_IR_IO_REUSE
  80. if (ir_io_suspend()) {
  81. return 1;
  82. } else {
  83. ir_io_sus = 1;
  84. }
  85. #endif
  86. #if TCFG_ADKEY_LED_IO_REUSE
  87. if (led_io_suspend()) {
  88. return 1;
  89. } else {
  90. led_io_sus = 1;
  91. }
  92. #endif
  93. #if TCFG_ADKEY_LED_SPI_IO_REUSE
  94. if (led_spi_suspend()) {
  95. return 1;
  96. } else {
  97. led_spi_sus = 1;
  98. }
  99. #endif
  100. gpio_set_die(__this->adkey_pin, 0);
  101. gpio_set_direction(__this->adkey_pin, 1);
  102. gpio_set_pull_down(__this->adkey_pin, 0);
  103. if (__this->extern_up_en) {
  104. gpio_set_pull_up(__this->adkey_pin, 0);
  105. } else {
  106. gpio_set_pull_up(__this->adkey_pin, 1);
  107. }
  108. }
  109. return 0;
  110. }
  111. u8 adc_io_reuse_exit(u32 ch)
  112. {
  113. if (ch == __this->ad_channel) {
  114. #if TCFG_ADKEY_IR_IO_REUSE
  115. if (ir_io_sus) {
  116. ir_io_sus = 0;
  117. ir_io_resume();
  118. }
  119. #endif
  120. #if TCFG_ADKEY_LED_IO_REUSE
  121. if (led_io_sus) {
  122. led_io_sus = 0;
  123. led_io_resume();
  124. }
  125. #endif
  126. #if TCFG_ADKEY_LED_SPI_IO_REUSE
  127. if (led_spi_sus) {
  128. led_spi_sus = 0;
  129. led_spi_resume();
  130. }
  131. #endif
  132. }
  133. return 0;
  134. }
  135. #endif
  136. #endif /* #if TCFG_ADKEY_ENABLE */