Polytech Tours - Projet C++ embarqué sur cible mbed
GroveColourSensor.hpp@0:21e183c9ef81, 2016-04-24 (annotated)
- Committer:
- LecomteDelys
- Date:
- Sun Apr 24 15:43:51 2016 +0000
- Revision:
- 0:21e183c9ef81
Programme final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LecomteDelys | 0:21e183c9ef81 | 1 | /* |
LecomteDelys | 0:21e183c9ef81 | 2 | * Copyright (c) 2006-2013 ARM Limited |
LecomteDelys | 0:21e183c9ef81 | 3 | * |
LecomteDelys | 0:21e183c9ef81 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
LecomteDelys | 0:21e183c9ef81 | 5 | * you may not use this file except in compliance with the License. |
LecomteDelys | 0:21e183c9ef81 | 6 | * You may obtain a copy of the License at |
LecomteDelys | 0:21e183c9ef81 | 7 | * |
LecomteDelys | 0:21e183c9ef81 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
LecomteDelys | 0:21e183c9ef81 | 9 | * |
LecomteDelys | 0:21e183c9ef81 | 10 | * Unless required by applicable law or agreed to in writing, software |
LecomteDelys | 0:21e183c9ef81 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
LecomteDelys | 0:21e183c9ef81 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
LecomteDelys | 0:21e183c9ef81 | 13 | * See the License for the specific language governing permissions and |
LecomteDelys | 0:21e183c9ef81 | 14 | * limitations under the License. |
LecomteDelys | 0:21e183c9ef81 | 15 | */ |
LecomteDelys | 0:21e183c9ef81 | 16 | |
LecomteDelys | 0:21e183c9ef81 | 17 | #ifndef __GROVE_COLOUR_SENSOR_HPP__ |
LecomteDelys | 0:21e183c9ef81 | 18 | #define __GROVE_COLOUR_SENSOR_HPP__ |
LecomteDelys | 0:21e183c9ef81 | 19 | |
LecomteDelys | 0:21e183c9ef81 | 20 | #include "mbed.h" |
LecomteDelys | 0:21e183c9ef81 | 21 | |
LecomteDelys | 0:21e183c9ef81 | 22 | /** |
LecomteDelys | 0:21e183c9ef81 | 23 | * This module is based on the color sensor TCS3414CS with digital output over I2C. |
LecomteDelys | 0:21e183c9ef81 | 24 | * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_I2C_Color_Sensor_v0.9b |
LecomteDelys | 0:21e183c9ef81 | 25 | */ |
LecomteDelys | 0:21e183c9ef81 | 26 | #ifndef COLOR_H |
LecomteDelys | 0:21e183c9ef81 | 27 | #define COLOR_H |
LecomteDelys | 0:21e183c9ef81 | 28 | #include "Color.h" |
LecomteDelys | 0:21e183c9ef81 | 29 | #endif |
LecomteDelys | 0:21e183c9ef81 | 30 | |
LecomteDelys | 0:21e183c9ef81 | 31 | class GroveColourSensor { |
LecomteDelys | 0:21e183c9ef81 | 32 | public: |
LecomteDelys | 0:21e183c9ef81 | 33 | |
LecomteDelys | 0:21e183c9ef81 | 34 | // Constructeur qui initialise la connexion I2C |
LecomteDelys | 0:21e183c9ef81 | 35 | GroveColourSensor(PinName sda, PinName scl) : m_i2c(sda, scl) { |
LecomteDelys | 0:21e183c9ef81 | 36 | /* empty*/ |
LecomteDelys | 0:21e183c9ef81 | 37 | } |
LecomteDelys | 0:21e183c9ef81 | 38 | |
LecomteDelys | 0:21e183c9ef81 | 39 | // Méthode pour allumer le capteur |
LecomteDelys | 0:21e183c9ef81 | 40 | void powerUp(void) { |
LecomteDelys | 0:21e183c9ef81 | 41 | static const char powerUpCommand[] = {0x80, 0x03}; |
LecomteDelys | 0:21e183c9ef81 | 42 | if (m_i2c.write((SEVEN_BIT_ADDRESS << 1), powerUpCommand, sizeof(powerUpCommand)) != 0) { |
LecomteDelys | 0:21e183c9ef81 | 43 | std::cerr << "failed to power up the sensor" << std::endl; |
LecomteDelys | 0:21e183c9ef81 | 44 | } |
LecomteDelys | 0:21e183c9ef81 | 45 | } |
LecomteDelys | 0:21e183c9ef81 | 46 | |
LecomteDelys | 0:21e183c9ef81 | 47 | // Méthode pour éteindre le capteur |
LecomteDelys | 0:21e183c9ef81 | 48 | void powerDown(void) { |
LecomteDelys | 0:21e183c9ef81 | 49 | static const char powerDownCommand[] = {0x80, 0x00}; |
LecomteDelys | 0:21e183c9ef81 | 50 | /* turn down the color sensor */ |
LecomteDelys | 0:21e183c9ef81 | 51 | if (m_i2c.write((SEVEN_BIT_ADDRESS << 1), powerDownCommand, sizeof(powerDownCommand)) != 0) { |
LecomteDelys | 0:21e183c9ef81 | 52 | std::cerr << "failed to power down the sensor" << std::endl; |
LecomteDelys | 0:21e183c9ef81 | 53 | } |
LecomteDelys | 0:21e183c9ef81 | 54 | } |
LecomteDelys | 0:21e183c9ef81 | 55 | |
LecomteDelys | 0:21e183c9ef81 | 56 | // Méthode permettant de récupérer la valeur d'une couleur en donnant en paramètre une couleur de l'enum Colour_t |
LecomteDelys | 0:21e183c9ef81 | 57 | uint16_t readColour(Colour_t colour) { |
LecomteDelys | 0:21e183c9ef81 | 58 | char readColorRegistersCommand = 0xb0 + (2 * static_cast<unsigned>(colour)); |
LecomteDelys | 0:21e183c9ef81 | 59 | m_i2c.write((SEVEN_BIT_ADDRESS << 1), &readColorRegistersCommand, 1 /* size */); |
LecomteDelys | 0:21e183c9ef81 | 60 | |
LecomteDelys | 0:21e183c9ef81 | 61 | uint16_t colourValue; |
LecomteDelys | 0:21e183c9ef81 | 62 | m_i2c.read((SEVEN_BIT_ADDRESS << 1), reinterpret_cast<char *>(&colourValue), sizeof(uint16_t)); |
LecomteDelys | 0:21e183c9ef81 | 63 | return colourValue; |
LecomteDelys | 0:21e183c9ef81 | 64 | } |
LecomteDelys | 0:21e183c9ef81 | 65 | |
LecomteDelys | 0:21e183c9ef81 | 66 | // Méthode permettant de récupérer la valeur d'une couleur en donnant en paramètre le numéro de la couleur dans l'énum |
LecomteDelys | 0:21e183c9ef81 | 67 | uint16_t readColour(unsigned colour) { |
LecomteDelys | 0:21e183c9ef81 | 68 | if (colour >= NUM_COLORS) { |
LecomteDelys | 0:21e183c9ef81 | 69 | return 0; |
LecomteDelys | 0:21e183c9ef81 | 70 | } |
LecomteDelys | 0:21e183c9ef81 | 71 | return readColour(static_cast<Colour_t>(colour)); |
LecomteDelys | 0:21e183c9ef81 | 72 | } |
LecomteDelys | 0:21e183c9ef81 | 73 | |
LecomteDelys | 0:21e183c9ef81 | 74 | // Méthode permettant de récupérer les trois couleurs en donnant en paramètre une structure RGB (par référence) |
LecomteDelys | 0:21e183c9ef81 | 75 | void readColours(RGB &rgb) { |
LecomteDelys | 0:21e183c9ef81 | 76 | rgb.red= readColour(RED); |
LecomteDelys | 0:21e183c9ef81 | 77 | rgb.blue= readColour(BLUE); |
LecomteDelys | 0:21e183c9ef81 | 78 | rgb.green= readColour(GREEN); |
LecomteDelys | 0:21e183c9ef81 | 79 | } |
LecomteDelys | 0:21e183c9ef81 | 80 | |
LecomteDelys | 0:21e183c9ef81 | 81 | private: |
LecomteDelys | 0:21e183c9ef81 | 82 | static const uint8_t SEVEN_BIT_ADDRESS = 0x39; |
LecomteDelys | 0:21e183c9ef81 | 83 | I2C m_i2c; |
LecomteDelys | 0:21e183c9ef81 | 84 | }; |
LecomteDelys | 0:21e183c9ef81 | 85 | #endif /* #ifndef __GROVE_COLOUR_SENSOR_HPP__ */ |