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

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