3131#include "py/runtime.h"
3232#include "shared/runtime/interrupt_char.h"
3333#include "supervisor/usb.h"
34+ #include "supervisor/background_callback.h"
35+ #include "supervisor/shared/tick.h"
3436
3537#ifndef DEBUG
3638#define DEBUG (0)
@@ -110,9 +112,17 @@ STATIC bool report_contains(const hid_keyboard_report_t *report, uint8_t key) {
110112STATIC const char * old_buf = NULL ;
111113STATIC size_t buf_size = 0 ;
112114// this matches Linux default of 500ms to first repeat, 1/20s thereafter
113- STATIC const uint32_t initial_repeat_time = 500 ;
115+ enum { initial_repeat_time = 500 , default_repeat_time = 50 };
116+ STATIC uint64_t repeat_deadline ;
117+ STATIC void repeat_f (void * unused );
118+ background_callback_t repeat_cb = {repeat_f , NULL , NULL , NULL };
119+
120+ STATIC void set_repeat_deadline (uint64_t new_deadline ) {
121+ repeat_deadline = new_deadline ;
122+ background_callback_add_core (& repeat_cb );
123+ }
114124
115- STATIC void send_bufn (const char * buf , size_t n , uint32_t repeat_time ) {
125+ STATIC void send_bufn_core (const char * buf , size_t n ) {
116126 old_buf = buf ;
117127 buf_size = n ;
118128 // repeat_timeout = millis() + repeat_time;
@@ -130,26 +140,36 @@ STATIC void send_bufn(const char *buf, size_t n, uint32_t repeat_time) {
130140 }
131141}
132142
133- STATIC void send_bufz (const char * buf , uint32_t repeat_time ) {
134- send_bufn (buf , strlen (buf ), repeat_time );
143+ STATIC void send_bufn (const char * buf , size_t n ) {
144+ send_bufn_core (buf , n );
145+ set_repeat_deadline (supervisor_ticks_ms64 () + initial_repeat_time );
146+ }
147+
148+ STATIC void send_bufz (const char * buf ) {
149+ send_bufn (buf , strlen (buf ));
135150}
136151
137- STATIC void send_byte (uint8_t code , uint32_t repeat_time ) {
152+ STATIC void send_byte (uint8_t code ) {
138153 static char buf [1 ];
139154 buf [0 ] = code ;
140- send_bufn (buf , 1 , repeat_time );
155+ send_bufn (buf , 1 );
141156}
142157
143- #if 0
144- STATIC uint32_t repeat_timeout ;
145- STATIC const uint32_t default_repeat_time = 50 ;
146- // TODO: nothing actually SENDS the repetitions...
147- STATIC void send_repeat () {
158+ STATIC void send_repeat (void ) {
148159 if (old_buf ) {
149- send_bufn (old_buf , old_buf_size , default_repeat_time );
160+ uint64_t now = supervisor_ticks_ms64 ();
161+ if (now >= repeat_deadline ) {
162+ send_bufn_core (old_buf , buf_size );
163+ set_repeat_deadline (now + default_repeat_time );
164+ } else {
165+ background_callback_add_core (& repeat_cb );
166+ }
150167 }
151168}
152- #endif
169+
170+ STATIC void repeat_f (void * unused ) {
171+ send_repeat ();
172+ }
153173
154174hid_keyboard_report_t old_report ;
155175
@@ -175,7 +195,7 @@ STATIC void process_event(uint8_t dev_addr, uint8_t instance, const hid_keyboard
175195 return ;
176196 }
177197
178- // something was pressed or release , so cancel any key repeat
198+ // something was pressed or released , so cancel any key repeat
179199 old_buf = NULL ;
180200
181201 for (int i = 0 ; i < 6 ; i ++ ) {
@@ -205,7 +225,7 @@ STATIC void process_event(uint8_t dev_addr, uint8_t instance, const hid_keyboard
205225 } else if (ascii >= 'a' && ascii <= 'z' && caps ) {
206226 ascii ^= ('a' ^ 'A' );
207227 }
208- send_byte (ascii , initial_repeat_time );
228+ send_byte (ascii );
209229 continue ;
210230 }
211231
@@ -225,7 +245,7 @@ STATIC void process_event(uint8_t dev_addr, uint8_t instance, const hid_keyboard
225245 }
226246 if (mapper -> flags & FLAG_STRING ) {
227247 const char * msg = skip_nuls (mapper -> data , keycode - mapper -> first );
228- send_bufz (msg , initial_repeat_time );
248+ send_bufz (msg );
229249 } else if (mapper -> data ) {
230250 code = mapper -> data [keycode - mapper -> first ];
231251 } else {
@@ -240,7 +260,7 @@ STATIC void process_event(uint8_t dev_addr, uint8_t instance, const hid_keyboard
240260 if (alt ) {
241261 code ^= 0x80 ;
242262 }
243- send_byte (code , initial_repeat_time );
263+ send_byte (code );
244264 break ;
245265 }
246266 }
0 commit comments