Skip to content

Commit

Permalink
Added defensive programming
Browse files Browse the repository at this point in the history
  • Loading branch information
JaapvanEkris authored Sep 3, 2024
1 parent 99e082c commit e67bd37
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions app/peripherals/ble/ftms/RowerDataCharacteristic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
/*
Open Rowing Monitor, https://github.com/laberning/openrowingmonitor
Open Rowing Monitor, https://github.com/JaapvanEkris/openrowingmonitor
This implements the Rower Data Characteristic as defined by the Bluetooth SIG
Currently not many applications exist that support thes FTMS Characteristic for Rowing so its hard
Expand Down Expand Up @@ -61,32 +61,32 @@ export default class RowerDataCharacteristic extends bleno.Characteristic {
// see https://www.bluetooth.com/specifications/specs/gatt-specification-supplement-3/
// for some of the data types
// Stroke Rate in stroke/minute, value is multiplied by 2 to have a .5 precision
bufferBuilder.writeUInt8(Math.round(data.cycleStrokeRate * 2))
bufferBuilder.writeUInt8(data.cycleStrokeRate > 0 ? Math.round(data.cycleStrokeRate * 2) : 0)
// Stroke Count
bufferBuilder.writeUInt16LE(Math.round(data.totalNumberOfStrokes))
bufferBuilder.writeUInt16LE(data.totalNumberOfStrokes > 0 ? Math.round(data.totalNumberOfStrokes) : 0)
// Total Distance in meters
bufferBuilder.writeUInt24LE(Math.round(data.totalLinearDistance))
bufferBuilder.writeUInt24LE(data.totalLinearDistance > 0 ? Math.round(data.totalLinearDistance) : 0)
// Instantaneous Pace in seconds/500m
// if split is infinite (i.e. while pausing), should use the highest possible number (0xFFFF)
// todo: eventhough mathematically correct, setting 0xFFFF (65535s) causes some ugly spikes
// in some applications which could shift the axis (i.e. workout diagrams in MyHomeFit)
// so instead for now we use 0 here
bufferBuilder.writeUInt16LE(data.cyclePace !== Infinity && data.cyclePace < 65535 ? Math.round(data.cyclePace) : 0xFFFF)
// Instantaneous Power in watts
bufferBuilder.writeUInt16LE(Math.round(data.cyclePower))
bufferBuilder.writeUInt16LE(data.cyclePower > 0 ? Math.round(data.cyclePower) : 0)
// Energy in kcal
// Total energy in kcal
bufferBuilder.writeUInt16LE(Math.round(data.totalCalories))
bufferBuilder.writeUInt16LE(data.totalCalories > 0 ? Math.round(data.totalCalories) : 0)
// Energy per hour
// The Energy per Hour field represents the average expended energy of a user during a
// period of one hour.
bufferBuilder.writeUInt16LE(Math.round(data.totalCaloriesPerHour))
bufferBuilder.writeUInt16LE(data.totalCaloriesPerHour > 0 ? Math.round(data.totalCaloriesPerHour) : 0)
// Energy per minute
bufferBuilder.writeUInt8(Math.round(data.totalCaloriesPerMinute))
bufferBuilder.writeUInt8(data.totalCaloriesPerMinute > 0 ? Math.round(data.totalCaloriesPerMinute) : 0)
// Heart Rate: Beats per minute with a resolution of 1
bufferBuilder.writeUInt8(Math.round(data.heartrate))
bufferBuilder.writeUInt8(data.heartrate > 0 ? Math.round(data.heartrate) : 0)
// Elapsed Time: Seconds with a resolution of 1
bufferBuilder.writeUInt16LE(Math.round(data.totalMovingTime))
bufferBuilder.writeUInt16LE(data.totalMovingTime > 0 ? Math.round(data.totalMovingTime) : 0)

const buffer = bufferBuilder.getBuffer()
if (buffer.length > this._subscriberMaxValueSize) {
Expand Down

0 comments on commit e67bd37

Please sign in to comment.