Class Opt3001
A Texas Instruments OPT3001 ambient light sensor, and a driver for one on an I2C bus.
public sealed class Opt3001 : IDisposable
- Inheritance
-
Opt3001
- Implements
- Inherited Members
Remarks
The static members are the part's datasheet, decoded exactly as the manufacturer specifies, and Opt3001.Sim, a part that is not there.
An instance drives one part over an I2cBus, measuring on demand in single-shot mode; the part returns itself to shutdown after each conversion. Nothing is sent until Init() or the first Measure().
Constructors
Opt3001(I2cBus, byte, ConversionTime, byte)
Creates a driver for the part at address on bus.
public Opt3001(I2cBus bus, byte address, Opt3001.ConversionTime conversionTime = ConversionTime.Ms800, byte range = 12)
Parameters
busI2cBusThe bus the part is on.
addressbyteOne of the
Addressconstants, by where ADDR is tied.conversionTimeOpt3001.ConversionTimeHow long each conversion integrates.
rangebyteA fixed range number up to RangeMax, or RangeAutomatic.
Exceptions
- PamojaException
The native driver could not be created.
Fields
AddressGnd
The address with ADDR tied to ground.
public const byte AddressGnd = 68
Field Value
AddressScl
The address with ADDR tied to SCL.
public const byte AddressScl = 71
Field Value
AddressSda
The address with ADDR tied to SDA.
public const byte AddressSda = 70
Field Value
AddressVdd
The address with ADDR tied to VDD.
public const byte AddressVdd = 69
Field Value
RangeAutomatic
The range number that lets the part choose its own full-scale range.
public const byte RangeAutomatic = 12
Field Value
RangeMax
The highest fixed range number, 83865.60 lux full scale.
public const byte RangeMax = 11
Field Value
Properties
Configuration
The configuration the driver writes, with the part in shutdown.
public PamojaOpt3001Config Configuration { get; }
Property Value
Methods
ConfigBits(PamojaOpt3001Config)
Assembles the 16-bit OPT3001 configuration register value.
public static ushort ConfigBits(PamojaOpt3001Config config)
Parameters
configPamojaOpt3001ConfigThe config.
Returns
- ushort
The value the C ABI computes.
ConfigFromBits(ushort)
Parses a 16-bit OPT3001 configuration register value.
public static PamojaOpt3001Config ConfigFromBits(ushort bits)
Parameters
bitsushortThe bits.
Returns
- PamojaOpt3001Config
The decoded value.
Exceptions
- PamojaException
The device data is not valid.
ConversionMillis(bool)
Returns the conversion time an OPT3001 setting selects.
public static ushort ConversionMillis(bool longConversion)
Parameters
longConversionboolThe long conversion.
Returns
- ushort
The value the C ABI computes.
Dispose()
Releases the driver and its share of the bus.
public void Dispose()
FaultCount(byte)
Returns how many consecutive faults an OPT3001 fault-count code requires.
public static byte FaultCount(byte code)
Parameters
codebyteThe code.
Returns
- byte
The value the C ABI computes.
FullScaleMilliLux(byte)
Returns the full scale an OPT3001 range number covers.
public static uint? FullScaleMilliLux(byte rangeNumber)
Parameters
rangeNumberbyteThe range number.
Returns
- uint?
The value, or null where the part defines none.
Init()
Checks the part is an OPT3001 and writes the settings with the part in shutdown.
public void Init()
Exceptions
- PamojaException
Nothing answered, or either id register is not an OPT3001's.
IsAutomaticRange(byte)
Reports whether an OPT3001 range number sets the full scale automatically.
public static bool IsAutomaticRange(byte rangeNumber)
Parameters
rangeNumberbyteThe range number.
Returns
- bool
The value the C ABI computes.
LsbMilliLux(byte)
Returns the illuminance one count carries at an OPT3001 exponent.
public static uint? LsbMilliLux(byte exponent)
Parameters
exponentbyteThe exponent.
Returns
- uint?
The value, or null where the part defines none.
Lux(ushort)
Converts a raw OPT3001 result register to lux.
public static float Lux(ushort raw)
Parameters
rawushortThe raw.
Returns
- float
The value the C ABI computes.
Measure()
Runs one conversion and reads the illuminance, initializing the part first if needed.
public Opt3001Reading Measure()
Returns
- Opt3001Reading
The illuminance.
Exceptions
- PamojaException
As Init(), and when the conversion never finishes.
MilliLux(ushort)
Converts a raw OPT3001 result register to milli-lux.
public static uint MilliLux(ushort raw)
Parameters
rawushortThe raw.
Returns
- uint
The value the C ABI computes.
RawFromMilliLux(uint)
Builds the OPT3001 result register that decodes to an illuminance.
public static ushort RawFromMilliLux(uint milliLux)
Parameters
milliLuxuintThe milli lux.
Returns
- ushort
The value the C ABI computes.
SetLimits(uint, uint)
Writes the low and high limits the part's interrupt pin compares each result against.
public void SetLimits(uint lowMilliLux, uint highMilliLux)
Parameters
Exceptions
- PamojaException
The transfer failed.
WordFromBytes(ReadOnlySpan<byte>)
Reads the two bytes an OPT3001 sends for a register, most significant first.
public static ushort WordFromBytes(ReadOnlySpan<byte> bytes)
Parameters
bytesReadOnlySpan<byte>The bytes.
Returns
- ushort
The decoded value.
Exceptions
- PamojaException
The device data is not valid.
WordToBytes(ushort)
Builds the two bytes an OPT3001 sends for a register, most significant first.
public static byte[] WordToBytes(ushort word)
Parameters
wordushortThe word.
Returns
- byte[]
The decoded value.
Exceptions
- PamojaException
The device data is not valid.