Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.

tentapad.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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;
  39. void action_keyevent(keyevent_t event)
  40. {
  41. uint8_t key = event.key.col;
  42. if (config_mode) {
  43. /* config mode */
  44. switch (key) {
  45. case KEY_K1:
  46. if (event.pressed) {
  47. switch_layout();
  48. }
  49. break;
  50. case KEY_K2:
  51. if (event.pressed) {
  52. switch_backlight();
  53. }
  54. break;
  55. case KEY_CFG:
  56. if (event.pressed) {
  57. exit_config_mode();
  58. }
  59. break;
  60. }
  61. }
  62. else {
  63. /* normal mode */
  64. switch (key) {
  65. case KEY_K1: case KEY_K2:
  66. if (event.pressed) {
  67. /* press */
  68. switch (backlight_mode) {
  69. case 0: case 6:
  70. softpwm_led_on(key);
  71. break;
  72. case 1: case 2:
  73. softpwm_led_increase(LED_KEY_SIDE - 1 + backlight_mode, 32);
  74. case 3 ... 5:
  75. softpwm_led_set(key, backlight_brightness);
  76. break;
  77. }
  78. }
  79. else {
  80. /* release */
  81. switch (backlight_mode) {
  82. case 0: case 6:
  83. softpwm_led_off(key);
  84. break;
  85. case 1 ... 5:
  86. softpwm_led_set(key, 0);
  87. break;
  88. }
  89. }
  90. break;
  91. case KEY_TT:
  92. if (event.pressed) {
  93. vibration(64);
  94. switch (backlight_mode) {
  95. case 1: case 2:
  96. softpwm_led_increase(LED_KEY_SIDE - 1 + backlight_mode, 32);
  97. break;
  98. }
  99. }
  100. break;
  101. case KEY_CFG:
  102. if (event.pressed) {
  103. enter_config_mode();
  104. }
  105. break;
  106. }
  107. }
  108. }
  109. void enter_config_mode(void)
  110. {
  111. config_mode = 1;
  112. layer_modified = 0;
  113. backlight_modified = 0;
  114. backlight = backlight_mode;
  115. backlight_level(8);
  116. layer_on(CONFIG_LAYER);
  117. softpwm_led_set(0, 32 * (layer + 1));
  118. softpwm_led_set(1, 32 * (backlight + 1));
  119. buzzer(pgm_read_word(&tones[0]), 50);
  120. buzzer(pgm_read_word(&tones[4]), 50);
  121. buzzer(pgm_read_word(&tones[7]), 50);
  122. }
  123. void exit_config_mode(void)
  124. {
  125. config_mode = 0;
  126. backlight_level(backlight);
  127. layer_off(CONFIG_LAYER);
  128. if (layer_modified) {
  129. default_layer_set(1UL<<layer);
  130. eeconfig_write_default_layer(1UL<<layer);
  131. }
  132. buzzer(pgm_read_word(&tones[7]), 50);
  133. buzzer(pgm_read_word(&tones[4]), 50);
  134. buzzer(pgm_read_word(&tones[0]), 50);
  135. }
  136. void switch_layout(void)
  137. {
  138. if (!layer_modified) {
  139. layer = 0;
  140. layer_modified = 1;
  141. }
  142. else {
  143. layer = (layer + 1) % (last_layer() + 1);
  144. }
  145. dprintf("layer: %d\n", layer);
  146. dprintf("last layer: %d\n", last_layer());
  147. softpwm_led_set(0, 32 * (layer + 1));
  148. buzzer(pgm_read_word(&tones[layer]), 50);
  149. if (layer == 0) {
  150. _delay_ms(10);
  151. buzzer(pgm_read_word(&tones[layer]), 50);
  152. }
  153. }
  154. void switch_backlight(void)
  155. {
  156. if (!backlight_modified) {
  157. backlight = 0;
  158. backlight_modified = 1;
  159. }
  160. else {
  161. backlight = (backlight + 1) % (BACKLIGHT_LEVELS);
  162. }
  163. dprintf("backlight: %d\n", backlight);
  164. softpwm_led_set(1, 32 * (backlight + 1));
  165. buzzer(pgm_read_word(&tones[7 + backlight]), 50);
  166. if (backlight == 0) {
  167. _delay_ms(10);
  168. buzzer(pgm_read_word(&tones[7 + backlight]), 50);
  169. }
  170. }