diff --git a/HIDService.cpp b/HIDService.cpp index a4c072b..d57a2ac 100644 --- a/HIDService.cpp +++ b/HIDService.cpp @@ -78,14 +78,19 @@ HIDService *HIDService::service = NULL; // Singleton reference to the service // "PM_EVT_FLASH_GARBAGE_COLLECTION_FAILED", // }; - - - // Static method for peer_manager events (Bounce it to the instance, which has access to member vars) void HIDService::static_pm_events(const pm_evt_t* p_event) { getInstance()->pm_events(p_event); } +// Static method to update timing +void HIDService::setEventsPerSecond(uint32_t time) { + // Valid range of ~5 - 30 events + // Apply thresholds / checks + if(time==0) time = defaultTimeBetweenNotifies; + time = min(max((int)(1000.0/time), minTimeBetweenNotifies), maxTimeBetweenNotifies); + getInstance()->timeBetweenNotifies = time; +} void HIDService::pm_events(const pm_evt_t* p_event) { //DEBUG("PM Event %s conn %d, peer %d\n",m_event_str[p_event->evt_id], p_event->conn_handle, p_event->peer_id ); @@ -139,7 +144,8 @@ HIDService *HIDService::getInstance() HIDService::HIDService() : protocolMode(0x01), // Report Protocol reportMapUsed(0), - numReporters(0) + numReporters(0), + timeBetweenNotifies(defaultTimeBetweenNotifies) { // Initialize all report data memset(reporters, 0, sizeof(HIDReporter*)*numReportsMax); @@ -275,7 +281,7 @@ bool HIDService::notifyChrValue( int idx, const uint8_t *data, uint16_t length) static unsigned lastSend = 0; unsigned now = uBit.systemTime(); int diff = now-lastSend; - if(diffBoot Protocol; 1=>Report; Always 1 - + uint32_t timeBetweenNotifies; // Actual time between notifications + // Actual service data uint8_t reportMap[reportMapMaxSize]; unsigned reportMapUsed; diff --git a/README.md b/README.md index 5ed847b..10d87e5 100644 --- a/README.md +++ b/README.md @@ -235,6 +235,14 @@ keyboard.rawScancode() HID keyboards send "scancodes" that represent keys. You may want to send keys that aren't covered here, like the Function Keys (F1, etc.). You can do this by sending the scancode for the key. Supported scancodes can be found starting on page 83 of the "[HID Usage Tables for Universal Serial Bus (USB) v 1.21](https://usb.org/sites/default/files/hut1_21.pdf#page=83)". If you look up Keyboard F1 in the table, you'll find it has a scancode of 112 (in the AT-101 column of the table). So, to send an F1: ``[keyboard.sendString(keyboard.rawScancode(112))]`` +## Controlling Rates #keyboard-setEventsPerSecond + +```sig +keyboard.setEventsPerSecond() +``` + +Set the number of keys that can be sent per second. The maximum is 33 and the minimum is 5. The default is 28. + ## Detecting if the keyboard service use has changed #keyboard-setStatusChangeHandler ```sig diff --git a/keyboard.ts b/keyboard.ts index 7e69165..a19fa8d 100644 --- a/keyboard.ts +++ b/keyboard.ts @@ -164,4 +164,13 @@ namespace keyboard { export function releaseKeys() : void { return } + + //% block="set events per second | %rate keys/s" advanced=true + //% rate.min=5 rate.max=30 + //% shim=HIDService::updateEventsPerSecond + //% weight=50 + export function setEventsPerSecond(rate: number) : void { + return + } + }