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:
0:576e43bd193d
Child:
1:204db97d10c9

File content as of revision 0:576e43bd193d:

#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__*/