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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "action.h"
  16. #include "action_layer.h"
  17. #include "backlight.h"
  18. #include "softpwm_led.h"
  19. #include "eeconfig.h"
  20. #include "keymap_common.h"
  21. #include "vibration.h"
  22. #include "tentapad.h"
  23. #include "debug.h"
  24. uint8_t config_mode = 0;
  25. static uint8_t layer = 0;
  26. static uint8_t backlight = 0;
  27. static uint8_t layer_modified = 0;
  28. static uint8_t backlight_modified = 0;
  29. extern uint8_t backlight_mode;
  30. extern const uint8_t backlight_brightness;
  31. void action_keyevent(keyevent_t event)
  32. {
  33. uint8_t key = event.key.col;
  34. if (config_mode) {
  35. /* config mode */
  36. switch (key) {
  37. case KEY_K1:
  38. if (event.pressed) {
  39. switch_layout();
  40. }
  41. break;
  42. case KEY_K2:
  43. if (event.pressed) {
  44. switch_backlight();
  45. }
  46. break;
  47. case KEY_CFG:
  48. if (event.pressed) {
  49. exit_config_mode();
  50. }
  51. break;
  52. }
  53. }
  54. else {
  55. /* normal mode */
  56. switch (key) {
  57. case KEY_K1: case KEY_K2:
  58. if (event.pressed) {
  59. /* press */
  60. switch (backlight_mode) {
  61. case 0: case 6:
  62. softpwm_led_on(key);
  63. break;
  64. case 1: case 2:
  65. softpwm_led_increase(LED_KEY_SIDE - 1 + backlight_mode, 32);
  66. case 3 ... 5:
  67. softpwm_led_set(key, backlight_brightness);
  68. break;
  69. }
  70. }
  71. else {
  72. /* release */
  73. switch (backlight_mode) {
  74. case 0: case 6:
  75. softpwm_led_off(key);
  76. break;
  77. case 1 ... 5:
  78. softpwm_led_set(key, 0);
  79. break;
  80. }
  81. }
  82. break;
  83. case KEY_TT:
  84. if (event.pressed) {
  85. vibration(64);
  86. switch (backlight_mode) {
  87. case 1: case 2:
  88. softpwm_led_increase(LED_KEY_SIDE - 1 + backlight_mode, 32);
  89. break;
  90. }
  91. }
  92. break;
  93. case KEY_CFG:
  94. if (event.pressed) {
  95. enter_config_mode();
  96. }
  97. break;
  98. }
  99. }
  100. }
  101. void enter_config_mode(void)
  102. {
  103. config_mode = 1;
  104. layer_modified = 0;
  105. backlight_modified = 0;
  106. backlight = backlight_mode;
  107. backlight_level(8);
  108. layer_on(CONFIG_LAYER);
  109. }
  110. void exit_config_mode(void)
  111. {
  112. config_mode = 0;
  113. backlight_level(backlight);
  114. layer_off(CONFIG_LAYER);
  115. if (layer_modified) {
  116. default_layer_set(1UL<<layer);
  117. eeconfig_write_default_layer(1UL<<layer);
  118. }
  119. }
  120. void switch_layout(void)
  121. {
  122. if (!layer_modified) {
  123. layer = 0;
  124. layer_modified = 1;
  125. }
  126. else {
  127. layer = (layer + 1) % (last_layer() + 1);
  128. }
  129. xprintf("layer: %d\n", layer);
  130. xprintf("last layer: %d\n", last_layer());
  131. softpwm_led_set(0, 32 * (layer + 1));
  132. }
  133. void switch_backlight(void)
  134. {
  135. if (!backlight_modified) {
  136. backlight = 0;
  137. backlight_modified = 1;
  138. }
  139. else {
  140. backlight = (backlight + 1) % (BACKLIGHT_LEVELS);
  141. }
  142. xprintf("backlight: %d\n", backlight);
  143. softpwm_led_set(1, 32 * (backlight + 1));
  144. }