Skip to content

Commit

Permalink
Fixes in battery life, data aggregation and alarm delay..
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr committed Nov 27, 2023
1 parent b395167 commit 0202503
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion SleepGarmin-android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdk 33
compileSdk 34
namespace "com.urbandroid.sleep.garmin"

defaultConfig {
Expand Down
2 changes: 1 addition & 1 deletion SleepGarmin-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.1.1'
classpath 'com.android.tools.build:gradle:8.1.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This file was deleted.

Binary file not shown.
Binary file modified SleepGarmin-watch2/resources/drawables/launcher_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion SleepGarmin-watch2/source/AlarmManager.mc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AlarmManager {
function startAlarmAfterDelay() {
self.ctx.businessManager.logTransmit("AlarmManager#startAlarmAfterDelay");
if (alarmDelayTimerRunning) {
startAlarmNow(true);
self.ctx.alarmManager.startAlarmVibration();
}
}

Expand All @@ -46,7 +46,10 @@ class AlarmManager {
self.ctx.businessManager.logTransmit("AlarmManager#startAlarm, delay: " + delay);
if (delay == -1) {
startAlarmWithoutVibration();
} else if (delay == 0) {
startAlarmNow(true);
} else {
startAlarmNow(false);
alarmDelayTimerRunning = true;
alarmDelayTimer.start(method(:startAlarmAfterDelay), delay, false);
}
Expand Down
22 changes: 18 additions & 4 deletions SleepGarmin-watch2/source/BusinessManager.mc
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,30 @@ class BusinessManager {

var ctx;

var lastUpdate = -1;

var lastUpdateUi = -1;

function initialize(ctx) {
self.ctx = ctx;
}

// Hook that is called on every data received callback - we use this so that we do not have to have our own Timer, which is presumably battery intensive.
function onDataHook() {
DebugManager.log("BusinessManager onDataHook");
self.ctx.commManager.triggerSend();
updateTime(true);
lockScreen();

// DebugManager.log("BusinessManager onDataHook");
var now = System.getTimer();

if ((lastUpdate == -1) || (now - lastUpdate > 2000)) {
self.ctx.commManager.triggerSend();
lastUpdate = now;
}

if ((lastUpdateUi == -1) || (now - lastUpdateUi > 5000)) {
updateTime(true);
lockScreen();
lastUpdateUi = now;
}
}


Expand Down
12 changes: 12 additions & 0 deletions SleepGarmin-watch2/source/CommManager.mc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CommManager {
// From phone
static const MSG_START = "StartTracking";
static const MSG_START_HR = "StartHRTracking";
static const MSG_START_OXI = "StartOxiTracking";
static const MSG_STOP = "StopApp";
static const MSG_CHECK = "Check";
static const MSG_BATCH_SIZE = "BatchSize;";
Expand Down Expand Up @@ -238,6 +239,17 @@ class CommManager {
return;
}

if (msg.equals(CommManager.MSG_START_HR)) {
self.ctx.sensorManager.startHr();
return;
}

if (msg.equals(CommManager.MSG_START_OXI)) {
self.ctx.sensorManager.startOxi();
return;
}


if (msg.equals(CommManager.MSG_STOP)) {
self.ctx.businessManager.exit();
return;
Expand Down
6 changes: 3 additions & 3 deletions SleepGarmin-watch2/source/DataUtil.mc
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class DataUtil {
static function max(arr) {
if (arr.size() == 0) { return null; }

var max = arr[0];
for( var i = 1; i < arr.size(); i += 1 ) {
if (arr[i] > arr[i-1]) { max = arr[i]; }
var max = 0;
for( var i = 0; i < arr.size(); i += 1 ) {
if (arr[i] > max) { max = arr[i]; }
}
return max;
}
Expand Down
16 changes: 11 additions & 5 deletions SleepGarmin-watch2/source/SensorManager.mc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class SensorManager {

const SENSOR_PERIOD_SEC = 4;
const OXI_READING_PERIOD_SEC = 4;
const SENSOR_FREQ = 15;
const SENSOR_AGGREG_WINDOW = 10;

var ctx;

Expand All @@ -27,6 +29,13 @@ class SensorManager {
DebugManager.log("MaxSampleRate " + maxSampleRate);
}

function startHr() {
// Sensor.enableSensorType(Sensor.SensorType.SENSOR_HEARTRATE)
}

function startOxi() {
// Sensor.enableSensorType(Sensor.SensorType.SENSOR_PULSE_OXIMETRY)
}

function start() {
DebugManager.log("SensorManager startAccelerometer");
Expand All @@ -35,10 +44,7 @@ class SensorManager {
:period => SENSOR_PERIOD_SEC,
:accelerometer => {
:enabled => true,
:sampleRate => 10
},
:heartBeatIntervals => {
:enabled => true
:sampleRate => SENSOR_FREQ
}
};
Sensor.registerSensorDataListener(SensorManager.method(:onData), options);
Expand Down Expand Up @@ -76,7 +82,7 @@ class SensorManager {
accYBuf.addAll(yArr);
accZBuf.addAll(zArr);

var maxCount = 100; // Maximum number of values to go into one aggregate (sampleRate [Hz] x batchPeriod [s])
var maxCount = SENSOR_AGGREG_WINDOW * SENSOR_FREQ; // Maximum number of values to go into one aggregate (sampleRate [Hz] x batchPeriod [s])

// Since maximum sensor batching period is 4 seconds and we need to have aggregate period of 10 seconds, we need to aggregate two and half sensor batches. Then we need to retain the remaining half of the third batch.
// In order to do that, we first add all the data from three batches into one array and then aggregate just first 100 values, deleting them from the batch arrays.
Expand Down

0 comments on commit 0202503

Please sign in to comment.