From fca7c4f97e81dcb2914c95023b0eb0387b324436 Mon Sep 17 00:00:00 2001 From: mjs513 Date: Mon, 11 Sep 2017 22:15:28 -0400 Subject: [PATCH 1/2] Added support for Teensy 3.x and made Wire port selectable in setup --- Adafruit_TSL2561_U.cpp | 62 +++++++++++++++++++------------- Adafruit_TSL2561_U.h | 5 +++ examples/sensorapi/sensorapi.ino | 2 +- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/Adafruit_TSL2561_U.cpp b/Adafruit_TSL2561_U.cpp index 0517185..8e4ef50 100644 --- a/Adafruit_TSL2561_U.cpp +++ b/Adafruit_TSL2561_U.cpp @@ -22,7 +22,7 @@ #if defined(__AVR__) #include #include -#else +#elif !defined(TEENSYDUINO) #include "pgmspace.h" #endif #include @@ -44,15 +44,15 @@ /**************************************************************************/ void Adafruit_TSL2561_Unified::write8 (uint8_t reg, uint32_t value) { - Wire.beginTransmission(_addr); + wire -> beginTransmission(_addr); #if ARDUINO >= 100 - Wire.write(reg); - Wire.write(value & 0xFF); + wire -> write(reg); + wire -> write(value & 0xFF); #else - Wire.send(reg); - Wire.send(value & 0xFF); + wire -> send(reg); + wire -> send(value & 0xFF); #endif - Wire.endTransmission(); + wire -> endTransmission(); } /**************************************************************************/ @@ -62,19 +62,19 @@ void Adafruit_TSL2561_Unified::write8 (uint8_t reg, uint32_t value) /**************************************************************************/ uint8_t Adafruit_TSL2561_Unified::read8(uint8_t reg) { - Wire.beginTransmission(_addr); + wire -> beginTransmission(_addr); #if ARDUINO >= 100 - Wire.write(reg); + wire -> write(reg); #else - Wire.send(reg); + wire -> send(reg); #endif - Wire.endTransmission(); + wire -> endTransmission(); - Wire.requestFrom(_addr, 1); + wire -> requestFrom(_addr, 1); #if ARDUINO >= 100 - return Wire.read(); + return wire -> read(); #else - return Wire.receive(); + return wire -> receive(); #endif } @@ -87,21 +87,21 @@ uint16_t Adafruit_TSL2561_Unified::read16(uint8_t reg) { uint16_t x; uint16_t t; - Wire.beginTransmission(_addr); + wire -> beginTransmission(_addr); #if ARDUINO >= 100 - Wire.write(reg); + wire -> write(reg); #else - Wire.send(reg); + wire -> send(reg); #endif - Wire.endTransmission(); + wire -> endTransmission(); - Wire.requestFrom(_addr, 2); + wire -> requestFrom(_addr, 2); #if ARDUINO >= 100 - t = Wire.read(); - x = Wire.read(); + t = wire -> read(); + x = wire -> read(); #else - t = Wire.receive(); - x = Wire.receive(); + t = wire -> receive(); + x = wire -> receive(); #endif x <<= 8; x |= t; @@ -193,10 +193,22 @@ Adafruit_TSL2561_Unified::Adafruit_TSL2561_Unified(uint8_t addr, int32_t sensorI doing anything else) */ /**************************************************************************/ -boolean Adafruit_TSL2561_Unified::begin(void) +boolean Adafruit_TSL2561_Unified::begin() { - Wire.begin(); + wire = &Wire; + wire -> begin(); + return init(); +} +boolean Adafruit_TSL2561_Unified::begin(TwoWire *theWire) +{ + wire = theWire; + wire -> begin(); + return init(); +} + +boolean Adafruit_TSL2561_Unified::init() +{ /* Make sure we're actually connected */ uint8_t x = read8(TSL2561_REGISTER_ID); if (!(x & 0x0A)) diff --git a/Adafruit_TSL2561_U.h b/Adafruit_TSL2561_U.h index dd97473..b9a416f 100644 --- a/Adafruit_TSL2561_U.h +++ b/Adafruit_TSL2561_U.h @@ -42,6 +42,7 @@ #include #endif #include +#include "HardwareSerial.h" #ifdef __AVR_ATtiny85__ #include "TinyWireM.h" @@ -179,6 +180,8 @@ class Adafruit_TSL2561_Unified : public Adafruit_Sensor { public: Adafruit_TSL2561_Unified(uint8_t addr, int32_t sensorID = -1); boolean begin(void); + boolean begin(TwoWire *theWire); + boolean init(); /* TSL2561 Functions */ void enableAutoRange(bool enable); @@ -192,6 +195,8 @@ class Adafruit_TSL2561_Unified : public Adafruit_Sensor { void getSensor(sensor_t*); private: + TwoWire *wire; + int8_t _addr; boolean _tsl2561Initialised; boolean _tsl2561AutoGain; diff --git a/examples/sensorapi/sensorapi.ino b/examples/sensorapi/sensorapi.ino index 1a1138a..6c5c372 100644 --- a/examples/sensorapi/sensorapi.ino +++ b/examples/sensorapi/sensorapi.ino @@ -94,7 +94,7 @@ void setup(void) Serial.println("Light Sensor Test"); Serial.println(""); /* Initialise the sensor */ - if(!tsl.begin()) + if(!tsl.begin(&Wire2)) //use tsl.begin() to default to Wire, &Wire2 directs api to use Wire2, etc. { /* There was a problem detecting the TSL2561 ... check your connections */ Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!"); From e1fc8836ae56554450160665253ffbae33690eb1 Mon Sep 17 00:00:00 2001 From: mjs513 Date: Tue, 12 Sep 2017 13:10:58 -0400 Subject: [PATCH 2/2] Revert to begin to original, added comment --- examples/sensorapi/sensorapi.ino | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/sensorapi/sensorapi.ino b/examples/sensorapi/sensorapi.ino index 6c5c372..640cbb7 100644 --- a/examples/sensorapi/sensorapi.ino +++ b/examples/sensorapi/sensorapi.ino @@ -94,7 +94,9 @@ void setup(void) Serial.println("Light Sensor Test"); Serial.println(""); /* Initialise the sensor */ - if(!tsl.begin(&Wire2)) //use tsl.begin() to default to Wire, &Wire2 directs api to use Wire2, etc. + //use tsl.begin() to default to Wire, + //tsl.begin(&Wire2) directs api to use Wire2, etc. + if(!tsl.begin()) { /* There was a problem detecting the TSL2561 ... check your connections */ Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");