Skip to content

Commit

Permalink
Make disconnected sensors work properly #23
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Dec 6, 2020
1 parent fd832c6 commit 7eaac55
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
65 changes: 55 additions & 10 deletions src/bubbles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ SOFTWARE. */

Bubbles bubbles;
volatile bool doBubble;
volatile bool __attribute__((unused)) vesselDisconnected = false; // Semaphore for vessel disconnected
volatile bool __attribute__((unused)) ambientDisconnected = false; // Semaphore for ambient disconnected

bool Bubbles::start()
{
Expand All @@ -40,25 +42,69 @@ bool Bubbles::start()
return true;
}

bool Bubbles::update()
void Bubbles::update()
{ // Regular update loop, once per minute
// Get NTP Time
lastTime = getDTS();

// Store last values
lastBpm = getRawBpm();
lastAmb = getTemp(AMBSENSOR);
lastVes = getTemp(VESSENSOR);
bubAvg.push(lastBpm);

// Push values to circular buffers
// TODO: Handle aging out a sensor if it becomes disconnected
// Check Ambient device
lastAmb = getTemp(AMBSENSOR);
if ((config.bubble.tempinf && lastAmb == (float)DEVICE_DISCONNECTED_F) || (!config.bubble.tempinf && lastAmb == (float)DEVICE_DISCONNECTED_C))
{
// Ambient device disconnected
ambientDisconnected = true;
tempAmbAvg.clear();
}
else
{
// Ambient device connected
if (ambientDisconnected)
{
// Ambient newly connected
tempAmbAvg.clear();
}
ambientDisconnected = false;
}
// Push Ambient value to circular buffer
tempAmbAvg.push(lastAmb);

// Check Vessel device
lastVes = getTemp(VESSENSOR);
if ((config.bubble.tempinf && lastVes == (float)DEVICE_DISCONNECTED_F) || (!config.bubble.tempinf && lastVes == (float)DEVICE_DISCONNECTED_C))
{
// Vessel device disconnected
vesselDisconnected = true;
tempVesAvg.clear();
}
else
{
// Vessel device connected
if (vesselDisconnected)
{
// Vessel newly connected
tempVesAvg.clear();
}
vesselDisconnected = false;
}
// Push Vessel value to circular buffer
tempVesAvg.push(lastVes);
bubAvg.push(lastBpm);
sampleSize = tempVesAvg.size();

saveBpm();
return true;

Log.verbose(F("DEBUG: Current BPM is %D. Averages: BPM (%d) = %D, Ambient (%d) = %D, Vessel (%d) = %D." CR),
bubbles.lastBpm,
bubAvg.size(),
bubbles.getAvgBpm(),
tempAmbAvg.size(),
bubbles.getAvgAmbient(),
tempVesAvg.size(),
bubbles.getAvgVessel());

return;
}

float Bubbles::getRawBpm()
Expand All @@ -80,7 +126,6 @@ float Bubbles::getAvgAmbient()
uint8_t size = tempAmbAvg.size();
for (int i = 0; i < tempAmbAvg.size(); i++)
{
// float thisTemp = tempAmbAvg[i];
avg += tempAmbAvg[i] / size;
}
if (avg)
Expand All @@ -91,7 +136,7 @@ float Bubbles::getAvgAmbient()

float Bubbles::getAvgVessel()
{
// Return TEMPAVG readings to calculate average
// Retrieve TEMPAVG readings to calculate average
float avg = 0.0;
uint8_t size = tempVesAvg.size();
for (int i = 0; i < tempVesAvg.size(); i++)
Expand Down
3 changes: 1 addition & 2 deletions src/bubbles.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct Bubbles
public:
// Public Methods
bool start();
bool update(); // Call every 60 seconds
void update(); // Call every 60 seconds
float getAvgAmbient();
float getAvgVessel();
float getAvgBpm();
Expand All @@ -61,7 +61,6 @@ struct Bubbles
// Public Properties
float lastBpm; // Holds most recent count
String lastTime;
int sampleSize;
};

extern volatile int pulse;
Expand Down
8 changes: 1 addition & 7 deletions src/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,7 @@ void tickerLoop()
if (doBubble)
{
doBubble = false;
if (bubbles.update())
Log.verbose(F("Current BPM is %D. Averages (%l in sample): BPM = %D, Ambient = %D, Vessel = %D." CR),
bubbles.lastBpm,
bubbles.sampleSize,
bubbles.getAvgBpm(),
bubbles.getAvgAmbient(),
bubbles.getAvgVessel());
bubbles.update();
}

// Handle JSON posts
Expand Down

0 comments on commit 7eaac55

Please sign in to comment.