]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_steno.c
Add RGB commands to default Nyquist keymap
[qmk_firmware.git] / quantum / process_keycode / process_steno.c
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
21 // TxBolt Codes
22 #define TXB_NUL 0
23 #define TXB_S_L 0b00000001
24 #define TXB_T_L 0b00000010
25 #define TXB_K_L 0b00000100
26 #define TXB_P_L 0b00001000
27 #define TXB_W_L 0b00010000
28 #define TXB_H_L 0b00100000
29 #define TXB_R_L 0b01000001
30 #define TXB_A_L 0b01000010
31 #define TXB_O_L 0b01000100
32 #define TXB_STR 0b01001000
33 #define TXB_E_R 0b01010000
34 #define TXB_U_R 0b01100000
35 #define TXB_F_R 0b10000001
36 #define TXB_R_R 0b10000010
37 #define TXB_P_R 0b10000100
38 #define TXB_B_R 0b10001000
39 #define TXB_L_R 0b10010000
40 #define TXB_G_R 0b10100000
41 #define TXB_T_R 0b11000001
42 #define TXB_S_R 0b11000010
43 #define TXB_D_R 0b11000100
44 #define TXB_Z_R 0b11001000
45 #define TXB_NUM 0b11010000
46
47 #define TXB_GRP0 0b00000000
48 #define TXB_GRP1 0b01000000
49 #define TXB_GRP2 0b10000000
50 #define TXB_GRP3 0b11000000
51 #define TXB_GRPMASK 0b11000000
52
53 #define TXB_GET_GROUP(code) ((code & TXB_GRPMASK) >> 6)
54
55 #define BOLT_STATE_SIZE 4
56 #define GEMINI_STATE_SIZE 6
57
58 uint8_t state[MAX(BOLT_STATE_SIZE, GEMINI_STATE_SIZE)] = {0};
59 uint8_t pressed = 0;
60 steno_mode_t mode;
61
62 uint8_t boltmap[64] = {
63   TXB_NUL, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM,
64   TXB_S_L, TXB_S_L, TXB_T_L, TXB_K_L, TXB_P_L, TXB_W_L, TXB_H_L,
65   TXB_R_L, TXB_A_L, TXB_O_L, TXB_STR, TXB_STR, TXB_NUL, TXB_NUL,
66   TXB_NUL, TXB_STR, TXB_STR, TXB_E_R, TXB_U_R, TXB_F_R, TXB_R_R,
67   TXB_P_R, TXB_B_R, TXB_L_R, TXB_G_R, TXB_T_R, TXB_S_R, TXB_D_R,
68   TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_Z_R
69 };
70
71 #define BOLTMAP_MASK (sizeof(boltmap) - 1)
72
73
74 void steno_clear_state(void) {
75   memset(state, 0, sizeof(state));
76 }
77
78 void steno_init() {
79   if (!eeconfig_is_enabled()) {
80     eeconfig_init();
81   }
82   mode = eeprom_read_byte(EECONFIG_STENOMODE);
83 }
84
85 void steno_set_mode(steno_mode_t new_mode) {
86   steno_clear_state();
87   mode = new_mode;
88   eeprom_update_byte(EECONFIG_STENOMODE, mode);
89 }
90
91 void send_steno_state(uint8_t size, bool send_empty) {
92   for (uint8_t i = 0; i < size; ++i) {
93     if (state[i] || send_empty) {
94       virtser_send(state[i]);
95     }
96   }
97   steno_clear_state();
98 }
99
100 bool update_state_bolt(uint8_t key) {
101   uint8_t boltcode = boltmap[key];
102   state[TXB_GET_GROUP(boltcode)] |= boltcode;
103   return false;
104 }
105
106 bool send_state_bolt(void) {
107   send_steno_state(BOLT_STATE_SIZE, false);
108   virtser_send(0); // terminating byte
109   return false;
110 }
111
112 bool update_state_gemini(uint8_t key) {
113   state[key / 7] |= 1 << (6 - (key % 7));
114   return false;
115 }
116
117 bool send_state_gemini(void) {
118   state[0] |= 0x80; // Indicate start of packet
119   send_steno_state(GEMINI_STATE_SIZE, true);
120   return false;
121 }
122
123 bool process_steno(uint16_t keycode, keyrecord_t *record) {
124   switch (keycode) {
125     case QK_STENO_BOLT:
126       if (IS_PRESSED(record->event)) {
127         steno_set_mode(STENO_MODE_BOLT);
128       }
129       return false;
130
131     case QK_STENO_GEMINI:
132       if (IS_PRESSED(record->event)) {
133         steno_set_mode(STENO_MODE_GEMINI);
134       }
135       return false;
136
137     case STN__MIN...STN__MAX:
138       if (IS_PRESSED(record->event)) {
139         uint8_t key = keycode - QK_STENO;
140         ++pressed;
141         switch(mode) {
142           case STENO_MODE_BOLT:
143             return update_state_bolt(key);
144           case STENO_MODE_GEMINI:
145             return update_state_gemini(key);
146           default:
147             return false;
148         }
149       } else {
150         --pressed;
151         if (pressed <= 0) {
152           pressed = 0;
153           switch(mode) {
154             case STENO_MODE_BOLT:
155               return send_state_bolt();
156             case STENO_MODE_GEMINI:
157               return send_state_gemini();
158             default:
159               return false;
160           }
161         }
162       }
163
164   }
165   return true;
166 }