Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //!
- //! Copyright 2016 David Hilton
- //! All rights reserved.
- //! Redistribution and use in source and binary forms, with or without
- //! modification, are permitted provided that the following conditions are met:
- //! * Redistributions of source code must retain the above copyright
- //! notice, this list of conditions and the following disclaimer.
- //! * Redistributions in binary form must reproduce the above copyright
- //! notice, this list of conditions and the following disclaimer in the
- //! documentation and/or other materials provided with the distribution.
- //! * Neither the name of the <organization> nor the
- //! names of its contributors may be used to endorse or promote products
- //! derived from this software without specific prior written permission.
- //!
- //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- //! ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- //! WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- //! DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
- //! DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- //! (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- //! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- //! ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- //! (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- //! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- //!
- using Toybox.System;
- using Toybox.Time;
- using Toybox.Timer;
- using Toybox.Sensor;
- using Toybox.Math;
- class HRPoller {
- var rhr = 50;
- var mhr = 185;
- var hr_range;
- var max_sample_rate = 600;
- var min_sample_rate = 60;
- var enhanced_rhr_detection = true;
- var poll_timer;
- var hr_active = false;
- var last_update = 0; // time from epoch, I'm guessing. Should first update next run.
- var next_update = 0; // now!
- var last_rhr_time = 0;
- var enhanced_rhr_active = false;
- function start() {
- // load poll frequency settings
- // From AccelMagView.mc
- poll_timer = new Timer.Timer();
- poll_timer.start( method(:update), 1000, true );
- System.println("HRPoller started");
- }
- function stop() {
- // save poll frequency settings
- // turn off HR requesting, if active
- System.println("HRPoller stopped");
- }
- function update() {
- //System.println("HRPoller updating");
- var hr_range = mhr - rhr;
- var now = Time.now().value();
- var sensor_on_before_read = 30;
- if (!hr_active && now - last_update > next_update - sensor_on_before_read) {
- // turn on sensor, set callback
- System.println("turning on sensor");
- Sensor.setEnabledSensors( [Sensor.SENSOR_HEARTRATE] );
- hr_active = true;
- return;
- }
- if(hr_active && now - last_update > next_update) {
- last_update = now;
- // do we have a stable value?
- // read value, store, calculate callback time, maybe turn off sensor
- var hr = Sensor.getInfo().heartRate;
- // TODO: observe enhanced RHR calculation
- // if it's been over about 2 hours since the last long RHR check,
- // and HR is low, stay on for a bit.
- if (now - last_rhr_time > 7000 && hr < rhr*1.1) {
- System.println("enhanced rhr");
- next_update = 5*60;
- last_rhr_time = now;
- return; // read again in 3 minutes from now; in the meantime, the system will record HR
- } else {
- next_update = min_sample_rate*Math.pow((max_sample_rate/min_sample_rate),
- (mhr-hr)/hr_range.toFloat());
- }
- System.println(hr.toString()+"hr: next update in "+next_update.toString());
- // leave sensor on?
- if(next_update > sensor_on_before_read*1.5) {
- // garmin sensor app turns on the sensor for 2minutes; this seems excessive?
- // It turns on for 20+ seconds when doing a regular update. Maybe 30s?
- Sensor.setEnabledSensors( [] );
- hr_active = false;
- }
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment