u8x8_debounce.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. u8x8_debounce.c
  3. Key/button simple debounce algorithm (Addon for u8x8)
  4. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  5. Copyright (c) 2016, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "u8x8.h"
  29. static uint8_t u8x8_read_pin_state(u8x8_t *u8x8)
  30. {
  31. uint8_t i;
  32. uint8_t pin_state;
  33. pin_state = 255; /* be compatible with the setup of the default pin setup, which is 255 */
  34. for( i = 0; i < U8X8_PIN_INPUT_CNT; i++ )
  35. {
  36. pin_state <<= 1;
  37. /* the callback function should put the return value into this variable */
  38. u8x8->gpio_result = 1;
  39. u8x8_gpio_call(u8x8, U8X8_MSG_GPIO(i+U8X8_PIN_OUTPUT_CNT), 0);
  40. pin_state |= u8x8->gpio_result & 1;
  41. }
  42. return pin_state;
  43. }
  44. /*
  45. return 0 to U8X8_PIN_INPUT_CNT-1 if there is a difference
  46. return U8X8_PIN_INPUT_CNT if there is no difference
  47. */
  48. static uint8_t u8x8_find_first_diff(uint8_t a, uint8_t b)
  49. {
  50. uint8_t mask;
  51. uint8_t i;
  52. mask = 1;
  53. i = U8X8_PIN_INPUT_CNT;
  54. do
  55. {
  56. i--;
  57. if ( (a & mask) != (b & mask) )
  58. return i;
  59. mask <<= 1;
  60. } while( i > 0 );
  61. return U8X8_PIN_INPUT_CNT;
  62. }
  63. /*
  64. State A:
  65. u8x8->debounce_last_pin_state == current_state
  66. --> State A
  67. u8x8->debounce_last_pin_state != current_state
  68. --> u8x8->debounce_last_pin_state = current_state
  69. --> State B + cnt
  70. State B + cnt
  71. --> state--
  72. State B
  73. u8x8->debounce_last_pin_state == current_state
  74. --> keypress detected
  75. --> State C
  76. u8x8->debounce_last_pin_state != current_state
  77. --> State A
  78. State C
  79. u8x8->debounce_last_pin_state == current_state
  80. --> State C
  81. u8x8->debounce_last_pin_state != current_state
  82. --> State A
  83. */
  84. #ifdef __unix__xxxxxx_THIS_IS_DISABLED
  85. #include <stdio.h>
  86. #include <stdlib.h>
  87. uint8_t u8x8_GetMenuEvent(u8x8_t *u8x8)
  88. {
  89. int c;
  90. c = getc(stdin);
  91. switch(c)
  92. {
  93. case 'n':
  94. return U8X8_MSG_GPIO_MENU_NEXT;
  95. case 'p':
  96. return U8X8_MSG_GPIO_MENU_PREV;
  97. case 's':
  98. return U8X8_MSG_GPIO_MENU_SELECT;
  99. case 'h':
  100. return U8X8_MSG_GPIO_MENU_HOME;
  101. case 'x':
  102. exit(0);
  103. default:
  104. puts("press n, p, s, h or x");
  105. break;
  106. }
  107. return 0;
  108. }
  109. #else /* __unix__ */
  110. #define U8X8_DEBOUNCE_WAIT 2
  111. /* do debounce and return a GPIO msg which indicates the event */
  112. /* returns 0, if there is no event */
  113. #if defined(__GNUC__) && !defined(__CYGWIN__)
  114. # pragma weak u8x8_GetMenuEvent
  115. #endif
  116. uint8_t u8x8_GetMenuEvent(u8x8_t *u8x8)
  117. {
  118. uint8_t pin_state;
  119. uint8_t result_msg = 0; /* invalid message, no event */
  120. pin_state = u8x8_read_pin_state(u8x8);
  121. /* States A, B, C & D are encoded in the upper 4 bit*/
  122. switch(u8x8->debounce_state)
  123. {
  124. case 0x00: /* State A, default state */
  125. if ( u8x8->debounce_default_pin_state != pin_state )
  126. {
  127. //u8x8->debounce_last_pin_state = pin_state;
  128. u8x8->debounce_state = 0x010 + U8X8_DEBOUNCE_WAIT;
  129. }
  130. break;
  131. case 0x10: /* State B */
  132. //if ( u8x8->debounce_last_pin_state != pin_state )
  133. if ( u8x8->debounce_default_pin_state == pin_state )
  134. {
  135. u8x8->debounce_state = 0x00; /* back to state A */
  136. }
  137. else
  138. {
  139. /* keypress detected */
  140. u8x8->debounce_last_pin_state = pin_state;
  141. //result_msg = U8X8_MSG_GPIO_MENU_NEXT;
  142. u8x8->debounce_state = 0x020 + U8X8_DEBOUNCE_WAIT; /* got to state C */
  143. }
  144. break;
  145. case 0x20: /* State C */
  146. if ( u8x8->debounce_last_pin_state != pin_state )
  147. {
  148. u8x8->debounce_state = 0x00; /* back to state A */
  149. }
  150. else
  151. {
  152. u8x8->debounce_state = 0x030; /* got to state D */
  153. }
  154. break;
  155. case 0x30: /* State D */
  156. /* wait until key release */
  157. if ( u8x8->debounce_default_pin_state == pin_state )
  158. {
  159. u8x8->debounce_state = 0x00; /* back to state A */
  160. result_msg = U8X8_MSG_GPIO(u8x8_find_first_diff(u8x8->debounce_default_pin_state, u8x8->debounce_last_pin_state)+U8X8_PIN_OUTPUT_CNT);
  161. }
  162. else
  163. {
  164. //result_msg = U8X8_MSG_GPIO_MENU_NEXT;
  165. // maybe implement autorepeat here
  166. }
  167. break;
  168. default:
  169. u8x8->debounce_state--; /* count down, until there is a valid state */
  170. break;
  171. }
  172. return result_msg;
  173. }
  174. #endif /* __unix__ */