32 broken

This commit is contained in:
Yuri Trikoz
2020-06-22 03:11:02 +03:00
parent 40688130fd
commit 092bec7dde
34 changed files with 1308 additions and 341 deletions

7
include/Clock.h Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
#include "Global.h"
void startTimeSync();
void reconfigTime();

22
include/CommonTypes.h Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
enum ErrorType_t {
ET_NONE,
ET_FUNCTION,
ET_MODULE,
ET_SYSTEM
};
enum ErrorLevel_t {
EL_NONE,
EL_INFO,
EL_WARNING,
EL_ERROR
};
enum LedStatus_t {
LED_OFF,
LED_ON,
LED_SLOW,
LED_FAST
};

View File

@@ -2,19 +2,7 @@
#include <Arduino.h>
enum ErrorType_t {
ET_NONE,
ET_FUNCTION,
ET_MODULE,
ET_SYSTEM
};
enum ErrorLevel_t {
EL_NONE,
EL_INFO,
EL_WARNING,
EL_ERROR
};
#include "CommonTypes.h"
String getErrorLevelStr(ErrorLevel_t level);

View File

@@ -10,14 +10,18 @@
#include "ESP8266.h"
//
#include "Consts.h"
#include "CommonTypes.h"
#include "Errors.h"
#include "GyverFilters.h"
#include "UptimeInterval.h"
#include "Clock.h"
#include "Utils\FileUtils.h"
#include "Utils\JsonUtils.h"
#include "Utils\StringUtils.h"
#include "Utils\SysUtils.h"
#include "Utils\TimeUtils.h"
#include "Utils\PrintMessage.h"
#include "Utils\WiFiUtils.h"
//=========ПОДКЛЮЧЕНИЕ ОБЩИХ БИБЛИОТЕК===============
#include <Adafruit_BME280.h>
@@ -60,7 +64,7 @@ extern DallasTemperature sensors;
* Global vars
*/
enum { ROUTER_SEARCHING,
enum { WIFI_SCAN,
WIFI_MQTT_CONNECTION_CHECK,
SENSORS,
STEPPER1,
@@ -188,7 +192,7 @@ extern String getURL(const String &urls);
extern void servo_();
extern void led_blink(String satus);
extern void setLedStatus(LedStatus_t);
// Mqtt
extern void MQTT_init();
@@ -200,12 +204,6 @@ extern void sendCONTROL(String id, String topik, String state);
extern void do_mqtt_connection();
extern void handleMQTT();
// WiFiUtils
extern void WIFI_init();
extern void All_init();
extern bool StartAPMode();
extern void ROUTER_Connecting();
//Scenario
extern void eventGen(String event_name, String number);
extern String add_set(String param_name);
@@ -259,7 +257,7 @@ extern void delTimer(String number);
extern int readTimer(int number);
//Upgrade
extern void initUpgrade();
extern void init_updater();
// widget
extern void createWidget(String widget_name, String page_name, String page_number, String file, String topic);
@@ -290,4 +288,6 @@ extern void do_upgrade();
extern void uptime_init();
// Web
extern void web_init();
extern void web_init();
extern void telemetry_init();

View File

@@ -0,0 +1,72 @@
#pragma once
#include <Arduino.h>
class CharBuffer : Print {
public:
CharBuffer(size_t size) : _capacity(size < 2 ? 2 : size), _write(0), _read(0) {
_pool = new char[_capacity + 1];
memset(_pool, 0, _capacity + 1);
}
CharBuffer(const CharBuffer &src) {
_capacity = src._capacity;
_write = src._write;
memcpy(_pool, src._pool, src._write);
}
CharBuffer(const char *str) : CharBuffer(strlen(str) + 1) {
write((const uint8_t *)str, strlen(str));
}
~CharBuffer() {
delete _pool;
}
void clear() {
memset(_pool, 0, _capacity);
_write = 0;
_read = 0;
}
size_t size() const { return _capacity; }
size_t free() const { return _capacity - _write - 2; }
size_t available() const { return _write; }
const char *c_str() {
if (_pool[_write] != '\x00')
_pool[_write] = '\x00';
return _pool;
}
size_t write(char ch) {
return write((uint8_t)ch);
};
size_t write(const uint8_t ch) {
size_t n = 0;
if (_write < (_capacity - 2)) {
_pool[_write++] = ch;
n = 1;
}
return n;
}
size_t write(const uint8_t *ptr, const size_t size) {
size_t n = 0;
while (n < size) {
uint8_t ch = ptr[n++];
if (!write(ch))
break;
}
return n;
}
protected:
char *_pool;
size_t _capacity;
size_t _write;
size_t _read;
};

View File

@@ -0,0 +1,86 @@
#pragma once
#include <Arduino.h>
template <typename T, size_t BUFFER_SIZE>
class CircularBuffer {
public:
CircularBuffer() : _head{0}, _tail{0}, _full{false} {}
~CircularBuffer() {}
void reset() {
_head = _tail = _full = 0;
}
bool empty() const {
return _head == _tail && !_full;
}
bool full() const {
return _full;
}
size_t size() const {
size_t res = 0;
if (!_full) {
if (_head < _tail)
res = BUFFER_SIZE + _head - _tail;
else
res = _head - _tail;
} else {
res = BUFFER_SIZE;
}
return res;
}
void push(const T &item) {
if (_full) {
_tail++;
if (_tail == BUFFER_SIZE)
_tail = 0;
}
_pool[_head++] = item;
if (_head == BUFFER_SIZE)
_head = 0;
if (_head == _tail)
_full = true;
}
bool pop(T &item) {
bool res = false;
if (!empty()) {
item = _pool[_tail++];
if (_tail == BUFFER_SIZE) _tail = 0;
_full = false;
res = true;
}
return res;
}
bool pop_back(T &item) {
bool res = false;
if (!empty()) {
item = _pool[--_head];
_full = false;
res = true;
}
return res;
}
bool peek(T &item) const {
bool res = false;
if (!empty()) {
item = _pool[_tail];
res = true;
}
return res;
}
private:
T _pool[BUFFER_SIZE];
size_t _head;
size_t _tail;
bool _full;
};

View File

@@ -0,0 +1,45 @@
#pragma once
#include <Arduino.h>
#include "Module/Terminal.h"
#include "Module/CircularBuffer.h"
#include "Module/Runner.h"
class CommandShell {
public:
CommandShell(Runner *runner);
void setTerm(Terminal *term);
Terminal *term();
void showGreetings(bool = true);
void showFarewell(bool = true);
void clearHistory();
void addHistory(const char *);
bool getHistoryInput(String &);
void setEditLine(const String &);
bool active();
void loop();
private:
size_t printGreetings(Print *);
size_t printFarewell(Print *);
size_t printPrompt(Print *);
void onOpen(Print *out);
void onClose(Print *out);
void onData(const char *);
void onHistory(Print *out);
bool getLastInput(String &);
private:
CircularBuffer<String, 4> _history;
Terminal *_term;
Runner *_runner;
String _path;
bool _active;
bool _greetings;
bool _farewell;
};

68
include/Module/EditLine.h Normal file
View File

@@ -0,0 +1,68 @@
#pragma once
#include "Module/CharBuffer.h"
class EditLine : public CharBuffer {
public:
EditLine(size_t size) : CharBuffer(size){};
char &operator[](size_t i) { return _pool[i]; }
char operator[](size_t i) const { return _pool[i]; }
EditLine &operator=(const EditLine &src) {
delete[] _pool;
_pool = new char[src._capacity];
memcpy(_pool, src._pool, src._capacity);
_read = src._read;
_write = src._write;
return *this;
}
void del() {
size_t i;
for (i = _write; i < _capacity; ++i)
_pool[i] = _pool[i + 1];
_pool[i] = '\x00';
}
bool backspace() {
bool res = false;
if (prev()) {
del();
res = true;
}
return res;
}
bool next() {
bool res = false;
if (_write < _capacity - 1) {
_write++;
res = true;
}
return res;
}
bool prev() {
bool res = false;
if (_write > 0) {
_write--;
res = true;
}
return res;
}
size_t home() {
size_t res = _write;
_write = 0;
return res;
}
size_t end() {
size_t n;
for (n = 0; n < _capacity - 1; ++n)
if (_pool[n] == '\x00') break;
return n;
}
};

84
include/Module/Module.h Normal file
View File

@@ -0,0 +1,84 @@
#pragma once
#include <Arduino.h>
enum ModuleState_t {
MOD_INIT,
MOD_INIT_FAILED,
MOD_INIT_COMPLETE,
MOD_START_FAILED,
MOD_ACTIVE
};
class Module {
protected:
virtual bool onInit() { return true; };
virtual void onEnd(){};
virtual bool onStart() { return true; }
virtual void onStop(){};
virtual void onLoop() = 0;
protected:
Print *_out;
public:
Module() : _state{MOD_INIT} {}
bool init(bool force = false) {
if (_state > MOD_INIT_COMPLETE) {
return true;
}
if (_state == MOD_INIT_FAILED && !force) {
return false;
}
_state = onInit() ? MOD_INIT_COMPLETE : MOD_INIT_FAILED;
return _state == MOD_INIT_COMPLETE;
}
bool start(bool force = false) {
if (_state == MOD_ACTIVE) {
return true;
}
if (_state == MOD_START_FAILED && !force) {
return false;
}
if (_state < MOD_INIT_COMPLETE) {
if (!init(force)) {
return false;
}
}
_state = onStart() ? MOD_ACTIVE : MOD_START_FAILED;
return _state == MOD_ACTIVE;
}
void stop() {
if (_state < MOD_ACTIVE) {
return;
}
onStop();
_state = MOD_INIT_COMPLETE;
};
void end() {
if (_state < MOD_INIT_FAILED) {
return;
}
onEnd();
_state = MOD_INIT;
};
void loop() {
if (_state == MOD_ACTIVE || start()) onLoop();
};
void setOutput(Print *p) { _out = p; }
ModuleState_t getState() {
return _state;
}
private:
ModuleState_t _state;
};

8
include/Module/Runner.h Normal file
View File

@@ -0,0 +1,8 @@
#pragma once
#include <Print.h>
class Runner {
public:
virtual void run(const char*, Print*);
};

50
include/Module/Telnet.h Normal file
View File

@@ -0,0 +1,50 @@
#pragma once
#include "Global.h"
#include "Module/Module.h"
#include "Module/Terminal.h"
#include "Module/CommandShell.h"
#include <functional>
enum TelnetEvent_t {
TE_CONNECTED,
TE_DISCONNECTED
};
typedef std::function<void(TelnetEvent_t, WiFiClient*)> TelnetEventHandler;
class Telnet : public Module {
public:
Telnet(uint16_t port) : _port{port}, _lastConnected{false} {};
public:
void setEventHandler(TelnetEventHandler);
void sendData(const String&);
bool hasClient();
bool isShellActive();
protected:
bool onInit() override;
void onEnd() override;
bool onStart() override;
void onStop() override;
void onLoop() override;
private:
void onConnect();
void onDisconnect();
void onData();
void onOpen();
void onClose();
private:
TelnetEventHandler _eventHandler;
uint16_t _port;
bool _lastConnected;
WiFiClient _client;
WiFiServer* _server;
Terminal* _term;
CommandShell* _shell;
};

183
include/Module/Terminal.h Normal file
View File

@@ -0,0 +1,183 @@
#pragma once
#include "Module/EditLine.h"
#define A_NORMAL 0x0000 // normal
#define A_UNDERLINE 0x0001 // underline
#define A_REVERSE 0x0002 // reverse
#define A_BLINK 0x0004 // blink
#define A_BOLD 0x0008 // bold
#define A_DIM 0x0010 // dim
#define A_STANDOUT A_BOLD // standout (same as bold)
#define F_BLACK 0x0100 // foreground black
#define F_RED 0x0200 // foreground red
#define F_GREEN 0x0300 // foreground green
#define F_BROWN 0x0400 // foreground brown
#define F_BLUE 0x0500 // foreground blue
#define F_MAGENTA 0x0600 // foreground magenta
#define F_CYAN 0x0700 // foreground cyan
#define F_WHITE 0x0800 // foreground white
#define F_YELLOW F_BROWN // some terminals show brown as yellow (with A_BOLD)
#define F_COLOR 0x0F00 // foreground mask
#define B_BLACK 0x1000 // background black
#define B_RED 0x2000 // background red
#define B_GREEN 0x3000 // background green
#define B_BROWN 0x4000 // background brown
#define B_BLUE 0x5000 // background blue
#define B_MAGENTA 0x6000 // background magenta
#define B_CYAN 0x7000 // background cyan
#define B_WHITE 0x8000 // background white
#define B_YELLOW B_BROWN // some terminals show brown as yellow (with A_BOLD)
#define B_COLOR 0xF000 // background mask
#define CHAR_NULL 0x00
#define CHAR_BEL 0x07
#define CHAR_BS 0x08
#define CHAR_SPACE 0x20
#define CHAR_TAB 0x09
#define CHAR_LF 0x0a
#define CHAR_CR 0x0d
#define CHR_ZERO 0x30
#define KEY_DEL 0x7f
#define KEY_DOWN 0x80
#define KEY_UP 0x81
#define KEY_LEFT 0x82
#define KEY_RIGHT 0x83
#define KEY_HOME 0x84
#define KEY_INS 0x86
#define KEY_PAGE_DOWN 0x87
#define KEY_PAGE_UP 0x88
#define KEY_END 0x89
#define CHAR_LT 0x8b
#define CHAR_CSI 0x9b
#define CHAR_ESC 0x1b
#define CHAR_BIN 0xFF
#define ESC_CURSOR_HOME "\x1b[H"
#define ESC_SAVE_CURSOR "\x1b[s"
#define ESC_UNSAVE_CURSOR "\x1b[u"
#define ESC_SAVE_CURSOR_AND_ATTRS "\x1b[7"
#define ESC_RESTORE_CURSOR_AND_ATTRS "\x1b[8"
#define ESC_CLEAR "\x1b[2J"
#define ESC_CLEAR_BOTTOM "\x1b[J"
#define ESC_CLEAR_EOL "\x1b[0K"
#define ESC_CURSOR_UP "\x1b[1A"
#define ESC_CURSOR_DOWN "\x1b[1B"
#define ESC_CURSOR_FORWARD "\x1b[1C"
#define ESC_CURSOR_BACKWARD "\x1b[1D"
#define SEQ_CSI PSTR("\033[") // code introducer
#define SEQ_LOAD_G1 PSTR("\033)0") // load G1 character set
#define SEQ_CLEAR PSTR("\033[2J") // clear screen
#define SEQ_ATTRSET PSTR("\033[0") // set attributes, e.g. "\033[0;7;1m"
#define SEQ_ATTRSET_BOLD PSTR(";1") // bold
#define SEQ_ATTRSET_DIM PSTR(";2") // dim
#define SEQ_ATTRSET_FCOLOR PSTR(";3") // forground color
#define SEQ_ATTRSET_UNDERLINE PSTR(";4") // underline
#define SEQ_ATTRSET_BCOLOR PSTR(";4") // background color
#define SEQ_ATTRSET_BLINK PSTR(";5") // blink
#define SEQ_ATTRSET_REVERSE PSTR(";7") // reverse
enum TerminalEventEnum {
EVENT_OPEN,
EVENT_CLOSE,
EVENT_TAB
};
enum SpecialKeyEnum { SPEC_KEY_UP,
SPEC_KEY_TAB,
SPEC_KEY_ENTER,
SPEC_KEY_ESC };
typedef std::function<bool(SpecialKeyEnum key)> SpecialKeyPressedEvent;
typedef std::function<void(TerminalEventEnum, Stream *)> TerminalEventHandler;
typedef std::function<void(const char *)> TerminalInputEventHandler;
enum EOLType_t { CRLF,
LFCR,
LF,
CR };
enum State { ST_INACTIVE,
ST_NORMAL,
ST_ESC_SEQ,
ST_CTRL_SEQ };
class Terminal : public Print {
public:
Terminal(Stream *stream = nullptr);
~Terminal();
void setStream(Stream *stream);
void setEOL(EOLType_t code);
void enableControlCodes(bool enabled = true);
void enableEcho(bool enabled = true);
void enableColors(bool enabled = true);
void setOnEvent(TerminalEventHandler);
void setOnSpecKeyPress(SpecialKeyPressedEvent);
void setOnReadLine(TerminalInputEventHandler);
bool setLine(const uint8_t *bytes, size_t size);
CharBuffer &getLine();
void backsp();
void clear();
void clear_line();
size_t println(const char *str);
size_t println(void);
size_t write_P(PGM_P str);
size_t write(uint8_t c);
size_t write(const uint8_t *buf, size_t size);
void writeByDigit(uint8_t i);
bool available();
void loop();
void start();
void quit();
void initscr();
void attrset(uint16_t attr);
private:
void move(uint8_t y, uint8_t x);
TerminalEventHandler eventHandler_;
TerminalInputEventHandler inputHandler_;
uint8_t attr = 0xff;
uint8_t curY = 0xff;
uint8_t curX = 0xff;
unsigned long lastReceived = 0;
State state = ST_INACTIVE;
Stream *_stream;
EditLine _line;
char cc_buf[32] = {0};
size_t cc_pos = 0;
bool _color = false;
bool _controlCodes = false;
bool _echo = false;
EOLType_t _eol = CRLF;
struct ControlCode {
const char *cc;
const char ch;
};
ControlCode keyMap[10] = {
{"G", KEY_HOME}, // 71 Home key
{"H", KEY_UP}, // 72 Up arrow
{"I", KEY_PAGE_UP}, // 73 PageUp
{"K", KEY_LEFT}, // 75 Left arrow
{"M", KEY_RIGHT}, // 77 Right arrow
{"O", KEY_END}, // 79 End key
{"P", KEY_DOWN}, // 80 Down arrow
{"Q", KEY_PAGE_DOWN}, // 81 PageDown
{"R", KEY_INS}, // 82 Insert
{"S", KEY_DEL}, // 83 Delete
};
};

View File

@@ -4,7 +4,7 @@
class UptimeInterval {
public:
UptimeInterval(unsigned long interval, boolean postpone = true) : _interval{interval} {
UptimeInterval(unsigned long interval, boolean postpone = true) : _next{0}, _interval{interval} {
reset(postpone);
}
@@ -28,5 +28,5 @@ class UptimeInterval {
static unsigned long _uptime_seconds;
private:
unsigned long _interval, _next;
unsigned long _next, _interval;
};

View File

@@ -0,0 +1,55 @@
#pragma once
#include <Arduino.h>
#include "FS.h"
#ifdef ESP32
#include "LITTLEFS.h"
#define LittleFS LITTLEFS
#endif
#ifdef ESP8266
#include <LittleFS.h>
#endif
class FileHelper {
public:
FileHelper(const String filename);
/*
* Проверить существование
*/
void exists();
/*
* Удалить файл
*/
void remove();
/*
* Открыть файл установить позицию @position
*/
File seek(size_t position = 0);
/*
* Чтение строки с содержащей @substr
*/
String readFileString(const String substr);
/*
* Добовление строки @str в файл
*/
String appendStr(const String str);
/*
* Запись строки
*/
String writeStr(const String);
/*
* Чтение в строку
*/
String readStr(size_t);
/*
* Размер в байтах
*/
size_t getSize();
};

View File

@@ -11,6 +11,7 @@
#ifdef ESP8266
#include <LittleFS.h>
#endif
/*
* Инициализация ФС
*/

View File

@@ -0,0 +1,32 @@
#pragma once
#include "Arduino.h"
#include "CommonTypes.h"
#include "Utils\StringUtils.h"
#include "Utils\TimeUtils.h"
#include "Errors.h"
#define pm PrintMessage(MODULE)
class PrintMessage {
public:
PrintMessage(const char* module) {
_module = module;
}
void error(const String str) {
print(EL_ERROR, str);
}
void info(const String str) {
print(EL_INFO, str);
}
private:
void print(const ErrorLevel_t level, const String& str) {
Serial.printf("%s [%s] [%s] %s\n", prettyMillis().c_str(), getErrorLevelStr(level).c_str(), _module, str.c_str());
}
private:
const char* _module;
};

View File

@@ -2,6 +2,8 @@
#include <Arduino.h>
#include "CommonTypes.h"
uint8_t hexStringToUint8(String hex);
uint16_t hexStringToUint16(String hex);

View File

@@ -7,10 +7,6 @@
void Time_Init();
void time_check();
void reconfigTime();
/*
* Получение текущего времени
*/
@@ -22,7 +18,7 @@ String getTimeUnix();
/*
* Параметр время
* Результат выполнения
* @result результат
*/
boolean getUnixTimeStr(String&);
@@ -38,3 +34,9 @@ String getDateDigitalFormated();
int timeToMin(String Time);
const String prettyMillis(unsigned long time_ms = millis());
int timeZoneInSeconds(const byte timeZone);
bool hasTimeSynced();
int getBiasInSeconds();

10
include/Utils/WiFiUtils.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
#include "Global.h"
boolean scanWiFi(String ssid);
void startSTAMode();
bool startAPMode();