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

View File

@ -9,7 +9,7 @@ bool check_bounds(image_t* p, int x, int y) {
return false;
}
if ((x > p->width) || (y > p->height)) {
if ((x >= p->width) || (y >= p->height)) {
return false;
}
@ -36,3 +36,8 @@ void clear_pixels(image_t* p) {
}
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_HEIGHT 40
typedef struct image_t {
struct _image_t {
int width;
int height;
byte data[MAX_WIDTH * MAX_HEIGHT];
};
typedef struct _image_t image_t;
byte get_pixel(image_t* p, int x, int y);
void set_pixel(image_t* p, int x, int y, byte value);
void clear_pixels(image_t* p);
void set_size(image_t* p, int width, int height);