Class TCS3200

java.lang.Object
com.diozero.devices.sandpit.TCS3200
All Implemented Interfaces:
InputEventListener<DigitalInputEvent>, java.io.Closeable, java.lang.AutoCloseable, java.lang.Runnable

public class TCS3200
extends java.lang.Object
implements InputEventListener<DigitalInputEvent>, java.lang.Runnable, java.io.Closeable

This class reads RGB values from a TCS3200 colour sensor. Connections:

 GND   Ground.
 VDD   Supply Voltage (2.7-5.5V)
 /OE   Output enable, active low. When OE is high OUT is disabled
       allowing multiple sensors to share the same OUT line.
 OUT   Output frequency square wave.
 S0/S1 Output frequency scale selection.
 S2/S3 Colour filter selection.
 
  • OUT is a square wave whose frequency is proprtional to the intensity of the selected filter colour.
  • S2/S3 selects between red, green, blue, and no filter.
  • S0/S1 scales the frequency at 100%, 20%, 2% or off.

To take a reading the colour filters are selected in turn for a fraction of a second and the frequency is read and converted to Hz.

  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static class  TCS3200.Filter  
    static class  TCS3200.Frequency  
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static int NOT_SET  
  • Constructor Summary

    Constructors 
    Constructor Description
    TCS3200​(int outGpio, int s2Gpio, int s3Gpio)  
    TCS3200​(int outGpio, int s2Gpio, int s3Gpio, int s0Gpio, int s1Gpio, int oeGpio)  
  • Method Summary

    Modifier and Type Method Description
    void close()  
    int[] getBlackLevel()
    Get the black level calibration
    TCS3200.Frequency getFrequency()
    Get the current frequency scaling
    int[] getHertz()
    Get the latest hertz reading
    int[] getRgb()
    Get the latest RGB reading.
    int getSampleSize()
    Get the sample size
    double getUpdateInterval()
    Get the interval between RGB updates
    int[] getWhiteLevel()
    Get the white level calibration
    void pause()
    Pause reading (until a call to resume).
    void resume()
    Resume reading (after a call to pause)
    void run()  
    void setBlackLevel​(int[] rgb)
    Set the black level calibration
    void setFilter​(TCS3200.Filter f)
    Set the colour to be sampled.
    void setFrequency​(TCS3200.Frequency f)
    Set the frequency scaling.
    void setSampleSize​(int samples)
    Set the sample size (number of frequency cycles to accumulate)
    void setUpdateInterval​(double interval)
    Set the interval between RGB updates
    void setWhiteLevel​(int[] rgb)
    Set the white level calibration
    void valueChanged​(DigitalInputEvent event)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • TCS3200

      public TCS3200​(int outGpio, int s2Gpio, int s3Gpio)
    • TCS3200

      public TCS3200​(int outGpio, int s2Gpio, int s3Gpio, int s0Gpio, int s1Gpio, int oeGpio)
  • Method Details

    • getRgb

      public int[] getRgb()
      Get the latest RGB reading. The raw colour hertz readings are converted to RGB values as follows: RGB = 255 * (Fv - Fb) / (Fw - Fb) Where Fv is the sampled hertz, Fw is the calibrated white hertz, and Fb is the calibrated black hertz.
      Returns:
      RGB values constrained to be between 0 and 255
    • getHertz

      public int[] getHertz()
      Get the latest hertz reading
      Returns:
      latest hertz reading
    • getBlackLevel

      public int[] getBlackLevel()
      Get the black level calibration
      Returns:
      black level calibration
    • setBlackLevel

      public void setBlackLevel​(int[] rgb)
      Set the black level calibration
      Parameters:
      rgb - new black levels
    • getWhiteLevel

      public int[] getWhiteLevel()
      Get the white level calibration
      Returns:
      white level calibration
    • setWhiteLevel

      public void setWhiteLevel​(int[] rgb)
      Set the white level calibration
      Parameters:
      rgb - new white levels
    • getFrequency

      public TCS3200.Frequency getFrequency()
      Get the current frequency scaling
      Returns:
      the current frequency scaling
    • setFrequency

      public void setFrequency​(TCS3200.Frequency f)
      Set the frequency scaling.
       f  S0  S1  Frequency scaling
       0  L   L   Off
       1  L   H   2%
       2  H   L   20%
       3  H   H   100%
       
      Parameters:
      f - Sampling frequency
    • getUpdateInterval

      public double getUpdateInterval()
      Get the interval between RGB updates
      Returns:
      the interval between RGB updates
    • setUpdateInterval

      public void setUpdateInterval​(double interval)
      Set the interval between RGB updates
      Parameters:
      interval - the interval between RGB updates ( 0.1 <= interval < 2.0)
    • getSampleSize

      public int getSampleSize()
      Get the sample size
      Returns:
      the sample size
    • setSampleSize

      public void setSampleSize​(int samples)
      Set the sample size (number of frequency cycles to accumulate)
      Parameters:
      samples - the sample size
    • pause

      public void pause()
      Pause reading (until a call to resume).
    • resume

      public void resume()
      Resume reading (after a call to pause)
    • setFilter

      public void setFilter​(TCS3200.Filter f)
      Set the colour to be sampled. f S2 S3 Photodiode 0 L L Red 1 H H Green 2 L H Blue 3 H L Clear (no filter)
      Parameters:
      f - Filter
    • valueChanged

      public void valueChanged​(DigitalInputEvent event)
      Specified by:
      valueChanged in interface InputEventListener<DigitalInputEvent>
    • run

      public void run()
      Specified by:
      run in interface java.lang.Runnable
    • close

      public void close() throws java.io.IOException
      Specified by:
      close in interface java.lang.AutoCloseable
      Specified by:
      close in interface java.io.Closeable
      Throws:
      java.io.IOException