基类数组,使用派生类填充(arduino :: ESP2866)

时间:2019-01-17 09:32:27

标签: c++ arrays object arduino polymorphism

我试图用派生类填充基类。 我能够填充该对象,但是当我尝试访问它时,ESP重新启动。此行将触发重置:

tBuf = IOs[i]->printState() ;   //  ESP2866 wdt reset!

但是我认为最有可能在人口方面犯了一个错误:

GPIO_NONE *IOs[PIN_NUMBER];
GPIO_NONE temp_GPIO = GPIO_NONE();
temp_GPIO.setPin(PINS[i],i);
IOs[i]= &temp_GPIO;

不幸的是,我无法弄清楚我犯错的地方了。

这是相关部分:

//Class.h
class GPIO_NONE
{
  protected:
    uint8_t         pin;
    uint8_t         pin_gpio_number;
  public:
        virtual void setPin(uint8_t _pin, uint8_t _pin_gpio_number);
    virtual String printState();
};

class GPIO_IN : public GPIO_NONE
{
  protected:

  public:
        virtual void setPin(uint8_t _pin, uint8_t _pin_gpio_number);
    virtual String printState();

};

//Class.ccp
void GPIO_NONE::setPin(uint8_t _pin, uint8_t _pin_gpio_number)
{
  pinMode(pin, OUTPUT);
  pin = _pin;
  pin_gpio_number = _pin_gpio_number;

  //Pclient.publish("sensor/Dx", "none", true); 
}

String GPIO_NONE::printState()
{
  String buf = "NONE GPIO";
  Serial.println(buf);
  buf =+ pin_gpio_number;
  Serial.println(buf);
  Serial.println(buf);
  return ( buf );
}

void GPIO_IN::setPin(uint8_t _pin, uint8_t _pin_gpio_number)
{
  pinMode(pin, INPUT);
  pin = _pin;
  pin_gpio_number = _pin_gpio_number;
  //Pclient.subscribe("homeassistant/switch1");
}

String GPIO_IN::printState()
{
  String buf = "IN GPIO";
  buf =+ pin_gpio_number;
  buf =+ " ";
  buf =+ digitalRead(pin);
  Serial.println(buf);
  return ( buf );
}

//Initialization of a object array with a derived objects:
void add_io_none(int i){
    GPIO_NONE temp_GPIO = GPIO_NONE();
    temp_GPIO.setPin(PINS[i],i);
    IOs[i]= &temp_GPIO;
}  
void add_io_in(int i){
    GPIO_IN temp_GPIO = GPIO_IN();
    temp_GPIO.setPin(PINS[i],i);
    IOs[i]= & temp_GPIO;
}

//Printing out the status of the object array ()
String printGPIO(int arg_cnt, char **args)
{
  String buf = "";
  String tBuf;
  for(uint8_t i=0; i<PIN_NUMBER; i++){
    Serial.println(i);
    tBuf = IOs[i]->printState() ;   //  ESP2866 wdt reset!
    buf+=tBuf;
    Serial.println(buf);
  }
  return ( buf );
}

0 个答案:

没有答案