Fixed multiple errors in network and image handling

This commit is contained in:
frubi 2019-12-06 23:58:05 +01:00
parent ebb547c53f
commit fa4542a53b
3 changed files with 46 additions and 25 deletions

View File

@ -5,15 +5,21 @@
#define DATA 8 #define DATA 8
#define CLOCK 9 #define CLOCK 9
typedef struct source_t { struct _source_t {
// width and height of current frame
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
// delay until next frame is shown
unsigned int delay; unsigned int delay;
// position of current pixel
int x; int x;
int y; int y;
}; };
typedef struct _source_t source_t;
byte mac[] = { 0xBE, 0xB7, 0x5C, 0x30, 0xC3, 0x04 }; byte mac[] = { 0xBE, 0xB7, 0x5C, 0x30, 0xC3, 0x04 };
IPAddress ip(10, 23, 42, 24); IPAddress ip(10, 23, 42, 24);
IPAddress router(10, 23, 42, 1); IPAddress router(10, 23, 42, 1);
@ -34,12 +40,11 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
image.width = 32;
image.height = 40;
pinMode(DATA, OUTPUT); pinMode(DATA, OUTPUT);
pinMode(CLOCK, OUTPUT); pinMode(CLOCK, OUTPUT);
pinMode(LOAD, OUTPUT); pinMode(LOAD, OUTPUT);
Serial.println("setup done");
} }
void send_block(image_t* p, int x, int y) { void send_block(image_t* p, int x, int y) {
@ -117,6 +122,10 @@ void load() {
void default_image(image_t* p) { void default_image(image_t* p) {
static int offset = 0; static int offset = 0;
// reset image to maximum size
set_size(p, 32767, 32767);
// toggle all pixels in tilted bars
int dim = max(p->width, p->height); int dim = max(p->width, p->height);
for (int n = 0; n < dim; n++) { for (int n = 0; n < dim; n++) {
int x = (n + offset) % p->width; int x = (n + offset) % p->width;
@ -165,6 +174,9 @@ bool read_header(EthernetClient cli, source_t* src) {
} }
if (offset > 5) { if (offset > 5) {
src->x = 0;
src->y = 0;
offset = 0; offset = 0;
complete = true; complete = true;
} }
@ -173,13 +185,8 @@ bool read_header(EthernetClient cli, source_t* src) {
} }
bool read_pixels(EthernetClient cli, source_t* src, image_t* img) { bool read_pixels(EthernetClient cli, source_t* src, image_t* img) {
// position of current pixel
static int x = 0;
static int y = 0;
// copy dimension from header // copy dimension from header
img->width = src->width; set_size(img, src->width, src->height);
img->height = src->height;
while (true) { while (true) {
int value = cli.read(); int value = cli.read();
@ -187,14 +194,14 @@ bool read_pixels(EthernetClient cli, source_t* src, image_t* img) {
return false; return false;
} }
set_pixel(img, x, y, value); set_pixel(img, src->x, src->y, value);
x++; src->x++;
if (x >= img->width) { if (src->x >= src->width) {
x = 0; src->x = 0;
y++; src->y++;
if (y >= img->height) { if (src->y >= src->height) {
y = 0; src->y = 0;
return true; return true;
} }
} }
@ -202,7 +209,7 @@ bool read_pixels(EthernetClient cli, source_t* src, image_t* img) {
} }
void loop() { void loop() {
static bool seen_client = false; static unsigned long last_activity = 0;
static bool in_header = true; static bool in_header = true;
// if an incoming client connects, there will be bytes available to read: // if an incoming client connects, there will be bytes available to read:
@ -216,8 +223,14 @@ void loop() {
Serial.print(source.height); Serial.print(source.height);
Serial.print(" delay="); Serial.print(" delay=");
Serial.println(source.delay); Serial.println(source.delay);
if ((source.width == 0) || (source.height == 0)) {
Serial.println("invalid dimension");
client.stop();
} else {
in_header = false; in_header = false;
} }
}
} else { } else {
if (read_pixels(client, &source, &image) == true) { if (read_pixels(client, &source, &image) == true) {
Serial.println("pixels complete"); Serial.println("pixels complete");
@ -226,12 +239,11 @@ void loop() {
send_image(&image); send_image(&image);
} }
} }
last_activity = millis();
seen_client = true;
} else { } else {
//if (seen_client == false) { if ((millis() - last_activity) > 60000) {
default_image(&image); default_image(&image);
send_image(&image); send_image(&image);
//} }
} }
} }

View File

@ -9,7 +9,7 @@ bool check_bounds(image_t* p, int x, int y) {
return false; return false;
} }
if ((x > p->width) || (y > p->height)) { if ((x >= p->width) || (y >= p->height)) {
return false; return false;
} }
@ -36,3 +36,8 @@ void clear_pixels(image_t* p) {
} }
memset(p->data, 0, sizeof(p->data)); memset(p->data, 0, sizeof(p->data));
} }
void set_size(image_t* p, int width, int height) {
p->width = min(width, MAX_WIDTH);
p->height = min(height, MAX_HEIGHT);
}

View File

@ -3,14 +3,18 @@
#define MAX_WIDTH 32 #define MAX_WIDTH 32
#define MAX_HEIGHT 40 #define MAX_HEIGHT 40
typedef struct image_t { struct _image_t {
int width; int width;
int height; int height;
byte data[MAX_WIDTH * MAX_HEIGHT]; byte data[MAX_WIDTH * MAX_HEIGHT];
}; };
typedef struct _image_t image_t;
byte get_pixel(image_t* p, int x, int y); byte get_pixel(image_t* p, int x, int y);
void set_pixel(image_t* p, int x, int y, byte value); void set_pixel(image_t* p, int x, int y, byte value);
void clear_pixels(image_t* p); void clear_pixels(image_t* p);
void set_size(image_t* p, int width, int height);