Skip to content

Commit b651a97

Browse files
Advance day, start work on persistance
1 parent aa9a413 commit b651a97

3 files changed

Lines changed: 67 additions & 14 deletions

File tree

src/components/heartrate/HeartRateController.cpp

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ using namespace Pinetime::Controllers;
77
void HeartRateController::Update(HeartRateController::States newState, uint8_t heartRate) {
88
this->state = newState;
99
if (this->heartRate != heartRate) {
10-
1110
uint32_t ts = xTaskGetTickCount();
12-
uint32_t zone;
13-
//auto adjustMax = pdMS_TO_TICKS(300000); // 5 minutes
14-
for (zone = zoneSettings.bpmTarget.size() - 1; i < zoneSettings.bpmTarget.size(); --i) {
11+
uint32_t z;
12+
zone = 0;
13+
auto adjustMax = pdMS_TO_TICKS(zoneSettings.adjustDelay);
14+
for (z = zoneSettings.bpmTarget.size() - 1; i < zoneSettings.bpmTarget.size(); --i) {
1515
if (this->heartRate >= zoneSettings.bpmTarget[i]) {
1616
uint32_t dt = ts - lastActiveTime;
1717
currentActivity.zoneTime[i] += dt;
18-
18+
zone = z + 1;
1919
// don't make increases unless this is consistantly higher than normal (zone 5 is max)
20-
if (zone >= 4 && dt > zoneSettings.adjustDelay) {
21-
zoneSettings.maxHeartRate = zoneSettings.maxHeartRate >= this->heartRate ? zoneSettings.maxHeartRate : this->heartRate;
20+
if (zoneSettings.allowCalibration && zone >= 5 && dt > adjustMax) {
21+
zoneSettings.maxHeartRate = zoneSettings.maxHeartRate >= this->heartRate ? zoneSettings.maxHeartRate : this->heartRate;
22+
zoneSettings.bpmTarget = bpmZones(zoneSettings.percentTarget, zoneSettings.maxHeartRate);
2223
}
2324
break;
2425
}
@@ -31,6 +32,46 @@ void HeartRateController::Update(HeartRateController::States newState, uint8_t h
3132
}
3233
}
3334

35+
void HeartRateController::AdvanceDay() {
36+
HeartRateZones<uint16_t> convertedActivity {};
37+
auto ticksPerSecond = pdMS_TO_TICKS(1000);
38+
39+
for (uint32_t i = 0; i < convertedActivity.zoneTime.size(); i++) {
40+
convertedActivity.zoneTime[i] = fixed_rounding(currentActivity.totalTime, ticksPerSecond);
41+
}
42+
43+
activity[0] = convertedActivity;
44+
activity++;
45+
}
46+
47+
void HeartRateController::SaveSettingsToFile() const {
48+
lfs_dir systemDir;
49+
if (fs.DirOpen("/.system", &systemDir) != LFS_ERR_OK) {
50+
fs.DirCreate("/.system");
51+
}
52+
fs.DirClose(&systemDir);
53+
lfs_file_t heartRateZoneFile;
54+
if (fs.FileOpen(&heartRateZoneFile, "/.system/hrzs.dat", LFS_O_WRONLY | LFS_O_CREAT) != LFS_ERR_OK) {
55+
NRF_LOG_WARNING("[HeartRateController] Failed to open heart rate zone settings file for saving");
56+
return;
57+
}
58+
59+
fs.FileWrite(&heartRateZoneFile, reinterpret_cast<const uint8_t*>(&zoneSettings), sizeof(zoneSettings));
60+
fs.FileClose(&heartRateZoneFile);
61+
NRF_LOG_INFO("[HeartRateController] Saved heart rate zone settings with format version %u to file", zoneSettings.version);
62+
63+
lfs_file_t zoneDataFile;
64+
if (fs.FileOpen(&zoneDataFile, "/.system/hrz.dat", LFS_O_WRONLY | LFS_O_CREAT) != LFS_ERR_OK) {
65+
NRF_LOG_WARNING("[HeartRateController] Failed to open heart rate zone data file for saving");
66+
return;
67+
}
68+
69+
fs.FileWrite(&zoneDataFile, reinterpret_cast<const uint8_t*>(&activity), sizeof(activity));
70+
fs.FileClose(&zoneDataFile);
71+
NRF_LOG_INFO("[HeartRateController] Saved heart rate zone data with format version %u to file", zoneSettings.version);
72+
73+
}
74+
3475
void HeartRateController::Enable() {
3576
if (task != nullptr) {
3677
state = States::NotEnoughData;

src/components/heartrate/HeartRateController.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ namespace Pinetime {
1515
}
1616

1717
namespace Controllers {
18-
template<typename T>
18+
template <typename T>
1919
struct HeartRateZones {
2020
// 1440 minutes in a day (11 bits), 86400 seconds (17 bits)
21-
T zoneTime[5] = {};
21+
T zoneTime[5] = {};
22+
2223
T totalTime() const {
2324
return zoneTime[0] + zoneTime[1] + zoneTime[2] + zoneTime[3] + zoneTime[4];
2425
}
@@ -30,27 +31,29 @@ namespace Pinetime {
3031

3132
constexpr int16_t fixed_rounding(int16_t value, int16_t divisor) {
3233
// true evil: we use >>'s signed behavior to propagate the leading 1 across all bits, eg: we have 0xffff or 0x0000
33-
int16_t signed_value = value >> 15u;
34+
int16_t signed_value = value >> 15u;
3435
int16_t half_divisor = (divisor / 2);
3536
return (value + half_divisor - (divisor & signed_value)) / divisor; // we replace the * by "1" with an &
3637
}
3738

38-
template<size_t N>
39+
template <size_t N>
3940
constexpr std::array<uint8_t, N> bpmZones(std::array<uint8_t, N>& percentages, uint8_t maxBeatsPerMinute) {
4041
std::array<uint8_t, N> targets {};
4142
const uint16_t bpm = maxBeatsPerMinute;
4243
for (uint32_t i = 0; i < N; i++) {
43-
targets[i++] = (uint8_t)fixed_rounding((uint16_t) percentages[i] * bpm, 100);
44+
targets[i++] = (uint8_t) fixed_rounding((uint16_t) percentages[i] * bpm, 100);
4445
}
4546
return targets;
4647
};
4748

4849
struct HeartRateZoneSettings {
50+
uint32_t version = 0;
4951
uint8_t age = 25;
5052
uint8_t maxHeartRate = maxHeartRateEstimate(age);
5153
std::array<uint8_t, 5> percentTarget = {50, 60, 70, 80, 90};
52-
std::array<uint8_t, 5> bpmTarget = bpmZones(percentTarget,maxHeartRate);
53-
uint32_t adjustDelay = 300000;
54+
std::array<uint8_t, 5> bpmTarget = bpmZones(percentTarget, maxHeartRate);
55+
uint32_t adjustMsDelay = 300000;
56+
bool allowCalibration = true;
5457
};
5558

5659
HeartRateZoneSettings zoneSettings {};
@@ -74,8 +77,15 @@ namespace Pinetime {
7477
return heartRate;
7578
}
7679

80+
uint8_t Zone() const {
81+
return zone;
82+
}
83+
7784
void SetService(Pinetime::Controllers::HeartRateService* service);
7885

86+
void AdvanceDay();
87+
void SaveSettingsToFile();
88+
7989
private:
8090
Applications::HeartRateTask* task = nullptr;
8191
States state = States::Stopped;
@@ -86,6 +96,7 @@ namespace Pinetime {
8696
// Heart Rate Zone Storage
8797
HeartRateZones<uint32_t> currentActivity = {};
8898
Utility::CircularBuffer<HeartRateZones<uint16_t>, 31> activity = {};
99+
uint8_t zone = 0;
89100
};
90101
}
91102
}

src/systemtask/SystemTask.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ void SystemTask::Work() {
335335
case Messages::OnNewDay:
336336
motionSensor.ResetStepCounter();
337337
motionController.AdvanceDay();
338+
heartRateController.AdvanceDay();
338339
break;
339340
case Messages::OnNewHour:
340341
using Pinetime::Controllers::AlarmController;

0 commit comments

Comments
 (0)