From 4faa2157eda533271c30788591c4f81dd43e8fda Mon Sep 17 00:00:00 2001 From: Thomas Ballmann Date: Mon, 2 Mar 2020 20:58:40 +0100 Subject: [PATCH] rest api completed #15 --- include/app.h | 1 + include/image.h | 13 +++++++++--- include/imagePNG.h | 3 +++ src/app.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++---- src/image.cpp | 49 ++++++++++++++++++++------------------------ src/imagePNG.cpp | 42 +++++++++++++++----------------------- src/main.cpp | 4 ++++ 7 files changed, 103 insertions(+), 60 deletions(-) diff --git a/include/app.h b/include/app.h index 7b6fdc8..c2498a2 100644 --- a/include/app.h +++ b/include/app.h @@ -2,5 +2,6 @@ #define APP_H void setupApp(); +void loopApp(); #endif \ No newline at end of file diff --git a/include/image.h b/include/image.h index 9186f8b..6e8a847 100644 --- a/include/image.h +++ b/include/image.h @@ -3,11 +3,18 @@ #include -#include "imageWBMP.h" -#include "imagePNG.h" +typedef struct +{ + size_t format; + int x; + int y; + int w; + int h; + bool dithering; +} structImageProcess; void ImageNew(int x, int y, int w, int h, bool dithering); void ImageWriteBuffer(uint8_t buff[], size_t c); void ImageFlushBuffer(); -#endif \ No newline at end of file +#endif /* IMAGE_H */ \ No newline at end of file diff --git a/include/imagePNG.h b/include/imagePNG.h index 552f9f2..4c842fa 100644 --- a/include/imagePNG.h +++ b/include/imagePNG.h @@ -1,6 +1,9 @@ #ifndef IMAGE_PNG_H #define IMAGE_PNG_H +#include "image.h" +extern structImageProcess ImageProcess; + // @see http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html const char ImageHeaderPNG[] = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"; diff --git a/src/app.cpp b/src/app.cpp index 35a35a4..9a1b41e 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -7,6 +7,7 @@ #include "device.h" #include "playlist.h" #include "image.h" +#include "display.h" AsyncWebServer server(80); @@ -16,6 +17,7 @@ void setupWifiScan(); void setupWifiConnect(); void setupCurrentImage(); void setupApiFace(); +bool updateDisplayRequired = false; void setupApp() { @@ -86,6 +88,20 @@ void setupApp() Serial.println("setup configure - done"); } +void loopApp() +{ + if (updateDisplayRequired) + { + Serial.println("loop app update display"); + + // stop playlist to show the new image + PlaylistResetTimer(); + + updateDisplayRequired = false; + display.nextPage(); + } +} + void setupSettingsGet() { server.on("/api/settings", HTTP_GET, [](AsyncWebServerRequest *request) { @@ -231,10 +247,6 @@ static void handle_update_progress_cb(AsyncWebServerRequest *request, String fil if (!index) { Serial.printf("UploadStart: %s\n", filename.c_str()); - - // stop playlist to show the new image - PlaylistResetTimer(); - ImageNew(0, 0, 0, 0, true); } @@ -244,12 +256,43 @@ static void handle_update_progress_cb(AsyncWebServerRequest *request, String fil { Serial.printf("UploadEnd: %s, %u B\n", filename.c_str(), index + len); ImageFlushBuffer(); + + updateDisplayRequired = true; } } void setupApiFace() { server.on("/api/face", HTTP_POST, [](AsyncWebServerRequest *request) { + Serial.println("post request"); + +/* + int params = request->params(); + for (int i = 0; i < params; i++) + { + AsyncWebParameter *p = request->getParam(i); + if (p->isFile()) + { + Serial.printf("
  • FILE[%s]: %s, size: %u
  • ", p->name().c_str(), p->value().c_str(), p->size()); + } + else if (p->isPost()) + { + Serial.printf("
  • POST[%s]: %s
  • ", p->name().c_str(), p->value().c_str()); + } + else + { + Serial.printf("
  • GET[%s]: %s
  • ", p->name().c_str(), p->value().c_str()); + } + } + + if (request->hasParam("dithering", true)) + { + AsyncWebParameter *p = request->getParam("dithering", true); + Serial.println(p->value()); + dithering = p->value().toInt() == 1; + } + */ + request->send(200, "application/ld+json; charset=utf-8", "{}"); }, handle_update_progress_cb); diff --git a/src/image.cpp b/src/image.cpp index 7951a46..c839246 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -1,47 +1,42 @@ #include "image.h" +#include "imageWBMP.h" +#include "imagePNG.h" -typedef struct -{ - size_t format; - int x; - int y; - int w; - int h; - bool dithering; -} ImageProcess; -ImageProcess Image; +structImageProcess ImageProcess; void ImageNew(int x, int y, int w, int h, bool dithering) { - Image.x = x; - Image.y = y; - Image.w = w; - Image.h = h; - Image.dithering = dithering; + Serial.printf("ImageNew: x: %d, y: %d, dithering: %d \n", x, y, dithering); + + ImageProcess.x = x; + ImageProcess.y = y; + ImageProcess.w = w; + ImageProcess.h = h; + ImageProcess.dithering = dithering; } void ImageWriteBuffer(uint8_t buff[], size_t c) { // initial detect format - if (Image.format == 0) + if (ImageProcess.format == 0) { if (memcmp(buff, ImageHeaderWBMP, sizeof(ImageHeaderWBMP) - 1) == 0) { Serial.println(" image format: WBMP"); - Image.format = 2; + ImageProcess.format = 2; wbmpOpenFramebuffer(); } else if (memcmp(buff, ImageHeaderPNG, sizeof(ImageHeaderPNG) - 1) == 0) { Serial.println(" image format: PNG"); - Image.format = 3; + ImageProcess.format = 3; pngOpenFramebuffer(); } else { - Image.format = 1; + ImageProcess.format = 1; Serial.println(" unkown image format. first header are:"); Serial.println(buff[0]); Serial.println(buff[1]); @@ -53,7 +48,7 @@ void ImageWriteBuffer(uint8_t buff[], size_t c) } // write display frame - switch (Image.format) + switch (ImageProcess.format) { // WBMP case 2: @@ -69,7 +64,7 @@ void ImageWriteBuffer(uint8_t buff[], size_t c) void ImageFlushBuffer() { // update display - switch (Image.format) + switch (ImageProcess.format) { // WBMP case 2: @@ -82,10 +77,10 @@ void ImageFlushBuffer() } // clear settings - Image.format = 0; - Image.x = 0; - Image.y = 0; - Image.w = 0; - Image.h = 0; - Image.dithering = false; + ImageProcess.format = 0; + ImageProcess.x = 0; + ImageProcess.y = 0; + ImageProcess.w = 0; + ImageProcess.h = 0; + ImageProcess.dithering = false; } \ No newline at end of file diff --git a/src/imagePNG.cpp b/src/imagePNG.cpp index 9f54dd7..7d0242f 100644 --- a/src/imagePNG.cpp +++ b/src/imagePNG.cpp @@ -10,50 +10,40 @@ static constexpr int MAX_WIDTH = 640; static int16_t curRowDelta[MAX_WIDTH + 1]; static int16_t nextRowDelta[MAX_WIDTH + 1]; -bool dithering = true; - void on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4]); void setupImagePNG() { Serial.println("setupPNG"); + pngle = pngle_new(); + pngle_set_draw_callback(pngle, on_draw); + Serial.println("setupPNG done"); } void pngOpenFramebuffer() { displayOpen(); + pngle_reset(pngle); - if (pngle) - { - pngle_destroy(pngle); - } - - pngle = pngle_new(); - pngle_set_draw_callback(pngle, on_draw); + memset(curRowDelta, 0, sizeof(curRowDelta)); + memset(nextRowDelta, 0, sizeof(nextRowDelta)); } void pngWriteFramebuffer(int offset, uint8_t bitmap[], int c) { - if (pngle) - { - int fed = pngle_feed(pngle, bitmap, c); - if (fed < 0) - { - Serial.println(pngle_error(pngle)); - } - } - else + int fed = pngle_feed(pngle, bitmap, c); + if (fed < 0) { - Serial.println("forgot pngle_new() ?"); + Serial.println(pngle_error(pngle)); } } void pngFlushFramebuffer() { - pngle_destroy(pngle); - displayFlush(); + Serial.println("pngFlushFramebuffer"); + pngle_reset(pngle); } void on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4]) @@ -66,7 +56,7 @@ void on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uin int16_t blackOrWhite; // Add errors to color if there are - if (dithering) + if (ImageProcess.dithering) { gray += curRowDelta[x]; } @@ -80,7 +70,7 @@ void on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uin blackOrWhite = 255; } - if (dithering) + if (ImageProcess.dithering) { int16_t oldPixel = gray; int16_t newPixel = blackOrWhite; @@ -98,10 +88,10 @@ void on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uin if (x == 0 && y > 0) { // new line - memcpy(curRowDelta, nextRowDelta, sizeof(&curRowDelta)); - memset(nextRowDelta, 0, sizeof(&nextRowDelta)); + memcpy(curRowDelta, nextRowDelta, sizeof(curRowDelta)); + memset(nextRowDelta, 0, sizeof(nextRowDelta)); } } - displayWritePixel(x, y, blackOrWhite); + displayWritePixel(ImageProcess.x + x, ImageProcess.y + y, blackOrWhite); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 73ad123..bc15f46 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,6 +29,8 @@ void setup() setupSettings(); setupDevice(); + setupImagePNG(); + setupWlan(); if (wlan_isConnected()) { @@ -59,5 +61,7 @@ void loop() //loopCloud(); } + loopApp(); + //loopDevice(); } \ No newline at end of file