Skip to content

Commit

Permalink
Split sensor code out into their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
kadaan committed Mar 9, 2021
1 parent fc08da9 commit f19752a
Show file tree
Hide file tree
Showing 15 changed files with 550 additions and 467 deletions.
180 changes: 1 addition & 179 deletions include/BLE_Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,182 +200,4 @@ class SpinBLEClient
};
};

extern SpinBLEClient spinBLEClient;

class SensorData
{
public:
SensorData(String id) : id(id){};

String getId();
virtual bool hasHeartRate() = 0;
virtual bool hasCadence() = 0;
virtual bool hasPower() = 0;
virtual bool hasSpeed() = 0;
virtual int getHeartRate() = 0;
virtual float getCadence() = 0;
virtual int getPower() = 0;
virtual float getSpeed() = 0;
virtual void decode(uint8_t *data, size_t length) = 0;

protected:
String id;
};

class KnownDevice
{
public:
KnownDevice(NimBLEUUID uuid, std::shared_ptr<SensorData> sensorData): uuid(uuid), sensorData(sensorData){};
NimBLEUUID getUUID();
std::shared_ptr<SensorData> decode(uint8_t *data, size_t length);

private:
NimBLEUUID uuid;
std::shared_ptr<SensorData> sensorData;
};

class SensorDataFactory
{
public:
SensorDataFactory(){};

std::shared_ptr<SensorData> getSensorData(BLERemoteCharacteristic *characteristic, uint8_t *data, size_t length);

private:
std::vector<KnownDevice*> knownDevices;
static std::shared_ptr<SensorData> NULL_SENSOR_DATA;
};

class NullData : public SensorData
{
public:
NullData() : SensorData("Null"){};

virtual bool hasHeartRate();
virtual bool hasCadence();
virtual bool hasPower();
virtual bool hasSpeed();
virtual int getHeartRate();
virtual float getCadence();
virtual int getPower();
virtual float getSpeed();
virtual void decode(uint8_t *data, size_t length);
};

class HeartRateData : public SensorData
{
public:
HeartRateData() : SensorData("HRM"){};

virtual bool hasHeartRate();
virtual bool hasCadence();
virtual bool hasPower();
virtual bool hasSpeed();
virtual int getHeartRate();
virtual float getCadence();
virtual int getPower();
virtual float getSpeed();
virtual void decode(uint8_t *data, size_t length);

private:
int heartrate;
};

class CyclePowerData : public SensorData
{
public:
CyclePowerData() : SensorData("CPS"){};

virtual bool hasHeartRate();
virtual bool hasCadence();
virtual bool hasPower();
virtual bool hasSpeed();
virtual int getHeartRate();
virtual float getCadence();
virtual int getPower();
virtual float getSpeed();
virtual void decode(uint8_t *data, size_t length);

private:
int power;
float cadence;
float crankRev = 0;
float lastCrankRev = 0;
float lastCrankEventTime = 0;
float crankEventTime = 0;
uint8_t missedReadingCount = 0;
};

class FlywheelData : public SensorData
{
public:
FlywheelData() : SensorData("FLYW"){};

virtual bool hasHeartRate();
virtual bool hasCadence();
virtual bool hasPower();
virtual bool hasSpeed();
virtual int getHeartRate();
virtual float getCadence();
virtual int getPower();
virtual float getSpeed();
virtual void decode(uint8_t *data, size_t length);

private:
bool hasData = false;
float cadence;
int power;
};

class FitnessMachineIndoorBikeData : public SensorData
{
public:
FitnessMachineIndoorBikeData() : SensorData("FTMS"){};

enum Types : uint8_t
{
InstantaneousSpeed = 0,
AverageSpeed = 1,
InstantaneousCadence = 2,
AverageCadence = 3,
TotalDistance = 4,
ResistanceLevel = 5,
InstantaneousPower = 6,
AveragePower = 7,
TotalEnergy = 8,
EnergyPerHour = 9,
EnergyPerMinute = 10,
HeartRate = 11,
MetabolicEquivalent = 12,
ElapsedTime = 13,
RemainingTime = 14
};

static constexpr uint8_t FieldCount = Types::RemainingTime + 1;

virtual bool hasHeartRate();
virtual bool hasCadence();
virtual bool hasPower();
virtual bool hasSpeed();
virtual int getHeartRate();
virtual float getCadence();
virtual int getPower();
virtual float getSpeed();
virtual void decode(uint8_t *data, size_t length);

private:
double_t values[FieldCount];

static uint8_t const flagBitIndices[];
static uint8_t const flagEnabledValues[];
static size_t const byteSizes[];
static uint8_t const signedFlags[];
static double_t const resolutions[];
static int convert(int value, size_t length, uint8_t isSigned);
};

/******************CPS***************************/
void BLE_CPSDecode(BLERemoteCharacteristic *pBLERemoteCharacteristic);

/******************FTMS**************************/
void BLE_FTMSDecode(BLERemoteCharacteristic *pBLERemoteCharacteristic);
extern SpinBLEClient spinBLEClient;
29 changes: 29 additions & 0 deletions include/sensors/CyclePowerData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <Arduino.h>
#include "SensorData.h"

class CyclePowerData : public SensorData
{
public:
CyclePowerData() : SensorData("CPS"){};

bool hasHeartRate();
bool hasCadence();
bool hasPower();
bool hasSpeed();
int getHeartRate();
float getCadence();
int getPower();
float getSpeed();
void decode(uint8_t *data, size_t length);

private:
int power;
float cadence;
float crankRev = 0;
float lastCrankRev = 0;
float lastCrankEventTime = 0;
float crankEventTime = 0;
uint8_t missedReadingCount = 0;
};
53 changes: 53 additions & 0 deletions include/sensors/FitnessMachineIndoorBikeData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <Arduino.h>
#include "SensorData.h"

class FitnessMachineIndoorBikeData : public SensorData
{
public:
FitnessMachineIndoorBikeData() : SensorData("FTMS"){};

bool hasHeartRate();
bool hasCadence();
bool hasPower();
bool hasSpeed();
int getHeartRate();
float getCadence();
int getPower();
float getSpeed();
void decode(uint8_t *data, size_t length);

enum Types : uint8_t
{
InstantaneousSpeed = 0,
AverageSpeed = 1,
InstantaneousCadence = 2,
AverageCadence = 3,
TotalDistance = 4,
ResistanceLevel = 5,
InstantaneousPower = 6,
AveragePower = 7,
TotalEnergy = 8,
EnergyPerHour = 9,
EnergyPerMinute = 10,
HeartRate = 11,
MetabolicEquivalent = 12,
ElapsedTime = 13,
RemainingTime = 14
};

static constexpr uint8_t FieldCount = Types::RemainingTime + 1;

private:
double_t values[FieldCount];

// See: https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.characteristic.indoor_bike_data.xml
static uint8_t const flagBitIndices[];
static uint8_t const flagEnabledValues[];
static size_t const byteSizes[];
static uint8_t const signedFlags[];
static double_t const resolutions[];

static int convert(int value, size_t length, uint8_t isSigned);
};
24 changes: 24 additions & 0 deletions include/sensors/FlywheelData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "SensorData.h"

class FlywheelData : public SensorData
{
public:
FlywheelData() : SensorData("FLYW"){};

bool hasHeartRate();
bool hasCadence();
bool hasPower();
bool hasSpeed();
int getHeartRate();
float getCadence();
int getPower();
float getSpeed();
void decode(uint8_t *data, size_t length);

private:
bool hasData = false;
float cadence;
int power;
};
22 changes: 22 additions & 0 deletions include/sensors/HeartRateData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "SensorData.h"

class HeartRateData : public SensorData
{
public:
HeartRateData() : SensorData("HRM"){};

bool hasHeartRate();
bool hasCadence();
bool hasPower();
bool hasSpeed();
int getHeartRate();
float getCadence();
int getPower();
float getSpeed();
void decode(uint8_t *data, size_t length);

private:
int heartrate;
};
23 changes: 23 additions & 0 deletions include/sensors/SensorData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <Arduino.h>

class SensorData
{
public:
SensorData(String id) : id(id){};

String getId();
virtual bool hasHeartRate() = 0;
virtual bool hasCadence() = 0;
virtual bool hasPower() = 0;
virtual bool hasSpeed() = 0;
virtual int getHeartRate() = 0;
virtual float getCadence() = 0;
virtual int getPower() = 0;
virtual float getSpeed() = 0;
virtual void decode(uint8_t *data, size_t length) = 0;

private:
String id;
};
50 changes: 50 additions & 0 deletions include/sensors/SensorDataFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <memory>
#include <Arduino.h>
#include <NimBLEDevice.h>
#include "SensorData.h"
#include "CyclePowerData.h"
#include "FlywheelData.h"
#include "FitnessMachineIndoorBikeData.h"
#include "HeartRateData.h"

class SensorDataFactory
{
public:
SensorDataFactory(){};

std::shared_ptr<SensorData> getSensorData(BLERemoteCharacteristic *characteristic, uint8_t *data, size_t length);

private:
class KnownDevice
{
public:
KnownDevice(NimBLEUUID uuid, std::shared_ptr<SensorData> sensorData) : uuid(uuid), sensorData(sensorData){};
NimBLEUUID getUUID();
std::shared_ptr<SensorData> decode(uint8_t *data, size_t length);

private:
NimBLEUUID uuid;
std::shared_ptr<SensorData> sensorData;
};

class NullData : public SensorData
{
public:
NullData() : SensorData("Null"){};

virtual bool hasHeartRate();
virtual bool hasCadence();
virtual bool hasPower();
virtual bool hasSpeed();
virtual int getHeartRate();
virtual float getCadence();
virtual int getPower();
virtual float getSpeed();
virtual void decode(uint8_t *data, size_t length);
};

std::vector<KnownDevice *> knownDevices;
static std::shared_ptr<SensorData> NULL_SENSOR_DATA;
};
Loading

0 comments on commit f19752a

Please sign in to comment.