dhiltonp

dhiltonp

Jan 3rd, 2016
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. //!
  2. //! Copyright 2016 David Hilton
  3. //! All rights reserved.
  4. //! Redistribution and use in source and binary forms, with or without
  5. //! modification, are permitted provided that the following conditions are met:
  6. //!     * Redistributions of source code must retain the above copyright
  7. //!       notice, this list of conditions and the following disclaimer.
  8. //!     * Redistributions in binary form must reproduce the above copyright
  9. //!       notice, this list of conditions and the following disclaimer in the
  10. //!       documentation and/or other materials provided with the distribution.
  11. //!     * Neither the name of the <organization> nor the
  12. //!       names of its contributors may be used to endorse or promote products
  13. //!       derived from this software without specific prior written permission.
  14. //!
  15. //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. //! ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. //! WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. //! DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  19. //! DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. //! (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. //! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. //! ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. //! (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. //! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. //!
  26.  
  27. using Toybox.System;
  28. using Toybox.Time;
  29. using Toybox.Timer;
  30. using Toybox.Sensor;
  31. using Toybox.Math;
  32.  
  33. class HRPoller {
  34.     var rhr = 50;
  35.     var mhr = 185;
  36.     var hr_range;
  37.     var max_sample_rate = 600;
  38.     var min_sample_rate = 60;
  39.    
  40.     var enhanced_rhr_detection = true;
  41.    
  42.     var poll_timer;
  43.     var hr_active = false;
  44.     var last_update = 0; // time from epoch, I'm guessing. Should first update next run.
  45.     var next_update = 0; // now!
  46.    
  47.     var last_rhr_time = 0;
  48.     var enhanced_rhr_active = false;
  49.  
  50.     function start() {
  51.         // load poll frequency settings
  52.        
  53.         // From AccelMagView.mc
  54.         poll_timer = new Timer.Timer();
  55.         poll_timer.start( method(:update), 1000, true );
  56.         System.println("HRPoller started");
  57.     }
  58.    
  59.     function stop() {
  60.         // save poll frequency settings
  61.         // turn off HR requesting, if active
  62.         System.println("HRPoller stopped");
  63.     }
  64.    
  65.     function update() {
  66.         //System.println("HRPoller updating");
  67.         var hr_range = mhr - rhr;
  68.         var now = Time.now().value();
  69.         var sensor_on_before_read = 30;
  70.        
  71.         if (!hr_active && now - last_update > next_update - sensor_on_before_read) {
  72.             // turn on sensor, set callback
  73.             System.println("turning on sensor");
  74.             Sensor.setEnabledSensors( [Sensor.SENSOR_HEARTRATE] );
  75.             hr_active = true;
  76.             return;
  77.         }
  78.        
  79.         if(hr_active && now - last_update > next_update) {
  80.             last_update = now;
  81.        
  82.             // do we have a stable value?
  83.             // read value, store, calculate callback time, maybe turn off sensor
  84.             var hr = Sensor.getInfo().heartRate;
  85.            
  86.             // TODO: observe enhanced RHR calculation
  87.             // if it's been over about 2 hours since the last long RHR check,
  88.             //  and HR is low, stay on for a bit.
  89.             if (now - last_rhr_time > 7000 && hr < rhr*1.1) {
  90.                 System.println("enhanced rhr");
  91.                 next_update = 5*60;
  92.                 last_rhr_time = now;
  93.                 return; // read again in 3 minutes from now; in the meantime, the system will record HR
  94.             } else {
  95.                 next_update = min_sample_rate*Math.pow((max_sample_rate/min_sample_rate),
  96.                                   (mhr-hr)/hr_range.toFloat());
  97.             }
  98.             System.println(hr.toString()+"hr: next update in "+next_update.toString());
  99.            
  100.             // leave sensor on?
  101.             if(next_update > sensor_on_before_read*1.5) {
  102.                 // garmin sensor app turns on the sensor for 2minutes; this seems excessive?
  103.                 // It turns on for 20+ seconds when doing a regular update. Maybe 30s?
  104.                 Sensor.setEnabledSensors( [] );
  105.                 hr_active = false;
  106.             }
  107.            
  108.             return;
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment