create bmp print screen on the fly #22

This commit is contained in:
Thomas Ballmann 2020-03-14 11:04:25 +01:00
parent 7c385836d3
commit 48f9f0bd02
3 changed files with 78 additions and 3 deletions

View File

@ -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

View File

@ -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));
}

View File

@ -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));