Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexGyver committed Apr 14, 2021
1 parent 4594301 commit e6e25df
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 125 deletions.
44 changes: 44 additions & 0 deletions minimLibs/asyncReadTest/AsyncStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef AsyncStream_h
#define AsyncStream_h

template < uint16_t SIZE >
class AsyncStream {
public:
AsyncStream(Stream* port, char ter = ';', uint16_t tout = 50) {
_port = port;
_tout = tout;
_ter = ter;
}
bool available() {
if (_port -> available()) {
if (!_parseF) {
_parseF = true;
_count = 0;
_tmr = millis();
}
char ch = _port -> read();
if (ch == _ter) {
buf[_count] = NULL;
_parseF = false;
return true;
} else if (_count < SIZE - 1) buf[_count++] = ch;
_tmr = millis();
}
if (_parseF && millis() - _tmr >= _tout) {
_parseF = false;
return true;
}
return false;
}

char buf[SIZE];

private:
Stream* _port;
char _ter;
uint16_t _tout, _count = 0;
uint32_t _tmr = 0;
bool _parseF = false;
};

#endif
20 changes: 20 additions & 0 deletions minimLibs/asyncReadTest/asyncReadTest.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// асинхронная читалка Serial
// а также любых из класса Stream

#include "AsyncStream.h"

// указываем обработчик, терминатор (символ конца приёма) и таймаут в мс
// в <> указан размер буфера!
AsyncStream<40> serial(&Serial, '\n');

void setup() {
Serial.begin(9600);
}

// строка для теста (отправь в сериал)
// 1234,3.14,hello,4567,lolkek,qwerty
void loop() {
if (serial.available()) { // если данные получены
Serial.println(serial.buf); // выводим их (как char*)
}
}
119 changes: 32 additions & 87 deletions minimLibs/mString/mString.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,105 +34,42 @@ char* mFtoa(double value, int8_t decimals, char *buffer) {
uint32_t exp = 1;
while (decimals--) exp *= 10;
exp *= (float)value;
/*buffer += 9;
buffer = mUtoa(exp, buffer);
--buffer = '.';
buffer -= 11;
buffer = mLtoa(mant, buffer, 0);*/
buffer = ltoa(mant, buffer, DEC);
byte len = strlen(buffer);
*(buffer + len++) = '.';
ltoa(exp, buffer + len++, DEC);
return buffer;
}

template <uint16_t SIZE>
template < uint16_t SIZE >
class mString {
public:
char buffer[SIZE] = "NULL";
char* buf = buffer;
uint16_t size = SIZE;
char buf[SIZE] = "";
uint16_t length() {
return strlen(buf);
}
void clear() {
buf[0] = NULL;
}
mString(char* extBuffer = NULL, uint16_t newSize = 0) {
if (extBuffer) {
buf = extBuffer;
size = newSize;
}
}

// constructor
/*mString(char* buffer, int newSize = -1) {
buf = buffer;
size = newSize;
}*/
/*mString (const char c) {
//init();
add(c);
}
mString (const char* data) {
//init();
add(data);
}
mString (const __FlashStringHelper *data) {
//init();
add(data);
}
mString (uint32_t value) {
//init();
add(value);
}
mString (int32_t value) {
//init();
add(value);
}
mString (uint16_t value) {
//init();
add(value);
}
mString (int16_t value) {
//init();
add(value);
}
mString (uint8_t value) {
//init();
add(value);
}
mString (int8_t value) {
//init();
add(value);
}
mString (double value, byte dec = 2) {
//init();
add(value, dec);
}*/

// add
mString& add(const char c) {
int len = length();
if (len + 1 >= size) return *this;
if (len + 1 >= SIZE) return *this;
buf[len++] = c;
buf[len] = NULL;
return *this;
}
mString& add(const char* data) {
if (length() + strlen(data) >= size) return *this;
strcpy(buf + length(), data);
if (length() + strlen(data) >= SIZE) return *this;
strcat(buf, data);
return *this;
}
mString& add(const __FlashStringHelper *data) {
PGM_P p = reinterpret_cast<PGM_P>(data);
if (length() + strlen_P(p) >= size) return *this;
if (length() + strlen_P(p) >= SIZE) return *this;
strcpy_P(buf + length(), p);
return *this;
/*do {
buf[len] = (char)pgm_read_byte_near(p++);
} while (buf[len++] != 0);
*/
}
mString& add(uint32_t value) {
char vBuf[11];
Expand Down Expand Up @@ -200,48 +137,48 @@ class mString {
return add(value);
}
mString& operator += (mString data) {
return add(data);
return add(data.buf);
}
mString& operator += (String data) {
return add(data);
}

// +
mString operator + (const char c) {
return mString(*this) += c;
return (*this).add(c);
}
mString operator + (const char* data) {
return mString(*this) += data;
return (*this).add(data);
}
mString operator + (const __FlashStringHelper *data) {
return mString(*this) += data;
return (*this).add(data);
}
mString operator + (uint32_t value) {
return mString(*this) += value;
return (*this).add(value);
}
mString operator + (int32_t value) {
return mString(*this) += value;
return (*this).add(value);
}
mString operator + (uint16_t value) {
return mString(*this) += value;
return (*this).add(value);
}
mString operator + (int16_t value) {
return mString(*this) += value;
return (*this).add(value);
}
mString operator + (uint8_t value) {
return mString(*this) += value;
return (*this).add(value);
}
mString operator + (int8_t value) {
return mString(*this) += value;
return (*this).add(value);
}
mString operator + (double value) {
return mString(*this) += value;
return (*this).add(value);
}
mString operator + (mString data) {
return mString(*this) += data;
return (*this).add(data);
}
mString operator + (String data) {
return mString(*this) += data;
return (*this).add(data);
}

// assign
Expand Down Expand Up @@ -285,10 +222,6 @@ class mString {
clear();
return add(value);
}
mString& operator = (mString data) {
clear();
return add(data);
}
mString& operator = (String data) {
clear();
return add(data);
Expand Down Expand Up @@ -338,7 +271,7 @@ class mString {

// convert & parse
char operator [] (uint16_t index) const {
return buf[index];//(index < size ? buf[index] : 0);
return buf[index];
}
char& operator [] (uint16_t index) {
return buf[index];
Expand Down Expand Up @@ -372,6 +305,18 @@ class mString {
strcpy(arr, buf + from);
buf[to] = backup;
}
int split(char** ptrs, char div = ',') {
int i = 0, j = 1;
ptrs[0] = buf;
while (buf[i]) {
if (buf[i] == div) {
buf[i] = NULL;
ptrs[j++] = buf + i + 1;
}
i++;
}
return j;
}
void truncate(uint16_t amount) {
uint16_t len = length();
if (amount >= len) clear();
Expand Down
Loading

0 comments on commit e6e25df

Please sign in to comment.