Class library for a TCS3200 Color Sensor Module.

Dependents:   LCD PROJETO PROJETO_MECATRONICO Bottle_Tracker_Insper ... more

Committer:
grantphillips
Date:
Tue May 03 20:45:40 2016 +0000
Revision:
0:b98e768bc655
v1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
grantphillips 0:b98e768bc655 1 /* TCS3200 Library v1.0
grantphillips 0:b98e768bc655 2 * Copyright (c) 2016 Grant Phillips
grantphillips 0:b98e768bc655 3 * grant.phillips@nmmu.ac.za
grantphillips 0:b98e768bc655 4 *
grantphillips 0:b98e768bc655 5 *
grantphillips 0:b98e768bc655 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
grantphillips 0:b98e768bc655 7 * of this software and associated documentation files (the "Software"), to deal
grantphillips 0:b98e768bc655 8 * in the Software without restriction, including without limitation the rights
grantphillips 0:b98e768bc655 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
grantphillips 0:b98e768bc655 10 * copies of the Software, and to permit persons to whom the Software is
grantphillips 0:b98e768bc655 11 * furnished to do so, subject to the following conditions:
grantphillips 0:b98e768bc655 12 *
grantphillips 0:b98e768bc655 13 * The above copyright notice and this permission notice shall be included in
grantphillips 0:b98e768bc655 14 * all copies or substantial portions of the Software.
grantphillips 0:b98e768bc655 15 *
grantphillips 0:b98e768bc655 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
grantphillips 0:b98e768bc655 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
grantphillips 0:b98e768bc655 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
grantphillips 0:b98e768bc655 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
grantphillips 0:b98e768bc655 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
grantphillips 0:b98e768bc655 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
grantphillips 0:b98e768bc655 22 * THE SOFTWARE.
grantphillips 0:b98e768bc655 23 */
grantphillips 0:b98e768bc655 24
grantphillips 0:b98e768bc655 25 #ifndef TCS3200_H
grantphillips 0:b98e768bc655 26 #define TCS3200_H
grantphillips 0:b98e768bc655 27
grantphillips 0:b98e768bc655 28 #include "mbed.h"
grantphillips 0:b98e768bc655 29
grantphillips 0:b98e768bc655 30 /** Class library for a TCS3200 Color Sensor Module.
grantphillips 0:b98e768bc655 31 *
grantphillips 0:b98e768bc655 32 * Example:
grantphillips 0:b98e768bc655 33 * @code
grantphillips 0:b98e768bc655 34 * #include "mbed.h"
grantphillips 0:b98e768bc655 35 * #include "TCS3200.h"
grantphillips 0:b98e768bc655 36 *
grantphillips 0:b98e768bc655 37 * TCS3200 color(PD_8, PD_9, PD_10, PD_11, PA_1); //Create a TCS3200 object
grantphillips 0:b98e768bc655 38 * // S0 S1 S2 S3 OUT
grantphillips 0:b98e768bc655 39 *
grantphillips 0:b98e768bc655 40 * int main() {
grantphillips 0:b98e768bc655 41 * long red, green, blue, clear;
grantphillips 0:b98e768bc655 42 *
grantphillips 0:b98e768bc655 43 * //Set the scaling factor to 100%
grantphillips 0:b98e768bc655 44 * color.SetMode(TCS3200::SCALE_100);
grantphillips 0:b98e768bc655 45 *
grantphillips 0:b98e768bc655 46 * while(1){
grantphillips 0:b98e768bc655 47 * //Read the HIGH pulse width in nS for each color.
grantphillips 0:b98e768bc655 48 * //The lower the value, the more of that color is detected
grantphillips 0:b98e768bc655 49 * red = color.ReadRed();
grantphillips 0:b98e768bc655 50 * green = color.ReadGreen();
grantphillips 0:b98e768bc655 51 * blue = color.ReadBlue();
grantphillips 0:b98e768bc655 52 * clear = color.ReadClear();
grantphillips 0:b98e768bc655 53 *
grantphillips 0:b98e768bc655 54 * printf("RED: %10d GREEN: %10d BLUE: %10d CLEAR: %10d ", red, green, blue, clear);
grantphillips 0:b98e768bc655 55 *
grantphillips 0:b98e768bc655 56 * wait(0.1);
grantphillips 0:b98e768bc655 57 * }
grantphillips 0:b98e768bc655 58 * }
grantphillips 0:b98e768bc655 59 * @endcode
grantphillips 0:b98e768bc655 60 */
grantphillips 0:b98e768bc655 61
grantphillips 0:b98e768bc655 62 class TCS3200 {
grantphillips 0:b98e768bc655 63 public:
grantphillips 0:b98e768bc655 64 enum TCS3200Mode {
grantphillips 0:b98e768bc655 65 POWERDOWN = 0,
grantphillips 0:b98e768bc655 66 SCALE_2 = 1,
grantphillips 0:b98e768bc655 67 SCALE_20 = 2,
grantphillips 0:b98e768bc655 68 SCALE_100 = 3,
grantphillips 0:b98e768bc655 69 };
grantphillips 0:b98e768bc655 70
grantphillips 0:b98e768bc655 71 /** Create a TCS3200 object connected to the specified pins.
grantphillips 0:b98e768bc655 72 * @param S0 Frequency scaling output pin S0
grantphillips 0:b98e768bc655 73 * @param S1 Frequency scaling output pin S1
grantphillips 0:b98e768bc655 74 * @param S2 Photo diode selection output pin S2
grantphillips 0:b98e768bc655 75 * @param S3 Photo diode selection output pin S3
grantphillips 0:b98e768bc655 76 * @param OUT Frequency input pin
grantphillips 0:b98e768bc655 77 */
grantphillips 0:b98e768bc655 78 TCS3200(PinName S0, PinName S1, PinName S2, PinName S3, PinName OUT);
grantphillips 0:b98e768bc655 79
grantphillips 0:b98e768bc655 80 /** Reads the output signal's HIGH pulse for RED.
grantphillips 0:b98e768bc655 81 * @param
grantphillips 0:b98e768bc655 82 * None
grantphillips 0:b98e768bc655 83 * @return
grantphillips 0:b98e768bc655 84 * Duration as nanoseconds (ns).
grantphillips 0:b98e768bc655 85 */
grantphillips 0:b98e768bc655 86 long ReadRed();
grantphillips 0:b98e768bc655 87
grantphillips 0:b98e768bc655 88 /** Reads the output signal's HIGH pulse for GREEN.
grantphillips 0:b98e768bc655 89 * @param
grantphillips 0:b98e768bc655 90 * None
grantphillips 0:b98e768bc655 91 * @return
grantphillips 0:b98e768bc655 92 * Duration as nanoseconds (ns).
grantphillips 0:b98e768bc655 93 */
grantphillips 0:b98e768bc655 94 long ReadGreen();
grantphillips 0:b98e768bc655 95
grantphillips 0:b98e768bc655 96 /** Reads the output signal's HIGH pulse for BLUE.
grantphillips 0:b98e768bc655 97 * @param
grantphillips 0:b98e768bc655 98 * None
grantphillips 0:b98e768bc655 99 * @return
grantphillips 0:b98e768bc655 100 * Duration as nanoseconds (ns).
grantphillips 0:b98e768bc655 101 */
grantphillips 0:b98e768bc655 102 long ReadBlue();
grantphillips 0:b98e768bc655 103
grantphillips 0:b98e768bc655 104 /** Reads the output signal's HIGH pulse for CLEAR.
grantphillips 0:b98e768bc655 105 * @param
grantphillips 0:b98e768bc655 106 * None
grantphillips 0:b98e768bc655 107 * @return
grantphillips 0:b98e768bc655 108 * Duration as nanoseconds (ns).
grantphillips 0:b98e768bc655 109 */
grantphillips 0:b98e768bc655 110 long ReadClear();
grantphillips 0:b98e768bc655 111
grantphillips 0:b98e768bc655 112 /** Sets the mode of operation.
grantphillips 0:b98e768bc655 113 * @param
grantphillips 0:b98e768bc655 114 * mode POWERDOWN, SCALE_2 (2% scaling), SCALE_20 (20% scaling), SCALE_100 (100% scaling).
grantphillips 0:b98e768bc655 115 * @return
grantphillips 0:b98e768bc655 116 * None
grantphillips 0:b98e768bc655 117 */
grantphillips 0:b98e768bc655 118 void SetMode(TCS3200Mode mode);
grantphillips 0:b98e768bc655 119
grantphillips 0:b98e768bc655 120 private:
grantphillips 0:b98e768bc655 121 DigitalOut mS0;
grantphillips 0:b98e768bc655 122 DigitalOut mS1;
grantphillips 0:b98e768bc655 123 DigitalOut mS2;
grantphillips 0:b98e768bc655 124 DigitalOut mS3;
grantphillips 0:b98e768bc655 125 InterruptIn signal;
grantphillips 0:b98e768bc655 126 Timer timer;
grantphillips 0:b98e768bc655 127 long pulsewidth;
grantphillips 0:b98e768bc655 128 void HighTrigger();
grantphillips 0:b98e768bc655 129 void LowTrigger();
grantphillips 0:b98e768bc655 130 };
grantphillips 0:b98e768bc655 131
grantphillips 0:b98e768bc655 132 #endif