B4R Code Snippet TCS34725 RGB Sensor interface with Wemos mini...

I was able to interface the TCS 34725 sensor with ESP8266 via inline C code and the Adafruit library attached herewith:
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public RED, BLUE,GREEN,CLEAR,COLOR_TEMP,LUX As Long
    Dim r,b,g As Float
    Private Timer1 As Timer
   
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    RunNative("setup",Null)
   
   
    Timer1.Initialize("Timer1_Tick", 1000)
    Timer1.Enabled = True
   
End Sub


Private Sub Timer1_Tick
  
    RunNative("read",Null)
    r=r/CLEAR
    r=r*256
    g=g/CLEAR
    g=g*256
    b=b/CLEAR
    b=b*256
    RED=r
    GREEN=g
    BLUE=b
    Log("R:",RED)
    Log("G:",GREEN)
    Log("B:",BLUE)
    Log("CLEAR:",CLEAR)
    Log("COLOR TEMP:",COLOR_TEMP," K")
    Log("LUX:",LUX)
End Sub


#if C
#include <Wire.h>
#include "Adafruit_TCS34725.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_60X);
void setup(B4R::Object* o){
tcs.begin();

}
  
void read (B4R::Object* o) {
   uint16_t r, g, b, c, colorTemp, lux;
  
  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature(r, g, b);
  lux = tcs.calculateLux(r, g, b);
  
  b4r_main::_r=r;
  b4r_main::_g=g;
  b4r_main::_b=b;
  b4r_main::_clear=c;
  b4r_main::_color_temp=colorTemp;
  b4r_main::_lux=lux;
}

#End if
 

Attachments

  • Adafruit_TCS34725.zip
    16 KB · Views: 384
Top