You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

tentapad.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Copyright 2014 Kai Ryu <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <util/delay.h>
  16. #include "action.h"
  17. #include "action_layer.h"
  18. #include "backlight.h"
  19. #include "softpwm_led.h"
  20. #include "eeconfig.h"
  21. #include "keymap_common.h"
  22. #include "vibration.h"
  23. #include "pitches.h"
  24. #include "buzzer.h"
  25. #include "tentapad.h"
  26. #include "debug.h"
  27. static const uint16_t tones[] PROGMEM = {
  28. NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5,
  29. NOTE_C6, NOTE_D6, NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6,
  30. NOTE_C7
  31. };
  32. uint8_t config_mode = 0;
  33. static uint8_t layer = 0;
  34. static uint8_t backlight = 0;
  35. static uint8_t layer_modified = 0;
  36. static uint8_t backlight_modified = 0;
  37. extern uint8_t backlight_mode;
  38. extern const uint8_t backlight_brightness_mid;
  39. extern const uint8_t backlight_brightness_high;
  40. void action_keyevent(keyevent_t event)
  41. {
  42. uint8_t key = event.key.col;
  43. if (config_mode) {
  44. /* config mode */
  45. switch (key) {
  46. case KEY_K1:
  47. if (event.pressed) {
  48. switch_layout();
  49. }
  50. break;
  51. case KEY_K2:
  52. if (event.pressed) {
  53. switch_backlight();
  54. }
  55. break;
  56. case KEY_CFG:
  57. if (event.pressed) {
  58. exit_config_mode();
  59. }
  60. break;
  61. }
  62. }
  63. else {
  64. /* normal mode */
  65. switch (key) {
  66. case KEY_K1: case KEY_K2:
  67. if (event.pressed) {
  68. /* press */
  69. switch (backlight_mode) {
  70. case 0: case 6:
  71. softpwm_led_on(key);
  72. break;
  73. case 1: case 2:
  74. softpwm_led_increase(LED_KEY_SIDE - 1 + backlight_mode, 32);
  75. case 3 ... 5:
  76. softpwm_led_set(key, backlight_brightness_high);
  77. break;
  78. }
  79. }
  80. else {
  81. /* release */
  82. switch (backlight_mode) {
  83. case 0: case 6:
  84. softpwm_led_off(key);
  85. break;
  86. case 1 ... 5:
  87. softpwm_led_set(key, 0);
  88. break;
  89. }
  90. }
  91. break;
  92. case KEY_TT:
  93. if (event.pressed) {
  94. vibration(32);
  95. switch (backlight_mode) {
  96. case 1: case 2:
  97. softpwm_led_increase(LED_KEY_SIDE - 1 + backlight_mode, 32);
  98. break;
  99. }
  100. }
  101. break;
  102. case KEY_TP:
  103. switch (backlight_mode) {
  104. case 1: case 5:
  105. if (event.pressed) {
  106. softpwm_led_set(LED_BOARD_SIDE, backlight_brightness_high);
  107. }
  108. else {
  109. softpwm_led_set(LED_BOARD_SIDE, backlight_brightness_mid);
  110. }
  111. break;
  112. case 2: case 4:
  113. if (event.pressed) {
  114. softpwm_led_set(LED_KEY_SIDE, backlight_brightness_high);
  115. }
  116. else {
  117. softpwm_led_set(LED_KEY_SIDE, backlight_brightness_mid);
  118. }
  119. break;
  120. }
  121. break;
  122. case KEY_CFG:
  123. if (event.pressed) {
  124. enter_config_mode();
  125. }
  126. break;
  127. }
  128. }
  129. }
  130. void enter_config_mode(void)
  131. {
  132. config_mode = 1;
  133. layer_modified = 0;
  134. backlight_modified = 0;
  135. backlight = backlight_mode;
  136. backlight_level(8);
  137. softpwm_led_set(0, 32 * (layer + 1));
  138. softpwm_led_set(1, 32 * (backlight + 1));
  139. buzzer(pgm_read_word(&tones[0]), 50);
  140. buzzer(pgm_read_word(&tones[2]), 50);
  141. buzzer(pgm_read_word(&tones[4]), 50);
  142. buzzer(pgm_read_word(&tones[7]), 50);
  143. }
  144. void exit_config_mode(void)
  145. {
  146. config_mode = 0;
  147. backlight_level(backlight);
  148. if (layer_modified) {
  149. default_layer_set(1UL<<layer);
  150. eeconfig_write_default_layer(1UL<<layer);
  151. }
  152. buzzer(pgm_read_word(&tones[7]), 50);
  153. buzzer(pgm_read_word(&tones[4]), 50);
  154. buzzer(pgm_read_word(&tones[2]), 50);
  155. buzzer(pgm_read_word(&tones[0]), 50);
  156. }
  157. void switch_layout(void)
  158. {
  159. if (!layer_modified) {
  160. layer = 0;
  161. layer_modified = 1;
  162. }
  163. else {
  164. layer = (layer + 1) % (last_layer() + 1);
  165. }
  166. dprintf("layer: %d\n", layer);
  167. dprintf("last layer: %d\n", last_layer());
  168. set_layer_indicator(layer);
  169. }
  170. void switch_backlight(void)
  171. {
  172. if (!backlight_modified) {
  173. backlight = 0;
  174. backlight_modified = 1;
  175. }
  176. else {
  177. backlight = (backlight + 1) % (BACKLIGHT_LEVELS);
  178. }
  179. dprintf("backlight: %d\n", backlight);
  180. set_backlight_indicator(backlight);
  181. }
  182. void set_layer_indicator(uint8_t layer)
  183. {
  184. layer = (layer < 7) ? layer : 7;
  185. softpwm_led_set(0, 32 * (layer + 1) - 1);
  186. buzzer(pgm_read_word(&tones[layer]), 50);
  187. if (layer == 0) {
  188. _delay_ms(10);
  189. buzzer(pgm_read_word(&tones[layer]), 50);
  190. }
  191. }
  192. void set_backlight_indicator(uint8_t backlight)
  193. {
  194. softpwm_led_set(1, 32 * (backlight + 1) - 1);
  195. buzzer(pgm_read_word(&tones[7 + backlight]), 50);
  196. if (backlight == 0) {
  197. _delay_ms(10);
  198. buzzer(pgm_read_word(&tones[7 + backlight]), 50);
  199. }
  200. }