Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: CapCouleur FindTheColor
GroveColourSensor.hpp
00001 /* 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef __GROVE_COLOUR_SENSOR_HPP__ 00018 #define __GROVE_COLOUR_SENSOR_HPP__ 00019 00020 #include "mbed.h" 00021 00022 /** 00023 * This module is based on the color sensor TCS3414CS with digital output over I2C. 00024 * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_I2C_Color_Sensor_v0.9b 00025 * 00026 * example use: 00027 * 00028 * GroveColourSensor colorSensor(I2C_SDA0, I2C_SCL0); 00029 * 00030 * colorSensor.powerUp(); 00031 * unsigned colour; 00032 * for (colour = GroveColourSensor::GREEN; colour < GroveColourSensor::NUM_COLORS; colour++) { 00033 * uint16_t colourValue = colorSensor.readColour(colour); 00034 * ... 00035 * } 00036 * colorSensor.powerDown(); 00037 */ 00038 00039 class GroveColourSensor { 00040 public: 00041 enum Colour_t { 00042 GREEN = 0, 00043 RED, 00044 BLUE, 00045 CLEAR, 00046 NUM_COLORS 00047 }; 00048 00049 GroveColourSensor(PinName sda, PinName scl) : i2c(sda, scl) { 00050 /* empty*/ 00051 } 00052 00053 void powerUp(void) { 00054 static const char powerUpCommand[] = {0x80, 0x03}; 00055 /* turn on the color sensor */ 00056 if (i2c.write((SEVEN_BIT_ADDRESS << 1), powerUpCommand, sizeof(powerUpCommand)) != 0) { 00057 error("failed to power up the sensor"); 00058 } 00059 } 00060 00061 void powerDown(void) { 00062 static const char powerDownCommand[] = {0x80, 0x00}; 00063 /* turn on the color sensor */ 00064 if (i2c.write((SEVEN_BIT_ADDRESS << 1), powerDownCommand, sizeof(powerDownCommand)) != 0) { 00065 error("failed to power down the sensor"); 00066 } 00067 } 00068 00069 uint16_t readColour(Colour_t colour) { 00070 char readColorRegistersCommand = 0xb0 + (2 * static_cast<unsigned>(colour)); 00071 i2c.write((SEVEN_BIT_ADDRESS << 1), &readColorRegistersCommand, 1 /* size */); 00072 00073 uint16_t colourValue; 00074 i2c.read((SEVEN_BIT_ADDRESS << 1), reinterpret_cast<char *>(&colourValue), sizeof(uint16_t)); 00075 return colourValue; 00076 } 00077 00078 uint16_t readColour(unsigned colour) { 00079 if (colour >= NUM_COLORS) { 00080 return 0; 00081 } 00082 00083 return readColour(static_cast<Colour_t>(colour)); 00084 } 00085 00086 private: 00087 static const uint8_t SEVEN_BIT_ADDRESS = 0x39; 00088 00089 I2C i2c; 00090 }; 00091 #endif /* #ifndef __GROVE_COLOUR_SENSOR_HPP__ */
Generated on Tue Jul 12 2022 16:35:24 by
1.7.2
Grove Colour Sensor