在类中调用另一个构造函数作为全局[关闭]

问题描述 投票:1回答:2

我正在为我的DHT传感器创建一个库。我有两个名字为AM_2301.cppAM_2301.h的文件:

AM_2301.h

#ifndef AM2301_h
#define AM2301_h

#include "Arduino.h"

struct Two_float {
    float temp;
    float humidity;
};

extern int pinn;

class AM_2301
{
  public:
    AM_2301(int pin);
    void begin();
    struct Two_float read();
  private:
    int _pin;
};

#endif

AM_2301.cpp

#include "Arduino.h"
#include "AM_2301.h"
#include "DHT.h"

//#define pinn 3
int pinn;
DHT dht(pinn, DHT21);

AM_2301::AM_2301(int pin)
{
    pinMode(pin, OUTPUT);
    Serial.print("pinn: ");
    Serial.println(pinn);
    _pin = pin;
    pinn = pin;
    //DHT dht(pinn, DHT21);
}


void AM_2301::begin(){
    dht.begin();
}

struct Two_float AM_2301::read()
{
    struct Two_float output;
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    // check if returns are valid, if they are NaN (not a number) then something went wrong!
    if (isnan(t) || isnan(h))
    {
        Serial.println("Failed to read from DHT");
        t = 0;
        h = 0;
    }
    output = {t, h};
    return output;
}

main.cpp

#include "Arduino.h"
#include "AM_2301.h"

int pin =3 ;

AM_2301 AM(pin);

void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
    AM.begin();
}

void loop() {

    struct Two_float val;
    val = AM.read();
    Serial.print("Temp: ");
    Serial.print(val.temp);
    Serial.print(" , ");
    Serial.print("Humidity: ");
    Serial.println(val.humidity);
    delay(2000);
    // put your main code here, to run repeatedly:
}

但问题是我想在构造函数中声明一个引脚号,并且该引脚转到AM_2301.cpp中的另一个构造函数但我不知道如何实现它。我想让dht对象成为我班级中所有其他函数的全局对象。

c++ constructor arduino
2个回答
2
投票

你需要使DHT对象成为AM_2301类的一部分,然后使用member initializer list对其进行初始化。

AM_2301.h

#ifndef AM2301_h
#define AM2301_h

#include "Arduino.h"

struct Two_float {
    float temp;
    float humidity;
};

class AM_2301
{
  public:
    AM_2301(int pin);
    void begin();
    struct Two_float read();
  private:
    DHT dht; // add a DHT object
};

#endif

AM_2301.cpp

#include "Arduino.h"
#include "AM_2301.h"
#include "DHT.h"

AM_2301::AM_2301(int pin) : dht(pin, DHT21) {} // initialize the DHT object

// other code stays the same

2
投票

最后我根据paddy的answer找到了一个解决方案,我必须使用来自我的静态实例的指针。这是修改后的AM_2301.cpp

#include "Arduino.h"
#include "AM_2301.h"
#include "DHT.h"

static DHT *dht = NULL;  //define the pointer


AM_2301::AM_2301(int pin)
{
    pinMode(pin, OUTPUT);
    Serial.print("_pin: ");
    Serial.println(_pin);
    _pin = pin;
    dht = new DHT(_pin, DHT21);  //pass the initialization
}


void AM_2301::begin(){
    dht->begin();
}

struct Two_float AM_2301::read()
{
    struct Two_float output;
    float h = dht->readHumidity();
    float t = dht->readTemperature();
    // check if returns are valid, if they are NaN (not a number) then something went wrong!
    if (isnan(t) || isnan(h))
    {
        Serial.println("Failed to read from DHT");
        t = 0;
        h = 0;
    }
    output = {t, h};
    return output;
}
© www.soinside.com 2019 - 2024. All rights reserved.