rest api completed #15

pull/1/head
Thomas Ballmann 4 years ago
parent c0bb7f6237
commit 4faa2157ed

@ -2,5 +2,6 @@
#define APP_H #define APP_H
void setupApp(); void setupApp();
void loopApp();
#endif #endif

@ -3,11 +3,18 @@
#include <Arduino.h> #include <Arduino.h>
#include "imageWBMP.h" typedef struct
#include "imagePNG.h" {
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 ImageNew(int x, int y, int w, int h, bool dithering);
void ImageWriteBuffer(uint8_t buff[], size_t c); void ImageWriteBuffer(uint8_t buff[], size_t c);
void ImageFlushBuffer(); void ImageFlushBuffer();
#endif #endif /* IMAGE_H */

@ -1,6 +1,9 @@
#ifndef IMAGE_PNG_H #ifndef IMAGE_PNG_H
#define 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 // @see http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html
const char ImageHeaderPNG[] = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"; const char ImageHeaderPNG[] = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";

@ -7,6 +7,7 @@
#include "device.h" #include "device.h"
#include "playlist.h" #include "playlist.h"
#include "image.h" #include "image.h"
#include "display.h"
AsyncWebServer server(80); AsyncWebServer server(80);
@ -16,6 +17,7 @@ void setupWifiScan();
void setupWifiConnect(); void setupWifiConnect();
void setupCurrentImage(); void setupCurrentImage();
void setupApiFace(); void setupApiFace();
bool updateDisplayRequired = false;
void setupApp() void setupApp()
{ {
@ -86,6 +88,20 @@ void setupApp()
Serial.println("setup configure - done"); 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() void setupSettingsGet()
{ {
server.on("/api/settings", HTTP_GET, [](AsyncWebServerRequest *request) { server.on("/api/settings", HTTP_GET, [](AsyncWebServerRequest *request) {
@ -231,10 +247,6 @@ static void handle_update_progress_cb(AsyncWebServerRequest *request, String fil
if (!index) if (!index)
{ {
Serial.printf("UploadStart: %s\n", filename.c_str()); Serial.printf("UploadStart: %s\n", filename.c_str());
// stop playlist to show the new image
PlaylistResetTimer();
ImageNew(0, 0, 0, 0, true); 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); Serial.printf("UploadEnd: %s, %u B\n", filename.c_str(), index + len);
ImageFlushBuffer(); ImageFlushBuffer();
updateDisplayRequired = true;
} }
} }
void setupApiFace() void setupApiFace()
{ {
server.on("/api/face", HTTP_POST, [](AsyncWebServerRequest *request) { 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("<li>FILE[%s]: %s, size: %u</li>", p->name().c_str(), p->value().c_str(), p->size());
}
else if (p->isPost())
{
Serial.printf("<li>POST[%s]: %s</li>", p->name().c_str(), p->value().c_str());
}
else
{
Serial.printf("<li>GET[%s]: %s</li>", 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", "{}"); request->send(200, "application/ld+json; charset=utf-8", "{}");
}, },
handle_update_progress_cb); handle_update_progress_cb);

@ -1,47 +1,42 @@
#include "image.h" #include "image.h"
#include "imageWBMP.h"
#include "imagePNG.h"
typedef struct structImageProcess ImageProcess;
{
size_t format;
int x;
int y;
int w;
int h;
bool dithering;
} ImageProcess;
ImageProcess Image;
void ImageNew(int x, int y, int w, int h, bool dithering) void ImageNew(int x, int y, int w, int h, bool dithering)
{ {
Image.x = x; Serial.printf("ImageNew: x: %d, y: %d, dithering: %d \n", x, y, dithering);
Image.y = y;
Image.w = w; ImageProcess.x = x;
Image.h = h; ImageProcess.y = y;
Image.dithering = dithering; ImageProcess.w = w;
ImageProcess.h = h;
ImageProcess.dithering = dithering;
} }
void ImageWriteBuffer(uint8_t buff[], size_t c) void ImageWriteBuffer(uint8_t buff[], size_t c)
{ {
// initial detect format // initial detect format
if (Image.format == 0) if (ImageProcess.format == 0)
{ {
if (memcmp(buff, ImageHeaderWBMP, sizeof(ImageHeaderWBMP) - 1) == 0) if (memcmp(buff, ImageHeaderWBMP, sizeof(ImageHeaderWBMP) - 1) == 0)
{ {
Serial.println(" image format: WBMP"); Serial.println(" image format: WBMP");
Image.format = 2; ImageProcess.format = 2;
wbmpOpenFramebuffer(); wbmpOpenFramebuffer();
} }
else if (memcmp(buff, ImageHeaderPNG, sizeof(ImageHeaderPNG) - 1) == 0) else if (memcmp(buff, ImageHeaderPNG, sizeof(ImageHeaderPNG) - 1) == 0)
{ {
Serial.println(" image format: PNG"); Serial.println(" image format: PNG");
Image.format = 3; ImageProcess.format = 3;
pngOpenFramebuffer(); pngOpenFramebuffer();
} }
else else
{ {
Image.format = 1; ImageProcess.format = 1;
Serial.println(" unkown image format. first header are:"); Serial.println(" unkown image format. first header are:");
Serial.println(buff[0]); Serial.println(buff[0]);
Serial.println(buff[1]); Serial.println(buff[1]);
@ -53,7 +48,7 @@ void ImageWriteBuffer(uint8_t buff[], size_t c)
} }
// write display frame // write display frame
switch (Image.format) switch (ImageProcess.format)
{ {
// WBMP // WBMP
case 2: case 2:
@ -69,7 +64,7 @@ void ImageWriteBuffer(uint8_t buff[], size_t c)
void ImageFlushBuffer() void ImageFlushBuffer()
{ {
// update display // update display
switch (Image.format) switch (ImageProcess.format)
{ {
// WBMP // WBMP
case 2: case 2:
@ -82,10 +77,10 @@ void ImageFlushBuffer()
} }
// clear settings // clear settings
Image.format = 0; ImageProcess.format = 0;
Image.x = 0; ImageProcess.x = 0;
Image.y = 0; ImageProcess.y = 0;
Image.w = 0; ImageProcess.w = 0;
Image.h = 0; ImageProcess.h = 0;
Image.dithering = false; ImageProcess.dithering = false;
} }

@ -10,50 +10,40 @@ static constexpr int MAX_WIDTH = 640;
static int16_t curRowDelta[MAX_WIDTH + 1]; static int16_t curRowDelta[MAX_WIDTH + 1];
static int16_t nextRowDelta[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 on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4]);
void setupImagePNG() void setupImagePNG()
{ {
Serial.println("setupPNG"); Serial.println("setupPNG");
pngle = pngle_new();
pngle_set_draw_callback(pngle, on_draw);
Serial.println("setupPNG done"); Serial.println("setupPNG done");
} }
void pngOpenFramebuffer() void pngOpenFramebuffer()
{ {
displayOpen(); displayOpen();
pngle_reset(pngle);
if (pngle) memset(curRowDelta, 0, sizeof(curRowDelta));
{ memset(nextRowDelta, 0, sizeof(nextRowDelta));
pngle_destroy(pngle);
}
pngle = pngle_new();
pngle_set_draw_callback(pngle, on_draw);
} }
void pngWriteFramebuffer(int offset, uint8_t bitmap[], int c) void pngWriteFramebuffer(int offset, uint8_t bitmap[], int c)
{ {
if (pngle) int fed = pngle_feed(pngle, bitmap, c);
{ if (fed < 0)
int fed = pngle_feed(pngle, bitmap, c);
if (fed < 0)
{
Serial.println(pngle_error(pngle));
}
}
else
{ {
Serial.println("forgot pngle_new() ?"); Serial.println(pngle_error(pngle));
} }
} }
void pngFlushFramebuffer() void pngFlushFramebuffer()
{ {
pngle_destroy(pngle); Serial.println("pngFlushFramebuffer");
displayFlush(); 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]) 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; int16_t blackOrWhite;
// Add errors to color if there are // Add errors to color if there are
if (dithering) if (ImageProcess.dithering)
{ {
gray += curRowDelta[x]; 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; blackOrWhite = 255;
} }
if (dithering) if (ImageProcess.dithering)
{ {
int16_t oldPixel = gray; int16_t oldPixel = gray;
int16_t newPixel = blackOrWhite; 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) if (x == 0 && y > 0)
{ {
// new line // new line
memcpy(curRowDelta, nextRowDelta, sizeof(&curRowDelta)); memcpy(curRowDelta, nextRowDelta, sizeof(curRowDelta));
memset(nextRowDelta, 0, sizeof(&nextRowDelta)); memset(nextRowDelta, 0, sizeof(nextRowDelta));
} }
} }
displayWritePixel(x, y, blackOrWhite); displayWritePixel(ImageProcess.x + x, ImageProcess.y + y, blackOrWhite);
} }

@ -29,6 +29,8 @@ void setup()
setupSettings(); setupSettings();
setupDevice(); setupDevice();
setupImagePNG();
setupWlan(); setupWlan();
if (wlan_isConnected()) if (wlan_isConnected())
{ {
@ -59,5 +61,7 @@ void loop()
//loopCloud(); //loopCloud();
} }
loopApp();
//loopDevice(); //loopDevice();
} }
Loading…
Cancel
Save