From 48ff7c5d352ac6d526ba949f8825fe21c44290ba Mon Sep 17 00:00:00 2001
From: Manon MALIQUE <manon.malique@irit.fr>
Date: Tue, 2 Apr 2024 09:49:06 +0200
Subject: [PATCH] Adding comments

---
 .../neocampus/advertisedDeviceCallbacks.cpp   | 28 ++++++++--
 .../neocampus/advertisedDeviceCallbacks.h     |  9 ++--
 .../libraries/neocampus/neocampus_utils.cpp   |  3 +-
 .../libraries/neocampus/neocampus_utils.h     |  3 ++
 .../libraries/neocampus_drivers/SHT3x.cpp     |  8 ++-
 neosensor/libraries/neocampus_drivers/SHT3x.h |  2 -
 .../neocampus_drivers/generic_driver.cpp      |  3 +-
 neosensor/neosensor.ino                       | 54 ++++++-------------
 8 files changed, 58 insertions(+), 52 deletions(-)

diff --git a/neosensor/libraries/neocampus/advertisedDeviceCallbacks.cpp b/neosensor/libraries/neocampus/advertisedDeviceCallbacks.cpp
index dc6e4e53..d7cf1e66 100644
--- a/neosensor/libraries/neocampus/advertisedDeviceCallbacks.cpp
+++ b/neosensor/libraries/neocampus/advertisedDeviceCallbacks.cpp
@@ -1,15 +1,33 @@
 //BLE device
-/*
+
 #include <advertisedDeviceCallbacks.h>
 
+
+
+// ***** getTemperature *****
+float advertisedDeviceCallbacks::getTemperature(BLEAdvertisedDevice device) {
+  uint8_t temp_lb = (uint8_t) device.getManufacturerData().c_str()[1]; // temperature lower byte 
+  log_debug(F("temp_lb :")); log_debug(temp_lb);
+  uint8_t temp_hb = (uint8_t)(device.getManufacturerData().c_str()[2]); // temperature higher byte 
+  log_debug(F("\ntemp_hb :")); log_debug(temp_hb);
+  int16_t temperature = (int16_t)((uint16_t)(temp_hb)<<8) + (uint16_t)temp_lb;
+  log_debug(F("\ntemperature :")); log_debug(temperature);log_flush();
+  return ((float)(temperature)/10);
+}
+
+
+
 //Display the BLE Device data
 void advertisedDeviceCallbacks::onResult(BLEAdvertisedDevice device) {
     if (device.getName() == "TP358 (5551)") {
         log_debug(F("\nReference temperature : "));
-        log_debug(getTemperature(device));
-        log_debug(F("\nReference humidity : "));
-        log_debug(getHumidity(device)); 
+        valBLE=getTemperature(device);
+        log_debug(valBLE);
         log_flush();
+        _gotcalibrationvalue = true;
+    }else{
+        log_debug(device.getName());
     }
+    
 }
-*/
\ No newline at end of file
+
diff --git a/neosensor/libraries/neocampus/advertisedDeviceCallbacks.h b/neosensor/libraries/neocampus/advertisedDeviceCallbacks.h
index 0ac860d2..786460d1 100644
--- a/neosensor/libraries/neocampus/advertisedDeviceCallbacks.h
+++ b/neosensor/libraries/neocampus/advertisedDeviceCallbacks.h
@@ -1,17 +1,18 @@
 //BLE device
-/*
+
 #include <Arduino.h>
 #include <BLEDevice.h>
 #include <BLEUtils.h>
 #include <BLEScan.h>
 #include <BLEAdvertisedDevice.h>
 #include "neocampus_debug.h"
+#include "neocampus_utils.h"
 
- *
+/*
  * Class
- *
+ */
 class advertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks{
 public:
     void onResult(BLEAdvertisedDevice device);
+    float getTemperature(BLEAdvertisedDevice device);
 };
-*/
\ No newline at end of file
diff --git a/neosensor/libraries/neocampus/neocampus_utils.cpp b/neosensor/libraries/neocampus/neocampus_utils.cpp
index 5342720b..900ee54e 100644
--- a/neosensor/libraries/neocampus/neocampus_utils.cpp
+++ b/neosensor/libraries/neocampus/neocampus_utils.cpp
@@ -119,6 +119,7 @@ bool checkCLEARswitch( uint8_t sw_pin ) {
  * if decr_sw_pin == -1 OR incr_sw_pin-->== -1 check disabled
  */
 bool checkCALIBRATIONswitch(uint8_t decr_sw_pin, uint8_t incr_sw_pin) {
+
   if (decr_sw_pin == INVALID_GPIO) {
     log_debug(F("\n[CALIBRATION] no DECR_SW defined !")); log_flush();
     return false;
@@ -162,7 +163,7 @@ bool checkCALIBRATIONswitch(uint8_t decr_sw_pin, uint8_t incr_sw_pin) {
     delay(250);
   }
   if (_res==true){
-    _need2calibrate=true;
+    _need2calibrate=true; 
   }
   return _res;
 }
diff --git a/neosensor/libraries/neocampus/neocampus_utils.h b/neosensor/libraries/neocampus/neocampus_utils.h
index ffdda5b6..264418a1 100644
--- a/neosensor/libraries/neocampus/neocampus_utils.h
+++ b/neosensor/libraries/neocampus/neocampus_utils.h
@@ -38,7 +38,10 @@
 extern bool _need2reboot;
 // calibrate flag
 extern bool _need2calibrate; 
+// value of the BLE device
+extern float valBLE;
 
+extern bool _gotcalibrationvalue;
 
 
 /* ----------------------------------------------------------------------------
diff --git a/neosensor/libraries/neocampus_drivers/SHT3x.cpp b/neosensor/libraries/neocampus_drivers/SHT3x.cpp
index 5e2ffc78..49874e8a 100644
--- a/neosensor/libraries/neocampus_drivers/SHT3x.cpp
+++ b/neosensor/libraries/neocampus_drivers/SHT3x.cpp
@@ -285,11 +285,15 @@ boolean SHT3x::getTemp( float *pval )
   // 100.0 x _tmp = (17500 x _tmp) / (16384 x 4) - 4500
   _tmp = ((4375 * _tmp) >> 14) - 4500;
   *pval = ((float)_tmp / 100.0f);
-  
-  if ((not _need2reboot) && _need2calibrate){
+  log_debug(F("[SHT3x] Need to calibrate ?\n"));log_debug(_need2calibrate);log_flush();
+  if ((not _need2reboot) && _need2calibrate && _gotcalibrationvalue){
+    log_debug(_gotcalibrationvalue);
+    log_debug(F("[SHT3x] Let's calibrate !\n"));log_debug(_need2calibrate);log_flush();
     auto result = calibrate(pval, _a, _b); // updating of (a,b)
     _a = std::get<0>(result);
     _b = std::get<1>(result);
+  }else{
+    log_debug(F("[SHT3x] No need to calibrate :'(\n"));log_debug(_need2calibrate);log_flush();
   }
   // calibration
   *pval = *pval * _a;
diff --git a/neosensor/libraries/neocampus_drivers/SHT3x.h b/neosensor/libraries/neocampus_drivers/SHT3x.h
index bc9d8731..53110f75 100644
--- a/neosensor/libraries/neocampus_drivers/SHT3x.h
+++ b/neosensor/libraries/neocampus_drivers/SHT3x.h
@@ -127,8 +127,6 @@ class SHT3x : public generic_driver {
 
     // device detection
     static boolean is_device( uint8_t );
-    Sketch uses 1103089 bytes (84%) of program storage space. Maximum is 1310720 bytes.
-Global variables use 49528 bytes (15%) of dynamic memory, leaving 278152 bytes for local variables. Maximum is 327680 bytes.
   private:
     // -- private methods
     bool _readSensor( uint16_t* );                // low-level function to read value registers
diff --git a/neosensor/libraries/neocampus_drivers/generic_driver.cpp b/neosensor/libraries/neocampus_drivers/generic_driver.cpp
index feeb2e70..08291832 100644
--- a/neosensor/libraries/neocampus_drivers/generic_driver.cpp
+++ b/neosensor/libraries/neocampus_drivers/generic_driver.cpp
@@ -172,8 +172,9 @@ void generic_driver::setDataSent( void ) {
  */
 std::tuple<float, float> generic_driver::calibrate(float* pval, float a, float b){
   //TO-DO : Add an extern global variable -> ThermoPro sensor value : valBLE to replace the one below
-  float valBLE = 20.5; 
+  log_debug(F("[generic_drivers] BLE Value :"));log_debug(valBLE);log_debug(F("/n"));log_flush();
   b= valBLE-*pval;
+  log_debug(F("[generic_drivers] b :"));log_debug(b);log_debug(F("/n"));log_flush();
   // calibration is done so no need to do it again
   _need2calibrate=false;
   return std::make_tuple(a, b);
diff --git a/neosensor/neosensor.ino b/neosensor/neosensor.ino
index ede0df8b..0f37b800 100644
--- a/neosensor/neosensor.ino
+++ b/neosensor/neosensor.ino
@@ -122,16 +122,15 @@
 #include "wifiParametersMgt.h"
 
 //BLE device
-//#include <advertisedDeviceCallbacks.h>
-
+#include <advertisedDeviceCallbacks.h>
+BLEScan* pBLEScan;
 
 /*
  * Definitions
 */
 
 //BLE device
-#define SCAN_TIME 5                   // scan period in second
-bool BLEScanActivated = false;
+#define SCAN_TIME 10                   // scan period in second
 #define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8))
 
 // Debug related definitions
@@ -186,6 +185,8 @@ typedef enum {
  */
 bool _need2reboot = false;              // flag to tell a reboot is requested
 bool _need2calibrate = false;           // flag to tell a calibration is requested 
+float valBLE= 43;
+bool _gotcalibrationvalue = false;
 // WiFi parameters management statically allocated instance
 wifiParametersMgt wifiParameters = wifiParametersMgt();
 
@@ -357,7 +358,7 @@ void endLoop( void ) {
   static unsigned long _lastCheck = 0;    // elapsed ms since last check
 
 #if 0
-  // ONY FOR DEBUGGING
+  // ONLY FOR DEBUGGING
   static unsigned long _lastJSONdisplay = 0;    // elapsed ms since last displying shared JSON
   // 90s second elapsed ?
   if( ((millis() - _lastJSONdisplay) >= (unsigned long)90*1000UL) == true ) {
@@ -786,40 +787,17 @@ void lateSetup( void ) {
 
 //BLE device
 // ***** start BLE scan  *****
-/*
 void startScanBLE() {
-  BLEScan* pBLEScan = BLEDevice::getScan(); // start scanning
+  pBLEScan = BLEDevice::getScan(); // start scanning
   pBLEScan->setAdvertisedDeviceCallbacks(new advertisedDeviceCallbacks(), true);
-  pBLEScan->setActiveScan(true);
-  pBLEScan->start(SCAN_TIME, scanCompleteCB);
-  BLEScanActivated = true;
+  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
+  pBLEScan->start(SCAN_TIME,scanCompleteCB);
 }
-
 // ***** scan Complete Call Back  *****
 static void scanCompleteCB(BLEScanResults scanResults) { // Callback invoked when scanning has completed
-  BLEScanActivated = false;
-}
-
-// ***** manage BLE scan  *****
-void manageBLEscan() {
-  if (!BLEScanActivated)
-  startScanBLE(); // start scanning
-}
-
-// ***** getTemperature *****
-float getTemperature(BLEAdvertisedDevice device) {
-  uint8_t temp_lb = (uint8_t) device.getManufacturerData().c_str()[1]; // temperature lower byte 
-  uint8_t temp_hb = (uint8_t)(device.getManufacturerData().c_str()[2]); // temperature higher byte 
-  int16_t temperature = (int16_t)((uint16_t)(temp_hb)<<8) + (uint16_t)temp_lb;
-  return ((float)(temperature)/10);
+  log_debug(F("scan completed"));log_flush();
 }
 
-// ***** getHumidity *****
-uint8_t getHumidity(BLEAdvertisedDevice device) {
-  const uint8_t *pHum = (const uint8_t *)&device.getManufacturerData().c_str()[3];
-  return *pHum;
-}
-*/
 // --- SETUP -------------------------------------------------------------------
 void setup() {
 
@@ -1230,14 +1208,14 @@ if( res!=uint8_t(0x44) ) {
   modulesList.startAll( &sensocampus, sharedRoot );
 
   //BLE device
-  /*
+  
   //Init BLE
   BLEDevice::init("");
-  manageBLEscan();
+  startScanBLE();
   log_debug(F("\nSetup of BLE device ..."));log_flush();
   delay(1000);
-  log_debug(F(" done !"));log_flush();
-  */
+  log_debug(F(" done !\n"));log_flush();
+  
 
   //check if the sensor need a calibration
   #ifdef INCR_SW
@@ -1261,7 +1239,9 @@ void loop() {
    */
   modulesList.processAll();
 
-
+  if(_need2calibrate){
+    pBLEScan->clearResults();
+  }
   /* 
    * end of main loop
    */
-- 
GitLab