From a9959895485203ebd78404205856405769acba65 Mon Sep 17 00:00:00 2001
From: francois <francois@clever.amilab.irit.fr>
Date: Tue, 15 Mar 2022 15:25:25 +0100
Subject: [PATCH] corrected TSL2561 detection with latest i2c primitives from
 neOCampus

---
 .../libraries/neocampus_drivers/SCD4x.cpp     |  2 +-
 .../libraries/neocampus_drivers/TSL2561.cpp   | 39 ++++++++++---------
 tests/ikea_pm2.5/ikea_pm2.5.ino               |  4 +-
 3 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/neosensor/libraries/neocampus_drivers/SCD4x.cpp b/neosensor/libraries/neocampus_drivers/SCD4x.cpp
index 8e0010be..5acce2c9 100644
--- a/neosensor/libraries/neocampus_drivers/SCD4x.cpp
+++ b/neosensor/libraries/neocampus_drivers/SCD4x.cpp
@@ -337,7 +337,7 @@ bool SCD4x::_readSensor( void ) {
     status = true;
 
     // DEBUG
-    hex_dump( (char*)buf, sizeof(buf) );
+    //hex_dump( (char*)buf, sizeof(buf) );
     //break;
 
     // CO2 + CRC
diff --git a/neosensor/libraries/neocampus_drivers/TSL2561.cpp b/neosensor/libraries/neocampus_drivers/TSL2561.cpp
index a82920d2..d096bc0d 100644
--- a/neosensor/libraries/neocampus_drivers/TSL2561.cpp
+++ b/neosensor/libraries/neocampus_drivers/TSL2561.cpp
@@ -33,6 +33,8 @@
     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
     @history
+    F.Thiebolt  mar.22  switch to newer i2c primitives that better match TSL2561
+                        communications requirements.
     2017  - mostly rewritten for neOCampus
 
 */
@@ -209,30 +211,29 @@ void TSL2561::_powerOFF( uint8_t a ) {
  */
 bool TSL2561::_check_identity( uint8_t a ) {
 
+  uint8_t res, data;
+
   // stop'n start device
   _powerOFF(a); delay(10);
+  data = (uint8_t)(-1);
+  res = readList( a, (uint8_t)(TSL2561_COMMAND_BIT | TSL2561_CLEAR_BIT | TSL2561_REGISTER_CONTROL), &data, 1 );
+  if( !res ) return false;
+  //log_debug(F("\n[TSL2561] REGISTER_CONTROL = 0x"));log_debug(data,HEX);log_flush();
+  if( (data&0x03)!=0x00 ) return false;
+
   _powerON(a); delay(10);
+  data = (uint8_t)(-1);
+  res = readList( a, (uint8_t)(TSL2561_COMMAND_BIT | TSL2561_CLEAR_BIT | TSL2561_REGISTER_CONTROL), &data, 1 );
+  if( !res ) return false;
+  //log_debug(F("\n[TSL2561] REGISTER_CONTROL = 0x"));log_debug(data,HEX);log_flush();
+  if( (data&0x03)!=0x03 ) return false;
 
   // check Register ID
-  Wire.beginTransmission(a);
-  Wire.write((uint8_t)(TSL2561_COMMAND_BIT | TSL2561_CLEAR_BIT | TSL2561_REGISTER_CONTROL));
-  Wire.beginTransmission(a);
-  Wire.requestFrom(a, (uint8_t)1);
-
-TO BE CONTINUED
-
-  else yield();
-
-  uint8_t i=0;
-  while( Wire.available() and (i < tabsize) ) {
-    tab[i++] = Wire.read();
-  }
-  Wire.endTransmission();
-
-TO BE CONTINUED
-
-  uint8_t _res = read8(a,TSL2561_REGISTER_ID);
-  if( (_res&0xF0)!=REGISTER_ID_TSL2561 and (_res&0xF0)!=0x00  ) return false;
+  data = (uint8_t)(-1);
+  res = readList( a, (uint8_t)(TSL2561_COMMAND_BIT | TSL2561_CLEAR_BIT | TSL2561_REGISTER_ID), &data, 1 );
+  if( !res ) return false;
+  //log_debug(F("\n[TSL2561] REGISTER_ID = 0x"));log_debug(data,HEX);log_flush();
+  if( (data&0xF0)!=REGISTER_ID_TSL2561 and !(data&0xF0) ) return false;
 
   return true;
 }
diff --git a/tests/ikea_pm2.5/ikea_pm2.5.ino b/tests/ikea_pm2.5/ikea_pm2.5.ino
index daf553eb..9839cec4 100644
--- a/tests/ikea_pm2.5/ikea_pm2.5.ino
+++ b/tests/ikea_pm2.5/ikea_pm2.5.ino
@@ -99,7 +99,7 @@ void PMS::passiveMode()
 void PMS::requestRead()
 {
   if (_mode == MODE_PASSIVE) {
-    // uint8_t command[] = { 0x11, 0x01, 0x02, 0xEC }; // [PREFIX] [nb_octets] [code_command] [checksum]
+    //uint8_t command[] = { 0x11, 0x01, 0x02, 0xEC }; // [PREFIX] [nb_octets] [code_command] [checksum]
     uint8_t command[] = { 0x11, 0x02, 0x0b, 0x01, 0xE1 }; // [PREFIX] [nb_octets] [code_command] [checksum]
     _stream->write(command, sizeof(command));
   }
@@ -202,7 +202,7 @@ void PMS::loop()
 
 
 /* Declarations */
-//#define DEBUG_SERIAL
+#define DEBUG_SERIAL
 #define PM_PASSIVE_MODE   1   // PASSIVE vs ACTIVE modes
 #define PM_POWER_SAVING   1   // enable sleep() wakeUp() cycles to save power
 
-- 
GitLab