/**
 * Makecode package for the TSL2591 Light sensor.
 *
 * More details here: https://ams.com/documents/20143/36005/TSL2591_DS000338_6-00.pdf/090eb50d-bb18-5b45-4938-9b3672f86b80
 */ const TSL2591_I2C_ADDRESS = 0x29 ; const TSL2591_REGISTER_COMMAND = 0xA0 ; const TSL2591_REGISTER_COMMAND_SET_INT = 0xE4 ; const TSL2591_REGISTER_COMMAND_CLEAR_ALS_INT = 0xE6 ; const TSL2591_REGISTER_COMMAND_CLEAR_ALS_NO_PERS_INT = 0xE7 ; const TSL2591_REGISTER_COMMAND_CLEAR_NO_PERS_INT = 0xEA ; const TSL2591_REGISTER_ENABLE = 0x00 ; const TSL2591_REGISTER_NPIEN_ENABLE = 0x80 ; const TSL2591_REGISTER_SAI_ENABLE = 0x40 ; const TSL2591_REGISTER_AIEN_ENABLE = 0x10 ; const TSL2591_REGISTER_AEN_ENABLE = 0x02 ; const TSL2591_REGISTER_PON_ENABLE = 0x01 ; const TSL2591_REGISTER_POFF_ENABLE = 0x00 ; const TSL2591_REGISTER_CONTROL = 0x01 ; const TSL2591_REGISTER_CONTROL_SRESET = 0x80 ; const TSL2591_REGISTER_PID = 0x11 ; const TSL2591_REGISTER_ID = 0x12 ; const TSL2591_REGISTER_STATUS = 0x13 ; const TSL2591_REGISTER_STATUS_NPINTR = 0x20 ; const TSL2591_REGISTER_STATUS_AINT = 0x10 ; const TSL2591_REGISTER_STATUS_AVALID = 0x01 ; const TSL2591_REGISTER_C0DATAL = 0x14 ; const TSL2591_REGISTER_C0DATAH = 0x15 ; const TSL2591_REGISTER_C1DATAL = 0x16 ; const TSL2591_REGISTER_C1DATAH = 0x17 ; const TSL2591_REGISTER_AILTL = 0x04 ; const TSL2591_REGISTER_AILTH = 0x05 ; const TSL2591_REGISTER_AIHTL = 0x06 ; const TSL2591_REGISTER_AIHTH = 0x07 ; const TSL2591_REGISTER_NPAILTL = 0x08 ; const TSL2591_REGISTER_NPAILTH = 0x09 ; const TSL2591_REGISTER_NPAIHTL = 0x0A ; const TSL2591_REGISTER_NPAIHTH = 0x0B ; const TSL2591_REGISTER_PERSIST = 0x0B ; enum TSL2591_AGAIN { AGAIN_LOW = 0x00 , AGAIN_MEDIUM = 0x10 , AGAIN_HIGH = 0x20 , AGAIN_MAX = 0x30 } enum TSL2591_ATIME { ATIME_100_MS = 0x00 , ATIME_200_MS = 0x01 , ATIME_300_MS = 0x02 , ATIME_400_MS = 0x03 , ATIME_500_MS = 0x04 , ATIME_600_MS = 0x05 } namespace RegisterHelper { /**
     * Write register of the address location
     */ export function writeRegister ( addr , reg , dat ) { let _registerBuffer = pins . createBuffer ( 2 ) ; _registerBuffer [ 0 ] = reg ; _registerBuffer [ 1 ] = dat ; pins . i2cWriteBuffer ( addr , _registerBuffer ) ; } /**
     * Read a 8-byte register of the address location
     */ export function readRegister8 ( addr , reg ) { pins . i2cWriteNumber ( addr , reg , NumberFormat . UInt8BE ) ; return pins . i2cReadNumber ( addr , NumberFormat . UInt8BE ) ; } /**
     * Read a (UInt16) 16-byte register of the address location
     */ export function readRegisterUInt16 ( addr , reg ) { pins . i2cWriteNumber ( addr , reg , NumberFormat . UInt8BE ) ; return pins . i2cReadNumber ( addr , NumberFormat . UInt16LE ) ; } /**
     * Read a (Int16) 16-byte register of the address location
     */ export function readRegisterInt16 ( addr , reg ) { pins . i2cWriteNumber ( addr , reg , NumberFormat . UInt8BE ) ; return pins . i2cReadNumber ( addr , NumberFormat . Int16LE ) ; } } namespace TSL2591 { let TSL2591_I2C_ADDR = TSL2591_I2C_ADDRESS ; export let isConnected = false ; let atimeIntegrationValue ; let gainSensorValue ; export function initSensor ( ) { let device_id = RegisterHelper . readRegister8 ( TSL2591_I2C_ADDR , TSL2591_REGISTER_COMMAND | TSL2591_REGISTER_ID ) ; if ( device_id != 0x50 ) { isConnected = false ; } else isConnected = true ; } export function setATIME ( atime ) { atimeIntegrationValue = atime ; configureSensor ( atimeIntegrationValue , gainSensorValue ) ; } export function setGAIN ( gain ) { gainSensorValue = gain ; configureSensor ( atimeIntegrationValue , gainSensorValue ) ; } export function configureSensor ( atime , gain ) { while ( ! isConnected ) { initSensor ( ) ; } atimeIntegrationValue = atime ; gainSensorValue = gain ; enableSensor ( ) ; RegisterHelper . writeRegister ( TSL2591_I2C_ADDR , TSL2591_REGISTER_COMMAND | TSL2591_REGISTER_CONTROL , atimeIntegrationValue | gainSensorValue ) ; disableSensor ( ) ; } export function enableSensor ( ) { if ( isConnected ) RegisterHelper . writeRegister ( TSL2591_I2C_ADDR , TSL2591_REGISTER_COMMAND | TSL2591_REGISTER_ENABLE , TSL2591_REGISTER_PON_ENABLE | TSL2591_REGISTER_AEN_ENABLE | TSL2591_REGISTER_AIEN_ENABLE | TSL2591_REGISTER_NPIEN_ENABLE ) ; else return ; } export function disableSensor ( ) { if ( isConnected ) RegisterHelper . writeRegister ( TSL2591_I2C_ADDR , TSL2591_REGISTER_COMMAND | TSL2591_REGISTER_ENABLE , TSL2591_REGISTER_POFF_ENABLE ) ; else return ; } export function getRawLuminosity ( ) { while ( ! isConnected ) { initSensor ( ) ; } enableSensor ( ) ; for ( let x = 0 ; x < atimeIntegrationValue ; x ++ ) { basic . pause ( 120 ) ; } let yChannel ; yChannel = RegisterHelper . readRegisterUInt16 ( TSL2591_I2C_ADDR , TSL2591_REGISTER_COMMAND | TSL2591_REGISTER_C1DATAL ) ; yChannel <<= 16 ; yChannel = RegisterHelper . readRegisterUInt16 ( TSL2591_I2C_ADDR , TSL2591_REGISTER_COMMAND | TSL2591_REGISTER_C0DATAL ) ; disableSensor ( ) ; return yChannel ; } export function getFullSpectrum ( ) { let fs = getRawLuminosity ( ) ; return ( fs & 0xFFFF ) ; } export function getInfraredSpectrum ( ) { let is = getRawLuminosity ( ) ; return ( is >> 16 ) ; } export function getVisibleSpectrum ( ) { let vs = getRawLuminosity ( ) ; return ( ( vs & 0xFFFF ) - ( vs >> 16 ) ) ; } export function start ( atime , gain ) { while ( ! isConnected ) { initSensor ( ) ; } configureSensor ( atimeIntegrationValue , gainSensorValue ) ; disableSensor ( ) ; } } TSL2591 . start ( TSL2591_ATIME . ATIME_100_MS , TSL2591_AGAIN . AGAIN_MEDIUM ) ; basic . forever ( function ( ) { basic . pause ( 2000 ) ; serial . writeLine ( TSL2591 . isConnected + "" ) ; serial . writeLine ( TSL2591 . getRawLuminosity ( ) + "" ) ; } ) ;