Bonjour à tous
Comme beaucoup, j'ai acheté "pour voir" une carte esp32
Je fais donc quelques essais avec, et je tente d'utiliser ma librairie de machines à états YASM
Et là, c'est le drame...
/home/yann/Arduino/libraries/yasm/yasm.cpp:33:55: error: default argument given for parameter 2 of 'void YASM::next(void (*)(), bool)' [-fpermissive]
void YASM::next(void (*pnextstate)(), bool reset=false)
^
In file included from /home/yann/Arduino/libraries/yasm/yasm.cpp:31:0:
/home/yann/Arduino/libraries/yasm/yasm.h:39:8: note: previous specification in 'void YASM::next(void (*)(), bool)' here
void next(void (*pnextsate)(), bool reset=false);
^
La première erreur, elle me surprend, mais je la comprends. Il est donc interdit de spécifier un paramètre optionnel par défaut en C++ "pur" ? Fort ennuyeux, je vais devoir dédoubler toutes les méthodes, c'est naze.
La seconde, par contre, ça m'échappe. Bien sûr que c'est déjà défini dans le .h, il sert à quoi sinon le .h ? ???
voilà les fichiers qui vont avec :
yasm.h
#define YASM_VERSION "1.0.0"
#ifndef YASM_h
#define YASM_h
#include "Arduino.h"
class YASM{
public:
YASM(){_pState=YASM::nop;};
void next(void (*pnextsate)(), bool reset=false);
bool run();
void stop();
void resume(){YASM::_pState=YASM::_pLastState;};
unsigned long timeOnState(){return (millis()-YASM::_enterTime);};
bool elapsed(unsigned long durationms);
bool periodic(unsigned long durationms, bool first=true);
bool isFirstRun(){return YASM::_isFirstRun;};
unsigned int runCount(){return YASM::_runCount;};
bool isInState(void (*pstate)()){return pstate==YASM::_pState;};
private:
void (*_pState)();
void (*_pLastState)();
unsigned long _enterTime;
unsigned long _lastTime;
double _runCount;
static void nop(){};
bool _isFirstRun;
};
#endif
et yasm.cpp
#include "yasm.h"
void YASM::next(void (*pnextstate)(), bool reset=false)
{
if ((!reset) && (pnextstate == _pState)) return; //we do nothing if we stay in the same state
//we change the state so we set the associated values as needed
_isFirstRun = true;
_lastTime = _enterTime = millis();
_runCount = 0;
//and enventually change exec pointer to the next state
_pState = pnextstate;
}
bool YASM::run()
{
if(_pState==YASM::nop) return false; //check if the machine is currently stopped
//and just return false if this is the case
//now machine is running
_pLastState=_pState; //store the current state
_pState(); //run the current state
//if pState is the same now than before running it means we did not got state
//change request during _pState() execution so next time we again will run the
//same state, so we clear the flag and increment runs counter
if (_pState==_pLastState) {
_isFirstRun = false;
_runCount++;
}
return true; //and eventually report machine was running this time
}
void YASM::stop()
{
//Same check done at the end of run() for the case when a
//state just run once and calls itself for stop().
//Without doing so here _isFirstRun and _runCount are not updated prior to
//stop the machine, and this leads to wrong values used on resume()
if (_pState==_pLastState) {
_isFirstRun = false;
_runCount++;
}
//Then stop the machine
_pState = YASM::nop;
}
bool YASM::elapsed(unsigned long durationms)
{
if (YASM::timeOnState()>durationms) return true;
return false;
}
bool YASM::periodic(unsigned long durationms, bool first=true)
{
unsigned long time = (millis()-_lastTime);
if( time >= durationms )
{
_lastTime += time;
return true;
}
if(first) return _isFirstRun;
return false;
}