/**
 * Makecode package for the TCS34725 Color sensor.
 *
 * More details here: https://ams.com/documents/20143/36005/TCS3472_DS000390_2-00.pdf/6e452176-2407-faaf-a590-d526c78c7432
 */ const TCS34725_I2C_ADDRESS = 0x29 ; const TCS34725_REGISTER_COMMAND = 0x80 ; const TCS34725_REGISTER_ENABLE = 0x00 ; const TCS34725_REGISTER_AIEN_ENABLE = 0x10 ; const TCS34725_REGISTER_WEN_ENABLE = 0x08 ; const TCS34725_REGISTER_AEN_ENABLE = 0x02 ; const TCS34725_REGISTER_PON_ENABLE = 0x01 ; const TCS34725_REGISTER_ATIME = 0x01 ; const TCS34725_REGISTER_WTIME = 0x03 ; const TCS34725_REGISTER_AILTL = 0x04 ; const TCS34725_REGISTER_AILTH = 0x05 ; const TCS34725_REGISTER_AIHTL = 0x06 ; const TCS34725_REGISTER_AIHTH = 0x07 ; const TCS34725_REGISTER_PERS = 0x0C ; const TCS34725_REGISTER_CONFIG = 0x0D ; const TCS34725_REGISTER_CONFIG_WLONG = 0x02 ; const TCS34725_REGISTER_CONTROL = 0x0F ; const TCS34725_REGISTER_ID = 0x12 ; const TCS34725_REGISTER_STATUS = 0x13 ; const TCS34725_REGISTER_STATUS_AINT = 0x10 ; const TCS34725_REGISTER_STATUS_AVALID = 0x01 ; const TCS34725_REGISTER_CDATAL = 0x14 ; const TCS34725_REGISTER_CDATAH = 0x15 ; const TCS34725_REGISTER_RDATAL = 0x16 ; const TCS34725_REGISTER_RDATAH = 0x17 ; const TCS34725_REGISTER_GDATAL = 0x18 ; const TCS34725_REGISTER_GDATAH = 0x19 ; const TCS34725_REGISTER_BDATAL = 0x1A ; const TCS34725_REGISTER_BDATAH = 0x1B ; enum TCS34725_ATIME { ATIME_2_4_MS = 0xFF , ATIME_24_MS = 0xF6 , ATIME_100_MS = 0xD5 , ATIME_154_MS = 0xC0 , ATIME_700_MS = 0x00 } enum TCS34725_WTIME { WTIME_2_4_MS = 0xFF , WTIME_204_MS = 0xAB , WTIME_614_MS = 0x00 } enum TCS34725_APERS { APERS_0_CLEAR = 0b0000 , APERS_1_CLEAR = 0b0001 , APERS_2_CLEAR = 0b0010 , APERS_3_CLEAR = 0b0011 , APERS_5_CLEAR = 0b0100 , APERS_10_CLEAR = 0b0101 , APERS_15_CLEAR = 0b0110 , APERS_20_CLEAR = 0b0111 , APERS_25_CLEAR = 0b1000 , APERS_30_CLEAR = 0b1001 , APERS_35_CLEAR = 0b1010 , APERS_40_CLEAR = 0b1011 , APERS_45_CLEAR = 0b1100 , APERS_50_CLEAR = 0b1101 , APERS_55_CLEAR = 0b1110 , APERS_60_CLEAR = 0b1111 } enum TCS34725_AGAIN { AGAIN_1X = 0x0 , AGAIN_4X = 0x1 , AGAIN_16X = 0x2 , AGAIN_60X = 0x3 } 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 TCS34725 { let TCS34725_I2C_ADDR = TCS34725_I2C_ADDRESS ; export let isConnected = false ; let atimeIntegrationValue = 0 ; let gainSensorValue = 0 ; export function initSensor ( ) { let device_id = RegisterHelper . readRegister8 ( TCS34725_I2C_ADDRESS , TCS34725_REGISTER_COMMAND | TCS34725_REGISTER_ID ) ; if ( ( device_id != 0x44 ) && ( device_id != 0x10 ) ) { isConnected = false ; } else isConnected = true ; } export function turnSensorOn ( atime ) { RegisterHelper . writeRegister ( TCS34725_I2C_ADDR , TCS34725_REGISTER_COMMAND | TCS34725_REGISTER_ENABLE , TCS34725_REGISTER_PON_ENABLE ) ; basic . pause ( 300 ) ; RegisterHelper . writeRegister ( TCS34725_I2C_ADDR , TCS34725_REGISTER_COMMAND | TCS34725_REGISTER_ENABLE , TCS34725_REGISTER_PON_ENABLE | TCS34725_REGISTER_AEN_ENABLE ) ; pauseSensorForIntegrationTime ( atime ) ; } export function pauseSensorForIntegrationTime ( atime ) { switch ( atime ) { case TCS34725_ATIME . ATIME_2_4_MS : { basic . pause ( 2.4 ) ; break ; } case TCS34725_ATIME . ATIME_24_MS : { basic . pause ( 24 ) ; break ; } case TCS34725_ATIME . ATIME_100_MS : { basic . pause ( 100 ) ; break ; } case TCS34725_ATIME . ATIME_154_MS : { basic . pause ( 154 ) ; break ; } case TCS34725_ATIME . ATIME_700_MS : { basic . pause ( 700 ) ; break ; } } } export function turnSensorOff ( ) { let sensorReg = RegisterHelper . readRegister8 ( TCS34725_I2C_ADDR , TCS34725_REGISTER_COMMAND | TCS34725_REGISTER_ENABLE ) ; RegisterHelper . writeRegister ( TCS34725_I2C_ADDR , TCS34725_REGISTER_COMMAND | TCS34725_REGISTER_ENABLE , sensorReg & ~ ( TCS34725_REGISTER_PON_ENABLE | TCS34725_REGISTER_AEN_ENABLE ) ) ; } export function setATIMEintegration ( atime ) { if ( ! isConnected ) initSensor ( ) ; RegisterHelper . writeRegister ( TCS34725_I2C_ADDR , TCS34725_REGISTER_COMMAND | TCS34725_REGISTER_ATIME , atime ) ; atimeIntegrationValue = atime ; } export function setGAINsensor ( gain ) { if ( ! isConnected ) initSensor ( ) ; RegisterHelper . writeRegister ( TCS34725_I2C_ADDR , TCS34725_REGISTER_COMMAND | TCS34725_REGISTER_CONTROL , gain ) ; gainSensorValue = gain ; } export function start ( atime , gain ) { while ( ! isConnected ) { initSensor ( ) ; } setATIMEintegration ( atime ) ; setGAINsensor ( gain ) ; turnSensorOn ( atime ) ; } export type RGBC = { red : number ; green : number ; blue : number ; clear : number ; } ; export function getSensorRGB ( ) { while ( ! isConnected ) { initSensor ( ) ; } let redColorValue = RegisterHelper . readRegisterUInt16 ( TCS34725_I2C_ADDR , TCS34725_REGISTER_COMMAND | TCS34725_REGISTER_RDATAL ) ; let greenColorValue = RegisterHelper . readRegisterUInt16 ( TCS34725_I2C_ADDR , TCS34725_REGISTER_COMMAND | TCS34725_REGISTER_GDATAL ) ; let blueColorValue = RegisterHelper . readRegisterUInt16 ( TCS34725_I2C_ADDR , TCS34725_REGISTER_COMMAND | TCS34725_REGISTER_BDATAL ) ; let clearColorValue = RegisterHelper . readRegisterUInt16 ( TCS34725_I2C_ADDR , TCS34725_REGISTER_COMMAND | TCS34725_REGISTER_CDATAL ) ; pauseSensorForIntegrationTime ( atimeIntegrationValue ) ; let sum = clearColorValue ; let r = 0 ; let g = 0 ; let b = 0 ; if ( clearColorValue == 0 ) { return { red : 0 , green : 0 , blue : 0 , clear : 0 } ; } else { r = redColorValue / sum * 255 ; g = greenColorValue / sum * 255 ; b = blueColorValue / sum * 255 ; return { red : r , green : g , blue : b , clear : clearColorValue } ; } } } let strip = null ; strip = neopixel . create ( DigitalPin . P1 , 30 , NeoPixelMode . RGB ) ; strip . setBrightness ( 255 ) ; TCS34725 . start ( TCS34725_ATIME . ATIME_2_4_MS , TCS34725_AGAIN . AGAIN_1X ) ; basic . forever ( function ( ) { basic . pause ( 2000 ) ; serial . writeLine ( TCS34725 . isConnected + "" ) ; let a = TCS34725 . getSensorRGB ( ) ; strip . showColor ( neopixel . rgb ( a . red , a . green , a . blue ) ) ; } ) ;