добавил централизованную функцию serial print и утилиты времени

This commit is contained in:
Dmitry Borisenko
2021-12-22 21:13:24 +01:00
parent 7a76ddcc93
commit d18463bfe1
6 changed files with 221 additions and 7 deletions

View File

@@ -1,6 +1,10 @@
#pragma once
//внешние глобальные директории
#include <Arduino.h>
#include <ArduinoJson.h>
#include <FS.h>
#include <StreamUtils.h>
#include <StreamUtils.h>
//внутренние глобальные директории проекта
#include "Utils\SerialPrint.h"

View File

@@ -0,0 +1,5 @@
#pragma once
#include "Global.h"
#include "Utils/TimeUtils.h"
void SerialPrint(String errorLevel, String module, String msg);

65
include/utils/TimeUtils.h Normal file
View File

@@ -0,0 +1,65 @@
#pragma once
#include "Global.h"
#define ONE_MINUTE_s 60
#define ONE_HOUR_m 60
#define ONE_HOUR_s 60 * ONE_MINUTE_s
#define LEAP_YEAR(Y) (((1970 + Y) > 0) && !((1970 + Y) % 4) && (((1970 + Y) % 100) || !((1970 + Y) % 400)))
#define MIN_DATETIME 1575158400
#define ONE_SECOND_ms 1000
/*
* Время (мс) прошедщее с @since
*/
unsigned long millis_since(unsigned long sinse);
/*
* Интерввал времени (мс) между @start и @finish
*/
unsigned long millis_passed(unsigned long start, unsigned long finish);
/*
* Форматиронное время интервала (мс)
* "чч:мм:cc",
* "дд чч:мм", если > 24 часов
*/
const String prettyMillis(unsigned long time_ms = millis());
/*
* Форматиронное время интервала (c)
* "чч:мм:cc",
* "дд чч:мм", если > 24 часов
*/
const String prettySeconds(unsigned long time_s);
/*
* Тайм зона в секундах
*/
int getOffsetInSeconds(int timezone);
/*
* Тайм зона в минутах
*/
int getOffsetInMinutes(int timezone);
/*
* Разбивает время на составляющие
*/
struct Time_t {
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t day_of_week;
uint8_t day_of_month;
uint8_t month;
uint16_t day_of_year;
uint16_t year;
unsigned long days;
unsigned long valid;
};
void breakEpochToTime(unsigned long epoch, Time_t& tm);
// void timeInit();

View File

@@ -2,10 +2,10 @@
bool fileSystemInit() {
if (!FileFS.begin()) {
Serial.println("FS Init ERROR, may be FS was not flashed");
SerialPrint(F("E"), F("FS"), F("Init ERROR, may be FS was not flashed"));
return false;
}
Serial.println("FS Init completed");
SerialPrint(F("i"), F("FS"), F("Init completed"));
return true;
}
@@ -13,7 +13,7 @@ File seekFile(const String& filename, size_t position) {
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
Serial.println("[E] file error");
SerialPrint(F("E"), F("FS"), F("seek file error"));
}
file.seek(position, SeekSet);
return file;
@@ -53,9 +53,9 @@ const String filepath(const String& filename) {
bool cutFile(const String& src, const String& dst) {
String srcPath = filepath(src);
String dstPath = filepath(dst);
Serial.println("cut " + srcPath + " to " + dstPath);
SerialPrint(F("i"), F("FS"), "cut " + srcPath + " to " + dstPath);
if (!FileFS.exists(srcPath)) {
Serial.println("not exist: " + srcPath);
SerialPrint(F("E"), F("FS"), "not exist: " + srcPath);
return false;
}
if (FileFS.exists(dstPath)) {
@@ -63,7 +63,6 @@ bool cutFile(const String& src, const String& dst) {
}
auto srcFile = FileFS.open(srcPath, "r");
auto dstFile = FileFS.open(dstPath, "w");
uint8_t buf[512];
while (srcFile.available()) {
size_t len = srcFile.read(buf, 512);

View File

@@ -0,0 +1,9 @@
#include "Utils\SerialPrint.h"
void SerialPrint(String errorLevel, String module, String msg) {
Serial.println(prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg);
String tosend = "[" + errorLevel + "] [" + module + "] " + msg;
// ws.textAll(tosend);
}

132
src/utils/TimeUtils.cpp Normal file
View File

@@ -0,0 +1,132 @@
#include "Utils/TimeUtils.h"
static const uint8_t days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const char* TIME_FORMAT PROGMEM = "%02d:%02d:%02d";
static const char* TIME_FORMAT_WITH_DAYS PROGMEM = "%dd %02d:%02d";
const String prettySeconds(unsigned long time_s) {
unsigned long tmp = time_s;
unsigned long seconds;
unsigned long minutes;
unsigned long hours;
unsigned long days;
seconds = tmp % 60;
tmp = tmp / 60;
minutes = tmp % 60;
tmp = tmp / 60;
hours = tmp % 24;
days = tmp / 24;
char buf[32];
if (days) {
sprintf_P(buf, TIME_FORMAT_WITH_DAYS, days, hours, minutes, seconds);
} else {
sprintf_P(buf, TIME_FORMAT, hours, minutes, seconds);
}
return String(buf);
}
const String prettyMillis(unsigned long time_ms) {
return prettySeconds(time_ms / 1000);
}
unsigned long millis_since(unsigned long sinse) {
return millis_passed(sinse, millis());
}
unsigned long millis_passed(unsigned long start, unsigned long finish) {
unsigned long result = 0;
if (start <= finish) {
unsigned long passed = finish - start;
if (passed <= __LONG_MAX__) {
result = static_cast<long>(passed);
} else {
result = static_cast<long>((__LONG_MAX__ - finish) + start + 1u);
}
} else {
unsigned long passed = start - finish;
if (passed <= __LONG_MAX__) {
result = static_cast<long>(passed);
result = -1 * result;
} else {
result = static_cast<long>((__LONG_MAX__ - start) + finish + 1u);
result = -1 * result;
}
}
return result;
}
int getOffsetInSeconds(int timezone) {
return getOffsetInMinutes(timezone) * ONE_MINUTE_s;
}
int getOffsetInMinutes(int timezone) {
return timezone * ONE_HOUR_m;
}
void breakEpochToTime(unsigned long epoch, Time_t& tm) {
unsigned long time = epoch;
tm.second = time % 60;
time /= 60; // now it is minutes
tm.minute = time % 60;
time /= 60; // now it is hours
tm.hour = time % 24;
time /= 24; // now it is days
tm.days = time;
tm.day_of_week = ((time + 4) % 7) + 1; // Sunday is day 1
uint8_t year = 0;
unsigned long days = 0;
while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
year++;
}
tm.year = year - 30;
days -= LEAP_YEAR(year) ? 366 : 365;
time -= days; // now it is days in this year, starting at 0
tm.day_of_year = time;
uint8_t month;
uint8_t month_length;
for (month = 0; month < 12; month++) {
if (1 == month) { // february
if (LEAP_YEAR(year)) {
month_length = 29;
} else {
month_length = 28;
}
} else {
month_length = days_in_month[month];
}
if (time >= month_length) {
time -= month_length;
} else {
break;
}
}
tm.month = month + 1;
tm.day_of_month = time + 1;
tm.valid = (epoch > MIN_DATETIME);
}
//void timeInit() {
// ts.add(
// TIME, 1000, [&](void*) {
// String timenow = timeNow->getTimeWOsec();
// static String prevTime;
// if (prevTime != timenow) {
// prevTime = timenow;
// jsonWriteStr(configLiveJson, "timenow", timenow);
// eventGen2("timenow", timenow);
// SerialPrint("I", F("NTP"), timenow);
// }
// },
// nullptr, true);
// SerialPrint("I", F("NTP"), F("Handle time init"));
//}