rest api completed #15

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

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

@ -3,11 +3,18 @@
#include <Arduino.h>
#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
#endif /* IMAGE_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";

@ -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("<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", "{}");
},
handle_update_progress_cb);

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

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

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