aboutsummaryrefslogtreecommitdiff
path: root/quantum/send_string
diff options
context:
space:
mode:
authorNick Brassel <[email protected]>2025-01-21 08:24:39 +1100
committerGitHub <[email protected]>2025-01-21 08:24:39 +1100
commita6a0dc803908a426c331b698acab663a900c2b10 (patch)
treeeb023e6338fabc4032d0eb13def39a41792d8c37 /quantum/send_string
parent47575d4af16700e2dea8353c446dd4874e38d333 (diff)
downloadqmk_firmware-a6a0dc803908a426c331b698acab663a900c2b10.tar.gz
qmk_firmware-a6a0dc803908a426c331b698acab663a900c2b10.zip
Consolidate send_string implementations. (#24817)
Diffstat (limited to 'quantum/send_string')
-rw-r--r--quantum/send_string/send_string.c88
-rw-r--r--quantum/send_string/send_string.h8
2 files changed, 47 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) {
send_string_with_delay(string, TAP_CODE_DELAY);
}
-void send_string_with_delay(const char *string, uint8_t interval) {
+void send_string_with_delay_impl(char (*getter)(void *), void *arg, uint8_t interval) {
while (1) {
- char ascii_code = *string;
+ char ascii_code = getter(arg);
if (!ascii_code) break;
if (ascii_code == SS_QMK_PREFIX) {
- ascii_code = *(++string);
+ ascii_code = getter(arg);
if (ascii_code == SS_TAP_CODE) {
// tap
- uint8_t keycode = *(++string);
+ uint8_t keycode = getter(arg);
tap_code(keycode);
} else if (ascii_code == SS_DOWN_CODE) {
// down
- uint8_t keycode = *(++string);
+ uint8_t keycode = getter(arg);
register_code(keycode);
} else if (ascii_code == SS_UP_CODE) {
// up
- uint8_t keycode = *(++string);
+ uint8_t keycode = getter(arg);
unregister_code(keycode);
} else if (ascii_code == SS_DELAY_CODE) {
// delay
- int ms = 0;
- uint8_t keycode = *(++string);
+ int ms = 0;
+ ascii_code = getter(arg);
- while (isdigit(keycode)) {
+ while (isdigit(ascii_code)) {
ms *= 10;
- ms += keycode - '0';
- keycode = *(++string);
+ ms += ascii_code - '0';
+ ascii_code = getter(arg);
}
wait_ms(ms);
}
wait_ms(interval);
+
+ // if we had a delay that terminated with a null, we're done
+ if (ascii_code == 0) break;
} else {
send_char_with_delay(ascii_code, interval);
}
-
- ++string;
}
}
+typedef struct send_string_memory_state_t {
+ const char *string;
+} send_string_memory_state_t;
+
+char send_string_get_next_ram(void *arg) {
+ send_string_memory_state_t *state = (send_string_memory_state_t *)arg;
+ char ret = *state->string;
+ state->string++;
+ return ret;
+}
+
+void send_string_with_delay(const char *string, uint8_t interval) {
+ send_string_memory_state_t state = {string};
+ send_string_with_delay_impl(send_string_get_next_ram, &state, interval);
+}
+
void send_char(char ascii_code) {
send_char_with_delay(ascii_code, TAP_CODE_DELAY);
}
@@ -297,42 +314,15 @@ void send_string_P(const char *string) {
send_string_with_delay_P(string, TAP_CODE_DELAY);
}
-void send_string_with_delay_P(const char *string, uint8_t interval) {
- while (1) {
- char ascii_code = pgm_read_byte(string);
- if (!ascii_code) break;
- if (ascii_code == SS_QMK_PREFIX) {
- ascii_code = pgm_read_byte(++string);
-
- if (ascii_code == SS_TAP_CODE) {
- // tap
- uint8_t keycode = pgm_read_byte(++string);
- tap_code(keycode);
- } else if (ascii_code == SS_DOWN_CODE) {
- // down
- uint8_t keycode = pgm_read_byte(++string);
- register_code(keycode);
- } else if (ascii_code == SS_UP_CODE) {
- // up
- uint8_t keycode = pgm_read_byte(++string);
- unregister_code(keycode);
- } else if (ascii_code == SS_DELAY_CODE) {
- // delay
- int ms = 0;
- uint8_t keycode = pgm_read_byte(++string);
-
- while (isdigit(keycode)) {
- ms *= 10;
- ms += keycode - '0';
- keycode = pgm_read_byte(++string);
- }
- wait_ms(ms);
- }
- } else {
- send_char_with_delay(ascii_code, interval);
- }
+char send_string_get_next_progmem(void *arg) {
+ send_string_memory_state_t *state = (send_string_memory_state_t *)arg;
+ char ret = pgm_read_byte(state->string);
+ state->string++;
+ return ret;
+}
- ++string;
- }
+void send_string_with_delay_P(const char *string, uint8_t interval) {
+ send_string_memory_state_t state = {string};
+ send_string_with_delay_impl(send_string_get_next_progmem, &state, interval);
}
#endif
diff --git a/quantum/send_string/send_string.h b/quantum/send_string/send_string.h
index f727ec507d..4f91252075 100644
--- a/quantum/send_string/send_string.h
+++ b/quantum/send_string/send_string.h
@@ -161,4 +161,12 @@ void send_string_with_delay_P(const char *string, uint8_t interval);
*/
#define SEND_STRING_DELAY(string, interval) send_string_with_delay_P(PSTR(string), interval)
+/**
+ * \brief Actual implementation function that iterates and sends the string returned by the getter function.
+ *
+ * The getter assumes that the next byte is available to be read, and returns it. `arg` is passed in and can be whatever
+ * makes most sense for the getter -- each invocation of `getter` must advance its position in the source.
+ */
+void send_string_with_delay_impl(char (*getter)(void *), void *arg, uint8_t interval);
+
/** \} */