C ++ & Esp8266 LoadStoreAlignmentCause с указателем - PullRequest
0 голосов
/ 06 февраля 2020

Я пытаюсь получить доступ к функции указателя, и она не работает, и это дает мне исключение LoadStoreAlignmentCause. Кроме того, я хочу проверить, существует ли указатель, но для этого он всегда возвращает true.

LedFunction.h

#include "Led/LedStates.h"
class LedStates;
class LedFunction {
  public:
    LedStates *state;
    virtual bool init();
    bool loadValues();
    virtual void render() = 0;
};

LedFunction. cpp

#include "Led/LedFunction.h"

bool LedFunction::init() {
  return false;
}

RainbowFunction.h

class RainbowFunction: public LedFunction {
  public:
    RainbowFunction() {
      Serial.println("Rainbow Constructor.");
    }

  void render() {
Serial.println("From Rainbow...");
    }
  }
};

LedStates.h

#include "Handlers/LedHandler.h"
#include "Led/LedFunction.h"

class LedHandler;
class LedFunction;
class LedStates {
  public:
    uint8_t (*values)[3];
    int count = 0;
    bool dirty = false;
    LedHandler* ledHandler;
    LedFunction* function = 0;

    LedStates(LedHandler* handler);

    void setFunction(LedFunction *newFunction);
    void setRgb(int i, uint8_t r, uint8_t g, uint8_t b);
    void render(); //TODO check virtual key
    void setValues(LedStates &to);
    void commit();
    void fade(LedStates &to, long f0, long f1);
};

LedStates. cpp

#include "Led/LedStates.h"

#include "Led/Animations/RainbowFunction.h"

LedStates::LedStates(LedHandler* handler) {
  this->ledHandler = handler;
  count = ledHandler->getLength();
  values = new uint8_t[count][3];

  this->setFunction(new RainbowFunction());
}

void LedStates::setFunction(LedFunction* newFunction) {
  Serial.println("SETTING FUNCTION");
  if(function)
    delete function; //TODO check virtual destructor
  function = newFunction;
  if(!function)
    return;
  function->state = this;
  Serial.println("-----Setting Done-----");
}

void LedStates::render() {
  Serial.println(2);
  Serial.println("B:" + (String) (function != 0));
  Serial.println("B:" + (String) (function != false));
  if(function == nullptr) { //This is the check that is not working properly
    Serial.println(22222);
    //delay(1000);
    //function->render();
  } else {
    Serial.println(33333);
    function->render();
  }
  Serial.println(3);
}

LedHandler.h

#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include <FastLED.h>

//#include "Led/LedFunction.h"
#include "Led/LedStates.h"
#include "Led/Fading.h"

class LedStates;
class LedHandler {
  public:
    LedHandler(int length, uint16_t pin);
    void clear();
    void show();
    void setColor(int s, int r, int g, int b);
    void loop();

    Adafruit_NeoPixel getStrip();
    int getLength();
  private:
    LedStates* currentState;
    LedStates* targetState;
    Fader<LedStates> *ledFader;

    int length;
    Adafruit_NeoPixel strip;
    CRGB* leds;
};

LedHandler. cpp

#include "Handlers/LedHandler.h"

LedHandler::LedHandler(int length, uint16_t pin) {
  Serial.begin(115200);

  this->length = length;

  this->strip = Adafruit_NeoPixel(length, pin);
  this->strip.begin();

  CRGB* arr = new CRGB[length];
  this->leds = arr;
  FastLED.addLeds<WS2812B, 6, RGB>(leds, 60).setCorrection(TypicalLEDStrip);


  //Serial.println("-----Creating States-----");
  LedStates currentLedStates = LedStates(this);
  LedStates targetLedStates = LedStates(this);
  Fader<LedStates> ledFader = Fader<LedStates>(currentLedStates, targetLedStates);
  //Serial.println("-----Created States-----");

  this->currentState = &currentLedStates;
  this->targetState = &targetLedStates;
  this->ledFader = &ledFader;
}

void LedHandler::loop() {
  Serial.println("--::--::--::--::--::--::--");
  currentState->render();
  Serial.println(99);
  Serial.println(6);
  currentState->commit();
  Serial.println("-------------------------");
  delay(10000);
}

Выход Serialmonitor:

SETTING FUNCTION
-----Setting Done-----
Rainbow Constructor.
SETTING FUNCTION
-----Setting Done-----
--::--::--::--::--::--::--
2
B:1
B:1
33333

Exception (9):
epc1=0x40202a92 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4020d32d depc=0x00000000

1 Ответ

2 голосов
/ 06 февраля 2020

Эти строки определяют локальные переменные внутри функции LedHandler::LedHandler(int length, uint16_t pin):

    LedStates currentLedStates = LedStates(this);
    LedStates targetLedStates = LedStates(this);

Эти строки запоминают адрес локальных переменных:

    this->currentState = &currentLedStates;
    this->targetState = &targetLedStates;

Эта строка удаляет локальные переменные так, память может использоваться для чего-то другого:

}

, и эта строка вызывает что-то другое (никто не знает, что это будет):

currentState->render();
...