Table of Contents

Class Opt3001

Namespace
Pamoja.Sensors
Assembly
Pamoja.Sensors.dll

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

bus I2cBus

The bus the part is on.

address byte

One of the Address constants, by where ADDR is tied.

conversionTime Opt3001.ConversionTime

How long each conversion integrates.

range byte

A 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

byte

AddressScl

The address with ADDR tied to SCL.

public const byte AddressScl = 71

Field Value

byte

AddressSda

The address with ADDR tied to SDA.

public const byte AddressSda = 70

Field Value

byte

AddressVdd

The address with ADDR tied to VDD.

public const byte AddressVdd = 69

Field Value

byte

RangeAutomatic

The range number that lets the part choose its own full-scale range.

public const byte RangeAutomatic = 12

Field Value

byte

RangeMax

The highest fixed range number, 83865.60 lux full scale.

public const byte RangeMax = 11

Field Value

byte

Properties

Configuration

The configuration the driver writes, with the part in shutdown.

public PamojaOpt3001Config Configuration { get; }

Property Value

PamojaOpt3001Config

Methods

ConfigBits(PamojaOpt3001Config)

Assembles the 16-bit OPT3001 configuration register value.

public static ushort ConfigBits(PamojaOpt3001Config config)

Parameters

config PamojaOpt3001Config

The 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

bits ushort

The 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

longConversion bool

The 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

code byte

The 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

rangeNumber byte

The 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

rangeNumber byte

The 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

exponent byte

The 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

raw ushort

The 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

raw ushort

The 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

milliLux uint

The 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

lowMilliLux uint

The low limit, in millilux.

highMilliLux uint

The high limit, in millilux.

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

bytes ReadOnlySpan<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

word ushort

The word.

Returns

byte[]

The decoded value.

Exceptions

PamojaException

The device data is not valid.