Skip to content

Commit

Permalink
Updated the SmingCore and samples to match the coding standards.
Browse files Browse the repository at this point in the history
  • Loading branch information
slav-at-attachix committed Jul 24, 2018
1 parent 6a35853 commit 3865134
Show file tree
Hide file tree
Showing 297 changed files with 8,022 additions and 8,645 deletions.
9 changes: 4 additions & 5 deletions Sming/SmingCore/ArduinoCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
extern "C" {
#endif

#define abs(x) ((x)>0?(x):-(x))
#define abs(x) ((x) > 0 ? (x) : -(x))
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#endif
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#define round(x) ((x) >= 0 ? (long)((x) + 0.5) : (long)((x)-0.5))

void yield();

#ifdef __cplusplus
}
#endif


#endif /* SMINGCORE_ARDUINOCOMPAT_H_ */
40 changes: 27 additions & 13 deletions Sming/SmingCore/AtClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#define debugf(fmt, ...)
#endif

AtClient::AtClient(HardwareSerial* stream) : stream(stream) {
AtClient::AtClient(HardwareSerial* stream) : stream(stream)
{
this->stream->setCallback(StreamDataReceivedDelegate(&AtClient::processor, this));
}

void AtClient::processor(Stream &source, char arrivedChar, uint16_t availableCharsCount) {
void AtClient::processor(Stream& source, char arrivedChar, uint16_t availableCharsCount)
{
if(!currentCommand.text.length()) {
return;
}
Expand All @@ -40,9 +42,9 @@ void AtClient::processor(Stream &source, char arrivedChar, uint16_t availableCha
debugf("Processing: %d ms, %s", millis(), currentCommand.text.substring(0, 20).c_str());

char response[availableCharsCount];
for (int i = 0; i < availableCharsCount; i++) {
for(int i = 0; i < availableCharsCount; i++) {
response[i] = stream->read();
if (response[i] == '\r' || response[i] == '\n') {
if(response[i] == '\r' || response[i] == '\n') {
response[i] = '\0';
}
}
Expand Down Expand Up @@ -72,7 +74,9 @@ void AtClient::processor(Stream &source, char arrivedChar, uint16_t availableCha
next();
}

void AtClient::send(const String& text, const String& altResponse /* ="" */, uint32_t timeoutMs /* = AT_TIMEOUT */, int retries /* = 0 */) {
void AtClient::send(const String& text, const String& altResponse /* ="" */, uint32_t timeoutMs /* = AT_TIMEOUT */,
int retries /* = 0 */)
{
AtCommand atCommand;
atCommand.text = text;
atCommand.response2 = altResponse;
Expand All @@ -82,7 +86,9 @@ void AtClient::send(const String& text, const String& altResponse /* ="" */, uin
send(atCommand);
}

void AtClient::send(const String& text, AtReceiveCallback onReceive, uint32_t timeoutMs /* = AT_TIMEOUT */, int retries /* = 0 */) {
void AtClient::send(const String& text, AtReceiveCallback onReceive, uint32_t timeoutMs /* = AT_TIMEOUT */,
int retries /* = 0 */)
{
AtCommand atCommand;
atCommand.text = text;
atCommand.onReceive = onReceive;
Expand All @@ -92,7 +98,9 @@ void AtClient::send(const String& text, AtReceiveCallback onReceive, uint32_t ti
send(atCommand);
}

void AtClient::send(const String& text, AtCompleteCallback onComplete, uint32_t timeoutMs /* = AT_TIMEOUT */, int retries /* = 0 */) {
void AtClient::send(const String& text, AtCompleteCallback onComplete, uint32_t timeoutMs /* = AT_TIMEOUT */,
int retries /* = 0 */)
{
AtCommand atCommand;
atCommand.text = text;
atCommand.onComplete = onComplete;
Expand All @@ -104,7 +112,8 @@ void AtClient::send(const String& text, AtCompleteCallback onComplete, uint32_t

// Low Level Communication Functions

void AtClient::send(AtCommand command) {
void AtClient::send(AtCommand command)
{
if(currentCommand.text.length()) {
queue.enqueue(command);
return;
Expand All @@ -113,17 +122,20 @@ void AtClient::send(AtCommand command) {
sendDirect(command);
}

void AtClient::sendDirect(AtCommand command) {
void AtClient::sendDirect(AtCommand command)
{
state = eAtRunning;
commandTimer.stop();
currentCommand = command;
stream->print(command.text);
debugf("Sent: timeout: %d, current %d ms, name: %s", currentCommand.timeout, millis(), command.text.substring(0, 20).c_str());
debugf("Sent: timeout: %d, current %d ms, name: %s", currentCommand.timeout, millis(),
command.text.substring(0, 20).c_str());
commandTimer.initializeMs(currentCommand.timeout, TimerDelegate(&AtClient::ticker, this)).startOnce();
}

// Low Level Queue Functions
void AtClient::resend() {
void AtClient::resend()
{
state = eAtOK;
if(currentCommand.text.length()) {
sendDirect(currentCommand);
Expand All @@ -133,7 +145,8 @@ void AtClient::resend() {
next();
}

void AtClient::next() {
void AtClient::next()
{
if(state == eAtError) {
debugf("We are at error state! No next");
return;
Expand All @@ -146,7 +159,8 @@ void AtClient::next() {
}
}

void AtClient::ticker() {
void AtClient::ticker()
{
debugf("Ticker =================> ");
if(!currentCommand.text.length()) {
commandTimer.stop();
Expand Down
59 changes: 32 additions & 27 deletions Sming/SmingCore/AtClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,45 @@

class AtClient;

typedef Delegate<bool(AtClient& atClient, Stream& source)> AtReceiveCallback; // << If the callback returns true then this means that we have
// finished successfully processing the command
typedef Delegate<bool(AtClient& atClient, String& reply)> AtCompleteCallback; // << If the callback returns true then this means that we have
// finished successfully processing the command
typedef Delegate<bool(AtClient& atClient, Stream& source)> AtReceiveCallback;
// ^ If the callback returns true then this means that we have
// finished successfully processing the command
typedef Delegate<bool(AtClient& atClient, String& reply)> AtCompleteCallback;
// ^ If the callback returns true then this means that we have
// finished successfully processing the command

typedef struct {
String text; // << the actual AT command
String response2; // << alternative successful response
int timeout; // << timeout in milliseconds
int retries; // << number of retries before giving up
bool breakOnError = true; // << stop executing next command if that one has failed
AtReceiveCallback onReceive = 0; // << if set you can process manually all incoming data in a callback
String text; // << the actual AT command
String response2; // << alternative successful response
int timeout; // << timeout in milliseconds
int retries; // << number of retries before giving up
bool breakOnError = true; // << stop executing next command if that one has failed
AtReceiveCallback onReceive = 0; // << if set you can process manually all incoming data in a callback
AtCompleteCallback onComplete = 0; // if set then you can process the complete response manually
} AtCommand;

typedef enum {
eAtOK = 0,
eAtRunning,
eAtError
} AtState;
typedef enum { eAtOK = 0, eAtRunning, eAtError } AtState;

template<typename T, int rawSize>
class SimpleQueue: public FIFO<T, rawSize> {
virtual const T& operator[](unsigned int) const { }
virtual T& operator[](unsigned int) { }
template <typename T, int rawSize> class SimpleQueue : public FIFO<T, rawSize>
{
virtual const T& operator[](unsigned int) const
{
}
virtual T& operator[](unsigned int)
{
}
};

/**
* @brief Class that facilitates the communication with an AT device.
*/
class AtClient {

class AtClient
{
public:
AtClient(HardwareSerial* stream);
virtual ~AtClient() {}
virtual ~AtClient()
{
}

/**
* @brief Sends AT command
Expand All @@ -68,7 +72,7 @@ class AtClient {
* @param timeoutMs uint32_t Time in milliseconds to wait for response
* @param retries int Retries on error
*/
void send(const String& text, const String& altResponse = "", uint32_t timeoutMs = AT_TIMEOUT, int retries = 0);
void send(const String& text, const String& altResponse = "", uint32_t timeoutMs = AT_TIMEOUT, int retries = 0);

/**
* @brief Sends AT command
Expand Down Expand Up @@ -108,7 +112,8 @@ class AtClient {
* @brief Returns the current state
* @return AtState
*/
AtState getState() {
AtState getState()
{
return state;
}

Expand All @@ -129,12 +134,12 @@ class AtClient {
/**
* @brief Processes response data.
*/
virtual void processor(Stream &source, char arrivedChar, uint16_t availableCharsCount);
virtual void processor(Stream& source, char arrivedChar, uint16_t availableCharsCount);

private:
SimpleQueue<AtCommand, 10> queue; // << Queue for the commands to be executed
HardwareSerial* stream; // << The main communication stream
Timer commandTimer; // timer used for commands with timeout
HardwareSerial* stream; // << The main communication stream
Timer commandTimer; // timer used for commands with timeout
AtState state = eAtOK;

/**
Expand Down
7 changes: 3 additions & 4 deletions Sming/SmingCore/Clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ void delay(uint32_t time)
{
int quotient = time / MAX_SAFE_DELAY;
int remainder = time % MAX_SAFE_DELAY;
for(int i=0, max = quotient + 1; i < max ; i++) {
for(int i = 0, max = quotient + 1; i < max; i++) {
if(i == quotient) {
os_delay_us(remainder * 1000);
}
else {
} else {
os_delay_us(MAX_SAFE_DELAY * 1000);
}

system_soft_wdt_feed ();
system_soft_wdt_feed();
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sming/SmingCore/Clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ extern "C" {
* @note This function uses ESP8266 _system time_ clock which pauses during sleep. Function is provided for compatibility with Arduino. For date and time functionality, use SystemClock
* @see SystemClockClass
*/
unsigned long millis(void) __attribute__((weak));
unsigned long millis(void) __attribute__((weak));

/** @brief Get the time from clock in microseconds
* @retval "unsigned long" Quantity of microseconds elapsed since clock epoch
* @note Clock epoch will reset every 71 minutes, 47 seconds, 967296 microseconds
* @note This function uses ESP8266 _system time_ clock which pauses during sleep. Function is provided for compatibility with Arduino. For date and time functionality, use SystemClock
* @see SystemClockClass
*/
unsigned long micros(void) __attribute__((weak));
unsigned long micros(void) __attribute__((weak));

/** @brief Pause execution
* @param time Duration of delay in milliseconds
Expand Down
22 changes: 9 additions & 13 deletions Sming/SmingCore/Data/CircularBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

#include "CircularBuffer.h"

CircularBuffer::CircularBuffer(int size): buffer(new char[size]), readPos(buffer), writePos(buffer), size(size)
CircularBuffer::CircularBuffer(int size) : buffer(new char[size]), readPos(buffer), writePos(buffer), size(size)
{

}

CircularBuffer::~CircularBuffer()
Expand All @@ -33,8 +32,8 @@ uint16_t CircularBuffer::readMemoryBlock(char* data, int bufSize)
size_t bytesAvailable = available();
size_t sizeToRead = (bufSize < bytesAvailable) ? bufSize : bytesAvailable;
size_t sizeRead = sizeToRead;
char * start = readPos;
if(writePos < readPos && sizeToRead > (size_t) ((buffer + size) - readPos)) {
char* start = readPos;
if(writePos < readPos && sizeToRead > (size_t)((buffer + size) - readPos)) {
size_t topSize = (buffer + size) - readPos;
memcpy(data, readPos, topSize);
start = buffer;
Expand All @@ -54,11 +53,9 @@ bool CircularBuffer::seek(int len)

if(readPos < writePos) {
readPos += len;
}
else if(readPos + len > buffer + size) {
} else if(readPos + len > buffer + size) {
readPos = buffer + (len - (buffer + size - readPos));
}
else {
} else {
readPos += len;
}

Expand Down Expand Up @@ -86,17 +83,16 @@ size_t CircularBuffer::room() const
return readPos - writePos - 1;
}


String CircularBuffer::id()
{
// TODO: check if that is printing the address of the buffer...
return String((char *)&buffer);
return String((char*)&buffer);
}

size_t CircularBuffer::write(uint8_t charToWrite)
{
if(!room()) {
return 0;
return 0;
}

*writePos = charToWrite;
Expand All @@ -105,12 +101,12 @@ size_t CircularBuffer::write(uint8_t charToWrite)
return 1;
}

size_t CircularBuffer::write(const uint8_t *data, size_t bufSize)
size_t CircularBuffer::write(const uint8_t* data, size_t bufSize)
{
size_t space = room();
size_t sizeToWrite = (bufSize < space) ? bufSize : space;
size_t sizeWritten = sizeToWrite;
if(writePos >= readPos && sizeToWrite > (size_t) (buffer + size - writePos)) {
if(writePos >= readPos && sizeToWrite > (size_t)(buffer + size - writePos)) {
size_t topSize = buffer + size - writePos;
memcpy(writePos, data, topSize);
writePos = buffer;
Expand Down
Loading

0 comments on commit 3865134

Please sign in to comment.