Skip to content

Arbitrary HardwareSerials (Serial1, Serial2, ..) for Arduino Mega 2560 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions src/SparkFunESP8266Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,22 @@ size_t ESP8266Client::write(const uint8_t *buf, size_t size)

int ESP8266Client::available()
{
int available = esp8266.available();
if (available == 0)
{
// Delay for the amount of time it'd take to receive one character
delayMicroseconds((1 / esp8266._baud) * 10 * 1E6);
// Check again just to be sure:
available = esp8266.available();
}
return esp8266.available();
return receiveBuffer.available();
}

int ESP8266Client::read()
{
return esp8266.read();
return receiveBuffer.read();
}

int ESP8266Client::read(uint8_t *buf, size_t size)
{
if (esp8266.available() < size)
if (available() + esp8266.available() < size)
return 0;

for (int i=0; i<size; i++)
{
buf[i] = esp8266.read();
buf[i] = this->read();
}

return 1;
Expand Down Expand Up @@ -139,7 +131,7 @@ uint8_t ESP8266Client::connected()
return 0;
else if (available() > 0)
return 1;
else if (status() == ESP8266_STATUS_CONNECTED)
else if (esp8266._status.stat == ESP8266_STATUS_CONNECTED)
return 1;

return 0;
Expand Down
6 changes: 5 additions & 1 deletion src/SparkFunESP8266Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Distributed as-is; no warranty is given.
#include <IPAddress.h>
#include "Client.h"
#include "SparkFunESP8266WiFi.h"
#include "SparkFunESP8266ClientReadBuffer.h"

#define ESP8266_CLIENT_MAX_BUFFER_SIZE 256

class ESP8266Client : public Client {

Expand Down Expand Up @@ -58,10 +61,11 @@ class ESP8266Client : public Client {
using Print::write;

private:
ESP8266ClientReadBuffer receiveBuffer;
static uint16_t _srcport;
uint16_t _socket;
bool ipMuxEn;


uint8_t getFirstSocket();
};
Expand Down
75 changes: 75 additions & 0 deletions src/SparkFunESP8266ClientReadBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "SparkFunESP8266ClientReadBuffer.h"
#include "SparkFunESP8266WiFi.h"



int ESP8266ClientReadBuffer::available()
{
if (receiveBufferSize > 0)//client has already buffered some payload
return receiveBufferSize;

int available = esp8266.available();
if (available == 0)
{
// Delay for the amount of time it'd take to receive one character
delayMicroseconds((1 / esp8266._baud) * 10 * 1E6);
// Check again just to be sure:
available = esp8266.available();
}
return available;
}

int ESP8266ClientReadBuffer::read()
{
this->fillReceiveBuffer();//append to buffer BEFORE we read, so that chances are higher to detect AT commands

//read from buffer
if (receiveBufferSize > 0) {
uint8_t ret = receiveBuffer[0];
this->truncateReceiveBufferHead(0, 1);
return ret;
}

return -1;
}

void ESP8266ClientReadBuffer::truncateReceiveBufferHead(uint8_t startingOffset, uint8_t truncateLength) {
for (uint8_t i = startingOffset; i < receiveBufferSize - truncateLength; i++)//shift buffer content; todo: better implementation
receiveBuffer[i] = receiveBuffer[i + truncateLength];
receiveBufferSize -= truncateLength;
}

void ESP8266ClientReadBuffer::fillReceiveBuffer() {
//fill the receive buffer as much as possible from esp8266.read()

for (uint8_t attemps = 0; attemps < 5; attemps++) {//often 1st available() call does not yield all bytes => outer while
while (uint8_t availableBytes = esp8266.available() > 0) {
for (; availableBytes > 0 && receiveBufferSize < ESP8266_CLIENT_MAX_BUFFER_SIZE; availableBytes--) {
receiveBuffer[receiveBufferSize++] = esp8266.read();
}
delay(10);
}
delay(10);
}

this->cleanReceiveBufferFromAT();
}

void ESP8266ClientReadBuffer::cleanReceiveBufferFromAT() {
//get rid of these esp8266 commands
this->cleanReceiveBufferFromAT("\r\n\r\n+IPD", 5);//typical answer looks like \r\n\r\n+IPD,0,4:<payload>
}

void ESP8266ClientReadBuffer::cleanReceiveBufferFromAT(const char *atCommand, uint8_t additionalSuffixToKill) {
uint8_t atLen = strlen(atCommand);

//uint8_t offset = 0;
for (uint8_t offset = 0; offset <= receiveBufferSize - atLen; offset++) {
if (0 == memcmp((receiveBuffer + offset), atCommand, atLen)) {
//found the at command. KILL IT!
this->truncateReceiveBufferHead(offset, atLen + additionalSuffixToKill);
break;
}
}
}

22 changes: 22 additions & 0 deletions src/SparkFunESP8266ClientReadBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef _SPARKFUNESP8266CLIENTBUFFER_H_
#define _SPARKFUNESP8266CLIENTBUFFER_H_
#include <Arduino.h>

#define ESP8266_CLIENT_MAX_BUFFER_SIZE 256

class ESP8266ClientReadBuffer {
public:
int available();
int read();

protected:
char receiveBufferSize = 0;
uint8_t receiveBuffer[ESP8266_CLIENT_MAX_BUFFER_SIZE];

void fillReceiveBuffer();
void truncateReceiveBufferHead(uint8_t startingOffset, uint8_t truncateLength);
void cleanReceiveBufferFromAT();
void cleanReceiveBufferFromAT(const char *atCommand, uint8_t additionalSuffixToKill);
};

#endif
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy