Skip to content

Commit

Permalink
Merge pull request #127 from kadaan/Reduce_instantiation
Browse files Browse the repository at this point in the history
Reduce instantiation
  • Loading branch information
doudar authored Mar 9, 2021
2 parents 8ec0e75 + f19752a commit fdf34f9
Show file tree
Hide file tree
Showing 20 changed files with 728 additions and 529 deletions.
282 changes: 81 additions & 201 deletions include/BLE_Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void updateHeartRateMeasurementChar();

class MyServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer *, ble_gap_conn_desc* desc);
void onConnect(BLEServer *, ble_gap_conn_desc *desc);
void onDisconnect(BLEServer *);
};

Expand All @@ -80,244 +80,124 @@ class MyCallbacks : public BLECharacteristicCallbacks

//*****************************Client*****************************

//Keeping the task outside the class so we don't need a mask.
//Keeping the task outside the class so we don't need a mask.
//We're only going to run one anyway.
void bleClientTask(void *pvParameters);
void bleClientTask(void *pvParameters);

//UUID's the client has methods for
//BLEUUID serviceUUIDs[4] = {FITNESSMACHINESERVICE_UUID, CYCLINGPOWERSERVICE_UUID, HEARTSERVICE_UUID, FLYWHEEL_UART_SERVICE_UUID};
//BLEUUID charUUIDs[4] = {FITNESSMACHINEINDOORBIKEDATA_UUID, CYCLINGPOWERMEASUREMENT_UUID, HEARTCHARACTERISTIC_UUID, FLYWHEEL_UART_TX_UUID};

enum userSelect : uint8_t {
HR = 1,
PM = 2,
CSC = 3,
CT = 4
};

class myAdvertisedBLEDevice{
public: //eventually these shoul be made private
BLEAdvertisedDevice *advertisedDevice = nullptr;
NimBLEAddress peerAddress;
int connectedClientID = BLE_HS_CONN_HANDLE_NONE;
BLEUUID serviceUUID = (uint16_t)0x0000;
BLEUUID charUUID = (uint16_t)0x0000;
bool userSelectedHR = false;
bool userSelectedPM = false;
bool userSelectedCSC = false;
bool userSelectedCT = false;
bool doConnect = false;
enum userSelect : uint8_t
{
HR = 1,
PM = 2,
CSC = 3,
CT = 4
};


void set(BLEAdvertisedDevice *device, int id = BLE_HS_CONN_HANDLE_NONE, BLEUUID inserviceUUID = (uint16_t)0x0000, BLEUUID incharUUID = (uint16_t)0x0000){
advertisedDevice = device;
class myAdvertisedBLEDevice
{
public: //eventually these shoul be made private
BLEAdvertisedDevice *advertisedDevice = nullptr;
NimBLEAddress peerAddress;
int connectedClientID = BLE_HS_CONN_HANDLE_NONE;
BLEUUID serviceUUID = (uint16_t)0x0000;
BLEUUID charUUID = (uint16_t)0x0000;
bool userSelectedHR = false;
bool userSelectedPM = false;
bool userSelectedCSC = false;
bool userSelectedCT = false;
bool doConnect = false;

void set(BLEAdvertisedDevice *device, int id = BLE_HS_CONN_HANDLE_NONE, BLEUUID inserviceUUID = (uint16_t)0x0000, BLEUUID incharUUID = (uint16_t)0x0000)
{
advertisedDevice = device;
peerAddress = device->getAddress();
connectedClientID = id;
connectedClientID = id;
serviceUUID = BLEUUID(inserviceUUID);
charUUID = BLEUUID(incharUUID);
charUUID = BLEUUID(incharUUID);
}

void reset() {
advertisedDevice = nullptr;
//NimBLEAddress peerAddress;
connectedClientID = BLE_HS_CONN_HANDLE_NONE;
serviceUUID = (uint16_t)0x0000;
charUUID = (uint16_t)0x0000;
userSelectedHR = false;
userSelectedPM = false;
userSelectedCSC = false;
userSelectedCT = false;
doConnect = false;
void reset()
{
advertisedDevice = nullptr;
//NimBLEAddress peerAddress;
connectedClientID = BLE_HS_CONN_HANDLE_NONE;
serviceUUID = (uint16_t)0x0000;
charUUID = (uint16_t)0x0000;
userSelectedHR = false;
userSelectedPM = false;
userSelectedCSC = false;
userSelectedCT = false;
doConnect = false;
}

//userSelectHR = 1,userSelectPM = 2,userSelectCSC = 3,userSelectCT = 4
void setSelected (userSelect flags)
{
switch (flags)
//userSelectHR = 1,userSelectPM = 2,userSelectCSC = 3,userSelectCT = 4
void setSelected(userSelect flags)
{
case 1:
userSelectedHR = true;
break;
case 2:
userSelectedPM = true;
break;
case 3:
userSelectedCSC = true;
break;
case 4:
userSelectedCT = true;
break;
switch (flags)
{
case 1:
userSelectedHR = true;
break;
case 2:
userSelectedPM = true;
break;
case 3:
userSelectedCSC = true;
break;
case 4:
userSelectedCT = true;
break;
}
}

}



};

class SpinBLEClient
{

public: //Not all of these need to be public. This should be cleaned up later.
boolean connectedPM = false;
boolean connectedHR = false;
boolean connectedCD = false;
boolean doScan = false;
bool intentionalDisconnect = false;
int noReadingIn = 0;
int cscCumulativeCrankRev = 0;
int cscLastCrankEvtTime = 0;

class SpinBLEClient{

public: //Not all of these need to be public. This should be cleaned up later.
boolean doConnectPM = false;
boolean doConnectHR = false;
boolean connectedPM = false;
boolean connectedHR = false;
boolean doScan = false;
bool intentionalDisconnect = false;
float crankRev[2] = {0, 0};
float crankEventTime[2] = {0, 0};
int noReadingIn = 0;
int cscCumulativeCrankRev = 0;
int cscLastCrankEvtTime = 0;

BLERemoteCharacteristic *pRemoteCharacteristic = nullptr;
BLERemoteCharacteristic *pRemoteCharacteristic = nullptr;

//BLEDevices myBLEDevices;
myAdvertisedBLEDevice myBLEDevices[NUM_BLE_DEVICES];

void start();
void serverScan(bool connectRequest);
void serverScan(bool connectRequest);
bool connectToServer();
void scanProcess();
void disconnect();
//Check for duplicate services of BLEClient and remove the previoulsy connected one.
//Check for duplicate services of BLEClient and remove the previoulsy connected one.
void removeDuplicates(BLEClient *pClient);

private:

class MyAdvertisedDeviceCallback : public NimBLEAdvertisedDeviceCallbacks
{
public:
void onResult(NimBLEAdvertisedDevice *);
public:
void onResult(NimBLEAdvertisedDevice *);
};

class MyClientCallback : public NimBLEClientCallbacks
{
public:
void onConnect(BLEClient *);
void onDisconnect(BLEClient *);
uint32_t onPassKeyRequest();
bool onConfirmPIN(uint32_t);
void onAuthenticationComplete(ble_gap_conn_desc);
};
};

extern SpinBLEClient spinBLEClient;

class SensorData {
public:
SensorData(String id, uint8_t *data, size_t length) : id(id), data(data), length(length) {};

String getId();
virtual bool hasHeartRate() = 0;
virtual bool hasCadence() = 0;
virtual bool hasPower() = 0;
virtual int getHeartRate() = 0;
virtual float getCadence() = 0;
virtual int getPower() = 0;

protected:
String id;
uint8_t *data;
size_t length;
};

class SensorDataFactory {
public:
static std::unique_ptr<SensorData> getSensorData(BLERemoteCharacteristic *characteristic, uint8_t *data, size_t length);

private:
SensorDataFactory() {};
};

class NullData : public SensorData {
public:
NullData(uint8_t *data, size_t length) : SensorData("Null", data, length) {};


virtual bool hasHeartRate();
virtual bool hasCadence();
virtual bool hasPower();
virtual int getHeartRate();
virtual float getCadence();
virtual int getPower();
};

class HeartRateData : public SensorData {
public:
HeartRateData(uint8_t *data, size_t length) : SensorData("HRM", data, length) {};


virtual bool hasHeartRate();
virtual bool hasCadence();
virtual bool hasPower();
virtual int getHeartRate();
virtual float getCadence();
virtual int getPower();
};

class FlywheelData : public SensorData {
public:
FlywheelData(uint8_t *data, size_t length) : SensorData("FLYW", data, length) {};


virtual bool hasHeartRate();
virtual bool hasCadence();
virtual bool hasPower();
virtual int getHeartRate();
virtual float getCadence();
virtual int getPower();
};

class FitnessMachineIndoorBikeData : public SensorData {
public:
FitnessMachineIndoorBikeData(uint8_t *data, size_t length);

~FitnessMachineIndoorBikeData();

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
public:
void onConnect(BLEClient *);
void onDisconnect(BLEClient *);
uint32_t onPassKeyRequest();
bool onConfirmPIN(uint32_t);
void onAuthenticationComplete(ble_gap_conn_desc);
};

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

virtual bool hasHeartRate();
virtual bool hasCadence();
virtual bool hasPower();
virtual int getHeartRate();
virtual float getCadence();
virtual int getPower();

private:
int flags;
double_t *values;

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;
3 changes: 3 additions & 0 deletions include/SmartSpin_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class userParameters
int simulatedWatts;
int simulatedHr;
int simulatedCad;
float simulatedSpeed;
String deviceName;
int shiftStep;
int stepperPower;
Expand All @@ -38,6 +39,7 @@ class userParameters
int getSimulatedWatts() {return simulatedWatts;}
int getSimulatedHr() {return simulatedHr;}
float getSimulatedCad() {return simulatedCad;}
float getSimulatedSpeed() {return simulatedSpeed;}
const char* getDeviceName() {return deviceName.c_str();}
int getShiftStep() {return shiftStep;}
int getStepperPower() {return stepperPower;}
Expand All @@ -59,6 +61,7 @@ class userParameters
void setSimulatedWatts(int w) {simulatedWatts = w;}
void setSimulatedHr(int hr) {simulatedHr = hr;}
void setSimulatedCad(float cad) {simulatedCad = cad;}
void setSimulatedSpeed(float spd) {simulatedSpeed = spd;}
void setDeviceName(String dvcn) {deviceName = dvcn;}
void setShiftStep(int ss) {shiftStep = ss;}
void setStepperPower(int sp) {stepperPower = sp;}
Expand Down
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;
};
Loading

0 comments on commit fdf34f9

Please sign in to comment.