44
55using namespace Pinetime ::Controllers;
66
7+ HeartRateZoneSettings::HeartRateZoneSettings () {
8+ version = 0 ;
9+ adjustMsDelay = 300000 ;
10+ age = 25 ;
11+ maxHeartRate = maxHeartRateEstimate (age);
12+ percentTarget = {50 , 60 , 70 , 80 , 90 };
13+ bpmTarget = bpmZones (percentTarget, maxHeartRate);
14+ allowCalibration = true ;
15+ };
16+
17+ HeartRateController::HeartRateController (Pinetime::Controllers::FS& fs) : fs {fs} {};
18+
719void HeartRateController::Update (HeartRateController::States newState, uint8_t heartRate) {
820 this ->state = newState;
921 if (this ->heartRate != heartRate) {
1022 uint32_t ts = xTaskGetTickCount ();
1123 uint32_t z;
1224 zone = 0 ;
13- auto adjustMax = pdMS_TO_TICKS (zoneSettings. adjustDelay );
14- for (z = zoneSettings .bpmTarget .size () - 1 ; i < zoneSettings .bpmTarget .size (); --i ) {
15- if (this ->heartRate >= zoneSettings .bpmTarget [i ]) {
25+ auto adjustMax = pdMS_TO_TICKS (this -> zSettings . adjustMsDelay );
26+ for (z = this -> zSettings .bpmTarget .size () - 1 ; z < this -> zSettings .bpmTarget .size (); --z ) {
27+ if (this ->heartRate >= this -> zSettings .bpmTarget [z ]) {
1628 uint32_t dt = ts - lastActiveTime;
17- currentActivity.zoneTime [i ] += dt;
29+ currentActivity.zoneTime [z ] += dt;
1830 zone = z + 1 ;
1931 // don't make increases unless this is consistantly higher than normal (zone 5 is max)
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 );
32+ if (this -> zSettings .allowCalibration && zone >= 5 && dt > adjustMax) {
33+ this -> zSettings .maxHeartRate = this -> zSettings .maxHeartRate >= this ->heartRate ? this -> zSettings .maxHeartRate : this ->heartRate ;
34+ this -> zSettings .bpmTarget = bpmZones (this -> zSettings .percentTarget , this -> zSettings .maxHeartRate );
2335 }
2436 break ;
2537 }
@@ -36,8 +48,9 @@ void HeartRateController::AdvanceDay() {
3648 HeartRateZones<uint16_t > convertedActivity {};
3749 auto ticksPerSecond = pdMS_TO_TICKS (1000 );
3850
51+ auto totalTime = currentActivity.totalTime ();
3952 for (uint32_t i = 0 ; i < convertedActivity.zoneTime .size (); i++) {
40- convertedActivity.zoneTime [i] = fixed_rounding (currentActivity. totalTime , ticksPerSecond);
53+ convertedActivity.zoneTime [i] = fixed_rounding (totalTime, ticksPerSecond);
4154 }
4255
4356 activity[0 ] = convertedActivity;
@@ -56,9 +69,9 @@ void HeartRateController::SaveSettingsToFile() const {
5669 return ;
5770 }
5871
59- fs.FileWrite (&heartRateZoneFile, reinterpret_cast <const uint8_t *>(&zoneSettings) , sizeof (zoneSettings ));
72+ fs.FileWrite (&heartRateZoneFile, reinterpret_cast <const uint8_t *>(&( this -> zSettings )) , sizeof (this -> zSettings ));
6073 fs.FileClose (&heartRateZoneFile);
61- NRF_LOG_INFO (" [HeartRateController] Saved heart rate zone settings with format version %u to file" , zoneSettings .version );
74+ NRF_LOG_INFO (" [HeartRateController] Saved heart rate zone settings with format version %u to file" , this -> zSettings .version );
6275
6376 lfs_file_t zoneDataFile;
6477 if (fs.FileOpen (&zoneDataFile, " /.system/hrz.dat" , LFS_O_WRONLY | LFS_O_CREAT) != LFS_ERR_OK) {
@@ -68,8 +81,7 @@ void HeartRateController::SaveSettingsToFile() const {
6881
6982 fs.FileWrite (&zoneDataFile, reinterpret_cast <const uint8_t *>(&activity), sizeof (activity));
7083 fs.FileClose (&zoneDataFile);
71- NRF_LOG_INFO (" [HeartRateController] Saved heart rate zone data with format version %u to file" , zoneSettings.version );
72-
84+ NRF_LOG_INFO (" [HeartRateController] Saved heart rate zone data with format version %u to file" , this ->zSettings .version );
7385}
7486
7587void HeartRateController::Enable () {
0 commit comments