Grant Phillips / TCS3200

Dependents:   LCD PROJETO PROJETO_MECATRONICO Bottle_Tracker_Insper ... more

Files at this revision

API Documentation at this revision

Comitter:
grantphillips
Date:
Tue May 03 20:45:40 2016 +0000
Commit message:
v1.0

Changed in this revision

TCS3200.cpp Show annotated file Show diff for this revision Revisions of this file
TCS3200.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r b98e768bc655 TCS3200.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TCS3200.cpp	Tue May 03 20:45:40 2016 +0000
@@ -0,0 +1,72 @@
+#include "TCS3200.h"
+#include "mbed.h"
+ 
+ 
+TCS3200::TCS3200(PinName S0, PinName S1, PinName S2, PinName S3, PinName OUT) : 
+mS0(S0), mS1(S1), mS2(S2), mS3(S3), signal(OUT) 
+{
+    SetMode(SCALE_100);
+    signal.rise(this,&TCS3200::HighTrigger);
+    signal.fall(this,&TCS3200::LowTrigger);
+}
+ 
+long TCS3200::ReadRed() {
+    mS2=0;                    
+    mS3=0;
+    wait(0.1);     //Delay to allow frequency to change for the set color
+    return(pulsewidth);
+}
+
+long TCS3200::ReadGreen() {
+    mS2=1;                    
+    mS3=1;
+    wait(0.1);     //Delay to allow frequency to change for the set color
+    return(pulsewidth);
+}
+
+long TCS3200::ReadBlue() {
+    mS2=0;                    
+    mS3=1;
+    wait(0.1);     //Delay to allow frequency to change for the set color
+    return(pulsewidth);
+}
+
+long TCS3200::ReadClear() {
+    mS2=1;                    
+    mS3=0;
+    wait(0.1);     //Delay to allow frequency to change for the set color
+    return(pulsewidth);
+}
+
+void TCS3200::SetMode(TCS3200Mode mode) {
+    if(mode == POWERDOWN) {         //TCS3200 in power down
+        mS0 = 0;                     
+        mS1 = 0;
+    }
+    else if(mode == SCALE_2) {      //Output frequency at 2% scaling
+        mS0 = 0;                     
+        mS1 = 1;
+    }
+    else if(mode == SCALE_20) {     //Output frequency at 20% scaling
+        mS0 = 1;                     
+        mS1 = 0;
+    }
+    else if(mode == SCALE_100) {    //Output frequency at 100% scaling
+        mS0 = 1;                     
+        mS1 = 1;
+    }
+    else {                          //default is POWERDOWN                
+        mS0 = 0;                     
+        mS1 = 0;
+    }
+}
+
+void TCS3200::HighTrigger() {
+    timer.start();
+}
+
+void TCS3200::LowTrigger() {
+    timer.stop();
+    pulsewidth = timer.read_us();
+    timer.reset();
+}
\ No newline at end of file
diff -r 000000000000 -r b98e768bc655 TCS3200.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TCS3200.h	Tue May 03 20:45:40 2016 +0000
@@ -0,0 +1,132 @@
+/* TCS3200 Library v1.0
+ * Copyright (c) 2016 Grant Phillips
+ * grant.phillips@nmmu.ac.za
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+ 
+#ifndef TCS3200_H
+#define TCS3200_H
+ 
+#include "mbed.h"
+
+/** Class library for a TCS3200 Color Sensor Module.
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "TCS3200.h"
+ *
+ * TCS3200 color(PD_8, PD_9, PD_10, PD_11, PA_1);  //Create a TCS3200 object 
+ * //            S0    S1    S2     S3     OUT
+ * 
+ * int main() {
+ *     long red, green, blue, clear;
+ *    
+ *     //Set the scaling factor to 100%
+ *     color.SetMode(TCS3200::SCALE_100);        
+ *
+ *     while(1){
+ *         //Read the HIGH pulse width in nS for each color.
+ *         //The lower the value, the more of that color is detected 
+ *         red = color.ReadRed();
+ *         green = color.ReadGreen();
+ *         blue = color.ReadBlue();
+ *         clear = color.ReadClear();
+ *
+ *         printf("RED: %10d     GREEN: %10d     BLUE: %10d     CLEAR: %10d     ", red, green, blue, clear);
+ *            
+ *         wait(0.1);
+ *     }
+ * }
+ * @endcode
+ */
+ 
+class TCS3200 {
+  public:
+    enum TCS3200Mode {
+        POWERDOWN = 0,
+        SCALE_2 = 1,
+        SCALE_20 = 2,
+        SCALE_100 = 3,
+    };
+    
+    /** Create a TCS3200 object connected to the specified pins.
+    * @param S0 Frequency scaling output pin S0
+    * @param S1 Frequency scaling output pin S1
+    * @param S2 Photo diode selection output pin S2
+    * @param S3 Photo diode selection output pin S3
+    * @param OUT Frequency input pin
+    */
+    TCS3200(PinName S0, PinName S1, PinName S2, PinName S3, PinName OUT);
+    
+    /** Reads the output signal's HIGH pulse for RED.
+    * @param 
+    *     None
+    * @return 
+    *     Duration as nanoseconds (ns).
+    */
+    long ReadRed();
+    
+    /** Reads the output signal's HIGH pulse for GREEN.
+    * @param 
+    *     None
+    * @return 
+    *     Duration as nanoseconds (ns).
+    */
+    long ReadGreen();
+    
+    /** Reads the output signal's HIGH pulse for BLUE.
+    * @param 
+    *     None
+    * @return 
+    *     Duration as nanoseconds (ns).
+    */
+    long ReadBlue();
+    
+    /** Reads the output signal's HIGH pulse for CLEAR.
+    * @param 
+    *     None
+    * @return 
+    *     Duration as nanoseconds (ns).
+    */
+    long ReadClear();
+    
+    /** Sets the mode of operation. 
+    * @param 
+    *     mode POWERDOWN, SCALE_2 (2% scaling), SCALE_20 (20% scaling), SCALE_100 (100% scaling).
+    * @return 
+    *     None
+    */
+    void SetMode(TCS3200Mode mode);
+    
+  private:
+    DigitalOut mS0;
+    DigitalOut mS1;
+    DigitalOut mS2;
+    DigitalOut mS3;
+    InterruptIn signal;
+    Timer timer;
+    long pulsewidth;
+    void HighTrigger();
+    void LowTrigger();
+};
+ 
+#endif
\ No newline at end of file