Browse Source

Improve state/chord handling and clean up namespace

Some values that can never, ever, change were held in local
variables, rather than in PROGMEM. Fixed.

Change "pressed" to a signed int so the test for < 0 makes
sense, and to avoid possible weird failure modes in the
case where a key release comes in when pressed is already
zero. (Shouldn't happen, sure, but computers are weird.)

A lot of things in process_steno had external linkage for no
particular reason. They've been marked static. Stuff still
builds.

Distinguish between currently-held keys and keys that have
been held, and expose these values through a nicely-named API
so other code could, say, check on the current set of steno
chording in order to make displays. Also in passing fix up the
"state" value having external linkage so it could clash with
other people's variable declarations.

The API also provides hooks for key processing and steno chord
events, so you can monitor those events without having to
run in matrix_scan_user and recheck the values directly. Also
document these.

There is no path through processing a key that doesn't
end with a return false, so the nested return foo() are
gone and we just return false.
pjones-keymap
Seebs 1 year ago
parent
commit
c0baf2a964

+ 24
- 0
docs/feature_stenography.md View File

@@ -56,6 +56,29 @@ On the display tab click 'Open stroke display'. With Plover disabled you should
56 56
 * [Steno Jig](https://joshuagrams.github.io/steno-jig/)
57 57
 * More resources at the Plover [Learning Stenography](https://github.com/openstenoproject/plover/wiki/Learning-Stenography) wiki
58 58
 
59
+## Interfacing with the code
60
+
61
+The steno code has three interceptible hooks. If you define these functions, they will be called at certain points in processing; if they return true, processing continues, otherwise it's assumed you handled things.
62
+
63
+```C
64
+bool send_steno_chord_user(steno_mode_t mode, uint8_t chord[6]);
65
+```
66
+
67
+This function is called when a chord is about to be sent. Mode will be one of `STENO_MODE_BOLT` or `STENO_MODE_GEMINI`. This represents the actual chord that would be sent via whichever protocol. You can modify the chord provided to alter what gets sent. Remember to return true if you want the regular sending process to happen.
68
+
69
+```C
70
+bool process_steno_user(uint16_t keycode, keyrecord_t *record) { return true; }
71
+```
72
+
73
+This function is called when a keypress has come in, before it is processed. The keycode should be one of `QK_STENO_BOLT`, `QK_STENO_GEMINI`, or one of the `STN_*` key values.
74
+
75
+```C
76
+bool postprocess_steno_user(uint16_t keycode, keyrecord_t *record, steno_mode_t mode, uint8_t chord[6], int8_t pressed);
77
+```
78
+
79
+This function is called after a key has been processed, but before any decision about whether or not to send a chord. If `IS_PRESSED(record->event)` is false, and `pressed` is 0 or 1, the chord will be sent shortly, but has not yet been sent. This is where to put hooks for things like, say, live displays of steno chords or keys.
80
+
81
+
59 82
 ## Keycode Reference
60 83
 
61 84
 As defined in `keymap_steno.h`.
@@ -106,3 +129,4 @@ As defined in `keymap_steno.h`.
106 129
 |`STN_RES1`||(GeminiPR only)|
107 130
 |`STN_RES2`||(GeminiPR only)|
108 131
 |`STN_PWR`||(GeminiPR only)|
132
+

+ 88
- 48
quantum/process_keycode/process_steno.c View File

@@ -18,6 +18,7 @@
18 18
 #include "eeprom.h"
19 19
 #include "keymap_steno.h"
20 20
 #include "virtser.h"
21
+#include <string.h>
21 22
 
22 23
 // TxBolt Codes
23 24
 #define TXB_NUL 0
@@ -57,11 +58,12 @@
57 58
 #define GEMINI_STATE_SIZE 6
58 59
 #define MAX_STATE_SIZE GEMINI_STATE_SIZE
59 60
 
60
-uint8_t state[MAX_STATE_SIZE] = {0};
61
-uint8_t pressed = 0;
62
-steno_mode_t mode;
61
+static uint8_t state[MAX_STATE_SIZE] = {0};
62
+static uint8_t chord[MAX_STATE_SIZE] = {0};
63
+static int8_t pressed = 0;
64
+static steno_mode_t mode;
63 65
 
64
-uint8_t boltmap[64] = {
66
+static const uint8_t boltmap[64] PROGMEM = {
65 67
   TXB_NUL, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM,
66 68
   TXB_S_L, TXB_S_L, TXB_T_L, TXB_K_L, TXB_P_L, TXB_W_L, TXB_H_L,
67 69
   TXB_R_L, TXB_A_L, TXB_O_L, TXB_STR, TXB_STR, TXB_NUL, TXB_NUL,
@@ -70,8 +72,17 @@ uint8_t boltmap[64] = {
70 72
   TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_Z_R
71 73
 };
72 74
 
73
-void steno_clear_state(void) {
74
-  __builtin_memset(state, 0, sizeof(state));
75
+static void steno_clear_state(void) {
76
+  memset(state, 0, sizeof(state));
77
+  memset(chord, 0, sizeof(chord));
78
+}
79
+
80
+static void send_steno_state(uint8_t size, bool send_empty) {
81
+  for (uint8_t i = 0; i < size; ++i) {
82
+    if (chord[i] || send_empty) {
83
+      virtser_send(chord[i]);
84
+    }
85
+  }
75 86
 }
76 87
 
77 88
 void steno_init() {
@@ -87,79 +98,108 @@ void steno_set_mode(steno_mode_t new_mode) {
87 98
   eeprom_update_byte(EECONFIG_STENOMODE, mode);
88 99
 }
89 100
 
90
-void send_steno_state(uint8_t size, bool send_empty) {
91
-  for (uint8_t i = 0; i < size; ++i) {
92
-    if (state[i] || send_empty) {
93
-      virtser_send(state[i]);
101
+/* override to intercept chords right before they get sent.
102
+ * return zero to suppress normal sending behavior.
103
+ */
104
+__attribute__ ((weak))
105
+bool send_steno_chord_user(steno_mode_t mode, uint8_t chord[6]) { return true; }
106
+
107
+__attribute__ ((weak))
108
+bool postprocess_steno_user(uint16_t keycode, keyrecord_t *record, steno_mode_t mode, uint8_t chord[6], int8_t pressed) { return true; }
109
+
110
+__attribute__ ((weak))
111
+bool process_steno_user(uint16_t keycode, keyrecord_t *record) { return true; }
112
+
113
+static void send_steno_chord(void) {
114
+  if (send_steno_chord_user(mode, chord)) {
115
+    switch(mode) {
116
+      case STENO_MODE_BOLT:
117
+	send_steno_state(BOLT_STATE_SIZE, false);
118
+	virtser_send(0); // terminating byte
119
+	break;
120
+      case STENO_MODE_GEMINI:
121
+	chord[0] |= 0x80; // Indicate start of packet
122
+	send_steno_state(GEMINI_STATE_SIZE, true);
123
+	break;
94 124
     }
95 125
   }
96 126
   steno_clear_state();
97 127
 }
98 128
 
99
-bool update_state_bolt(uint8_t key) {
100
-  uint8_t boltcode = boltmap[key];
101
-  state[TXB_GET_GROUP(boltcode)] |= boltcode;
102
-  return false;
129
+uint8_t *steno_get_state(void) {
130
+  return &state[0];
103 131
 }
104 132
 
105
-bool send_state_bolt(void) {
106
-  send_steno_state(BOLT_STATE_SIZE, false);
107
-  virtser_send(0); // terminating byte
108
-  return false;
133
+uint8_t *steno_get_chord(void) {
134
+  return &chord[0];
109 135
 }
110 136
 
111
-bool update_state_gemini(uint8_t key) {
112
-  state[key / 7] |= 1 << (6 - (key % 7));
137
+static bool update_state_bolt(uint8_t key, bool press) {
138
+  uint8_t boltcode = pgm_read_byte(boltmap + key);
139
+  if (press) {
140
+    state[TXB_GET_GROUP(boltcode)] |= boltcode;
141
+    chord[TXB_GET_GROUP(boltcode)] |= boltcode;
142
+  } else {
143
+    state[TXB_GET_GROUP(boltcode)] &= ~boltcode;
144
+  }
113 145
   return false;
114 146
 }
115 147
 
116
-bool send_state_gemini(void) {
117
-  state[0] |= 0x80; // Indicate start of packet
118
-  send_steno_state(GEMINI_STATE_SIZE, true);
148
+static bool update_state_gemini(uint8_t key, bool press) {
149
+  int idx = key / 7;
150
+  uint8_t bit = 1 << (6 - (key % 7));
151
+  if (press) {
152
+    state[idx] |= bit;
153
+    chord[idx] |= bit;
154
+  } else {
155
+    state[idx] &= ~bit;
156
+  }
119 157
   return false;
120 158
 }
121 159
 
122 160
 bool process_steno(uint16_t keycode, keyrecord_t *record) {
123 161
   switch (keycode) {
124 162
     case QK_STENO_BOLT:
163
+      if (!process_steno_user(keycode, record)) {
164
+	return false;
165
+      }
125 166
       if (IS_PRESSED(record->event)) {
126 167
         steno_set_mode(STENO_MODE_BOLT);
127 168
       }
128 169
       return false;
129 170
 
130 171
     case QK_STENO_GEMINI:
172
+      if (!process_steno_user(keycode, record)) {
173
+	return false;
174
+      }
131 175
       if (IS_PRESSED(record->event)) {
132 176
         steno_set_mode(STENO_MODE_GEMINI);
133 177
       }
134 178
       return false;
135 179
 
136 180
     case STN__MIN...STN__MAX:
137
-      if (IS_PRESSED(record->event)) {
138
-        uint8_t key = keycode - QK_STENO;
139
-        ++pressed;
140
-        switch(mode) {
141
-          case STENO_MODE_BOLT:
142
-            return update_state_bolt(key);
143
-          case STENO_MODE_GEMINI:
144
-            return update_state_gemini(key);
145
-          default:
146
-            return false;
147
-        }
148
-      } else {
149
-        --pressed;
150
-        if (pressed <= 0) {
151
-          pressed = 0;
152
-          switch(mode) {
153
-            case STENO_MODE_BOLT:
154
-              return send_state_bolt();
155
-            case STENO_MODE_GEMINI:
156
-              return send_state_gemini();
157
-            default:
158
-              return false;
159
-          }
160
-        }
181
+      if (!process_steno_user(keycode, record)) {
182
+	return false;
161 183
       }
162
-
184
+      switch(mode) {
185
+	case STENO_MODE_BOLT:
186
+	  update_state_bolt(keycode - QK_STENO, IS_PRESSED(record->event));
187
+	case STENO_MODE_GEMINI:
188
+	  update_state_gemini(keycode - QK_STENO, IS_PRESSED(record->event));
189
+      }
190
+      // allow postprocessing hooks
191
+      if (postprocess_steno_user(keycode, record, mode, chord, pressed)) {
192
+	if (IS_PRESSED(record->event)) {
193
+	  ++pressed;
194
+	} else {
195
+	  --pressed;
196
+	  if (pressed <= 0) {
197
+	    pressed = 0;
198
+	    send_steno_chord();
199
+	  }
200
+	}
201
+      }
202
+      return false;
163 203
   }
164 204
   return true;
165 205
 }

+ 3
- 1
quantum/process_keycode/process_steno.h View File

@@ -27,5 +27,7 @@ typedef enum { STENO_MODE_BOLT, STENO_MODE_GEMINI } steno_mode_t;
27 27
 bool process_steno(uint16_t keycode, keyrecord_t *record);
28 28
 void steno_init(void);
29 29
 void steno_set_mode(steno_mode_t mode);
30
+uint8_t *steno_get_state(void);
31
+uint8_t *steno_get_chord(void);
30 32
 
31
-#endif
33
+#endif

Loading…
Cancel
Save