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

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