Skip to content

Commit d5304bc

Browse files
committed
Extract various classes/methods from main.cpp
1 parent c6d0041 commit d5304bc

6 files changed

Lines changed: 81 additions & 66 deletions

File tree

platformio.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@ framework = arduino
5454
[debug]
5555
build_type = debug
5656

57-
5857
[env:lolin_s2_mini]
5958
board = lolin_s2_mini
6059

6160
[env:esp32_c3_supermini]
6261
board = lolin_c3_mini
63-
64-

src/asyncbufferedtcplogger.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "asyncbufferedtcplogger.h"
2+
3+
APB::AsyncBufferedTCPLogger::AsyncBufferedTCPLogger(AsyncServer & loggerServer) {
4+
loggerServer.onClient([this](void *,AsyncClient *c){
5+
this->client = c;
6+
}, nullptr);
7+
}
8+
9+
size_t APB::AsyncBufferedTCPLogger::write(uint8_t c) {
10+
if(!client) {
11+
return 0;
12+
}
13+
buffer[currentPosition++] = c;
14+
if(c == '\n') {
15+
client->write(buffer.data(), currentPosition);
16+
reset();
17+
}
18+
return 1;
19+
}
20+
21+
void APB::AsyncBufferedTCPLogger::reset() {
22+
std::fill(std::begin(buffer), std::end(buffer), 0);
23+
currentPosition = 0;
24+
}

src/asyncbufferedtcplogger.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include <Print.h>
3+
#include <AsyncTCP.h>
4+
5+
namespace APB {
6+
class AsyncBufferedTCPLogger: public Print {
7+
public:
8+
AsyncBufferedTCPLogger(AsyncServer &loggerServer);
9+
10+
virtual size_t write(uint8_t c);
11+
private:
12+
AsyncClient *client = nullptr;
13+
void reset();
14+
std::array<char, 1024> buffer = {0};
15+
uint16_t currentPosition = 0;
16+
};
17+
18+
}

src/history.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,34 @@ void APB::History::Entry::setNullableFloat(JsonObject object, const char *field,
6666
}
6767
}
6868

69-
void APB::History::add(const Entry &entry)
70-
{
71-
if(lockInserts) {
72-
Log.warningln("[HISTORY] Inserts locked, unable to add entry");
73-
return;
74-
}
75-
_entries.push_back(entry);
76-
while(_entries.size() > maxSize) {
77-
_entries.pop_front();
78-
}
69+
void APB::History::add() {
70+
if(lockInserts) {
71+
Log.warningln("[HISTORY] Inserts locked, unable to add entry");
72+
return;
73+
}
74+
75+
APB::History::Entry entry {
76+
esp_timer_get_time() / 1000'000
77+
};
78+
79+
#ifndef APB_AMBIENT_TEMPERATURE_SENSOR_NONE
80+
entry.setAmbient(APB::Ambient::Instance.reading());
81+
#endif
82+
83+
#if APB_HEATERS_SIZE > 0
84+
for(uint8_t i=0; i<APB_HEATERS_TEMP_SENSORS; i++) {
85+
entry.heaters[i].set(APB::Heaters::Instance[i]);
86+
}
87+
#endif
88+
entry.setPower(APB::PowerMonitor::Instance.status());
89+
90+
_entries.push_back(entry);
91+
while(_entries.size() > maxSize) {
92+
_entries.pop_front();
93+
}
7994
}
8095

96+
8197
#define JSON_SERIALISER_TAG "[History::JsonSerialiser] "
8298

8399
APB::History::JsonSerialiser::JsonSerialiser(History &history) : history{history} {
@@ -127,3 +143,7 @@ int APB::History::JsonSerialiser::write(uint8_t *buffer, size_t maxLen, size_t i
127143
}
128144
return response;
129145
}
146+
147+
void APB::History::setup(Scheduler &scheduler) {
148+
new Task(APB_HISTORY_TASK_SECONDS, TASK_FOREVER, std::bind(&History::add, this), &scheduler, true);
149+
}

src/history.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <optional>
77
#include <ArduinoJson.h>
88
#include <memory>
9+
#include <TaskSchedulerDeclarations.h>
910

1011
#include "ambient/ambient.h"
1112
#include "powermonitor.h"
@@ -73,9 +74,10 @@ class History {
7374
std::unique_ptr<OverflowPrint> overflowPrint;
7475
};
7576

77+
void setup(Scheduler &scheduler);
7678
void setMaxSize(uint16_t maxSize) { this->maxSize = maxSize; }
79+
void add();
7780

78-
void add(const Entry &entry);
7981
Entries entries() const { return _entries; }
8082

8183
size_t jsonSize() const { return _entries.size() * HISTORY_ENTRY_SIZE; }

src/main.cpp

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,15 @@
1717
#include "statusled.h"
1818
#include <ArduinoOTA.h>
1919
#include <AsyncTCP.h>
20+
#include "asyncbufferedtcplogger.h"
2021

22+
#include "time.h"
23+
#include "esp_sntp.h"
2124
Scheduler scheduler;
2225
AsyncServer loggerServer{9911};
2326

24-
class BufferedLogger: public Print {
25-
public:
26-
BufferedLogger() {
27-
loggerServer.onClient([this](void *,AsyncClient *c){
28-
this->client = c;
29-
}, nullptr);
30-
}
31-
32-
virtual size_t write(uint8_t c) {
33-
if(!client) {
34-
return 0;
35-
}
36-
buffer[currentPosition++] = c;
37-
if(c == '\n') {
38-
client->write(buffer.data(), currentPosition);
39-
reset();
40-
}
41-
return 1;
42-
}
43-
44-
private:
45-
AsyncClient *client = nullptr;
46-
void reset() {
47-
std::fill(std::begin(buffer), std::end(buffer), 0);
48-
currentPosition = 0;
49-
}
50-
std::array<char, 1024> buffer = {0};
51-
uint16_t currentPosition = 0;
52-
};
53-
54-
BufferedLogger bufferedLogger;
27+
28+
APB::AsyncBufferedTCPLogger bufferedLogger{loggerServer};
5529

5630
#ifdef ONEBUTTON_USER_BUTTON_1
5731
OneButton userButton;
@@ -66,7 +40,6 @@ APB::WebServer webServer(scheduler);
6640
using namespace std::placeholders;
6741

6842
void setupArduinoOTA();
69-
void addHistoryEntry();
7043

7144
void setup() {
7245
Serial.begin(115200);
@@ -97,6 +70,7 @@ void setup() {
9770

9871
webServer.setup();
9972
setupArduinoOTA();
73+
APB::History::Instance.setup(scheduler);
10074

10175
#ifdef ONEBUTTON_USER_BUTTON_1
10276
userButton.attachDoubleClick([]() {
@@ -105,10 +79,8 @@ void setup() {
10579
});
10680
userButton.setup(ONEBUTTON_USER_BUTTON_1, INPUT, false);
10781
#endif
108-
new Task(APB_HISTORY_TASK_SECONDS, TASK_FOREVER, addHistoryEntry, &scheduler, true);
10982
}
11083

111-
uint64_t el = 0;
11284
void loop() {
11385
APB::WiFiManager::Instance.loop();
11486
scheduler.execute();
@@ -120,24 +92,6 @@ void loop() {
12092
ArduinoOTA.handle();
12193
}
12294

123-
void addHistoryEntry() {
124-
APB::History::Entry entry {
125-
esp_timer_get_time() / 1000'000
126-
};
127-
128-
#ifndef APB_AMBIENT_TEMPERATURE_SENSOR_NONE
129-
entry.setAmbient(APB::Ambient::Instance.reading());
130-
#endif
131-
132-
#if APB_HEATERS_SIZE > 0
133-
for(uint8_t i=0; i<APB_HEATERS_TEMP_SENSORS; i++) {
134-
entry.heaters[i].set(APB::Heaters::Instance[i]);
135-
}
136-
#endif
137-
entry.setPower(APB::PowerMonitor::Instance.status());
138-
APB::History::Instance.add(entry);
139-
}
140-
14195
void setupArduinoOTA() {
14296
ArduinoOTA
14397
.onStart([]() {

0 commit comments

Comments
 (0)