From dc8b0b0156936b522cd4bcb81cf3b704169829da Mon Sep 17 00:00:00 2001
From: Pietro Bortolus <Pietro.bortolus@skywarder.eu>
Date: Mon, 10 Feb 2025 12:21:27 +0100
Subject: [PATCH] [ND015A] updated init function

fixed the init function, it now storesthe recieved data in a struct
and compares the appropriated bytes to a string containing the sensor model
---
 src/shared/sensors/ND015X/ND015A.cpp | 51 +++++++++++++++++++---------
 src/shared/sensors/ND015X/ND015A.h   | 14 ++++++--
 2 files changed, 47 insertions(+), 18 deletions(-)

diff --git a/src/shared/sensors/ND015X/ND015A.cpp b/src/shared/sensors/ND015X/ND015A.cpp
index ba332cd5a..5ad1ecb88 100644
--- a/src/shared/sensors/ND015X/ND015A.cpp
+++ b/src/shared/sensors/ND015X/ND015A.cpp
@@ -34,25 +34,28 @@ ND015A::ND015A(SPIBusInterface& bus, miosix::GpioPin cs, SPIBusConfig spiConfig)
 
 bool ND015A::init()
 {
-    uint8_t data[10];
+    uint8_t* data;
+    NDD015ADataExtended.pressure =
+        (modeByte << 8) | rateByte;  // updating the first 2 bytes with the
+                                     // correct sensor settings
 
     SPITransaction spi(slave);
 
-    spi.read(data, sizeof(data));
+    data = reinterpret_cast<uint8_t*>(&NDD015ADataExtended);
 
-    // the following monstrosity is needed to check if the model number read
-    // from the SPI is correct, the numbers are the ASCII encoding of "ND015A"
+    spi.transfer(data, sizeof(NDD015ADataExtended));
 
-    if (data[9] == 0x41 && data[8] == 0x35 && data[7] == 0x31 &&
-        data[6] == 0x30 && data[5] == 0x44 && data[4] == 0x4E)
+    // check if the model returned by the sensor matches with the correct model
+    for (int i = 4; i < 10; i++)
     {
-        return true;
-    }
-    else
-    {
-        LOG_ERR(logger, "sensor model number did not match");
-        return false;
+        if ((uint8_t)(&data + i) != (sensorModel[i - 4]))
+        {
+            LOG_ERR(logger, "sensor model number did not match");
+            return false;
+        }
     }
+
+    return true;
 }
 
 bool ND015A::selfTest() { return true; }
@@ -68,21 +71,41 @@ void ND015A::setOutputDataRate(uint8_t odr)
         LOG_ERR(logger, "odr setting not valid, using default value (0x1C)");
         rateByte = 0x1C;
     }
+
+    SPITransaction spi(slave);
+
+    uint16_t SPIDataOut = (modeByte << 8) | rateByte;
+    spi.transfer16(SPIDataOut);
 }
 
 void ND015A::setIOWatchdog(IOWatchdogEnable iow)
 {
     modeByte = (modeByte & ~IO_WATCHDOG_MASK) | iow;
+
+    SPITransaction spi(slave);
+
+    uint16_t SPIDataOut = (modeByte << 8) | rateByte;
+    spi.transfer16(SPIDataOut);
 }
 
 void ND015A::setBWLimitFilter(BWLimitFilter bwl)
 {
     modeByte = (modeByte & ~BW_LIMIT_MASK) | bwl;
+
+    SPITransaction spi(slave);
+
+    uint16_t SPIDataOut = (modeByte << 8) | rateByte;
+    spi.transfer16(SPIDataOut);
 }
 
 void ND015A::setNotch(NotchEnable ntc)
 {
     modeByte = (modeByte & ~NOTCH_MASK) | ntc;
+
+    SPITransaction spi(slave);
+
+    uint16_t SPIDataOut = (modeByte << 8) | rateByte;
+    spi.transfer16(SPIDataOut);
 }
 
 ND015XData ND015A::sampleImpl()
@@ -92,10 +115,6 @@ ND015XData ND015A::sampleImpl()
 
     SPITransaction spi(slave);
 
-    spi.transfer16(SPIDataOut);  // we need to make an SPI transaction before
-                                 // reading the data to make sure the proper
-                                 // settings are used
-
     uint16_t SPIDataIn = spi.transfer16(SPIDataOut);
 
     data.pressure =
diff --git a/src/shared/sensors/ND015X/ND015A.h b/src/shared/sensors/ND015X/ND015A.h
index 2973205de..d6ec188e4 100644
--- a/src/shared/sensors/ND015X/ND015A.h
+++ b/src/shared/sensors/ND015X/ND015A.h
@@ -109,8 +109,18 @@ protected:
 
 private:
     SPISlave slave;
-    uint8_t modeByte = 0xF7;  // settings for the mode control register
-    uint8_t rateByte = 0x1C;  // settings for the rate control register
+    uint8_t modeByte   = 0xF7;  // settings for the mode control register
+    uint8_t rateByte   = 0x1C;  // settings for the rate control register
+    string sensorModel = "ND015A";
+
+    struct
+    {
+        uint16_t pressure;
+        uint16_t temperature;
+        uint8_t model[8];
+        uint8_t serial[4];
+        uint8_t build[6];
+    } NDD015ADataExtended;
 
     enum RegisterMask : uint8_t
     {
-- 
GitLab