mirror of
https://github.com/adafruit/Adafruit_TSL2561.git
synced 2025-07-31 17:34:29 +02:00
Added support for Teensy 3.x and made Wire port selectable in setup
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
#if defined(__AVR__)
|
#if defined(__AVR__)
|
||||||
#include <avr/pgmspace.h>
|
#include <avr/pgmspace.h>
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
#else
|
#elif !defined(TEENSYDUINO)
|
||||||
#include "pgmspace.h"
|
#include "pgmspace.h"
|
||||||
#endif
|
#endif
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -44,15 +44,15 @@
|
|||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
void Adafruit_TSL2561_Unified::write8 (uint8_t reg, uint32_t value)
|
void Adafruit_TSL2561_Unified::write8 (uint8_t reg, uint32_t value)
|
||||||
{
|
{
|
||||||
Wire.beginTransmission(_addr);
|
wire -> beginTransmission(_addr);
|
||||||
#if ARDUINO >= 100
|
#if ARDUINO >= 100
|
||||||
Wire.write(reg);
|
wire -> write(reg);
|
||||||
Wire.write(value & 0xFF);
|
wire -> write(value & 0xFF);
|
||||||
#else
|
#else
|
||||||
Wire.send(reg);
|
wire -> send(reg);
|
||||||
Wire.send(value & 0xFF);
|
wire -> send(value & 0xFF);
|
||||||
#endif
|
#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)
|
uint8_t Adafruit_TSL2561_Unified::read8(uint8_t reg)
|
||||||
{
|
{
|
||||||
Wire.beginTransmission(_addr);
|
wire -> beginTransmission(_addr);
|
||||||
#if ARDUINO >= 100
|
#if ARDUINO >= 100
|
||||||
Wire.write(reg);
|
wire -> write(reg);
|
||||||
#else
|
#else
|
||||||
Wire.send(reg);
|
wire -> send(reg);
|
||||||
#endif
|
#endif
|
||||||
Wire.endTransmission();
|
wire -> endTransmission();
|
||||||
|
|
||||||
Wire.requestFrom(_addr, 1);
|
wire -> requestFrom(_addr, 1);
|
||||||
#if ARDUINO >= 100
|
#if ARDUINO >= 100
|
||||||
return Wire.read();
|
return wire -> read();
|
||||||
#else
|
#else
|
||||||
return Wire.receive();
|
return wire -> receive();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,21 +87,21 @@ uint16_t Adafruit_TSL2561_Unified::read16(uint8_t reg)
|
|||||||
{
|
{
|
||||||
uint16_t x; uint16_t t;
|
uint16_t x; uint16_t t;
|
||||||
|
|
||||||
Wire.beginTransmission(_addr);
|
wire -> beginTransmission(_addr);
|
||||||
#if ARDUINO >= 100
|
#if ARDUINO >= 100
|
||||||
Wire.write(reg);
|
wire -> write(reg);
|
||||||
#else
|
#else
|
||||||
Wire.send(reg);
|
wire -> send(reg);
|
||||||
#endif
|
#endif
|
||||||
Wire.endTransmission();
|
wire -> endTransmission();
|
||||||
|
|
||||||
Wire.requestFrom(_addr, 2);
|
wire -> requestFrom(_addr, 2);
|
||||||
#if ARDUINO >= 100
|
#if ARDUINO >= 100
|
||||||
t = Wire.read();
|
t = wire -> read();
|
||||||
x = Wire.read();
|
x = wire -> read();
|
||||||
#else
|
#else
|
||||||
t = Wire.receive();
|
t = wire -> receive();
|
||||||
x = Wire.receive();
|
x = wire -> receive();
|
||||||
#endif
|
#endif
|
||||||
x <<= 8;
|
x <<= 8;
|
||||||
x |= t;
|
x |= t;
|
||||||
@@ -193,10 +193,22 @@ Adafruit_TSL2561_Unified::Adafruit_TSL2561_Unified(uint8_t addr, int32_t sensorI
|
|||||||
doing anything else)
|
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 */
|
/* Make sure we're actually connected */
|
||||||
uint8_t x = read8(TSL2561_REGISTER_ID);
|
uint8_t x = read8(TSL2561_REGISTER_ID);
|
||||||
if (!(x & 0x0A))
|
if (!(x & 0x0A))
|
||||||
|
@@ -42,6 +42,7 @@
|
|||||||
#include <WProgram.h>
|
#include <WProgram.h>
|
||||||
#endif
|
#endif
|
||||||
#include <Adafruit_Sensor.h>
|
#include <Adafruit_Sensor.h>
|
||||||
|
#include "HardwareSerial.h"
|
||||||
|
|
||||||
#ifdef __AVR_ATtiny85__
|
#ifdef __AVR_ATtiny85__
|
||||||
#include "TinyWireM.h"
|
#include "TinyWireM.h"
|
||||||
@@ -179,6 +180,8 @@ class Adafruit_TSL2561_Unified : public Adafruit_Sensor {
|
|||||||
public:
|
public:
|
||||||
Adafruit_TSL2561_Unified(uint8_t addr, int32_t sensorID = -1);
|
Adafruit_TSL2561_Unified(uint8_t addr, int32_t sensorID = -1);
|
||||||
boolean begin(void);
|
boolean begin(void);
|
||||||
|
boolean begin(TwoWire *theWire);
|
||||||
|
boolean init();
|
||||||
|
|
||||||
/* TSL2561 Functions */
|
/* TSL2561 Functions */
|
||||||
void enableAutoRange(bool enable);
|
void enableAutoRange(bool enable);
|
||||||
@@ -192,6 +195,8 @@ class Adafruit_TSL2561_Unified : public Adafruit_Sensor {
|
|||||||
void getSensor(sensor_t*);
|
void getSensor(sensor_t*);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
TwoWire *wire;
|
||||||
|
|
||||||
int8_t _addr;
|
int8_t _addr;
|
||||||
boolean _tsl2561Initialised;
|
boolean _tsl2561Initialised;
|
||||||
boolean _tsl2561AutoGain;
|
boolean _tsl2561AutoGain;
|
||||||
|
@@ -94,7 +94,7 @@ void setup(void)
|
|||||||
Serial.println("Light Sensor Test"); Serial.println("");
|
Serial.println("Light Sensor Test"); Serial.println("");
|
||||||
|
|
||||||
/* Initialise the sensor */
|
/* 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 */
|
/* There was a problem detecting the TSL2561 ... check your connections */
|
||||||
Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
|
Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
|
||||||
|
Reference in New Issue
Block a user