Skip to content

Commit

Permalink
Updates for better timing.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsiever committed Apr 21, 2024
1 parent ea6f56f commit 4271ee6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
16 changes: 11 additions & 5 deletions HIDService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(diff<minTimeBetweenNotifies) {
if(diff<timeBetweenNotifies) {
uBit.sleep(diff);
}
lastSend = now;
Expand Down
7 changes: 6 additions & 1 deletion HIDService.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const int numReportsMax = 4;
const int reportMapMaxSize = 200; // 172 is enough for any 3
const int reportMaxSize = 8; // Max size
const int minTimeBetweenNotifies = 30; // Minimum amount of time between sending notifies
const int maxTimeBetweenNotifies = 200; // Maximum amount of time between sending notifies
const int defaultTimeBetweenNotifies = 35; // Default amount of time between sending notifies

/**
* Class definition for a MicroBit BLE HID Service.
Expand All @@ -39,6 +41,8 @@ class HIDService : public MicroBitBLEService
public:
static HIDService *getInstance();

static void setEventsPerSecond(uint32_t events);

private:
static HIDService *service; // Singleton
friend class HIDReporter;
Expand Down Expand Up @@ -123,7 +127,8 @@ class HIDService : public MicroBitBLEService

// Can't be const (may be modified by stack; should be persistent)
uint8_t protocolMode; // 0=>Boot Protocol; 1=>Report; Always 1

uint32_t timeBetweenNotifies; // Actual time between notifications

// Actual service data
uint8_t reportMap[reportMapMaxSize];
unsigned reportMapUsed;
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}

0 comments on commit 4271ee6

Please sign in to comment.