diff options
| author | Nick Brassel <nick@tzarc.org> | 2025-01-21 08:24:39 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-21 08:24:39 +1100 |
| commit | a6a0dc803908a426c331b698acab663a900c2b10 (patch) | |
| tree | eb023e6338fabc4032d0eb13def39a41792d8c37 /quantum/send_string/send_string.c | |
| parent | 47575d4af16700e2dea8353c446dd4874e38d333 (diff) | |
Consolidate send_string implementations. (#24817)
Diffstat (limited to 'quantum/send_string/send_string.c')
| -rw-r--r-- | quantum/send_string/send_string.c | 88 |
1 files changed, 39 insertions, 49 deletions
diff --git a/quantum/send_string/send_string.c b/quantum/send_string/send_string.c index 44c5ec5ab9..602f24dabe 100644 --- a/quantum/send_string/send_string.c +++ b/quantum/send_string/send_string.c | |||
| @@ -150,48 +150,65 @@ void send_string(const char *string) { | |||
| 150 | send_string_with_delay(string, TAP_CODE_DELAY); | 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 | while (1) { | 154 | while (1) { |
| 155 | char ascii_code = *string; | 155 | char ascii_code = getter(arg); |
| 156 | if (!ascii_code) break; | 156 | if (!ascii_code) break; |
| 157 | if (ascii_code == SS_QMK_PREFIX) { | 157 | if (ascii_code == SS_QMK_PREFIX) { |
| 158 | ascii_code = *(++string); | 158 | ascii_code = getter(arg); |
| 159 | 159 | ||
| 160 | if (ascii_code == SS_TAP_CODE) { | 160 | if (ascii_code == SS_TAP_CODE) { |
| 161 | // tap | 161 | // tap |
| 162 | uint8_t keycode = *(++string); | 162 | uint8_t keycode = getter(arg); |
| 163 | tap_code(keycode); | 163 | tap_code(keycode); |
| 164 | } else if (ascii_code == SS_DOWN_CODE) { | 164 | } else if (ascii_code == SS_DOWN_CODE) { |
| 165 | // down | 165 | // down |
| 166 | uint8_t keycode = *(++string); | 166 | uint8_t keycode = getter(arg); |
| 167 | register_code(keycode); | 167 | register_code(keycode); |
| 168 | } else if (ascii_code == SS_UP_CODE) { | 168 | } else if (ascii_code == SS_UP_CODE) { |
| 169 | // up | 169 | // up |
| 170 | uint8_t keycode = *(++string); | 170 | uint8_t keycode = getter(arg); |
| 171 | unregister_code(keycode); | 171 | unregister_code(keycode); |
| 172 | } else if (ascii_code == SS_DELAY_CODE) { | 172 | } else if (ascii_code == SS_DELAY_CODE) { |
| 173 | // delay | 173 | // delay |
| 174 | int ms = 0; | 174 | int ms = 0; |
| 175 | uint8_t keycode = *(++string); | 175 | ascii_code = getter(arg); |
| 176 | 176 | ||
| 177 | while (isdigit(keycode)) { | 177 | while (isdigit(ascii_code)) { |
| 178 | ms *= 10; | 178 | ms *= 10; |
| 179 | ms += keycode - '0'; | 179 | ms += ascii_code - '0'; |
| 180 | keycode = *(++string); | 180 | ascii_code = getter(arg); |
| 181 | } | 181 | } |
| 182 | 182 | ||
| 183 | wait_ms(ms); | 183 | wait_ms(ms); |
| 184 | } | 184 | } |
| 185 | 185 | ||
| 186 | wait_ms(interval); | 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 | } else { | 190 | } else { |
| 188 | send_char_with_delay(ascii_code, interval); | 191 | send_char_with_delay(ascii_code, interval); |
| 189 | } | 192 | } |
| 190 | |||
| 191 | ++string; | ||
| 192 | } | 193 | } |
| 193 | } | 194 | } |
| 194 | 195 | ||
| 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); | ||
| 210 | } | ||
| 211 | |||
| 195 | void send_char(char ascii_code) { | 212 | void send_char(char ascii_code) { |
| 196 | send_char_with_delay(ascii_code, TAP_CODE_DELAY); | 213 | send_char_with_delay(ascii_code, TAP_CODE_DELAY); |
| 197 | } | 214 | } |
| @@ -297,42 +314,15 @@ void send_string_P(const char *string) { | |||
| 297 | send_string_with_delay_P(string, TAP_CODE_DELAY); | 314 | send_string_with_delay_P(string, TAP_CODE_DELAY); |
| 298 | } | 315 | } |
| 299 | 316 | ||
| 300 | void send_string_with_delay_P(const char *string, uint8_t interval) { | 317 | char send_string_get_next_progmem(void *arg) { |
| 301 | while (1) { | 318 | send_string_memory_state_t *state = (send_string_memory_state_t *)arg; |
| 302 | char ascii_code = pgm_read_byte(string); | 319 | char ret = pgm_read_byte(state->string); |
| 303 | if (!ascii_code) break; | 320 | state->string++; |
| 304 | if (ascii_code == SS_QMK_PREFIX) { | 321 | return ret; |
| 305 | ascii_code = pgm_read_byte(++string); | 322 | } |
| 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 | 323 | ||
| 335 | ++string; | 324 | void send_string_with_delay_P(const char *string, uint8_t interval) { |
| 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 | #endif | 328 | #endif |
