feat: add HeartRateProvider (bpm + HR zone)
Add Sensor and UserProfile permissions to manifest.xml (required for Toybox.Sensor and Toybox.UserProfile API access; build fails without them). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -20,7 +20,10 @@
|
|||||||
Use "Monkey C: Edit Permissions" from the Visual Studio Code command
|
Use "Monkey C: Edit Permissions" from the Visual Studio Code command
|
||||||
palette to update permissions.
|
palette to update permissions.
|
||||||
-->
|
-->
|
||||||
<iq:permissions/>
|
<iq:permissions>
|
||||||
|
<iq:uses-permission id="Sensor"/>
|
||||||
|
<iq:uses-permission id="UserProfile"/>
|
||||||
|
</iq:permissions>
|
||||||
<!--
|
<!--
|
||||||
Use "Monkey C: Edit Languages" from the Visual Studio Code command
|
Use "Monkey C: Edit Languages" from the Visual Studio Code command
|
||||||
palette to edit your compatible language list.
|
palette to edit your compatible language list.
|
||||||
|
|||||||
46
source/HeartRateProvider.mc
Normal file
46
source/HeartRateProvider.mc
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import Toybox.Lang;
|
||||||
|
import Toybox.Sensor;
|
||||||
|
import Toybox.UserProfile;
|
||||||
|
|
||||||
|
// Wraps the heart-rate sensor and maps the current bpm to a zone (1..5).
|
||||||
|
// Returns null bpm when no reading is available (e.g. simulator without data
|
||||||
|
// simulation), so the view can show a placeholder.
|
||||||
|
class HeartRateProvider {
|
||||||
|
private var _hr as Number?;
|
||||||
|
private var _zones as Array<Number>?;
|
||||||
|
|
||||||
|
public function initialize() {
|
||||||
|
_hr = null;
|
||||||
|
_zones = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onStart() as Void {
|
||||||
|
Sensor.setEnabledSensors([Sensor.SENSOR_HEARTRATE]);
|
||||||
|
Sensor.enableSensorEvents(method(:onSensor));
|
||||||
|
_zones = UserProfile.getHeartRateZones(UserProfile.HR_ZONE_SPORT_GENERIC);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onStop() as Void {
|
||||||
|
Sensor.enableSensorEvents(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onSensor(info as Sensor.Info) as Void {
|
||||||
|
_hr = info.heartRate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getHeartRate() as Number? {
|
||||||
|
return _hr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns 1..5, or 0 if HR/zones unavailable.
|
||||||
|
public function getZone() as Number {
|
||||||
|
if (_hr == null || _zones == null) { return 0; }
|
||||||
|
var z = _zones as Array<Number>;
|
||||||
|
var hr = _hr as Number;
|
||||||
|
// _zones holds 6 boundaries: [z1lo, z2lo, z3lo, z4lo, z5lo, max]
|
||||||
|
for (var i = z.size() - 2; i >= 0; i -= 1) {
|
||||||
|
if (hr >= z[i]) { return i + 1 > 5 ? 5 : i + 1; }
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user