From 48f9f0bd02fb226b30eeea7a7b2a11af49b48350 Mon Sep 17 00:00:00 2001 From: Thomas Ballmann Date: Sat, 14 Mar 2020 11:04:25 +0100 Subject: [PATCH] create bmp print screen on the fly #22 --- app/src/App.vue | 3 +-- src/app.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++- src/display.cpp | 6 +++++ 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/app/src/App.vue b/app/src/App.vue index d1b2e5a..86e53df 100644 --- a/app/src/App.vue +++ b/app/src/App.vue @@ -186,8 +186,7 @@ }, methods: { reloadStats (cb) { - console.log("reloadStats") - this.device_screen_src = "/fs/screen.bmp?" + new Date() + this.device_screen_src = "/current-image?" + Date.now() apiDevice.getStats(stats => { this.stats = stats diff --git a/src/app.cpp b/src/app.cpp index 0e2a979..feadf61 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -23,6 +23,12 @@ void setupApiFace(); void setupApiUpdate(); bool updateDisplayRequired = false; +// bmp +void write16(AsyncResponseStream &f, uint16_t v); +void write32(AsyncResponseStream &f, uint32_t v); +uint8_t filldata2[] = {0x0, 0x23, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0x0}; +// bmp + void setupApp() { Serial.println("setup configure"); @@ -171,7 +177,57 @@ void setupSettingsPost() void setupCurrentImage() { server.on("/current-image", HTTP_GET, [](AsyncWebServerRequest *request) { - request->send(SPIFFS, "/currentImage.bin"); + + uint8_t *bitmap = display.getBuffer(); + int16_t w = display.width(); + int16_t h = display.height(); + + uint16_t depth = 1; + uint32_t rowSizeCode = (w + 8 - depth) * depth / 8; + + // BMP rows are padded (if needed) to 4-byte boundary + uint32_t rowSizeBMP = (w * depth / 8 + 3) & ~3; + uint32_t headerSize = 40; + uint32_t imageOffset = 62; + uint32_t fileSize = imageOffset + h * rowSizeBMP; + + AsyncResponseStream *response = request->beginResponseStream("image/bmp", w * h / 8 + imageOffset); + //response->addHeader("Server", "ESP Async Web Server"); + + write16(*response, 0x4D42); // BMP signature + write32(*response, fileSize); // fileSize + write32(*response, 0); // creator bytes + write32(*response, imageOffset); // image offset + write32(*response, headerSize); // Header size + write32(*response, w); // image width + write32(*response, h); // image height + write16(*response, 1); // # planes + write16(*response, depth); // // bits per pixel + write32(*response, 0); // format uncompressed + + uint32_t j = 0; + for (uint32_t i = 34; i < imageOffset; i++) + { + response->write(filldata2[j++]); // remaining header bytes + } + + uint32_t rowidx = 0; + for (uint16_t row = 0; row < h; row++) // for each line + { + uint32_t colidx; + for (colidx = 0; colidx < rowSizeCode; colidx++) + { + uint8_t data = pgm_read_byte(&bitmap[rowidx + colidx]); + response->write(data); + } + rowidx += rowSizeCode; + while (colidx++ < rowSizeBMP) + { + response->write(uint8_t(0)); // padding + } + } + + request->send(response); }); } @@ -361,4 +417,18 @@ void setupApiUpdate() request->send(200, "application/ld+json; charset=utf-8", "{}"); }); +} + +void write16(AsyncResponseStream &f, uint16_t v) +{ + f.write(uint8_t(v)); + f.write(uint8_t(v >> 8)); +} + +void write32(AsyncResponseStream &f, uint32_t v) +{ + f.write(uint8_t(v)); + f.write(uint8_t(v >> 8)); + f.write(uint8_t(v >> 16)); + f.write(uint8_t(v >> 24)); } \ No newline at end of file diff --git a/src/display.cpp b/src/display.cpp index 5cdd9f2..6ab9732 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -51,11 +51,13 @@ void displayWriteFramebuffer(uint8_t bitmap[]) void displayFlush() { display.nextPage(); + /* // TODO setting ? if (true) { saveScreen(); } + */ } void printSplash() @@ -81,6 +83,7 @@ void printSplash() } while (display.nextPage()); } +// @deprecated void saveScreen() { unsigned long startMills = millis(); @@ -88,6 +91,7 @@ void saveScreen() Serial.println(millis() - startMills); } +// @deprecated void writeBitmap(const char filename[], const uint8_t bitmap[], int16_t w, int16_t h, uint16_t depth) { File bitmapFile; @@ -158,12 +162,14 @@ void writeBitmap(const char filename[], const uint8_t bitmap[], int16_t w, int16 } } +// @deprecated void write16(File &f, uint16_t v) { f.write(uint8_t(v)); f.write(uint8_t(v >> 8)); } +// @deprecated void write32(File &f, uint32_t v) { f.write(uint8_t(v));