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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. uint8_t default_layer = 0;
  8. uint16_t layer_switch_stat = 0;
  9. uint16_t layer_switch_get_stat(void)
  10. {
  11. return layer_switch_stat;
  12. }
  13. /* return highest layer whose state is on */
  14. uint8_t layer_switch_get_layer(void)
  15. {
  16. return biton16(layer_switch_stat);
  17. }
  18. static inline void stat_set(uint16_t stat)
  19. {
  20. debug("layer_switch: ");
  21. layer_switch_debug(); debug(" to ");
  22. layer_switch_stat = stat;
  23. layer_switch_debug(); debug("\n");
  24. clear_keyboard_but_mods(); // To avoid stuck keys
  25. }
  26. void layer_switch_clear(void)
  27. {
  28. stat_set(0);
  29. }
  30. void layer_switch_set(uint16_t stat)
  31. {
  32. stat_set(stat);
  33. }
  34. void layer_switch_move(uint8_t layer)
  35. {
  36. if (layer)
  37. stat_set(1<<layer);
  38. else
  39. stat_set(0); // fall back to default layer
  40. }
  41. void layer_switch_on(uint8_t layer)
  42. {
  43. stat_set(layer_switch_stat | (1<<layer));
  44. }
  45. void layer_switch_off(uint8_t layer)
  46. {
  47. stat_set(layer_switch_stat & ~(1<<layer));
  48. }
  49. void layer_switch_invert(uint8_t layer)
  50. {
  51. stat_set(layer_switch_stat ^ (1<<layer));
  52. }
  53. void layer_switch_or(uint16_t stat)
  54. {
  55. stat_set(layer_switch_stat | stat);
  56. }
  57. void layer_switch_and(uint16_t stat)
  58. {
  59. stat_set(layer_switch_stat & stat);
  60. }
  61. void layer_switch_xor(uint16_t stat)
  62. {
  63. stat_set(layer_switch_stat ^ stat);
  64. }
  65. void layer_switch_debug(void)
  66. {
  67. debug_hex16(layer_switch_stat); debug("("); debug_dec(layer_switch_get_layer()); debug(")");
  68. }
  69. action_t layer_switch_get_action(key_t key)
  70. {
  71. action_t action;
  72. action.code = ACTION_TRANSPARENT;
  73. /* higher layer first */
  74. for (int8_t i = 15; i >= 0; i--) {
  75. if (layer_switch_stat & (1<<i)) {
  76. action = action_for_key(i, key);
  77. if (action.code != ACTION_TRANSPARENT) {
  78. return action;
  79. }
  80. }
  81. }
  82. return action;
  83. }