mirror of https://github.com/ARMmbed/mbed-os.git
Add non-blocking support to ESP8266Interface
requires changes in the following - BufferedSerial - ESP8266 - ESP8266Interface
parent
a5800ef975
commit
4f8e8f5d75
|
@ -122,6 +122,10 @@ void BufferedSerial::rxIrq(void)
|
||||||
// read from the peripheral and make sure something is available
|
// read from the peripheral and make sure something is available
|
||||||
if(serial_readable(&_serial)) {
|
if(serial_readable(&_serial)) {
|
||||||
_rxbuf = serial_getc(&_serial); // if so load them into a buffer
|
_rxbuf = serial_getc(&_serial); // if so load them into a buffer
|
||||||
|
// trigger callback if necessary
|
||||||
|
if (_cbs[RxIrq]) {
|
||||||
|
_cbs[RxIrq]();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -136,6 +140,10 @@ void BufferedSerial::txIrq(void)
|
||||||
} else {
|
} else {
|
||||||
// disable the TX interrupt when there is nothing left to send
|
// disable the TX interrupt when there is nothing left to send
|
||||||
RawSerial::attach(NULL, RawSerial::TxIrq);
|
RawSerial::attach(NULL, RawSerial::TxIrq);
|
||||||
|
// trigger callback if necessary
|
||||||
|
if (_cbs[TxIrq]) {
|
||||||
|
_cbs[TxIrq]();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -155,4 +163,8 @@ void BufferedSerial::prime(void)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BufferedSerial::attach(Callback<void()> func, IrqType type)
|
||||||
|
{
|
||||||
|
_cbs[type] = func;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -80,6 +80,8 @@ private:
|
||||||
void txIrq(void);
|
void txIrq(void);
|
||||||
void prime(void);
|
void prime(void);
|
||||||
|
|
||||||
|
Callback<void()> _cbs[2];
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/** Create a BufferedSerial port, connected to the specified transmit and receive pins
|
/** Create a BufferedSerial port, connected to the specified transmit and receive pins
|
||||||
* @param tx Transmit pin
|
* @param tx Transmit pin
|
||||||
|
@ -135,6 +137,22 @@ public:
|
||||||
* @return The number of bytes written to the Serial Port Buffer
|
* @return The number of bytes written to the Serial Port Buffer
|
||||||
*/
|
*/
|
||||||
virtual ssize_t write(const void *s, std::size_t length);
|
virtual ssize_t write(const void *s, std::size_t length);
|
||||||
|
|
||||||
|
/** Attach a function to call whenever a serial interrupt is generated
|
||||||
|
* @param func A pointer to a void function, or 0 to set as none
|
||||||
|
* @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
|
||||||
|
*/
|
||||||
|
virtual void attach(Callback<void()> func, IrqType type=RxIrq);
|
||||||
|
|
||||||
|
/** Attach a member function to call whenever a serial interrupt is generated
|
||||||
|
* @param obj pointer to the object to call the member function on
|
||||||
|
* @param method pointer to the member function to call
|
||||||
|
* @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
|
||||||
|
*/
|
||||||
|
template <typename T, typename M>
|
||||||
|
void attach(T *obj, M method, IrqType type=RxIrq) {
|
||||||
|
attach(Callback<void()>(obj, method), type);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -167,3 +167,7 @@ bool ESP8266::writeable()
|
||||||
return _serial.writeable();
|
return _serial.writeable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ESP8266::attach(Callback<void()> func)
|
||||||
|
{
|
||||||
|
_serial.attach(func);
|
||||||
|
}
|
||||||
|
|
|
@ -144,6 +144,24 @@ public:
|
||||||
*/
|
*/
|
||||||
bool writeable();
|
bool writeable();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attach a function to call whenever network state has changed
|
||||||
|
*
|
||||||
|
* @param func A pointer to a void function, or 0 to set as none
|
||||||
|
*/
|
||||||
|
void attach(Callback<void()> func);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attach a function to call whenever network state has changed
|
||||||
|
*
|
||||||
|
* @param obj pointer to the object to call the member function on
|
||||||
|
* @param method pointer to the member function to call
|
||||||
|
*/
|
||||||
|
template <typename T, typename M>
|
||||||
|
void attach(T *obj, M method) {
|
||||||
|
attach(Callback<void()>(obj, method));
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BufferedSerial _serial;
|
BufferedSerial _serial;
|
||||||
ATParser _parser;
|
ATParser _parser;
|
||||||
|
|
|
@ -29,6 +29,9 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
|
||||||
: _esp(tx, rx, debug)
|
: _esp(tx, rx, debug)
|
||||||
{
|
{
|
||||||
memset(_ids, 0, sizeof(_ids));
|
memset(_ids, 0, sizeof(_ids));
|
||||||
|
memset(_cbs, 0, sizeof(_cbs));
|
||||||
|
|
||||||
|
_esp.attach(this, &ESP8266Interface::event);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ESP8266Interface::connect(
|
int ESP8266Interface::connect(
|
||||||
|
@ -203,4 +206,15 @@ int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *d
|
||||||
|
|
||||||
void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
|
void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
|
||||||
{
|
{
|
||||||
|
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
|
||||||
|
_cbs[socket->id].callback = callback;
|
||||||
|
_cbs[socket->id].data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESP8266Interface::event() {
|
||||||
|
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
|
||||||
|
if (_cbs[i].callback) {
|
||||||
|
_cbs[i].callback(_cbs[i].data);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,6 +167,12 @@ protected:
|
||||||
private:
|
private:
|
||||||
ESP8266 _esp;
|
ESP8266 _esp;
|
||||||
bool _ids[ESP8266_SOCKET_COUNT];
|
bool _ids[ESP8266_SOCKET_COUNT];
|
||||||
|
|
||||||
|
void event();
|
||||||
|
struct {
|
||||||
|
void (*callback)(void *);
|
||||||
|
void *data;
|
||||||
|
} _cbs[ESP8266_SOCKET_COUNT];
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue