Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Implement some power-consumption optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
kareltucek committed Oct 6, 2024
1 parent b24b176 commit 3bb6881
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 8 deletions.
46 changes: 41 additions & 5 deletions device/src/bt_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,35 @@
#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

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,
},
};

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -180,13 +205,24 @@ 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 {
DeviceState_SetConnection(ConnectionId_BluetoothHid, ConnectionType_None);
}
}

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:
Expand Down
2 changes: 2 additions & 0 deletions device/src/bt_conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions device/src/keyboard/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -90,6 +91,10 @@ void i2cPoller() {
if (masterTransferInProgress) {
lastStatus = processMasterTransfer();
}

if (CurrentPowerMode >= PowerMode_LightSleep) {
k_msleep(100);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion device/src/keyboard/key_scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "legacy/config_manager.h"
#include "legacy/macros/keyid_parser.h"
#include "attributes.h"
#include "power_mode.h"

// Thread definitions

Expand Down Expand Up @@ -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);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions device/src/state_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <zephyr/kernel.h>
#include "legacy/peripherals/merge_sensor.h"
#include "power_mode.h"
#include "bt_conn.h"

#define THREAD_STACK_SIZE 2000
#define THREAD_PRIORITY 5
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions right/src/power_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions right/src/power_mode.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef __POWER_MODE_H__
#define __POWER_MODE_H__
#ifndef __UHK_POWER_MODE_H__
#define __UHK_POWER_MODE_H__

// Includes:

Expand Down

0 comments on commit 3bb6881

Please sign in to comment.