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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_LAYER
  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. action_t layer_switch_get_action(key_t key)
  79. {
  80. action_t action;
  81. action.code = ACTION_TRANSPARENT;
  82. #ifndef NO_ACTION_LAYER
  83. /* keymap: top layer first */
  84. for (int8_t i = 15; i >= 0; i--) {
  85. if (keymap_stat & (1<<i)) {
  86. action = action_for_key(i, key);
  87. if (action.code != ACTION_TRANSPARENT) {
  88. return action;
  89. }
  90. }
  91. }
  92. #endif
  93. /* default layer */
  94. action = action_for_key(default_layer, key);
  95. return action;
  96. }