Discrete RGB colour sensor using a colour LED flashing at high speed and a monochrome LDR (light dependent resistor) for detecting the colour via ADC conversion. The library implements interrupt driven ADC conversion at high speed (370 RGB readings per second, 128 times oversampling per channelfor noise reduction). The detection can optionally run in background.

Dependents:   rgb_sensor_buffer discrete_rgb_color_sensor_example

rgb_sensor.h

Committer:
meriac
Date:
2014-06-25
Revision:
1:204db97d10c9
Parent:
0:576e43bd193d
Child:
2:4545984e62b6

File content as of revision 1:204db97d10c9:

/* Discrete RGB color sensor
 *
 * - uses single-channel light-dependent resistor (via ADC)
 *   and a RGB LED.
 * -  compensates background light
 *
 * Copyright (c) 2014 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 #ifndef __RGB_SENSOR_H__
#define __RGB_SENSOR_H__

#ifndef RGB_OVERSAMPLING
#define RGB_OVERSAMPLING 128
#endif/*RGB_OVERSAMPLING*/

#ifndef RGB_MAX_ADC_CLK
#define RGB_MAX_ADC_CLK 12400000UL
#endif/*RGB_MAX_ADC_CLK*/

#define RGB_CHANNELS 4

typedef struct
{
    int red,green,blue;
} TRGB_Channel;

typedef union
{
    TRGB_Channel ch;
    int data[3];
} TRGB;

class RGB_Sensor
{
    public:
      RGB_Sensor(PinName red, PinName green, PinName blue, PinName adc);
      ~RGB_Sensor(void);
      bool capture(TRGB &rgb);
      bool capture(void);
      bool wait(TRGB &rgb);
    protected:
      bool m_done;
      uint8_t m_adc_channel, m_rgb_channel;
      DigitalOut m_red, m_green, m_blue;
      int m_adc_aggregation[RGB_CHANNELS], m_adc_count;
      bool wait(void);
    private:
      static void __adc_irq(void);
      void adc_irq(void);
      static RGB_Sensor *m_global;
};

#endif/*__RGB_SENSOR_H__*/