Keyboard firmwares for Atmel AVR and Cortex-M
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.

layer_switch.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <stdint.h>
  2. #include "keyboard.h"
  3. #include "action.h"
  4. #include "debug.h"
  5. #include "util.h"
  6. #include "layer_switch.h"
  7. /*
  8. * Default Layer (0-15)
  9. */
  10. uint8_t default_layer = 0;
  11. void default_layer_set(uint8_t layer)
  12. {
  13. debug("default_layer_set: ");
  14. debug_dec(default_layer); debug(" to ");
  15. default_layer = layer;
  16. debug_dec(default_layer); debug("\n");
  17. clear_keyboard_but_mods(); // To avoid stuck keys
  18. }
  19. /*
  20. * Keymap Layer (0-15)
  21. */
  22. uint16_t keymap_stat = 0;
  23. /* return highest layer whose state is on */
  24. uint8_t keymap_get_layer(void)
  25. {
  26. return biton16(keymap_stat);
  27. }
  28. static void keymap_stat_set(uint16_t stat)
  29. {
  30. debug("keymap: ");
  31. keymap_debug(); debug(" to ");
  32. keymap_stat = stat;
  33. keymap_debug(); debug("\n");
  34. clear_keyboard_but_mods(); // To avoid stuck keys
  35. }
  36. void keymap_clear(void)
  37. {
  38. keymap_stat_set(0);
  39. }
  40. void keymap_set(uint16_t stat)
  41. {
  42. keymap_stat_set(stat);
  43. }
  44. void keymap_move(uint8_t layer)
  45. {
  46. keymap_stat_set(1<<layer);
  47. }
  48. void keymap_on(uint8_t layer)
  49. {
  50. keymap_stat_set(keymap_stat | (1<<layer));
  51. }
  52. void keymap_off(uint8_t layer)
  53. {
  54. keymap_stat_set(keymap_stat & ~(1<<layer));
  55. }
  56. void keymap_invert(uint8_t layer)
  57. {
  58. keymap_stat_set(keymap_stat ^ (1<<layer));
  59. }
  60. void keymap_or(uint16_t stat)
  61. {
  62. keymap_stat_set(keymap_stat | stat);
  63. }
  64. void keymap_and(uint16_t stat)
  65. {
  66. keymap_stat_set(keymap_stat & stat);
  67. }
  68. void keymap_xor(uint16_t stat)
  69. {
  70. keymap_stat_set(keymap_stat ^ stat);
  71. }
  72. void keymap_debug(void)
  73. {
  74. debug_hex16(keymap_stat); debug("("); debug_dec(keymap_get_layer()); debug(")");
  75. }
  76. #ifndef NO_ACTION_OVERLAY
  77. /*
  78. * Overlay Layer (16-31 = 0-15|0x10)
  79. */
  80. uint16_t overlay_stat = 0;
  81. /* return highest layer whose state is on */
  82. uint8_t overlay_get_layer(void)
  83. {
  84. return biton16(overlay_stat);
  85. }
  86. static void overlay_stat_set(uint16_t stat)
  87. {
  88. debug("overlay: ");
  89. overlay_debug(); debug(" to ");
  90. overlay_stat = stat;
  91. overlay_debug(); debug("\n");
  92. clear_keyboard_but_mods(); // To avoid stuck keys
  93. }
  94. void overlay_clear(void)
  95. {
  96. overlay_stat_set(0);
  97. }
  98. void overlay_set(uint16_t stat)
  99. {
  100. overlay_stat_set(stat);
  101. }
  102. void overlay_move(uint8_t layer)
  103. {
  104. overlay_stat_set(1<<layer);
  105. }
  106. void overlay_on(uint8_t layer)
  107. {
  108. overlay_stat_set(overlay_stat | (1<<layer));
  109. }
  110. void overlay_off(uint8_t layer)
  111. {
  112. overlay_stat_set(overlay_stat & ~(1<<layer));
  113. }
  114. void overlay_invert(uint8_t layer)
  115. {
  116. overlay_stat_set(overlay_stat ^ (1<<layer));
  117. }
  118. void overlay_or(uint16_t stat)
  119. {
  120. overlay_stat_set(overlay_stat | stat);
  121. }
  122. void overlay_and(uint16_t stat)
  123. {
  124. overlay_stat_set(overlay_stat & stat);
  125. }
  126. void overlay_xor(uint16_t stat)
  127. {
  128. overlay_stat_set(overlay_stat ^ stat);
  129. }
  130. void overlay_debug(void)
  131. {
  132. debug_hex16(overlay_stat); debug("("); debug_dec(overlay_get_layer()); debug(")");
  133. }
  134. #endif
  135. action_t layer_switch_get_action(key_t key)
  136. {
  137. action_t action;
  138. action.code = ACTION_TRANSPARENT;
  139. #ifndef NO_ACTION_OVERLAY
  140. /* overlay: top layer first */
  141. for (int8_t i = 15; i >= 0; i--) {
  142. if (overlay_stat & (1<<i)) {
  143. action = action_for_key(i | OVERLAY_BIT, key);
  144. if (action.code != ACTION_TRANSPARENT) {
  145. return action;
  146. }
  147. }
  148. }
  149. #endif
  150. /* keymap: top layer first */
  151. for (int8_t i = 15; i >= 0; i--) {
  152. if (keymap_stat & (1<<i)) {
  153. action = action_for_key(i, key);
  154. if (action.code != ACTION_TRANSPARENT) {
  155. return action;
  156. }
  157. }
  158. }
  159. /* default layer */
  160. action = action_for_key(default_layer, key);
  161. return action;
  162. }