SP1 vers 3
Dependents: SoufflerieSP1-vers2
Diff: wifiesp8266.cpp
- Revision:
- 0:58ef0c4fba67
diff -r 000000000000 -r 58ef0c4fba67 wifiesp8266.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wifiesp8266.cpp Tue Jun 08 10:37:18 2021 +0000 @@ -0,0 +1,73 @@ +#include "mbed.h" +#include "wifiesp8266.h" + +WifiEsp8266::WifiEsp8266(PinName Txd, PinName Rxd, float TimeOut) : RawSerial(Txd,Rxd) +{ + baud(Baud); + _TimeOut = TimeOut; + _DataPtr = 0; + _DataReady = false; + _Canal1Ok = false; + _Canal2Ok = false; + attach(this,&WifiEsp8266::receive, RawSerial::RxIrq); + if (_TimeOut != NULL) TGarde.attach(this,&WifiEsp8266::Tevent,_TimeOut); // pour chien de garde +} +void WifiEsp8266::Tevent(void) // chien de garde +{ + if (!_Canal1Ok) printf("$Y\r\n"); + if (!_Canal2Ok) printf("$y\r\n"); + _Canal1Ok = false; + _Canal2Ok = false; +} +char * WifiEsp8266::read(void) +{ + _DataReady = false; + return _Data; +} + +bool WifiEsp8266::dataready(void) +{ + return _DataReady; +} + +// reception phrase de type '$' "....' "\r\n" +void WifiEsp8266::receive(void) +{ + bool _PhraseComplete = false; + char inChar; + while (readable() && (_PhraseComplete == false)) { + inChar = getc(); + switch (inChar) { + case '$' : + _DataPtr = 0; + _DataTmp[_DataPtr++] = inChar; + _DataTmp[_DataPtr] = 0; + break; + case '\n' : + if (_DataPtr > 1) { + strcpy(_Data,_DataTmp); + _PhraseComplete = true; + if (_TimeOut!=NULL) { + if ((_Data[1] >> 5)==2) _Canal1Ok = true; + if ((_Data[1] >> 5)==3) _Canal2Ok = true; + } + } + _DataTmp[0] = 0; + _DataPtr = 0; + break; + case '\r' : + break; + default : + if (_DataPtr > 0) { + _DataTmp[_DataPtr++] = inChar; + _DataTmp[_DataPtr] = 0; + } + } + if (_DataPtr >= maxdata) { // si phrase trop longue vide phrase + _DataTmp[0] = 0; + _DataPtr = 0; + _PhraseComplete = false; + } + } + _DataReady = _PhraseComplete; +} \ No newline at end of file