Source for file hr_tools.php
Documentation is available at hr_tools.php
* Tools for heart rate computing and display
* @author Jean-Philippe Brunon <jp75018@free.fr>
* @copyright 2007-2009 Jean-Philippe Brunon
* @license http://www.opensource.org/licenses/gpl-license.php GPL
* @version $Id: hr_tools.php 22 2009-03-05 23:58:02Z jp75018 $
* General configuration file
require_once ('conf/config.php');
* Heart rate colors for some particuliar HR pct values.
* Used to compute colors by interpolation.
* @global array $GLOBALS['hr_col']
$GLOBALS['hr_col'] =
array(
'rest' =>
array(0X00, 0X00, 0Xff), // blue
'pct65' =>
array(0X00, 0Xff, 0X00), // green
'pct80' =>
array(0Xff, 0Xff, 0X00), // yellow
'pct90' =>
array(0Xff, 0X80, 0X00), // orange
'max' =>
array(0Xff, 0X00, 0X00), // red
* Get user rest and maximum heart rate from database
* @param integer $user_id User ID (as in database)
* @param integer &$rest_hr Rest heart rate computed if input is empty.
* @param integer &$max_hr Maximum heart rate computed if input is empty.
* @param integer $exe_max_hr Maximum heart rate found in exercise.
* (exe_max_hr cannot be > max_hr)
$request =
sprintf("SELECT gender, birthdate, rest_hr, max_hr FROM plw_user WHERE id = %d",
{ $rest_hr =
$user_info['rest_hr']; }
{ $max_hr =
$user_info['max_hr']; }
// If yet no max HR => Use 220/226 - age formula (if birthdate not null)
if ((! $max_hr) &&
($user_info['birthdate']))
if ($user_info['gender'] ==
'female')
$birthdate =
$user_info['birthdate'];
$birth_year =
substr($birthdate, 0, 4);
$birth_month =
substr($birthdate, 5, 2);
$birth_day =
substr($birthdate, 8, 2);
$user_birth_ts =
mktime(12, 0, 0, $birth_month, $birth_day, $birth_year);
$user_age =
($cur_ts -
$user_birth_ts) /
(365.25 *
86400);
$max_hr =
round($def_max_hr -
$user_age);
// If yet no max HR => Get default
// Check max HR is coherent with exercise data
if ($max_hr <
$exe_max_hr)
{ $max_hr =
$exe_max_hr; }
// If yet no rest HR => Get default
* Get day heart rate information within period
* Rest HR is minumum rest HR, max GR is maximum max HR within period.
* @param integer $user_id User ID (as in database)
* @param string $min_day Minimum date considered (format: YYYY-MM-DD)
* @param string $max_day Maximum date considered (format: YYYY-MM-DD)
* @return array Rest and max HR :
* array('rest_hr' => <value>, 'max_hr' => <value>)
$request =
sprintf("SELECT MIN(rest_hr) AS rest_hr, MAX(max_hr) AS max_hr FROM plw_day_info WHERE user_id = %d AND rest_hr > 0 AND max_hr > 0 AND day >= '%s' AND day <= '%s'",
$user_id, $min_day, $max_day);
* Get RGB color for a given heart rate value
* Rest HR is minumum rest HR, max HR is maximum max HR within period.
* @param integer $hr Heart rate value
* @param array $hr_lim HR special values
* ('rest', 'pct65', 'pct80', 'pct90', 'max')
* @param float $white_ratio Mix with white color (0: no, 1: white)
* @return array RGB color (array of 3 integers [0-255])
for ($i =
0; $i <
3; $i++
)
if ($hr <=
$hr_lim['pct65'])
$rgb_col[$i] =
$GLOBALS['hr_col']['rest'][$i] +
round(($GLOBALS['hr_col']['pct65'][$i] -
$GLOBALS['hr_col']['rest'][$i]) *
($hr -
$hr_lim['rest']) /
($hr_lim['pct65'] -
$hr_lim['rest']));
if (($hr >
$hr_lim['pct65']) &&
($hr <=
$hr_lim['pct80']))
$rgb_col[$i] =
$GLOBALS['hr_col']['pct65'][$i] +
round(($GLOBALS['hr_col']['pct80'][$i] -
$GLOBALS['hr_col']['pct65'][$i]) *
($hr -
$hr_lim['pct65']) /
($hr_lim['pct80'] -
$hr_lim['pct65']));
if (($hr >
$hr_lim['pct80']) &&
($hr <=
$hr_lim['pct90']))
$rgb_col[$i] =
$GLOBALS['hr_col']['pct80'][$i] +
round(($GLOBALS['hr_col']['pct90'][$i] -
$GLOBALS['hr_col']['pct80'][$i]) *
($hr -
$hr_lim['pct80']) /
($hr_lim['pct90'] -
$hr_lim['pct80']));
if ($hr >
$hr_lim['pct90'])
$rgb_col[$i] =
$GLOBALS['hr_col']['pct90'][$i] +
round(($GLOBALS['hr_col']['max'][$i] -
$GLOBALS['hr_col']['pct90'][$i]) *
($hr -
$hr_lim['pct90']) /
($hr_lim['max'] -
$hr_lim['pct90']));
{ $rgb_col[$i] +=
(255 -
$rgb_col[$i]) *
$white_ratio; }
return array($rgb_col[0], $rgb_col[1], $rgb_col[2]);
Documentation generated on Sat, 28 Mar 2009 23:15:19 +0000 by phpDocumentor 1.4.1