This commit is contained in:
Yuri Trikoz
2020-06-25 05:09:11 +03:00
parent 3daa517c7e
commit b50e1c9791
73 changed files with 260 additions and 296 deletions

View File

@@ -15,27 +15,34 @@ bool fileSystemInit() {
return true;
}
void removeFile(const String filename) {
if (!LittleFS.remove(filepath(filename))) {
pm.error("remove " + filename);
void removeFile(const String& filename) {
String path = filepath(filename);
if (LittleFS.exists(path)) {
if (!LittleFS.remove(path)) {
pm.error("remove " + path);
}
} else {
pm.info("not exist" + path);
}
}
File seekFile(const String filename, size_t position) {
auto file = LittleFS.open(filepath(filename), "r");
File seekFile(const String& filename, size_t position) {
String path = filepath(filename);
auto file = LittleFS.open(path, "r");
if (!file) {
pm.error("open " + filename);
pm.error("open " + path);
}
// поставим курсор в начало файла
file.seek(position, SeekSet);
return file;
}
String readFileString(const String filename, const String to_find) {
String res = "Failed";
auto file = LittleFS.open(filepath(filename), "r");
const String readFileString(const String& filename, const String& to_find) {
String path = filepath(filename);
String res = "failed";
auto file = LittleFS.open(path, "r");
if (!file) {
return "Failed";
return "failed";
}
if (file.find(to_find.c_str())) {
res = file.readStringUntil('\n');
@@ -44,19 +51,21 @@ String readFileString(const String filename, const String to_find) {
return res;
}
String addFile(const String filename, const String str) {
auto file = LittleFS.open(filepath(filename), "a");
const String addFile(const String& filename, const String& str) {
String path = filepath(filename);
auto file = LittleFS.open(path, "a");
if (!file) {
return "Failed";
return "failed";
}
file.println(str);
file.close();
return "Sucсess";
return "sucсess";
}
bool copyFile(const String src, const String dst, bool overwrite) {
bool copyFile(const String& src, const String& dst, bool overwrite) {
String srcPath = filepath(src);
String dstPath = filepath(dst);
pm.info("copy " + srcPath + " to " + dstPath);
if (!LittleFS.exists(srcPath)) {
pm.error("not exist: " + srcPath);
return false;
@@ -81,35 +90,37 @@ bool copyFile(const String src, const String dst, bool overwrite) {
return true;
}
String writeFile(const String filename, const String str) {
auto file = LittleFS.open(filepath(filename), "w");
const String writeFile(const String& filename, const String& str) {
String path = filepath(filename);
auto file = LittleFS.open(path, "w");
if (!file) {
return "Failed";
return "failed";
}
file.print(str);
file.close();
return "Sucсess";
return "sucсess";
}
String readFile(const String filename, size_t max_size) {
auto file = LittleFS.open(filepath(filename), "r");
const String readFile(const String& filename, size_t max_size) {
String path = filepath(filename);
auto file = LittleFS.open(path, "r");
if (!file) {
return "Failed";
return "failed";
}
size_t size = file.size();
if (size > max_size) {
file.close();
return "Large";
return "large";
}
String temp = file.readString();
file.close();
return temp;
}
String getFileSize(const String filename) {
const String getFileSize(const String filename) {
auto file = LittleFS.open(filepath(filename), "r");
if (!file) {
return "Failed";
return "failed";
}
size_t size = file.size();
file.close();

View File

@@ -1,54 +1,9 @@
#include "Utils/PresetUtils.h"
static const char* item_names[NUM_ITEMS] = {"relay", "pwm",
"dht11", "dht22", "analog",
"bmp280", "bme280", "dallas",
"termostat", "ultrasonic",
"motion", "stepper",
"servo", "firmware"};
static const char* config_file_fmt PROGMEM = "/conf/%s%03d.txt";
static const char* config_file_fmt = "/conf/%03d%s.txt";
const String getPresetFile(uint8_t preset, ConfigType_t type) {
const String getConfigFile(uint8_t preset, ConfigType_t type) {
char buf[64];
sprintf(buf, config_file_fmt, preset, type == CT_MACRO ? "c" : "s");
sprintf_P(buf, config_file_fmt, (type == CT_CONFIG) ? "c" : "s", preset);
return String(buf);
}
const String getItemName(Item_t item) {
return String(item_names[item]);
}
const Item_t getPresetItem(uint8_t num) {
Item_t res = NUM_ITEMS;
if (num >= 1 && num <= 7) {
res = RELAY;
} else if (num == 8) {
res = PWM;
} else if (num == 9) {
res = DHT11;
} else if (num == 10) {
res = DHT22;
} else if (num == 11) {
res = ANALOG;
} else if (num == 12) {
res = BMP280;
} else if (num == 13) {
res = BME280;
} else if (num == 14) {
res = DALLAS;
} else if (num == 15) {
res = TERMOSTAT;
} else if (num == 16) {
res = ULTRASONIC;
} else if (num >= 17 || num <= 18) {
res = MOTION;
} else if (num == 19) {
res = STEPPER;
} else if (num == 20) {
res = SERVO;
} else if (num == 21) {
res = FIRMWARE;
}
return res;
}

View File

@@ -28,7 +28,7 @@ time_t getSystemTime() {
}
bool hasTimeSynced() {
time_t now = time(nullptr);
unsigned long now = time(nullptr);
return now > millis();
}

30
src/Utils/i2c_bus.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "Utils/i2c_bus.h"
#include "Global.h"
void do_i2c_scanning() {
String tmp = i2c_scan();
if (tmp == "error") {
tmp = i2c_scan();
}
jsonWriteStr(configLiveJson, "i2c", tmp);
}
const String i2c_scan() {
String out;
byte count = 0;
Wire.begin();
for (byte i = 8; i < 120; i++) {
Wire.beginTransmission(i);
if (Wire.endTransmission() == 0) {
count++;
out += String(count) + ". 0x" + String(i, HEX) + "; ";
delay(1);
}
}
if (count == 0) {
return "error";
} else {
return out;
}
}