diff --git a/device/src/bt_conn.c b/device/src/bt_conn.c index 0baf70d6..9b916f34 100644 --- a/device/src/bt_conn.c +++ b/device/src/bt_conn.c @@ -7,12 +7,14 @@ #include "device_state.h" #include "keyboard/oled/screens/screen_manager.h" #include "keyboard/oled/widgets/widget.h" +#include "legacy/power_mode.h" #include "nus_client.h" #include "nus_server.h" #include "device.h" #include "keyboard/oled/screens/pairing_screen.h" #include "usb/usb.h" #include "keyboard/oled/widgets/widgets.h" +#include "power_mode.h" #define PeerCount 3 @@ -20,14 +22,20 @@ peer_t Peers[PeerCount] = { { .id = PeerIdLeft, .name = "left", + .isConnected = false, + .conn = NULL, }, { .id = PeerIdRight, .name = "right", + .isConnected = false, + .conn = NULL, }, { .id = PeerIdDongle, .name = "dongle", + .isConnected = false, + .conn = NULL, }, }; @@ -87,12 +95,27 @@ static void set_data_length_extension_params(struct bt_conn *conn) { } static void set_latency_params(struct bt_conn *conn) { - static const struct bt_le_conn_param conn_params = BT_LE_CONN_PARAM_INIT( - 6, 9, // keep it low, lowest allowed is 6 (7.5ms), lowest supported widely is 9 (11.25ms) - 0, // keeping it higher allows power saving on peripheral when there's nothing to send (keep it under 30 though) - 100 // connection timeout (*10ms) + static const struct bt_le_conn_param rightActive = BT_LE_CONN_PARAM_INIT( + 6, 6, // keep it low, lowest allowed is 6 (7.5ms), lowest supported widely is 9 (11.25ms) + 10, // keeping it higher allows power saving on peripheral when there's nothing to send (keep it under 30 though) + 300 // connection timeout (*10ms) ); - int err = bt_conn_le_param_update(conn, &conn_params); + static const struct bt_le_conn_param rightSleep = BT_LE_CONN_PARAM_INIT( + 100, 100, + 10, // so this means a ping every 1.25s? + 300 + ); + static const struct bt_le_conn_param slave = BT_LE_CONN_PARAM_INIT( + 6, 100, + 10, + 300 + ); + int err; + if (DEVICE_IS_UHK80_RIGHT) { + err = bt_conn_le_param_update(conn, CurrentPowerMode >= PowerMode_LightSleep ? &rightSleep : &rightActive); + } else { + err = bt_conn_le_param_update(conn, &slave); + } if (err) { printk("LE latencies update failed: %d", err); } @@ -133,6 +156,8 @@ static void connected(struct bt_conn *conn, uint8_t err) { DeviceState_SetConnection(ConnectionId_BluetoothHid, ConnectionType_Bt); } } else { + Peers[peerId].conn = bt_conn_ref(conn); + set_latency_params(conn); set_data_length_extension_params(conn); @@ -180,6 +205,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) { } if (peerId != PeerIdUnknown) { + bt_conn_unref(Peers[peerId].conn); Peers[peerId].isConnected = false; DeviceState_TriggerUpdate(); } else { @@ -187,6 +213,16 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) { } } +void Bt_UpdatePowerModes() { + if (DEVICE_IS_UHK80_RIGHT) { + for (uint8_t peerId = 0; peerId < PeerCount; peerId++) { + if (Peers[peerId].isConnected && Peers[peerId].conn) { + set_latency_params(Peers[peerId].conn); + } + } + } +} + bool Bt_DeviceIsConnected(device_id_t deviceId) { switch (deviceId) { case DeviceId_Uhk80_Left: diff --git a/device/src/bt_conn.h b/device/src/bt_conn.h index f02328fb..29c88a6f 100644 --- a/device/src/bt_conn.h +++ b/device/src/bt_conn.h @@ -22,6 +22,7 @@ typedef struct { uint8_t id; char name[PeerNameMaxLength + 1]; + struct bt_conn *conn; bt_addr_le_t addr; bool isConnected; } peer_t; @@ -32,6 +33,7 @@ char *GetPeerStringByConn(const struct bt_conn *conn); extern void bt_init(void); extern void num_comp_reply(uint8_t accept); + void Bt_UpdatePowerModes(); void Bt_SetDeviceConnected(device_id_t deviceId); bool Bt_DeviceIsConnected(uint8_t deviceId); diff --git a/device/src/keyboard/i2c.c b/device/src/keyboard/i2c.c index 30f77cb4..a300fccc 100644 --- a/device/src/keyboard/i2c.c +++ b/device/src/keyboard/i2c.c @@ -8,6 +8,7 @@ #include "legacy/slave_drivers/uhk_module_driver.h" #include "legacy/i2c.h" #include "keyboard/i2c.h" +#include "power_mode.h" // Thread definitions @@ -90,6 +91,10 @@ void i2cPoller() { if (masterTransferInProgress) { lastStatus = processMasterTransfer(); } + + if (CurrentPowerMode >= PowerMode_LightSleep) { + k_msleep(100); + } } } diff --git a/device/src/keyboard/key_scanner.c b/device/src/keyboard/key_scanner.c index 5bef4fe6..7dfe56f0 100644 --- a/device/src/keyboard/key_scanner.c +++ b/device/src/keyboard/key_scanner.c @@ -21,6 +21,7 @@ #include "legacy/config_manager.h" #include "legacy/macros/keyid_parser.h" #include "attributes.h" +#include "power_mode.h" // Thread definitions @@ -149,7 +150,11 @@ static void scanKeys() { void keyScanner() { while (true) { scanKeys(); - k_msleep(1); + if (CurrentPowerMode >= PowerMode_LightSleep) { + k_msleep(100); + } else { + k_msleep(1); + } } } diff --git a/device/src/state_sync.c b/device/src/state_sync.c index 44d60f64..b4861313 100644 --- a/device/src/state_sync.c +++ b/device/src/state_sync.c @@ -22,6 +22,7 @@ #include #include "legacy/peripherals/merge_sensor.h" #include "power_mode.h" +#include "bt_conn.h" #define THREAD_STACK_SIZE 2000 #define THREAD_PRIORITY 5 @@ -325,6 +326,10 @@ static void receiveProperty(device_id_t src, state_sync_prop_id_t propId, const EventVector_Set(EventVector_LedMapUpdateNeeded); } break; + case StateSyncPropertyId_PowerMode: + // for both local and remote! + Bt_UpdatePowerModes(); + break; case StateSyncPropertyId_MergeSensor: break; default: diff --git a/right/src/power_mode.c b/right/src/power_mode.c index 0158d5cb..85f390a3 100644 --- a/right/src/power_mode.c +++ b/right/src/power_mode.c @@ -4,11 +4,13 @@ #include "led_manager.h" #ifdef __ZEPHYR__ + #include "state_sync.h" #include "device_state.h" #include "usb/usb.h" #else #include "slave_drivers/is31fl3xxx_driver.h" #include "usb_composite_device.h" + #include "stubs.h" #endif ATTR_UNUSED static bool usbAwake = false; @@ -43,6 +45,9 @@ void PowerMode_Update() { static void lightSleep() { CurrentPowerMode = PowerMode_LightSleep; +#ifdef __ZEPHYR__ + StateSync_UpdateProperty(StateSyncPropertyId_PowerMode, &CurrentPowerMode); +#endif EventVector_Set(EventVector_LedManagerFullUpdateNeeded); EventVector_WakeMain(); } @@ -58,6 +63,9 @@ static void deepSleep() { static void wake() { CurrentPowerMode = PowerMode_Awake; +#ifdef __ZEPHYR__ + StateSync_UpdateProperty(StateSyncPropertyId_PowerMode, &CurrentPowerMode); +#endif EventVector_Set(EventVector_LedManagerFullUpdateNeeded); EventVector_WakeMain(); } diff --git a/right/src/power_mode.h b/right/src/power_mode.h index c91f278c..fba992cc 100644 --- a/right/src/power_mode.h +++ b/right/src/power_mode.h @@ -1,5 +1,5 @@ -#ifndef __POWER_MODE_H__ -#define __POWER_MODE_H__ +#ifndef __UHK_POWER_MODE_H__ +#define __UHK_POWER_MODE_H__ // Includes: