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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /*
  77. * Overlay Layer (16-31 = 0-15|0x10)
  78. */
  79. uint16_t overlay_stat = 0;
  80. /* return highest layer whose state is on */
  81. uint8_t overlay_get_layer(void)
  82. {
  83. return biton16(overlay_stat);
  84. }
  85. static void overlay_stat_set(uint16_t stat)
  86. {
  87. debug("overlay: ");
  88. overlay_debug(); debug(" to ");
  89. overlay_stat = stat;
  90. overlay_debug(); debug("\n");
  91. clear_keyboard_but_mods(); // To avoid stuck keys
  92. }
  93. void overlay_clear(void)
  94. {
  95. overlay_stat_set(0);
  96. }
  97. void overlay_set(uint16_t stat)
  98. {
  99. overlay_stat_set(stat);
  100. }
  101. void overlay_move(uint8_t layer)
  102. {
  103. overlay_stat_set(1<<layer);
  104. }
  105. void overlay_on(uint8_t layer)
  106. {
  107. overlay_stat_set(overlay_stat | (1<<layer));
  108. }
  109. void overlay_off(uint8_t layer)
  110. {
  111. overlay_stat_set(overlay_stat & ~(1<<layer));
  112. }
  113. void overlay_invert(uint8_t layer)
  114. {
  115. overlay_stat_set(overlay_stat ^ (1<<layer));
  116. }
  117. void overlay_or(uint16_t stat)
  118. {
  119. overlay_stat_set(overlay_stat | stat);
  120. }
  121. void overlay_and(uint16_t stat)
  122. {
  123. overlay_stat_set(overlay_stat & stat);
  124. }
  125. void overlay_xor(uint16_t stat)
  126. {
  127. overlay_stat_set(overlay_stat ^ stat);
  128. }
  129. void overlay_debug(void)
  130. {
  131. debug_hex16(overlay_stat); debug("("); debug_dec(overlay_get_layer()); debug(")");
  132. }
  133. action_t layer_switch_get_action(key_t key)
  134. {
  135. action_t action;
  136. action.code = ACTION_TRANSPARENT;
  137. /* overlay: top layer first */
  138. for (int8_t i = 15; i >= 0; i--) {
  139. if (overlay_stat & (1<<i)) {
  140. action = action_for_key(i | OVERLAY_BIT, key);
  141. if (action.code != ACTION_TRANSPARENT) {
  142. return action;
  143. }
  144. }
  145. }
  146. /* keymap: top layer first */
  147. for (int8_t i = 15; i >= 0; i--) {
  148. if (keymap_stat & (1<<i)) {
  149. action = action_for_key(i, key);
  150. if (action.code != ACTION_TRANSPARENT) {
  151. return action;
  152. }
  153. }
  154. }
  155. /* default layer */
  156. action = action_for_key(default_layer, key);
  157. return action;
  158. }