exemple utilisation

Dependents:   0-wifisp1-drivers

wifiesp8266.cpp

Committer:
schnf30
Date:
2022-05-18
Revision:
1:2cbeb8cedf2f
Parent:
0:58ef0c4fba67

File content as of revision 1:2cbeb8cedf2f:

#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;
}