process_steno.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Copyright 2017 Joseph Wasson
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "process_steno.h"
  17. #include "quantum_keycodes.h"
  18. #include "keymap_steno.h"
  19. #include "virtser.h"
  20. // TxBolt Codes
  21. #define TXB_NUL 0
  22. #define TXB_S_L 0b00000001
  23. #define TXB_T_L 0b00000010
  24. #define TXB_K_L 0b00000100
  25. #define TXB_P_L 0b00001000
  26. #define TXB_W_L 0b00010000
  27. #define TXB_H_L 0b00100000
  28. #define TXB_R_L 0b01000001
  29. #define TXB_A_L 0b01000010
  30. #define TXB_O_L 0b01000100
  31. #define TXB_STR 0b01001000
  32. #define TXB_E_R 0b01010000
  33. #define TXB_U_R 0b01100000
  34. #define TXB_F_R 0b10000001
  35. #define TXB_R_R 0b10000010
  36. #define TXB_P_R 0b10000100
  37. #define TXB_B_R 0b10001000
  38. #define TXB_L_R 0b10010000
  39. #define TXB_G_R 0b10100000
  40. #define TXB_T_R 0b11000001
  41. #define TXB_S_R 0b11000010
  42. #define TXB_D_R 0b11000100
  43. #define TXB_Z_R 0b11001000
  44. #define TXB_NUM 0b11010000
  45. #define TXB_GRP0 0b00000000
  46. #define TXB_GRP1 0b01000000
  47. #define TXB_GRP2 0b10000000
  48. #define TXB_GRP3 0b11000000
  49. #define TXB_GRPMASK 0b11000000
  50. #define TXB_GET_GROUP(code) ((code & TXB_GRPMASK) >> 6)
  51. #define BOLT_STATE_SIZE 4
  52. #define GEMINI_STATE_SIZE 6
  53. uint8_t state[MAX(BOLT_STATE_SIZE, GEMINI_STATE_SIZE)] = {0};
  54. uint8_t pressed = 0;
  55. steno_mode_t mode;
  56. uint8_t boltmap[64] = {
  57. TXB_NUL, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM,
  58. TXB_S_L, TXB_S_L, TXB_T_L, TXB_K_L, TXB_P_L, TXB_W_L, TXB_H_L,
  59. TXB_R_L, TXB_A_L, TXB_O_L, TXB_STR, TXB_STR, TXB_NUL, TXB_NUL,
  60. TXB_NUL, TXB_STR, TXB_STR, TXB_E_R, TXB_U_R, TXB_F_R, TXB_R_R,
  61. TXB_P_R, TXB_B_R, TXB_L_R, TXB_G_R, TXB_T_R, TXB_S_R, TXB_D_R,
  62. TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_Z_R
  63. };
  64. #define BOLTMAP_MASK (sizeof(boltmap) - 1)
  65. void steno_clear_state(void) {
  66. memset(state, 0, sizeof(state));
  67. }
  68. void steno_init() {
  69. if (!eeconfig_is_enabled()) {
  70. eeconfig_init();
  71. }
  72. mode = eeprom_read_byte(EECONFIG_STENOMODE);
  73. }
  74. void steno_set_mode(steno_mode_t new_mode) {
  75. steno_clear_state();
  76. mode = new_mode;
  77. eeprom_update_byte(EECONFIG_STENOMODE, mode);
  78. }
  79. void send_steno_state(uint8_t size, bool send_empty) {
  80. for (uint8_t i = 0; i < size; ++i) {
  81. if (state[i] || send_empty) {
  82. virtser_send(state[i]);
  83. }
  84. }
  85. steno_clear_state();
  86. }
  87. bool update_state_bolt(uint8_t key) {
  88. uint8_t boltcode = boltmap[key];
  89. state[TXB_GET_GROUP(boltcode)] |= boltcode;
  90. return false;
  91. }
  92. bool send_state_bolt(void) {
  93. send_steno_state(BOLT_STATE_SIZE, false);
  94. virtser_send(0); // terminating byte
  95. return false;
  96. }
  97. bool update_state_gemini(uint8_t key) {
  98. state[key / 7] |= 1 << (6 - (key % 7));
  99. return false;
  100. }
  101. bool send_state_gemini(void) {
  102. state[0] |= 0x80; // Indicate start of packet
  103. send_steno_state(GEMINI_STATE_SIZE, true);
  104. return false;
  105. }
  106. bool process_steno(uint16_t keycode, keyrecord_t *record) {
  107. switch (keycode) {
  108. case QK_STENO_BOLT:
  109. if (IS_PRESSED(record->event)) {
  110. steno_set_mode(STENO_MODE_BOLT);
  111. }
  112. return false;
  113. case QK_STENO_GEMINI:
  114. if (IS_PRESSED(record->event)) {
  115. steno_set_mode(STENO_MODE_GEMINI);
  116. }
  117. return false;
  118. case STN__MIN...STN__MAX:
  119. if (IS_PRESSED(record->event)) {
  120. uint8_t key = keycode - QK_STENO;
  121. ++pressed;
  122. switch(mode) {
  123. case STENO_MODE_BOLT:
  124. return update_state_bolt(key);
  125. case STENO_MODE_GEMINI:
  126. return update_state_gemini(key);
  127. default:
  128. return false;
  129. }
  130. } else {
  131. --pressed;
  132. if (pressed <= 0) {
  133. pressed = 0;
  134. switch(mode) {
  135. case STENO_MODE_BOLT:
  136. return send_state_bolt();
  137. case STENO_MODE_GEMINI:
  138. return send_state_gemini();
  139. default:
  140. return false;
  141. }
  142. }
  143. }
  144. }
  145. return true;
  146. }