Grove I2C Colour Sensor http://www.seeedstudio.com/depot/Grove-I2C-Color-Sensor-p-854.html?cPath=25_27
Dependents: CapCouleur FindTheColor
There's a BluetoothLowEnergy demo around this sensor: http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_GroveColourSensor/
GroveColourSensor.hpp@0:56d6b711b8c7, 2014-07-02 (annotated)
- Committer:
- rgrover1
- Date:
- Wed Jul 02 08:50:07 2014 +0000
- Revision:
- 0:56d6b711b8c7
Grove I2C Colour Sensor; http://www.seeedstudio.com/depot/Grove-I2C-Color-Sensor-p-854.html?cPath=25_27
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:56d6b711b8c7 | 1 | /* |
rgrover1 | 0:56d6b711b8c7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 0:56d6b711b8c7 | 3 | * |
rgrover1 | 0:56d6b711b8c7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:56d6b711b8c7 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:56d6b711b8c7 | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:56d6b711b8c7 | 7 | * |
rgrover1 | 0:56d6b711b8c7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:56d6b711b8c7 | 9 | * |
rgrover1 | 0:56d6b711b8c7 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:56d6b711b8c7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:56d6b711b8c7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:56d6b711b8c7 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:56d6b711b8c7 | 14 | * limitations under the License. |
rgrover1 | 0:56d6b711b8c7 | 15 | */ |
rgrover1 | 0:56d6b711b8c7 | 16 | |
rgrover1 | 0:56d6b711b8c7 | 17 | #ifndef __GROVE_COLOUR_SENSOR_HPP__ |
rgrover1 | 0:56d6b711b8c7 | 18 | #define __GROVE_COLOUR_SENSOR_HPP__ |
rgrover1 | 0:56d6b711b8c7 | 19 | |
rgrover1 | 0:56d6b711b8c7 | 20 | #include "mbed.h" |
rgrover1 | 0:56d6b711b8c7 | 21 | |
rgrover1 | 0:56d6b711b8c7 | 22 | /** |
rgrover1 | 0:56d6b711b8c7 | 23 | * This module is based on the color sensor TCS3414CS with digital output over I2C. |
rgrover1 | 0:56d6b711b8c7 | 24 | * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_I2C_Color_Sensor_v0.9b |
rgrover1 | 0:56d6b711b8c7 | 25 | * |
rgrover1 | 0:56d6b711b8c7 | 26 | * example use: |
rgrover1 | 0:56d6b711b8c7 | 27 | * |
rgrover1 | 0:56d6b711b8c7 | 28 | * GroveColourSensor colorSensor(I2C_SDA0, I2C_SCL0); |
rgrover1 | 0:56d6b711b8c7 | 29 | * |
rgrover1 | 0:56d6b711b8c7 | 30 | * colorSensor.powerUp(); |
rgrover1 | 0:56d6b711b8c7 | 31 | * unsigned colour; |
rgrover1 | 0:56d6b711b8c7 | 32 | * for (colour = GroveColourSensor::GREEN; colour < GroveColourSensor::NUM_COLORS; colour++) { |
rgrover1 | 0:56d6b711b8c7 | 33 | * uint16_t colourValue = colorSensor.readColour(colour); |
rgrover1 | 0:56d6b711b8c7 | 34 | * ... |
rgrover1 | 0:56d6b711b8c7 | 35 | * } |
rgrover1 | 0:56d6b711b8c7 | 36 | * colorSensor.powerDown(); |
rgrover1 | 0:56d6b711b8c7 | 37 | */ |
rgrover1 | 0:56d6b711b8c7 | 38 | |
rgrover1 | 0:56d6b711b8c7 | 39 | class GroveColourSensor { |
rgrover1 | 0:56d6b711b8c7 | 40 | public: |
rgrover1 | 0:56d6b711b8c7 | 41 | enum Colour_t { |
rgrover1 | 0:56d6b711b8c7 | 42 | GREEN = 0, |
rgrover1 | 0:56d6b711b8c7 | 43 | RED, |
rgrover1 | 0:56d6b711b8c7 | 44 | BLUE, |
rgrover1 | 0:56d6b711b8c7 | 45 | CLEAR, |
rgrover1 | 0:56d6b711b8c7 | 46 | NUM_COLORS |
rgrover1 | 0:56d6b711b8c7 | 47 | }; |
rgrover1 | 0:56d6b711b8c7 | 48 | |
rgrover1 | 0:56d6b711b8c7 | 49 | GroveColourSensor(PinName sda, PinName scl) : i2c(sda, scl) { |
rgrover1 | 0:56d6b711b8c7 | 50 | /* empty*/ |
rgrover1 | 0:56d6b711b8c7 | 51 | } |
rgrover1 | 0:56d6b711b8c7 | 52 | |
rgrover1 | 0:56d6b711b8c7 | 53 | void powerUp(void) { |
rgrover1 | 0:56d6b711b8c7 | 54 | static const char powerUpCommand[] = {0x80, 0x03}; |
rgrover1 | 0:56d6b711b8c7 | 55 | /* turn on the color sensor */ |
rgrover1 | 0:56d6b711b8c7 | 56 | if (i2c.write((SEVEN_BIT_ADDRESS << 1), powerUpCommand, sizeof(powerUpCommand)) != 0) { |
rgrover1 | 0:56d6b711b8c7 | 57 | error("failed to power up the sensor"); |
rgrover1 | 0:56d6b711b8c7 | 58 | } |
rgrover1 | 0:56d6b711b8c7 | 59 | } |
rgrover1 | 0:56d6b711b8c7 | 60 | |
rgrover1 | 0:56d6b711b8c7 | 61 | void powerDown(void) { |
rgrover1 | 0:56d6b711b8c7 | 62 | static const char powerDownCommand[] = {0x80, 0x00}; |
rgrover1 | 0:56d6b711b8c7 | 63 | /* turn on the color sensor */ |
rgrover1 | 0:56d6b711b8c7 | 64 | if (i2c.write((SEVEN_BIT_ADDRESS << 1), powerDownCommand, sizeof(powerDownCommand)) != 0) { |
rgrover1 | 0:56d6b711b8c7 | 65 | error("failed to power down the sensor"); |
rgrover1 | 0:56d6b711b8c7 | 66 | } |
rgrover1 | 0:56d6b711b8c7 | 67 | } |
rgrover1 | 0:56d6b711b8c7 | 68 | |
rgrover1 | 0:56d6b711b8c7 | 69 | uint16_t readColour(Colour_t colour) { |
rgrover1 | 0:56d6b711b8c7 | 70 | char readColorRegistersCommand = 0xb0 + (2 * static_cast<unsigned>(colour)); |
rgrover1 | 0:56d6b711b8c7 | 71 | i2c.write((SEVEN_BIT_ADDRESS << 1), &readColorRegistersCommand, 1 /* size */); |
rgrover1 | 0:56d6b711b8c7 | 72 | |
rgrover1 | 0:56d6b711b8c7 | 73 | uint16_t colourValue; |
rgrover1 | 0:56d6b711b8c7 | 74 | i2c.read((SEVEN_BIT_ADDRESS << 1), reinterpret_cast<char *>(&colourValue), sizeof(uint16_t)); |
rgrover1 | 0:56d6b711b8c7 | 75 | return colourValue; |
rgrover1 | 0:56d6b711b8c7 | 76 | } |
rgrover1 | 0:56d6b711b8c7 | 77 | |
rgrover1 | 0:56d6b711b8c7 | 78 | uint16_t readColour(unsigned colour) { |
rgrover1 | 0:56d6b711b8c7 | 79 | if (colour >= NUM_COLORS) { |
rgrover1 | 0:56d6b711b8c7 | 80 | return 0; |
rgrover1 | 0:56d6b711b8c7 | 81 | } |
rgrover1 | 0:56d6b711b8c7 | 82 | |
rgrover1 | 0:56d6b711b8c7 | 83 | return readColour(static_cast<Colour_t>(colour)); |
rgrover1 | 0:56d6b711b8c7 | 84 | } |
rgrover1 | 0:56d6b711b8c7 | 85 | |
rgrover1 | 0:56d6b711b8c7 | 86 | private: |
rgrover1 | 0:56d6b711b8c7 | 87 | static const uint8_t SEVEN_BIT_ADDRESS = 0x39; |
rgrover1 | 0:56d6b711b8c7 | 88 | |
rgrover1 | 0:56d6b711b8c7 | 89 | I2C i2c; |
rgrover1 | 0:56d6b711b8c7 | 90 | }; |
rgrover1 | 0:56d6b711b8c7 | 91 | #endif /* #ifndef __GROVE_COLOUR_SENSOR_HPP__ */ |