Skip to content

Commit 4f8e8f5

Browse files
committed
Add non-blocking support to ESP8266Interface
requires changes in the following - BufferedSerial - ESP8266 - ESP8266Interface
1 parent a5800ef commit 4f8e8f5

File tree

6 files changed

+72
-0
lines changed

6 files changed

+72
-0
lines changed

net/ESP8266Interface/ESP8266/ATParser/BufferedSerial/BufferedSerial.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ void BufferedSerial::rxIrq(void)
122122
// read from the peripheral and make sure something is available
123123
if(serial_readable(&_serial)) {
124124
_rxbuf = serial_getc(&_serial); // if so load them into a buffer
125+
// trigger callback if necessary
126+
if (_cbs[RxIrq]) {
127+
_cbs[RxIrq]();
128+
}
125129
}
126130

127131
return;
@@ -136,6 +140,10 @@ void BufferedSerial::txIrq(void)
136140
} else {
137141
// disable the TX interrupt when there is nothing left to send
138142
RawSerial::attach(NULL, RawSerial::TxIrq);
143+
// trigger callback if necessary
144+
if (_cbs[TxIrq]) {
145+
_cbs[TxIrq]();
146+
}
139147
break;
140148
}
141149
}
@@ -155,4 +163,8 @@ void BufferedSerial::prime(void)
155163
return;
156164
}
157165

166+
void BufferedSerial::attach(Callback<void()> func, IrqType type)
167+
{
168+
_cbs[type] = func;
169+
}
158170

net/ESP8266Interface/ESP8266/ATParser/BufferedSerial/BufferedSerial.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class BufferedSerial : public RawSerial
7979
void rxIrq(void);
8080
void txIrq(void);
8181
void prime(void);
82+
83+
Callback<void()> _cbs[2];
8284

8385
public:
8486
/** Create a BufferedSerial port, connected to the specified transmit and receive pins
@@ -135,6 +137,22 @@ class BufferedSerial : public RawSerial
135137
* @return The number of bytes written to the Serial Port Buffer
136138
*/
137139
virtual ssize_t write(const void *s, std::size_t length);
140+
141+
/** Attach a function to call whenever a serial interrupt is generated
142+
* @param func A pointer to a void function, or 0 to set as none
143+
* @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
144+
*/
145+
virtual void attach(Callback<void()> func, IrqType type=RxIrq);
146+
147+
/** Attach a member function to call whenever a serial interrupt is generated
148+
* @param obj pointer to the object to call the member function on
149+
* @param method pointer to the member function to call
150+
* @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
151+
*/
152+
template <typename T, typename M>
153+
void attach(T *obj, M method, IrqType type=RxIrq) {
154+
attach(Callback<void()>(obj, method), type);
155+
}
138156
};
139157

140158
#endif

net/ESP8266Interface/ESP8266/ESP8266.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,7 @@ bool ESP8266::writeable()
167167
return _serial.writeable();
168168
}
169169

170+
void ESP8266::attach(Callback<void()> func)
171+
{
172+
_serial.attach(func);
173+
}

net/ESP8266Interface/ESP8266/ESP8266.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ class ESP8266
144144
*/
145145
bool writeable();
146146

147+
/**
148+
* Attach a function to call whenever network state has changed
149+
*
150+
* @param func A pointer to a void function, or 0 to set as none
151+
*/
152+
void attach(Callback<void()> func);
153+
154+
/**
155+
* Attach a function to call whenever network state has changed
156+
*
157+
* @param obj pointer to the object to call the member function on
158+
* @param method pointer to the member function to call
159+
*/
160+
template <typename T, typename M>
161+
void attach(T *obj, M method) {
162+
attach(Callback<void()>(obj, method));
163+
}
164+
147165
private:
148166
BufferedSerial _serial;
149167
ATParser _parser;

net/ESP8266Interface/ESP8266Interface.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
2929
: _esp(tx, rx, debug)
3030
{
3131
memset(_ids, 0, sizeof(_ids));
32+
memset(_cbs, 0, sizeof(_cbs));
33+
34+
_esp.attach(this, &ESP8266Interface::event);
3235
}
3336

3437
int ESP8266Interface::connect(
@@ -203,4 +206,15 @@ int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *d
203206

204207
void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
205208
{
209+
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
210+
_cbs[socket->id].callback = callback;
211+
_cbs[socket->id].data = data;
212+
}
213+
214+
void ESP8266Interface::event() {
215+
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
216+
if (_cbs[i].callback) {
217+
_cbs[i].callback(_cbs[i].data);
218+
}
219+
}
206220
}

net/ESP8266Interface/ESP8266Interface.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
167167
private:
168168
ESP8266 _esp;
169169
bool _ids[ESP8266_SOCKET_COUNT];
170+
171+
void event();
172+
struct {
173+
void (*callback)(void *);
174+
void *data;
175+
} _cbs[ESP8266_SOCKET_COUNT];
170176
};
171177

172178
#endif

0 commit comments

Comments
 (0)