2018-11-26 11:58:15 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Ray Jones <ray@mrjones.id.au>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-11-06 09:43:54 +00:00
|
|
|
#ifndef __UTIL_CLASSES_H__
|
|
|
|
#define __UTIL_CLASSES_H__
|
|
|
|
|
2019-07-07 07:18:38 +00:00
|
|
|
//#include <string.h>
|
2018-12-16 07:34:39 +00:00
|
|
|
#include "DebugPort.h"
|
2018-12-21 21:48:39 +00:00
|
|
|
#include "../cfg/BTCConfig.h"
|
2018-11-06 09:43:54 +00:00
|
|
|
|
2018-12-20 04:19:59 +00:00
|
|
|
class CProtocol;
|
2018-11-06 09:43:54 +00:00
|
|
|
|
|
|
|
// a class to track the blue wire receive / transmit states
|
|
|
|
class CommStates {
|
|
|
|
public:
|
|
|
|
// comms states
|
|
|
|
enum eCS {
|
2018-12-21 21:48:39 +00:00
|
|
|
Idle, OEMCtrlRx, OEMCtrlValidate, HeaterRx1, HeaterValidate1, HeaterReport1, BTC_Tx, HeaterRx2, HeaterValidate2, HeaterReport2,TemperatureRead
|
2018-11-06 09:43:54 +00:00
|
|
|
};
|
|
|
|
|
2018-12-20 04:19:59 +00:00
|
|
|
private:
|
2018-12-21 21:48:39 +00:00
|
|
|
eCS _State;
|
|
|
|
int _Count;
|
|
|
|
unsigned long _delay;
|
|
|
|
bool _report;
|
2018-12-20 04:19:59 +00:00
|
|
|
public:
|
2018-11-06 09:43:54 +00:00
|
|
|
CommStates() {
|
2018-12-21 21:48:39 +00:00
|
|
|
_State = Idle;
|
|
|
|
_Count = 0;
|
|
|
|
_delay = millis();
|
|
|
|
_report = REPORT_STATE_MACHINE_TRANSITIONS;
|
2018-11-06 09:43:54 +00:00
|
|
|
}
|
2018-12-20 04:19:59 +00:00
|
|
|
void set(eCS eState);
|
2018-11-06 09:43:54 +00:00
|
|
|
eCS get() {
|
2018-12-21 21:48:39 +00:00
|
|
|
return _State;
|
2018-11-06 09:43:54 +00:00
|
|
|
}
|
|
|
|
bool is(eCS eState) {
|
2018-12-21 21:48:39 +00:00
|
|
|
return _State == eState;
|
2018-11-06 09:43:54 +00:00
|
|
|
}
|
2019-07-06 13:46:20 +00:00
|
|
|
bool collectData(CProtocol& Frame, uint8_t val, int limit = 24);
|
|
|
|
bool collectDataEx(CProtocol& Frame, uint8_t val, int limit = 24);
|
|
|
|
bool checkValidStart(uint8_t val);
|
2018-12-21 21:48:39 +00:00
|
|
|
void setDelay(int ms);
|
|
|
|
bool delayExpired();
|
2019-06-26 20:04:24 +00:00
|
|
|
bool toggleReporting() {
|
|
|
|
_report = !_report;
|
|
|
|
return isReporting();
|
|
|
|
};
|
|
|
|
bool isReporting() {
|
|
|
|
return _report != 0;
|
|
|
|
};
|
2018-11-06 09:43:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// a class to collect a new data byte from the blue wire
|
|
|
|
class sRxData {
|
|
|
|
bool newData;
|
|
|
|
int Value;
|
|
|
|
public:
|
|
|
|
sRxData() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
void reset() {
|
|
|
|
newData = false;
|
2019-06-26 20:04:24 +00:00
|
|
|
Value = 0;
|
2018-11-06 09:43:54 +00:00
|
|
|
}
|
|
|
|
void setValue(int value) {
|
|
|
|
newData = true;
|
|
|
|
Value = value;
|
|
|
|
}
|
|
|
|
bool available() {
|
|
|
|
return newData;
|
|
|
|
}
|
|
|
|
int getValue() {
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// a class to collect rx bytes into a string, typ. until a line terminator (handled elsewhere)
|
|
|
|
struct sRxLine {
|
2019-05-12 12:14:32 +00:00
|
|
|
char Line[1024];
|
2018-11-06 09:43:54 +00:00
|
|
|
int Len;
|
|
|
|
sRxLine() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
bool append(char val) {
|
|
|
|
if(Len < (sizeof(Line) - 1)) {
|
|
|
|
Line[Len++] = val;
|
|
|
|
Line[Len] = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void clear() {
|
|
|
|
Line[0] = 0;
|
|
|
|
Len = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// a class to generate time stamps depending if a heater or otherwise frame header is presented
|
|
|
|
class CContextTimeStamp {
|
|
|
|
unsigned long prevTime;
|
|
|
|
unsigned long refTime;
|
|
|
|
public:
|
|
|
|
CContextTimeStamp() {
|
|
|
|
refTime = 0;
|
|
|
|
prevTime = 0;
|
|
|
|
};
|
|
|
|
void setRefTime() {
|
|
|
|
refTime = millis();
|
|
|
|
};
|
2018-12-20 04:19:59 +00:00
|
|
|
void report(bool isDelta) {
|
|
|
|
if(isDelta) {
|
2018-11-24 11:03:47 +00:00
|
|
|
long delta = millis() - prevTime;
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf("%+8ldms ", delta);
|
2018-11-06 09:43:54 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
prevTime = millis();
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf("%8ldms ", prevTime - refTime);
|
2018-11-06 09:43:54 +00:00
|
|
|
}
|
|
|
|
};
|
2018-12-20 04:19:59 +00:00
|
|
|
void report() {
|
|
|
|
prevTime = millis();
|
2019-06-26 20:04:24 +00:00
|
|
|
DebugPort.printf("%8ldms ", prevTime - refTime);
|
2018-12-20 04:19:59 +00:00
|
|
|
};
|
2018-11-06 09:43:54 +00:00
|
|
|
};
|
|
|
|
|
2018-11-24 00:51:09 +00:00
|
|
|
|
|
|
|
struct CRect {
|
|
|
|
// types match with getTextBounds in Adafruit_GFX
|
|
|
|
int16_t xPos, yPos;
|
|
|
|
uint16_t width, height;
|
|
|
|
CRect() {
|
|
|
|
xPos = yPos = width = height = 0;
|
|
|
|
}
|
2019-07-20 06:08:43 +00:00
|
|
|
CRect(const CRect& a) {
|
|
|
|
xPos = a.xPos;
|
|
|
|
yPos = a.yPos;
|
|
|
|
width = a.width;
|
|
|
|
height = a.height;
|
|
|
|
}
|
2018-11-24 00:51:09 +00:00
|
|
|
void Expand(int val) {
|
|
|
|
xPos -= val;
|
|
|
|
yPos -= val;
|
|
|
|
width += 2 * val;
|
|
|
|
height += 2 * val;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-02 20:34:45 +00:00
|
|
|
class CProfile {
|
|
|
|
unsigned long tStart;
|
|
|
|
public:
|
|
|
|
CProfile();
|
|
|
|
unsigned long elapsed(bool reset = false);
|
|
|
|
};
|
|
|
|
|
|
|
|
enum eOTAmodes {
|
|
|
|
eOTAnormal, eOTAbrowser, eOTAWWW
|
|
|
|
};
|
|
|
|
|
2020-03-25 09:28:12 +00:00
|
|
|
void setHoldoff(unsigned long& holdoff, unsigned long period);
|
2019-06-02 20:34:45 +00:00
|
|
|
|
2018-11-06 09:43:54 +00:00
|
|
|
#endif // __UTIL_CLASSES_H__
|