Main firmware - basic functions
This commit is contained in:
parent
d8732643f3
commit
fb5c8361a7
|
@ -19,6 +19,21 @@ struct DummyWriter {
|
||||||
void write(const void*, uint32_t) {}
|
void write(const void*, uint32_t) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class CopyWriter {
|
||||||
|
public:
|
||||||
|
CopyWriter(uint8_t * bufferToWrite)
|
||||||
|
: bufferToWrite_( bufferToWrite )
|
||||||
|
{}
|
||||||
|
|
||||||
|
void write(const void* data, uint32_t length) {
|
||||||
|
memcpy(bufferToWrite_, data, length);
|
||||||
|
bufferToWrite_ += length;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t * bufferToWrite_;
|
||||||
|
};
|
||||||
|
|
||||||
template<typename Writer>
|
template<typename Writer>
|
||||||
class StreamingMsgPackEncoder
|
class StreamingMsgPackEncoder
|
||||||
{
|
{
|
||||||
|
@ -141,7 +156,7 @@ class ChunkedStreamingMsgPackEncoder
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ChunkedStreamingMsgPackEncoder(Writer * writer_, uint32_t offset, uint32_t maxSize)
|
ChunkedStreamingMsgPackEncoder(Writer * writer_, uint32_t offset, uint32_t maxSize)
|
||||||
: encoder_(writer_), offsetToStart_(offset), maxBytes_(maxSize), sentBytes_(0), sendingFinished_(false)
|
: encoder_(writer_), sentBytes_(0), maxBytes_(maxSize), offsetToStart_(offset), sendingFinished_(false)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void sendMap16(byte size) {
|
void sendMap16(byte size) {
|
||||||
|
@ -175,7 +190,6 @@ public:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t sizeForFullArray = sizeof(T) * length;
|
|
||||||
uint32_t elementsToSkip = 0;
|
uint32_t elementsToSkip = 0;
|
||||||
if( sentBytes_ < offsetToStart_ ) {
|
if( sentBytes_ < offsetToStart_ ) {
|
||||||
elementsToSkip = (offsetToStart_ - sentBytes_) / sizeof(T);
|
elementsToSkip = (offsetToStart_ - sentBytes_) / sizeof(T);
|
||||||
|
|
|
@ -20,7 +20,7 @@ lib_deps =
|
||||||
AsyncTCP
|
AsyncTCP
|
||||||
NTPClient
|
NTPClient
|
||||||
|
|
||||||
[env:native]
|
;[env:native]
|
||||||
platform = native
|
;platform = native
|
||||||
test_ignore = test_embedded
|
;test_ignore = test_embedded
|
||||||
build_flags = -g
|
;build_flags = -g
|
|
@ -5,6 +5,7 @@ const int CONFIG_SCALE_DOUT_PIN = D2;
|
||||||
const int CONFIG_SCALE_SCK_PIN = D3;
|
const int CONFIG_SCALE_SCK_PIN = D3;
|
||||||
const uint8_t CONFIG_TARE_AVG_COUNT = 50; // number of measurements in tare-phase (to find 0 )
|
const uint8_t CONFIG_TARE_AVG_COUNT = 50; // number of measurements in tare-phase (to find 0 )
|
||||||
const int CONFIG_VALUE_DIVIDER = 128; // uint32 measurements are divided by this factor, before stored in uint16_t
|
const int CONFIG_VALUE_DIVIDER = 128; // uint32 measurements are divided by this factor, before stored in uint16_t
|
||||||
|
const int CONFIG_MEASURE_DELAY = 100; // interval in ms between measurements
|
||||||
|
|
||||||
|
|
||||||
const uint32_t CONFIG_SESSION_CHUNK_SIZE = 1024*8 - 3 * sizeof(uint32_t);
|
const uint32_t CONFIG_SESSION_CHUNK_SIZE = 1024*8 - 3 * sizeof(uint32_t);
|
||||||
|
|
|
@ -26,14 +26,60 @@ template<typename Session_T>
|
||||||
class SessionManager
|
class SessionManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void begin();
|
SessionManager() : measuring_(false), lastCallTime_(0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void begin() {
|
||||||
|
scale.begin(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN);
|
||||||
|
scale.tare( CONFIG_TARE_AVG_COUNT );
|
||||||
|
|
||||||
|
session.init( timeClient.getEpochTime() );
|
||||||
|
}
|
||||||
|
|
||||||
|
void startMeasurements() {
|
||||||
|
measuring_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stopMeasurements() {
|
||||||
|
measuring_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void iteration() {
|
||||||
|
if( ! measuring_ ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uint16_t measurement;
|
||||||
|
scale.measure(measurement);
|
||||||
|
session.addPoint(measurement);
|
||||||
|
|
||||||
|
if( lastCallTime_ != 0) {
|
||||||
|
const long cycleDuration = millis() - lastCallTime_;
|
||||||
|
if( cycleDuration <= CONFIG_MEASURE_DELAY)
|
||||||
|
{
|
||||||
|
delay(CONFIG_MEASURE_DELAY - cycleDuration);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const long skipped = (cycleDuration / CONFIG_MEASURE_DELAY);
|
||||||
|
Serial.print("Warning: measurements skipped: ");
|
||||||
|
Serial.println(skipped);
|
||||||
|
|
||||||
|
for(int i=0; i < skipped; ++i)
|
||||||
|
session.addPoint(measurement);
|
||||||
|
|
||||||
|
delay(CONFIG_MEASURE_DELAY * (skipped + 1) - cycleDuration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastCallTime_ = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
Session_T & getSession() { return session; }
|
||||||
|
|
||||||
void startMeasurements();
|
|
||||||
void stopMeasurements();
|
|
||||||
void iteration();
|
|
||||||
private:
|
private:
|
||||||
MockScale scale;
|
MockScale scale;
|
||||||
Session_T session;
|
Session_T session;
|
||||||
|
bool measuring_;
|
||||||
|
long lastCallTime_;
|
||||||
};
|
};
|
||||||
|
|
||||||
SessionManager<Session_T> sessionManager;
|
SessionManager<Session_T> sessionManager;
|
||||||
|
@ -48,43 +94,35 @@ void httpSetup(SessionManager<Session_T> * sessionManager)
|
||||||
server.on("/api/session/start", HTTP_POST, [sessionManager](AsyncWebServerRequest * req) {
|
server.on("/api/session/start", HTTP_POST, [sessionManager](AsyncWebServerRequest * req) {
|
||||||
req->send(200, "text/plain", F("OK"));
|
req->send(200, "text/plain", F("OK"));
|
||||||
sessionManager->startMeasurements();
|
sessionManager->startMeasurements();
|
||||||
|
Serial.println("Started measurements");
|
||||||
});
|
});
|
||||||
server.on("/api/session/stop", HTTP_POST, [sessionManager](AsyncWebServerRequest * req) {
|
server.on("/api/session/stop", HTTP_POST, [sessionManager](AsyncWebServerRequest * req) {
|
||||||
req->send(200, "text/plain", F("OK"));
|
req->send(200, "text/plain", F("OK"));
|
||||||
sessionManager->stopMeasurements();
|
sessionManager->stopMeasurements();
|
||||||
|
Serial.println("Stopped measurements");
|
||||||
});
|
});
|
||||||
server.on("/api/session/data", HTTP_GET, [sessionManager](AsyncWebServerRequest * req) {
|
server.on("/api/session/data", HTTP_GET, [sessionManager](AsyncWebServerRequest * req) {
|
||||||
StreamingMsgPackEncoder<HttpWriterAdaptor> encoder(&adaptor);
|
uint32_t startIdx = 0;
|
||||||
|
if( req->hasParam("startIdx") ) {
|
||||||
req->send_P(200, "application/x-msgpack", const uint8_t * content, size_t len)
|
startIdx = req->getParam("startIdx")->value().toInt();
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
||||||
request->send(200, "text/plain", "Hello, world");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Send a GET request to <IP>/get?message=<message>
|
|
||||||
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
|
||||||
String message;
|
|
||||||
if (request->hasParam(PARAM_MESSAGE)) {
|
|
||||||
message = request->getParam(PARAM_MESSAGE)->value();
|
|
||||||
} else {
|
|
||||||
message = "No message sent";
|
|
||||||
}
|
}
|
||||||
request->send(200, "text/plain", "Hello, GET: " + message);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Send a POST request to <IP>/post with a form field message set to <message>
|
StreamingMsgPackEncoder<DummyWriter> encoderToDetermineSize(nullptr);
|
||||||
server.on("/post", HTTP_POST, [](AsyncWebServerRequest *request){
|
encoderToDetermineSize.setSizeCountMode(true);
|
||||||
String message;
|
sessionManager->getSession().serialize(encoderToDetermineSize, startIdx);
|
||||||
if (request->hasParam(PARAM_MESSAGE, true)) {
|
auto totalSize = encoderToDetermineSize.getContentLength();
|
||||||
message = request->getParam(PARAM_MESSAGE, true)->value();
|
Serial.print("Sending started of total size ");
|
||||||
} else {
|
Serial.println(totalSize);
|
||||||
message = "No message sent";
|
req->send("application/x-msgpack", totalSize, [=](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
|
||||||
}
|
Serial.print("Partial send maxLen ");
|
||||||
request->send(200, "text/plain", "Hello, POST: " + message);
|
Serial.print(maxLen);
|
||||||
|
Serial.print(" index ");
|
||||||
|
Serial.println("index");
|
||||||
|
CopyWriter copyWriter(buffer);
|
||||||
|
ChunkedStreamingMsgPackEncoder<CopyWriter> encoder(©Writer, index, index + maxLen);
|
||||||
|
sessionManager->getSession().serialize(encoder, startIdx);
|
||||||
|
return encoder.sentBytes() - index;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
server.onNotFound(onNotFound);
|
server.onNotFound(onNotFound);
|
||||||
|
@ -112,15 +150,14 @@ void setup()
|
||||||
// NTP
|
// NTP
|
||||||
timeClient.begin();
|
timeClient.begin();
|
||||||
timeClient.update();
|
timeClient.update();
|
||||||
session.init( timeClient.getEpochTime() );
|
|
||||||
|
|
||||||
// Scale
|
// Session
|
||||||
scale.begin(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN);
|
sessionManager.begin();
|
||||||
scale.tare( CONFIG_TARE_AVG_COUNT );
|
|
||||||
|
|
||||||
// HTTP & Websocket server
|
// HTTP & Websocket server
|
||||||
httpSetup();
|
httpSetup(&sessionManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
sessionManager.iteration();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue