Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit 3053a8e

Browse files
committed
added gearChanges()
1 parent ef61f32 commit 3053a8e

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ array getJSON(float $crank_length = null, int $ftp = null, array $data_required
360360
* ['timestamp', 'paused', 'temperature', 'lap', 'position_lat', 'position_long', 'distance', 'altitude', 'speed', 'heart_rate', 'cadence', 'power', 'quadrant-analysis']
361361
*/
362362
```
363+
Returns array of gear change information (if present, e.g. using Shimano D-Fly Wireless Di2 Transmitter):
364+
```php
365+
// By default, time spent in a gear whilst the timer is paused (e.g. autopause) is ignored. Set to false to include.
366+
array gearChanges($bIgnoreTimerPaused = true)
367+
```
363368

364369
##Acknowledgement
365370
This class has been created using information available in a Software Development Kit (SDK) made available by ANT ([thisisant.com](http://www.thisisant.com/resources/fit)).

src/phpFITFileAnalysis.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,146 @@ public function quadrantAnalysis($crank_length, $ftp, $selected_cadence = 90, $u
19991999

20002000
return $quadrant_plot;
20012001
}
2002+
2003+
/**
2004+
* Returns array of gear change information.
2005+
*/
2006+
public function gearChanges($bIgnoreTimerPaused = true)
2007+
{
2008+
/**
2009+
* Event enumerated values of interest
2010+
* 42 = front_gear_change
2011+
* 43 = rear_gear_change
2012+
*/
2013+
$fgcek = array_keys($this->data_mesgs['event']['event'], 42); // front gear change event keys
2014+
$rgcek = array_keys($this->data_mesgs['event']['event'], 43); // rear gear change event keys
2015+
2016+
/**
2017+
* gear_change_data (uint32)
2018+
* components:
2019+
* rear_gear_num 00000000 00000000 00000000 11111111
2020+
* rear_gear 00000000 00000000 11111111 00000000
2021+
* front_gear_num 00000000 11111111 00000000 00000000
2022+
* front_gear 11111111 00000000 00000000 00000000
2023+
* scale: 1, 1, 1, 1
2024+
* bits: 8, 8, 8, 8
2025+
*/
2026+
2027+
$fgc = []; // front gear components
2028+
$front_gears = [];
2029+
foreach ($fgcek as $k) {
2030+
$fgc_tmp = [
2031+
'timestamp' => $this->data_mesgs['event']['timestamp'][$k],
2032+
// 'data' => $this->data_mesgs['event']['data'][$k],
2033+
// 'event_type' => $this->data_mesgs['event']['event_type'][$k],
2034+
// 'event_group' => $this->data_mesgs['event']['event_group'][$k],
2035+
'rear_gear_num' => $this->data_mesgs['event']['data'][$k] & 255,
2036+
'rear_gear' => ($this->data_mesgs['event']['data'][$k] >> 8) & 255,
2037+
'front_gear_num' => ($this->data_mesgs['event']['data'][$k] >> 16) & 255,
2038+
'front_gear' => ($this->data_mesgs['event']['data'][$k] >> 24) & 255
2039+
];
2040+
2041+
$fgc[] = $fgc_tmp;
2042+
2043+
if (!array_key_exists($fgc_tmp['front_gear_num'], $front_gears)) {
2044+
$front_gears[$fgc_tmp['front_gear_num']] = $fgc_tmp['front_gear'];
2045+
}
2046+
}
2047+
ksort($front_gears);
2048+
2049+
$rgc = []; // rear gear components
2050+
$rear_gears = [];
2051+
foreach ($rgcek as $k) {
2052+
$rgc_tmp = [
2053+
'timestamp' => $this->data_mesgs['event']['timestamp'][$k],
2054+
// 'data' => $this->data_mesgs['event']['data'][$k],
2055+
// 'event_type' => $this->data_mesgs['event']['event_type'][$k],
2056+
// 'event_group' => $this->data_mesgs['event']['event_group'][$k],
2057+
'rear_gear_num' => $this->data_mesgs['event']['data'][$k] & 255,
2058+
'rear_gear' => ($this->data_mesgs['event']['data'][$k] >> 8) & 255,
2059+
'front_gear_num' => ($this->data_mesgs['event']['data'][$k] >> 16) & 255,
2060+
'front_gear' => ($this->data_mesgs['event']['data'][$k] >> 24) & 255
2061+
];
2062+
2063+
$rgc[] = $rgc_tmp;
2064+
2065+
if (!array_key_exists($rgc_tmp['rear_gear_num'], $rear_gears)) {
2066+
$rear_gears[$rgc_tmp['rear_gear_num']] = $rgc_tmp['rear_gear'];
2067+
}
2068+
}
2069+
ksort($rear_gears);
2070+
2071+
$timestamps = $this->data_mesgs['record']['timestamp'];
2072+
$first_ts = min($timestamps); // first timestamp
2073+
$last_ts = max($timestamps); // last timestamp
2074+
2075+
$fg = 0; // front gear at start of ride
2076+
$rg = 0; // rear gear at start of ride
2077+
2078+
if (isset($fgc[0]['timestamp'])) {
2079+
if ($first_ts == $fgc[0]['timestamp']) {
2080+
$fg = $fgc[0]['front_gear'];
2081+
}
2082+
else {
2083+
$fg = $fgc[0]['front_gear_num'] == 1 ? $front_gears[2] : $front_gears[1];
2084+
}
2085+
}
2086+
2087+
if (isset($rgc[0]['timestamp'])) {
2088+
if ($first_ts == $rgc[0]['timestamp']) {
2089+
$rg = $rgc[0]['rear_gear'];
2090+
}
2091+
else {
2092+
$rg = $rgc[0]['rear_gear_num'] == min($rear_gears) ? $rear_gears[$rgc[0]['rear_gear_num'] + 1] : $rear_gears[$rgc[0]['rear_gear_num'] - 1];
2093+
}
2094+
}
2095+
2096+
$fg_summary = [];
2097+
$rg_summary = [];
2098+
$combined = [];
2099+
$gears_array = [];
2100+
2101+
if($bIgnoreTimerPaused === true) {
2102+
$is_paused = $this->isPaused();
2103+
}
2104+
2105+
reset($fgc);
2106+
reset($rgc);
2107+
for ($i = $first_ts; $i < $last_ts; ++$i) {
2108+
if($bIgnoreTimerPaused === true && $is_paused[$i] === true) {
2109+
continue;
2110+
}
2111+
2112+
$fgc_tmp = current($fgc);
2113+
$rgc_tmp = current($rgc);
2114+
2115+
if ($i > $fgc_tmp['timestamp']) {
2116+
if (next($fgc) !== false) {
2117+
$fg = $fgc_tmp['front_gear'];
2118+
}
2119+
}
2120+
$fg_summary[$fg] = isset($fg_summary[$fg]) ? $fg_summary[$fg] + 1 : 1;
2121+
2122+
if ($i > $rgc_tmp['timestamp']) {
2123+
if (next($rgc) !== false) {
2124+
$rg = $rgc_tmp['rear_gear'];
2125+
}
2126+
}
2127+
$rg_summary[$rg] = isset($rg_summary[$rg]) ? $rg_summary[$rg] + 1 : 1;
2128+
2129+
$combined[$fg][$rg] = isset($combined[$fg][$rg]) ? $combined[$fg][$rg] + 1 : 1;
2130+
2131+
$gears_array[$i] = ['front_gear' => $fg, 'rear_gear' => $rg];
2132+
}
2133+
2134+
krsort($fg_summary);
2135+
krsort($rg_summary);
2136+
krsort($combined);
2137+
2138+
$output = ['front_gear_summary' => $fg_summary, 'rear_gear_summary' => $rg_summary, 'combined_summary' => $combined, 'gears_array' => $gears_array];
2139+
2140+
return $output;
2141+
}
20022142

20032143
/**
20042144
* Create a JSON object that contains available record message information and CPV/AEPF if requested/available.

0 commit comments

Comments
 (0)