Skip to content

File adc.h

File List > external-docs > libDaisy > src > per > adc.h

Go to the documentation of this file

Source Code

#pragma once
#ifndef DSY_ADC_H
#define DSY_ADC_H 
#include <stdint.h>
#include <stdlib.h>
#include "daisy_core.h"
#include "per/gpio.h"

#define DSY_ADC_MAX_CHANNELS 16 
namespace daisy
{
/* While there may not be many configuration options here,
using a struct like this allows us to add more configuration
later without breaking existing functionality.
*/

struct AdcChannelConfig
{
    enum MuxPin
    {
        MUX_SEL_0,    
        MUX_SEL_1,    
        MUX_SEL_2,    
        MUX_SEL_LAST, 
    };

    enum ConversionSpeed
    {
        SPEED_1CYCLES_5,
        SPEED_2CYCLES_5,
        SPEED_8CYCLES_5,
        SPEED_16CYCLES_5,
        SPEED_32CYCLES_5,
        SPEED_64CYCLES_5,
        SPEED_387CYCLES_5,
        SPEED_810CYCLES_5,
    };

    void InitSingle(Pin pin, ConversionSpeed speed = SPEED_8CYCLES_5);

    void InitMux(Pin             adc_pin,
                 size_t          mux_channels,
                 Pin             mux_0,
                 Pin             mux_1 = Pin(PORTX, 0),
                 Pin             mux_2 = Pin(PORTX, 0),
                 ConversionSpeed speed = SPEED_8CYCLES_5);

    GPIO            pin_;                   
    GPIO            mux_pin_[MUX_SEL_LAST]; 
    uint8_t         mux_channels_;          
    ConversionSpeed speed_;
};

class AdcHandle
{
  public:
    enum OverSampling
    {
        OVS_NONE, 
        OVS_4,    
        OVS_8,    
        OVS_16,   
        OVS_32,   
        OVS_64,   
        OVS_128,  
        OVS_256,  
        OVS_512,  
        OVS_1024, 
        OVS_LAST, 
    };

    AdcHandle() {}
    ~AdcHandle() {}
    void
    Init(AdcChannelConfig *cfg, size_t num_channels, OverSampling ovs = OVS_32);

    void Start();

    void Stop();

    uint16_t Get(uint8_t chn) const;

    uint16_t *GetPtr(uint8_t chn) const;

    float GetFloat(uint8_t chn) const;

    uint16_t GetMux(uint8_t chn, uint8_t idx) const;

    uint16_t *GetMuxPtr(uint8_t chn, uint8_t idx) const;

    float GetMuxFloat(uint8_t chn, uint8_t idx) const;

  private:
    OverSampling oversampling_;
    size_t       num_channels_;
};

} // namespace daisy

#endif // DSY_ADC_H