Search code examples
c++arduinoplatformio

cpp/PlatformIO class dependencies


I'm increasing an arduino project by adding an external Analog to Digital Converter.

For that I'm using the Mcp3208 library. My code is organise with one class by sensor and I need to access to a Mcp3208 instance (adc) from different classes.

So I created a common lib include in the different classes but this is not working. I'm searching from few hours on the net but I have not found an article to help me.

Can you explain me the right way to do that? I'm coding with Atom and the platformIO plugin.

I've a FSM class who instantiate sensors (.h) :

#ifndef FSM_H_
#define FSM_H_

#include "EnvironmentalSensor.h
#include "PowerSensor.h"

class FSM {
    FSM();
    virtual ~FSM();

public:
    do_stuff();

private:
    EnvironmentalSensor envSensor1(params);
    PowerSensor envSensor1(params);
}
#endif

The common class instantiate the Mcp3208 as adc (.h):

#ifndef COMMONS_H_
#define COMMONS_H_

#include "Mcp3208.h"

#define SPI_CS      29         // SPI slave select
#define ADC_VREF    5000     // 5V Vref
#define ADC_CLK     1600000  // SPI clock 1.6MHz

#define ADC_2_5 MCP3208::SINGLE_6

#endif /*COMMONS_H_ */

The common class (.cpp):

#include "Arduino.h"
#include "commons.h"

extern MCP3208 adc(ADC_VREF, SPI_CS);

void init_adc(){

    pinMode(SPI_CS, OUTPUT);        // set spi cs as an output pin
}

Here is an example for the Environmental class (.cpp), PowerSensor is equal :

#include "Arduino.h"
#include "EnvironmentalSensor.h"
#include "commons.h"

MCP3208 adc;

void EnvironmentalSensor::read(){
    adc.read(SINGLE_0);
}

Here is an example for the Environmental class (.h), PowerSensor is equal :

#ifndef EnvironmentalSensor_H_
#define EnvironmentalSensor_H_

#include "commons.h"

class EnvironmentalSensor{

public :
    read();
}

#endif

edit :

The problem is : in the EnvironmentalSensor.cpp the mcp3208 adc give me an error.

The error window provide me some help :

error:lib/EnvironmentalSensor/src/EnvironmentalSensor.cpp:49:10: error: nomatching function for call to 'MCP3208::MCP3208()' MCP3208 adc;

^

lib/Power/src/Power.cpp:38:9: note: candidates are: In file included from lib/commons/commons.h:4:0, from lib/Power/src/Power.h:33,

from lib/Power/src/Power.cpp:34: .piolibdeps/Mcp3208_ID1533/src/Mcp3208.h:74:3: note: MCP3208::MCP3208(uint16_t, uint8_t) MCP3208(uint16_t vref, uint8_t csPin);

^

.piolibdeps/Mcp3208_ID1533/src/Mcp3208.h:74:3: note: candidate expects 2 arguments, 0 provided .piolibdeps/Mcp3208_ID1533/src/Mcp3208.h:65:3: note: MCP3208::MCP3208(uint16_t, uint8_t, SPIClass*) MCP3208(uint16_t vref, uint8_t csPin, SPIClass *spi);

^

.piolibdeps/Mcp3208_ID1533/src/Mcp3208.h:65:3: note: candidate expects 3 arguments, 0 provided .piolibdeps/Mcp3208_ID1533/src/Mcp3208.h:16:7: note: constexpr MCP3208::MCP3208(const MCP3208&) class MCP3208 {

^

.piolibdeps/Mcp3208_ID1533/src/Mcp3208.h:16:7: note: candidate expects 1 argument, 0 provided .piolibdeps/Mcp3208_ID1533/src/Mcp3208.h:16:7: note: constexpr MCP3208::MCP3208(MCP3208&&) .piolibdeps/Mcp3208_ID1533/src/Mcp3208.h:16:7: note: candidate expects 1 argument, 0 provided

But for me, I have already declare the adc in the commons.cpp

Thank you, gilou


Solution

  • In your .h file, add the declaration:

    extern MCP3208 adc;
    

    In your .cpp file, add the definition:

    MCP3208 adc(ADC_VREF, SPI_CS);