keyboard stuff

Consolidate send_string implementations. (#24817)

authored by

Nick Brassel and committed by
GitHub
a6a0dc80 47575d4a

+64 -102
+4
builddefs/common_features.mk
··· 635 635 TRI_LAYER_ENABLE := yes 636 636 endif 637 637 638 + ifeq ($(strip $(DYNAMIC_KEYMAP_ENABLE)), yes) 639 + SEND_STRING_ENABLE := yes 640 + endif 641 + 638 642 VALID_CUSTOM_MATRIX_TYPES:= yes lite no 639 643 640 644 CUSTOM_MATRIX ?= no
+13 -53
quantum/dynamic_keymap.c
··· 245 245 } 246 246 } 247 247 248 + typedef struct send_string_eeprom_state_t { 249 + const uint8_t *ptr; 250 + } send_string_eeprom_state_t; 251 + 252 + char send_string_get_next_eeprom(void *arg) { 253 + send_string_eeprom_state_t *state = (send_string_eeprom_state_t *)arg; 254 + char ret = eeprom_read_byte(state->ptr); 255 + state->ptr++; 256 + return ret; 257 + } 258 + 248 259 void dynamic_keymap_macro_reset(void) { 249 260 void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR); 250 261 void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE); ··· 284 295 ++p; 285 296 } 286 297 287 - // Send the macro string by making a temporary string. 288 - char data[8] = {0}; 289 - // We already checked there was a null at the end of 290 - // the buffer, so this cannot go past the end 291 - while (1) { 292 - data[0] = eeprom_read_byte(p++); 293 - data[1] = 0; 294 - // Stop at the null terminator of this macro string 295 - if (data[0] == 0) { 296 - break; 297 - } 298 - if (data[0] == SS_QMK_PREFIX) { 299 - // Get the code 300 - data[1] = eeprom_read_byte(p++); 301 - // Unexpected null, abort. 302 - if (data[1] == 0) { 303 - return; 304 - } 305 - if (data[1] == SS_TAP_CODE || data[1] == SS_DOWN_CODE || data[1] == SS_UP_CODE) { 306 - // Get the keycode 307 - data[2] = eeprom_read_byte(p++); 308 - // Unexpected null, abort. 309 - if (data[2] == 0) { 310 - return; 311 - } 312 - // Null terminate 313 - data[3] = 0; 314 - } else if (data[1] == SS_DELAY_CODE) { 315 - // Get the number and '|' 316 - // At most this is 4 digits plus '|' 317 - uint8_t i = 2; 318 - while (1) { 319 - data[i] = eeprom_read_byte(p++); 320 - // Unexpected null, abort 321 - if (data[i] == 0) { 322 - return; 323 - } 324 - // Found '|', send it 325 - if (data[i] == '|') { 326 - data[i + 1] = 0; 327 - break; 328 - } 329 - // If haven't found '|' by i==6 then 330 - // number too big, abort 331 - if (i == 6) { 332 - return; 333 - } 334 - ++i; 335 - } 336 - } 337 - } 338 - send_string_with_delay(data, DYNAMIC_KEYMAP_MACRO_DELAY); 339 - } 298 + send_string_eeprom_state_t state = {p}; 299 + send_string_with_delay_impl(send_string_get_next_eeprom, &state, DYNAMIC_KEYMAP_MACRO_DELAY); 340 300 }
+39 -49
quantum/send_string/send_string.c
··· 150 150 send_string_with_delay(string, TAP_CODE_DELAY); 151 151 } 152 152 153 - void send_string_with_delay(const char *string, uint8_t interval) { 153 + void send_string_with_delay_impl(char (*getter)(void *), void *arg, uint8_t interval) { 154 154 while (1) { 155 - char ascii_code = *string; 155 + char ascii_code = getter(arg); 156 156 if (!ascii_code) break; 157 157 if (ascii_code == SS_QMK_PREFIX) { 158 - ascii_code = *(++string); 158 + ascii_code = getter(arg); 159 159 160 160 if (ascii_code == SS_TAP_CODE) { 161 161 // tap 162 - uint8_t keycode = *(++string); 162 + uint8_t keycode = getter(arg); 163 163 tap_code(keycode); 164 164 } else if (ascii_code == SS_DOWN_CODE) { 165 165 // down 166 - uint8_t keycode = *(++string); 166 + uint8_t keycode = getter(arg); 167 167 register_code(keycode); 168 168 } else if (ascii_code == SS_UP_CODE) { 169 169 // up 170 - uint8_t keycode = *(++string); 170 + uint8_t keycode = getter(arg); 171 171 unregister_code(keycode); 172 172 } else if (ascii_code == SS_DELAY_CODE) { 173 173 // delay 174 - int ms = 0; 175 - uint8_t keycode = *(++string); 174 + int ms = 0; 175 + ascii_code = getter(arg); 176 176 177 - while (isdigit(keycode)) { 177 + while (isdigit(ascii_code)) { 178 178 ms *= 10; 179 - ms += keycode - '0'; 180 - keycode = *(++string); 179 + ms += ascii_code - '0'; 180 + ascii_code = getter(arg); 181 181 } 182 182 183 183 wait_ms(ms); 184 184 } 185 185 186 186 wait_ms(interval); 187 + 188 + // if we had a delay that terminated with a null, we're done 189 + if (ascii_code == 0) break; 187 190 } else { 188 191 send_char_with_delay(ascii_code, interval); 189 192 } 193 + } 194 + } 190 195 191 - ++string; 192 - } 196 + typedef struct send_string_memory_state_t { 197 + const char *string; 198 + } send_string_memory_state_t; 199 + 200 + char send_string_get_next_ram(void *arg) { 201 + send_string_memory_state_t *state = (send_string_memory_state_t *)arg; 202 + char ret = *state->string; 203 + state->string++; 204 + return ret; 205 + } 206 + 207 + void send_string_with_delay(const char *string, uint8_t interval) { 208 + send_string_memory_state_t state = {string}; 209 + send_string_with_delay_impl(send_string_get_next_ram, &state, interval); 193 210 } 194 211 195 212 void send_char(char ascii_code) { ··· 297 314 send_string_with_delay_P(string, TAP_CODE_DELAY); 298 315 } 299 316 317 + char send_string_get_next_progmem(void *arg) { 318 + send_string_memory_state_t *state = (send_string_memory_state_t *)arg; 319 + char ret = pgm_read_byte(state->string); 320 + state->string++; 321 + return ret; 322 + } 323 + 300 324 void send_string_with_delay_P(const char *string, uint8_t interval) { 301 - while (1) { 302 - char ascii_code = pgm_read_byte(string); 303 - if (!ascii_code) break; 304 - if (ascii_code == SS_QMK_PREFIX) { 305 - ascii_code = pgm_read_byte(++string); 306 - 307 - if (ascii_code == SS_TAP_CODE) { 308 - // tap 309 - uint8_t keycode = pgm_read_byte(++string); 310 - tap_code(keycode); 311 - } else if (ascii_code == SS_DOWN_CODE) { 312 - // down 313 - uint8_t keycode = pgm_read_byte(++string); 314 - register_code(keycode); 315 - } else if (ascii_code == SS_UP_CODE) { 316 - // up 317 - uint8_t keycode = pgm_read_byte(++string); 318 - unregister_code(keycode); 319 - } else if (ascii_code == SS_DELAY_CODE) { 320 - // delay 321 - int ms = 0; 322 - uint8_t keycode = pgm_read_byte(++string); 323 - 324 - while (isdigit(keycode)) { 325 - ms *= 10; 326 - ms += keycode - '0'; 327 - keycode = pgm_read_byte(++string); 328 - } 329 - wait_ms(ms); 330 - } 331 - } else { 332 - send_char_with_delay(ascii_code, interval); 333 - } 334 - 335 - ++string; 336 - } 325 + send_string_memory_state_t state = {string}; 326 + send_string_with_delay_impl(send_string_get_next_progmem, &state, interval); 337 327 } 338 328 #endif
+8
quantum/send_string/send_string.h
··· 161 161 */ 162 162 #define SEND_STRING_DELAY(string, interval) send_string_with_delay_P(PSTR(string), interval) 163 163 164 + /** 165 + * \brief Actual implementation function that iterates and sends the string returned by the getter function. 166 + * 167 + * The getter assumes that the next byte is available to be read, and returns it. `arg` is passed in and can be whatever 168 + * makes most sense for the getter -- each invocation of `getter` must advance its position in the source. 169 + */ 170 + void send_string_with_delay_impl(char (*getter)(void *), void *arg, uint8_t interval); 171 + 164 172 /** \} */