Skip to content

Instantly share code, notes, and snippets.

@u0078867
Last active May 24, 2016 09:18
Show Gist options
  • Save u0078867/9df30eb7da64d8f43422faa70b1a9e52 to your computer and use it in GitHub Desktop.
Save u0078867/9df30eb7da64d8f43422faa70b1a9e52 to your computer and use it in GitHub Desktop.
Make implementation of Arduino WiFi101 (v0.8.0) WiFiClient.write() method non blocking (but data-loss)
// For file src/WiFiClient.cpp change ths method:
size_t WiFiClient::write(const uint8_t *buf, size_t size)
{
sint16 err;
if (_socket < 0 || size == 0) {
setWriteError();
return 0;
}
// Network led ON (rev A then rev B).
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 0);
m2m_wifi_handle_events(NULL);
while ((err = send(_socket, (void *)buf, size, 0)) < 0) {
// Exit on fatal error, retry if buffer not ready.
if (err != SOCK_ERR_BUFFER_FULL) {
setWriteError();
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1);
return 0;
}
m2m_wifi_handle_events(NULL);
}
// Network led OFF (rev A then rev B).
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1);
return size;
}
// into:
size_t WiFiClient::write(const uint8_t *buf, size_t size)
{
sint16 err;
if (_socket < 0 || size == 0) {
setWriteError();
return 0;
}
// Network led ON (rev A then rev B).
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 0);
m2m_wifi_handle_events(NULL);
unsigned long t1 = millis();
unsigned long t2 = t1;
while ((err = send(_socket, (void *)buf, size, 0)) < 0) {
t2 = millis();
if ((t2-t1) > 3) { // sending has to be completed by 3 ms (can change this), otherwise exit
return 0;
}
// Exit on fatal error, retry if buffer not ready.
if (err != SOCK_ERR_BUFFER_FULL) {
setWriteError();
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1);
return 0;
}
m2m_wifi_handle_events(NULL);
}
// Network led OFF (rev A then rev B).
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1);
return size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment