diff --git a/source/application/bluetooth.c b/source/application/bluetooth.c index 02cef3f9..77051e86 100644 --- a/source/application/bluetooth.c +++ b/source/application/bluetooth.c @@ -446,12 +446,13 @@ void bluetooth_setup() bond.keyset.keys_peer.p_id_key = &bond.peer_id_key; bond.keyset.keys_peer.p_pk = &bond.peer_private_key; - // Set connection parameters + // Set connection parameters - maximum power optimization + // Ultra-low power: Wake only once per second or less ble_gap_conn_params_t gap_conn_params = {0}; - gap_conn_params.min_conn_interval = (15 * 1000) / 1250; - gap_conn_params.max_conn_interval = (15 * 1000) / 1250; - gap_conn_params.slave_latency = 0; - gap_conn_params.conn_sup_timeout = (2000 * 1000) / 10000; + gap_conn_params.min_conn_interval = (1000 * 1000) / 1250; // 1000ms = 1 second + gap_conn_params.max_conn_interval = (2000 * 1000) / 1250; // 2000ms = 2 seconds max + gap_conn_params.slave_latency = 4; // Skip 4 additional events + gap_conn_params.conn_sup_timeout = (8000 * 1000) / 10000; // 8s timeout check_error(sd_ble_gap_ppcp_set(&gap_conn_params)); // Create the service UUIDs @@ -554,7 +555,7 @@ void bluetooth_setup() adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED; adv_params.primary_phy = BLE_GAP_PHY_1MBPS; adv_params.secondary_phy = BLE_GAP_PHY_1MBPS; - adv_params.interval = (20 * 1000) / 625; + adv_params.interval = (2000 * 1000) / 625; // 2000ms = 2 seconds (maximum power saving) // Configure the advertising set check_error(sd_ble_gap_adv_set_configure(&ble_handles.advertising, diff --git a/source/application/lua_libraries/imu.c b/source/application/lua_libraries/imu.c index bc29570e..7d15a01d 100644 --- a/source/application/lua_libraries/imu.c +++ b/source/application/lua_libraries/imu.c @@ -29,6 +29,7 @@ #include "lauxlib.h" #include "lua.h" #include "main.h" +#include "nrf_soc.h" #include "nrfx_gpiote.h" #include "nrfx_systick.h" #include "pinout.h" @@ -109,7 +110,7 @@ static imu_values_t get_imu_data(void) check_error(i2c_write(MAGNETOMETER, 0x1B, 0x80, 0x80).fail); check_error(i2c_write(MAGNETOMETER, 0x1D, 0x40, 0x40).fail); - // Wait until data is ready + // Wait until data is ready - power optimized while (true) { if (not_real_hardware) @@ -122,7 +123,9 @@ static imu_values_t get_imu_data(void) break; } - nrfx_systick_delay_ms(1); + // Power optimization: Use event-driven sleep instead of delay + // This allows CPU to sleep between magnetometer polls + check_error(sd_app_evt_wait()); } // Read magnetometer (14 bit signed integers) diff --git a/source/application/main.c b/source/application/main.c index 3d913237..85d6f3be 100644 --- a/source/application/main.c +++ b/source/application/main.c @@ -36,6 +36,7 @@ #include "nrf_clock.h" #include "nrf_gpio.h" #include "nrf_sdm.h" +#include "nrf_soc.h" #include "nrf.h" #include "nrfx_gpiote.h" #include "nrfx_log.h" @@ -476,5 +477,9 @@ int main(void) reload_watchdog(NULL, NULL); run_lua(bluetooth_is_paired()); + + // Power optimization: Put CPU to sleep until next event + // This prevents busy-wait loop that drains battery + check_error(sd_app_evt_wait()); } } \ No newline at end of file