pre-commit updates

This commit is contained in:
Magnus Persson 2022-04-04 23:16:24 +02:00
parent 2d67a44ad0
commit c779a45ea9
16 changed files with 236 additions and 208 deletions

View File

@ -24,6 +24,7 @@ SOFTWARE.
#if defined(ESP32)
#include <ble.hpp>
#include <string>
// Tilt UUID variants and data format, based on tilt-sim
//
@ -38,7 +39,8 @@ SOFTWARE.
BleSender::BleSender(const char* color) {
BLEDevice::init("");
// boost power to maximum, these might be changed once battery life using BLE has been tested.
// boost power to maximum, these might be changed once battery life using BLE
// has been tested.
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P9);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P9);
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_P9);
@ -72,13 +74,14 @@ void BleSender::sendData(float tempF, float gravSG) {
uint16_t temperature = tempF; // Deg F _or_ Deg F * 10 for Tilt Pro/HD
BLEBeacon oBeacon = BLEBeacon();
oBeacon.setManufacturerId(0x4C00); // fake Apple 0x004C LSB (ENDIAN_CHANGE_U16!)
oBeacon.setManufacturerId(
0x4C00); // fake Apple 0x004C LSB (ENDIAN_CHANGE_U16!)
oBeacon.setProximityUUID(_uuid);
oBeacon.setMajor(temperature);
oBeacon.setMinor(gravity);
std::string strServiceData = "";
strServiceData += (char)26; // Len
strServiceData += (char)0xFF; // Type
strServiceData += static_cast<char>(26); // Len
strServiceData += static_cast<char>(0xFF); // Type
strServiceData += oBeacon.getData();
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();

View File

@ -26,10 +26,11 @@ SOFTWARE.
#if defined(ESP32)
#include <NimBLEDevice.h>
#include <NimBLEBeacon.h>
#include <main.hpp>
#include <NimBLEDevice.h>
#include <config.hpp>
#include <main.hpp>
class BleSender {
private:
@ -38,11 +39,9 @@ class BleSender {
BLEUUID _uuid;
public:
BleSender(const char* color);
explicit BleSender(const char* color);
void sendData(float tempF, float gravSG);
};
#endif // ESP32
#endif // SRC_BLE_HPP_
// EOF

View File

@ -300,10 +300,11 @@ bool Config::loadFile() {
_formulaData.g[4] = doc[PARAM_FORMULA_DATA]["g5"].as<double>();
/*if( doc[PARAM_CONFIG_VER].isNull() ) {
// If this parameter is missing we need to reset the gyrocalibaration due to bug #29
_gyroCalibration.ax = _gyroCalibration.ay = _gyroCalibration.az = 0;
// If this parameter is missing we need to reset the gyrocalibaration due to
bug #29 _gyroCalibration.ax = _gyroCalibration.ay = _gyroCalibration.az = 0;
_gyroCalibration.gx = _gyroCalibration.gy = _gyroCalibration.gz = 0;
Log.warning(F("CFG : Old configuration format, clearing gyro calibration." CR));
Log.warning(F("CFG : Old configuration format, clearing gyro calibration."
CR));
}*/
_saveNeeded = false; // Reset save flag

View File

@ -360,7 +360,10 @@ class Config {
}
bool isBLEActive() { return _colorBLE.length() ? true : false; }
bool isWifiPushActive() {
return (isHttpActive() || isHttp2Active() || isHttp3Active() || isBrewfatherActive() || isInfluxDb2Active() || isMqttActive()) ? true : false;
return (isHttpActive() || isHttp2Active() || isHttp3Active() ||
isBrewfatherActive() || isInfluxDb2Active() || isMqttActive())
? true
: false;
}
const RawGyroData& getGyroCalibration() { return _gyroCalibration; }

View File

@ -197,8 +197,9 @@ float GyroSensor::calculateAngle(RawGyroData &raw) {
// Source: https://www.nxp.com/docs/en/application-note/AN3461.pdf
float vY = (acos(abs(ay) / sqrt(ax * ax + ay * ay + az * az)) * 180.0 / PI);
//float vZ = (acos(abs(az) / sqrt(ax * ax + ay * ay + az * az)) * 180.0 / PI);
//float vX = (acos(abs(ax) / sqrt(ax * ax + ay * ay + az * az)) * 180.0 / PI);
// float vZ = (acos(abs(az) / sqrt(ax * ax + ay * ay + az * az)) * 180.0 /
// PI); float vX = (acos(abs(ax) / sqrt(ax * ax + ay * ay + az * az)) * 180.0
// / PI);
#if LOG_LEVEL == 6 && !defined(GYRO_DISABLE_LOGGING)
// Log.notice(F("GYRO: angleX= %F." CR), vX);
Log.notice(F("GYRO: angleY= %F." CR), vY);

View File

@ -51,8 +51,7 @@ void tcp_cleanup() {
// Convert sg to plato
//
double convertToPlato(double sg) {
if (sg)
return 259 - (259 / sg);
if (sg) return 259 - (259 / sg);
return 0;
}
@ -358,19 +357,24 @@ void PerfLogging::pushInflux() {
myConfig.getMDNS(), myConfig.getID());
body += &buf[0];
#if defined(ESP8266)
snprintf(
&buf[0], sizeof(buf),
"angle=%.4f,gyro-ax=%d,gyro-ay=%d,gyro-az=%d,gyro-temp=%.2f,ds-temp=%.2f,heap=%d,heap-frag=%d,heap-max=%d,stack=%d",
snprintf(&buf[0], sizeof(buf),
"angle=%.4f,gyro-ax=%d,gyro-ay=%d,gyro-az=%d,gyro-temp=%.2f,ds-temp="
"%.2f,heap=%d,heap-frag=%d,heap-max=%d,stack=%d",
myGyro.getAngle(), myGyro.getLastGyroData().ax,
myGyro.getLastGyroData().ay, myGyro.getLastGyroData().az,
myGyro.getSensorTempC(), myTempSensor.getTempC(myConfig.isGyroTemp()), ESP.getFreeHeap(), ESP.getHeapFragmentation(), ESP.getMaxFreeBlockSize(), ESP.getFreeContStack());
myGyro.getSensorTempC(),
myTempSensor.getTempC(myConfig.isGyroTemp()), ESP.getFreeHeap(),
ESP.getHeapFragmentation(), ESP.getMaxFreeBlockSize(),
ESP.getFreeContStack());
#else // defined (ESP32)
snprintf(
&buf[0], sizeof(buf),
"angle=%.4f,gyro-ax=%d,gyro-ay=%d,gyro-az=%d,gyro-temp=%.2f,ds-temp=%.2f,heap=%d,heap-frag=%d,heap-max=%d",
snprintf(&buf[0], sizeof(buf),
"angle=%.4f,gyro-ax=%d,gyro-ay=%d,gyro-az=%d,gyro-temp=%.2f,ds-temp="
"%.2f,heap=%d,heap-frag=%d,heap-max=%d",
myGyro.getAngle(), myGyro.getLastGyroData().ax,
myGyro.getLastGyroData().ay, myGyro.getLastGyroData().az,
myGyro.getSensorTempC(), myTempSensor.getTempC(myConfig.isGyroTemp()), ESP.getFreeHeap(), 0, ESP.getMaxAllocHeap());
myGyro.getSensorTempC(),
myTempSensor.getTempC(myConfig.isGyroTemp()), ESP.getFreeHeap(), 0,
ESP.getMaxAllocHeap());
#endif
body += &buf[0];

View File

@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <ble.hpp>
#include <calc.hpp>
#include <config.hpp>
#include <gyro.hpp>
@ -30,7 +31,6 @@ SOFTWARE.
#include <tempsensor.hpp>
#include <webserver.hpp>
#include <wifi.hpp>
#include <ble.hpp>
// Define constats for this program
#ifdef DEACTIVATE_SLEEPMODE
@ -161,7 +161,8 @@ void setup() {
runMode = RunMode::wifiSetupMode;
}
bool needWifi = true; // Under ESP32 we dont need wifi if only BLE is active in gravityMode
bool needWifi = true; // Under ESP32 we dont need wifi if only BLE is active
// in gravityMode
// Do this setup for all modes exect wifi setup
switch (runMode) {
@ -185,7 +186,9 @@ void setup() {
#if defined(ESP32)
if (!myConfig.isWifiPushActive() && runMode == RunMode::gravityMode) {
Log.notice(F("Main: Wifi is not needed in gravity mode, skipping connection." CR));
Log.notice(
F("Main: Wifi is not needed in gravity mode, skipping "
"connection." CR));
needWifi = false;
}
#endif
@ -277,7 +280,8 @@ bool loopReadGravity() {
}
#endif
if (myWifi.isConnected()) { // no need to try if there is no wifi connection.
if (myWifi.isConnected()) { // no need to try if there is no wifi
// connection.
PushTarget push;
push.sendAll(angle, gravitySG, corrGravitySG, tempC,
(millis() - runtimeMillis) / 1000);
@ -356,7 +360,9 @@ void loop() {
case RunMode::gravityMode:
// If we didnt get a wifi connection, we enter sleep for a short time to
// conserve battery.
if (!myWifi.isConnected() && myConfig.isWifiPushActive()) { // no connection to wifi and we have defined push targets.
if (!myWifi.isConnected() &&
myConfig.isWifiPushActive()) { // no connection to wifi and we have
// defined push targets.
Log.notice(
F("MAIN: No connection to wifi established, sleeping for 60s." CR));
myWifi.stopDoubleReset();

View File

@ -111,12 +111,10 @@ void PushTarget::sendInfluxDb2(TemplatingEngine& engine) {
if (_lastCode == 204) {
_lastSuccess = true;
Log.notice(F("PUSH: InfluxDB2 push successful, response=%d" CR),
_lastCode);
Log.notice(F("PUSH: InfluxDB2 push successful, response=%d" CR), _lastCode);
} else {
ErrorFileLog errLog;
errLog.addEntry("PUSH: Influxdb push failed response=" +
String(_lastCode));
errLog.addEntry("PUSH: Influxdb push failed response=" + String(_lastCode));
}
_http.end();
@ -186,7 +184,8 @@ void PushTarget::addHttpHeader(HTTPClient& http, String header) {
//
// Send data to http target using POST
//
void PushTarget::sendHttpPost(TemplatingEngine& engine, bool isSecure, int index) {
void PushTarget::sendHttpPost(TemplatingEngine& engine, bool isSecure,
int index) {
#if !defined(PUSH_DISABLE_LOGGING)
Log.notice(F("PUSH: Sending values to http (%s)" CR),
index ? "http2" : "http");
@ -214,10 +213,11 @@ void PushTarget::sendHttpPost(TemplatingEngine& engine, bool isSecure, int index
_wifiSecure.setInsecure();
#if defined(ESP8266)
String host = serverPath.substring(8); // remove the prefix or the probe will fail, it needs a pure host name.
String host =
serverPath.substring(8); // remove the prefix or the probe will fail,
// it needs a pure host name.
int idx = host.indexOf("/");
if (idx!=-1)
host = host.substring(0, idx);
if (idx != -1) host = host.substring(0, idx);
if (_wifiSecure.probeMaxFragmentLength(host, 443, 512)) {
Log.notice(F("PUSH: HTTP server supports smaller SSL buffer." CR));
@ -254,12 +254,10 @@ void PushTarget::sendHttpPost(TemplatingEngine& engine, bool isSecure, int index
if (_lastCode == 200) {
_lastSuccess = true;
Log.notice(F("PUSH: HTTP post successful, response=%d" CR),
_lastCode);
Log.notice(F("PUSH: HTTP post successful, response=%d" CR), _lastCode);
} else {
ErrorFileLog errLog;
errLog.addEntry(
"PUSH: HTTP post failed response=" + String(_lastCode) +
errLog.addEntry("PUSH: HTTP post failed response=" + String(_lastCode) +
String(index == 0 ? " (http)" : " (http2)"));
}
@ -297,10 +295,11 @@ void PushTarget::sendHttpGet(TemplatingEngine& engine, bool isSecure) {
_wifiSecure.setInsecure();
#if defined(ESP8266)
String host = serverPath.substring(8); // remove the prefix or the probe will fail, it needs a pure host name.
String host =
serverPath.substring(8); // remove the prefix or the probe will fail,
// it needs a pure host name.
int idx = host.indexOf("/");
if (idx!=-1)
host = host.substring(0, idx);
if (idx != -1) host = host.substring(0, idx);
if (_wifiSecure.probeMaxFragmentLength(host, 443, 512)) {
Log.notice(F("PUSH: HTTP server supports smaller SSL buffer." CR));
@ -319,12 +318,10 @@ void PushTarget::sendHttpGet(TemplatingEngine& engine, bool isSecure) {
if (_lastCode == 200) {
_lastSuccess = true;
Log.notice(F("PUSH: HTTP get successful, response=%d" CR),
_lastCode);
Log.notice(F("PUSH: HTTP get successful, response=%d" CR), _lastCode);
} else {
ErrorFileLog errLog;
errLog.addEntry(
"PUSH: HTTP get failed response=" + String(_lastCode));
errLog.addEntry("PUSH: HTTP get failed response=" + String(_lastCode));
}
if (isSecure) {

View File

@ -51,9 +51,15 @@ class PushTarget {
float runTime);
void sendBrewfather(TemplatingEngine& engine);
void sendHttp1(TemplatingEngine& engine, bool isSecure) { sendHttpPost(engine, isSecure, 0); }
void sendHttp2(TemplatingEngine& engine, bool isSecure) { sendHttpPost(engine, isSecure, 1); }
void sendHttp3(TemplatingEngine& engine, bool isSecure) { sendHttpGet(engine, isSecure); }
void sendHttp1(TemplatingEngine& engine, bool isSecure) {
sendHttpPost(engine, isSecure, 0);
}
void sendHttp2(TemplatingEngine& engine, bool isSecure) {
sendHttpPost(engine, isSecure, 1);
}
void sendHttp3(TemplatingEngine& engine, bool isSecure) {
sendHttpGet(engine, isSecure);
}
void sendInfluxDb2(TemplatingEngine& engine);
void sendMqtt(TemplatingEngine& engine, bool isSecure);
int getLastCode() { return _lastCode; }

View File

@ -42,5 +42,4 @@ INCBIN(UploadHtm, "data/upload.min.htm");
#endif
INCBIN(FirmwareHtm, "data/firmware.min.htm");
// EOF

View File

@ -21,8 +21,8 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <templating.hpp>
#include <config.hpp>
#include <templating.hpp>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
@ -83,8 +83,10 @@ const char brewfatherFormat[] PROGMEM =
"}";
const char influxDbFormat[] PROGMEM =
"measurement,host=${mdns},device=${id},temp-format=${temp-unit},gravity-format=${gravity-unit} "
"gravity=${gravity},corr-gravity=${corr-gravity},angle=${angle},temp=${temp},battery=${battery},"
"measurement,host=${mdns},device=${id},temp-format=${temp-unit},gravity-"
"format=${gravity-unit} "
"gravity=${gravity},corr-gravity=${corr-gravity},angle=${angle},temp=${"
"temp},battery=${battery},"
"rssi=${rssi}\n";
const char mqttFormat[] PROGMEM =
@ -99,8 +101,9 @@ const char mqttFormat[] PROGMEM =
//
// Initialize the variables
//
void TemplatingEngine::initialize(float angle, float gravitySG, float corrGravitySG, float tempC, float runTime) {
void TemplatingEngine::initialize(float angle, float gravitySG,
float corrGravitySG, float tempC,
float runTime) {
// Names
setVal(TPL_MDNS, myConfig.getMDNS());
setVal(TPL_ID, myConfig.getID());
@ -134,8 +137,7 @@ void TemplatingEngine::initialize(float angle, float gravitySG, float corrGravit
if (myConfig.isGravitySG()) {
setVal(TPL_GRAVITY, gravitySG, 4);
setVal(TPL_GRAVITY_CORR, corrGravitySG, 4);
}
else {
} else {
setVal(TPL_GRAVITY, convertToPlato(gravitySG), 1);
setVal(TPL_GRAVITY_CORR, convertToPlato(corrGravitySG), 1);
}

View File

@ -26,10 +26,10 @@ SOFTWARE.
#include <gyro.hpp>
#include <helper.hpp>
#include <main.hpp>
#include <pushtarget.hpp>
#include <resources.hpp>
#include <templating.hpp>
#include <tempsensor.hpp>
#include <pushtarget.hpp>
#include <webserver.hpp>
#include <wifi.hpp>
@ -51,8 +51,7 @@ void WebServerHandler::webHandleConfig() {
double angle = 0;
if (myGyro.hasValue())
angle = myGyro.getAngle();
if (myGyro.hasValue()) angle = myGyro.getAngle();
double tempC = myTempSensor.getTempC(myConfig.isGyroTemp());
double gravity = calculateGravity(angle, tempC);
@ -62,7 +61,8 @@ void WebServerHandler::webHandleConfig() {
// Format the adjustment so we get rid of rounding errors
if (myConfig.isTempF())
doc[PARAM_TEMP_ADJ] = reduceFloatPrecision(convertCtoF(myConfig.getTempSensorAdjC()), 1);
doc[PARAM_TEMP_ADJ] =
reduceFloatPrecision(convertCtoF(myConfig.getTempSensorAdjC()), 1);
else
doc[PARAM_TEMP_ADJ] = reduceFloatPrecision(myConfig.getTempSensorAdjC(), 1);
@ -188,31 +188,38 @@ void WebServerHandler::webHandleUploadFile() {
}
#if LOG_LEVEL == 6 && !defined(WEB_DISABLE_LOGGING)
Log.verbose(F("WEB : webServer callback for /api/upload, receiving file %s, %d(%d) "
Log.verbose(
F("WEB : webServer callback for /api/upload, receiving file %s, %d(%d) "
"valid=%s, firmware=%s." CR),
f.c_str(), upload.currentSize, upload.totalSize, validFilename ? "yes" : "no", firmware ? "yes" : "no");
f.c_str(), upload.currentSize, upload.totalSize,
validFilename ? "yes" : "no", firmware ? "yes" : "no");
#endif
if (firmware) {
// Handle firmware update
uint32_t maxSketchSpace = 1044464; //(ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
// Handle firmware update, hardcode since function return wrong value.
uint32_t maxSketchSpace =
1044464; // (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (upload.status == UPLOAD_FILE_START) {
_uploadReturn = 200;
Log.notice(F("WEB : Start firmware upload, max sketch size %d kb." CR), maxSketchSpace/1024);
Log.notice(F("WEB : Start firmware upload, max sketch size %d kb." CR),
maxSketchSpace / 1024);
if (!Update.begin(maxSketchSpace, U_FLASH, PIN_LED)) {
ErrorFileLog errLog;
errLog.addEntry(F("WEB : Not enough space to store for this firmware."));
errLog.addEntry(
F("WEB : Not enough space to store for this firmware."));
_uploadReturn = 500;
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
Log.notice(F("WEB : Writing firmware upload %d (%d)." CR), upload.totalSize, maxSketchSpace);
Log.notice(F("WEB : Writing firmware upload %d (%d)." CR),
upload.totalSize, maxSketchSpace);
if (upload.totalSize > maxSketchSpace) {
Log.error(F("WEB : Firmware file is to large." CR));
_uploadReturn = 500;
} else if (Update.write(upload.buf, upload.currentSize) != upload.currentSize){
} else if (Update.write(upload.buf, upload.currentSize) !=
upload.currentSize) {
Log.warning(F("WEB : Firmware write was unsuccessful." CR));
_uploadReturn = 500;
}
@ -224,7 +231,8 @@ void WebServerHandler::webHandleUploadFile() {
ESP_RESET();
} else {
ErrorFileLog errLog;
errLog.addEntry(F("WEB : Failed to finish firmware flashing error=") + String(Update.getError()));
errLog.addEntry(F("WEB : Failed to finish firmware flashing error=") +
String(Update.getError()));
_uploadReturn = 500;
}
} else {
@ -244,15 +252,13 @@ void WebServerHandler::webHandleUploadFile() {
if (validFilename) _uploadFile = LittleFS.open(f, "w");
} else if (upload.status == UPLOAD_FILE_WRITE) {
Log.notice(F("WEB : Writing html upload." CR));
if (_uploadFile)
_uploadFile.write(
upload.buf,
upload.currentSize);
if (_uploadFile) _uploadFile.write(upload.buf, upload.currentSize);
} else if (upload.status == UPLOAD_FILE_END) {
Log.notice(F("WEB : Finish html upload." CR));
if (_uploadFile) {
_uploadFile.close();
Log.notice(F("WEB : Html file uploaded %d bytes." CR), upload.totalSize);
Log.notice(F("WEB : Html file uploaded %d bytes." CR),
upload.totalSize);
}
_server->sendHeader("Location", "/");
_server->send(303);
@ -321,8 +327,7 @@ void WebServerHandler::webHandleStatus() {
double angle = 0;
if (myGyro.hasValue())
angle = myGyro.getAngle();
if (myGyro.hasValue()) angle = myGyro.getAngle();
double tempC = myTempSensor.getTempC(myConfig.isGyroTemp());
double gravity = calculateGravity(angle, tempC);
@ -833,7 +838,8 @@ void WebServerHandler::webHandleTestPush() {
PushTarget push;
bool enabled = false;
if (!type.compareTo(PARAM_FORMAT_BREWFATHER) && myConfig.isBrewfatherActive()) {
if (!type.compareTo(PARAM_FORMAT_BREWFATHER) &&
myConfig.isBrewfatherActive()) {
push.sendBrewfather(engine);
enabled = true;
} else if (!type.compareTo(PARAM_FORMAT_HTTP1) && myConfig.isHttpActive()) {
@ -845,7 +851,8 @@ void WebServerHandler::webHandleTestPush() {
} else if (!type.compareTo(PARAM_FORMAT_HTTP3) && myConfig.isHttp3Active()) {
push.sendHttp3(engine, myConfig.isHttp3SSL());
enabled = true;
} else if (!type.compareTo(PARAM_FORMAT_INFLUXDB) && myConfig.isInfluxDb2Active()) {
} else if (!type.compareTo(PARAM_FORMAT_INFLUXDB) &&
myConfig.isInfluxDb2Active()) {
push.sendInfluxDb2(engine);
enabled = true;
} else if (!type.compareTo(PARAM_FORMAT_MQTT) && myConfig.isMqttActive()) {

View File

@ -29,9 +29,9 @@ SOFTWARE.
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#else // defined (ESP32)
#include <ESPmDNS.h>
#include <WebServer.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#endif
#include <incbin.h>
@ -103,8 +103,7 @@ class WebServerHandler {
gAboutHtmSize);
}
void webReturnTestHtm() {
_server->send_P(200, "text/html", (const char*)gTestHtmData,
gTestHtmSize);
_server->send_P(200, "text/html", (const char*)gTestHtmData, gTestHtmSize);
}
#else
void webReturnUploadHtm() {

View File

@ -364,7 +364,8 @@ bool WifiConnection::checkFirmwareVersion() {
// Download new html files to filesystem if they are present.
if (!ver["html"].isNull() && _newFirmware) {
Log.notice(F("WIFI: OTA checking if html files should be downloaded." CR));
Log.notice(
F("WIFI: OTA checking if html files should be downloaded." CR));
JsonArray htmlFiles = ver["html"].as<JsonArray>();
for (JsonVariant v : htmlFiles) {
String s = v;