Skip to content
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

Add Non-blocking Version of localIP Function for WiFiS3 library and fix FifoBuffer.h #408

Open
wants to merge 3 commits into
base: main
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
42 changes: 22 additions & 20 deletions cores/arduino/FifoBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

namespace arduino {


#define FIFO_DEFAULT_SIZE 64

template <typename T, uint32_t size = FIFO_DEFAULT_SIZE>
Expand All @@ -38,28 +37,30 @@ class FifoBuffer
synchronized {
return (uint32_t)(index + 1) % size;
}
return 0; // Fallback return
}

inline bool isEmpty() const { return (_numElems == 0); }
T _aucBuffer[size] ;
uint32_t _iHead ;
uint32_t _iTail ;
T _aucBuffer[size];
uint32_t _iHead;
uint32_t _iTail;
uint32_t _numElems;

public:
/* ---------------------------------------------------------------------- */
FifoBuffer( void ) {
memset( _aucBuffer, 0, size * sizeof(T) ) ;
FifoBuffer(void) {
memset(_aucBuffer, 0, size * sizeof(T));
clear();
}
/* ---------------------------------------------------------------------- */
bool store( T c ) {
bool store(T c) {
bool rv = true;
synchronized {
if (!isFull()) {
_aucBuffer[_iHead] = c ;
_aucBuffer[_iHead] = c;
_iHead = nextIndex(_iHead);
_numElems++;
}
else {
} else {
rv = false;
}
}
Expand All @@ -72,36 +73,39 @@ class FifoBuffer
_numElems = 0;
}
/* ---------------------------------------------------------------------- */
T read(bool *read_ok) {
T read(bool* read_ok) {
*read_ok = true;
if (isEmpty()) {
*read_ok = false;
return _aucBuffer[0];
return T(); // Return default-constructed object
}
synchronized {
T value = _aucBuffer[_iTail];
_iTail = nextIndex(_iTail);
_numElems--;

return value;
}
return T(); // Fallback return
}
/* ---------------------------------------------------------------------- */
int available() {
synchronized {
return _numElems;
}
return 0; // Fallback return
}
/* ---------------------------------------------------------------------- */
int freePositions() {
synchronized {
return (size - _numElems);
}
return 0; // Fallback return
}
/* ---------------------------------------------------------------------- */
T peek() {
if (isEmpty())
return -1;
return T(); // Return default-constructed object

return _aucBuffer[_iTail];
}
Expand All @@ -110,15 +114,13 @@ class FifoBuffer
synchronized {
return (_numElems == size);
}
return false; // Fallback return
}
/* ---------------------------------------------------------------------- */
uint32_t lenght() const { return size; }


uint32_t lenght() const { return size; }
};


} //namespace arduino
} // namespace arduino

#endif /* _ARDUINO_FIFO_BUFFER_DH_ */
#endif /* __cplusplus */
#endif /* __cplusplus */
23 changes: 23 additions & 0 deletions libraries/WiFiS3/src/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,29 @@ IPAddress CWifi::localIP() {
return local_IP;
}

IPAddress CWifi::localIPNonBlocking(unsigned long timeoutMs) {
modem.begin();
string res = "";
unsigned long start = millis();
IPAddress local_IP(0, 0, 0, 0);

while (local_IP == IPAddress(0, 0, 0, 0) && millis() - start < timeoutMs) {
if (modem.write(string(PROMPT(_MODE)), res, "%s", CMD_READ(_MODE))) {
if (atoi(res.c_str()) == 1) {
if (modem.write(string(PROMPT(_IPSTA)), res, "%s%d\\r\\n", CMD_WRITE(_IPSTA), IP_ADDR)) {
local_IP.fromString(res.c_str());
}
} else if (atoi(res.c_str()) == 2) {
if (modem.write(string(PROMPT(_IPSOFTAP)), res, CMD(_IPSOFTAP))) {
local_IP.fromString(res.c_str());
}
}
}
}
return local_IP;
}


/* -------------------------------------------------------------------------- */
IPAddress CWifi::subnetMask() {
/* -------------------------------------------------------------------------- */
Expand Down
3 changes: 2 additions & 1 deletion libraries/WiFiS3/src/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ class CWifi {
* return: IP address value
*/
IPAddress localIP();

IPAddress localIPNonBlocking(unsigned long timeoutMs);

/*
* Get the interface subnet mask address.
*
Expand Down
Loading