LED-BOARD/LED-Board.ino

238 lines
4.4 KiB
Arduino
Raw Normal View History

2019-11-10 19:50:40 +01:00
#include <Ethernet.h>
#include "image.h"
#define LOAD 7
2019-11-10 14:25:30 +01:00
#define DATA 8
#define CLOCK 9
2019-11-10 19:50:40 +01:00
typedef struct source_t {
unsigned int width;
unsigned int height;
unsigned int delay;
2019-11-10 14:25:30 +01:00
2019-11-10 19:50:40 +01:00
int x;
int y;
2019-11-10 14:25:30 +01:00
};
2019-11-10 19:50:40 +01:00
byte mac[] = { 0xBE, 0xB7, 0x5C, 0x30, 0xC3, 0x04 };
IPAddress ip(10, 23, 42, 24);
IPAddress router(10, 23, 42, 1);
IPAddress subnet(255, 255, 254, 0);
2019-11-10 14:25:30 +01:00
2019-11-10 19:50:40 +01:00
EthernetServer server(9000);
2019-11-10 14:25:30 +01:00
2019-11-10 19:50:40 +01:00
image_t image;
2019-11-10 14:25:30 +01:00
2019-11-10 19:50:40 +01:00
source_t source;
2019-11-10 14:25:30 +01:00
void setup() {
2019-11-10 19:50:40 +01:00
// setup network
Ethernet.init(10);
Ethernet.begin(mac, ip, router, router, subnet);
server.begin();
2019-11-10 14:25:30 +01:00
Serial.begin(115200);
2019-11-10 19:50:40 +01:00
image.width = 32;
image.height = 40;
2019-11-10 14:25:30 +01:00
2019-11-10 19:50:40 +01:00
pinMode(DATA, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(LOAD, OUTPUT);
2019-11-10 14:25:30 +01:00
}
2019-11-10 19:50:40 +01:00
void send_block(image_t* p, int x, int y) {
2019-11-10 14:25:30 +01:00
int order[32][2] = {
{ 1, 1 }, // 1
{ 1, 0 }, // 2
{ 0, 1 }, // 3
{ 1, 2 }, // 4
{ 0, 2 }, // 5
{ 1, 3 }, // 6
{ 0, 0 }, // 7
{ 0, 3 }, // 8
{ 0, 4 }, // 9
{ 1, 4 }, // 10
{ 0, 5 }, // 11
{ 1, 7 }, // 12
{ 1, 5 }, // 13
{ 0, 6 }, // 14
{ 1, 6 }, // 15
{ 0, 7 }, // 16
{ 3, 7 }, // 17
{ 2, 6 }, // 18
{ 2, 7 }, // 19
{ 3, 6 }, // 20
{ 2, 5 }, // 21
{ 3, 5 }, // 22
{ 3, 4 }, // 23
{ 2, 4 }, // 24
{ 3, 3 }, // 25
{ 2, 0 }, // 26
{ 3, 0 }, // 27
{ 2, 3 }, // 28
{ 3, 2 }, // 29
{ 2, 1 }, // 30
{ 3, 1 }, // 31
{ 2, 2 }, // 32
};
2019-11-10 19:50:40 +01:00
for (int n = 0; n < 32; n++) {
2019-11-10 14:25:30 +01:00
int x_offset = order[n][0];
int y_offset = order[n][1];
byte pixel = get_pixel(p, x + x_offset, y + y_offset);
digitalWrite(DATA, pixel);
clock();
2019-11-10 19:50:40 +01:00
}
2019-11-10 14:25:30 +01:00
// 33 bit - kein pixel - senden
clock();
}
2019-11-10 19:50:40 +01:00
void send_image(image_t* img) {
for (int y = 0; y < MAX_HEIGHT; y += 8) {
for (int x = 0; x < MAX_WIDTH; x += 4) {
send_block(img, x, y);
2019-11-10 14:25:30 +01:00
}
}
2019-11-10 19:50:40 +01:00
2019-11-10 14:25:30 +01:00
load();
}
void clock() {
2019-11-10 19:50:40 +01:00
digitalWrite(CLOCK, HIGH);
digitalWrite(CLOCK, LOW);
2019-11-10 14:25:30 +01:00
}
void load() {
2019-11-10 19:50:40 +01:00
digitalWrite(LOAD, HIGH);
digitalWrite(LOAD, LOW);
2019-11-10 14:25:30 +01:00
}
2019-11-10 19:50:40 +01:00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00...
// Width Height Delay Pixel
void default_image(image_t* p) {
2019-11-10 14:25:30 +01:00
static int offset = 0;
2019-11-10 19:50:40 +01:00
int dim = max(p->width, p->height);
for (int n = 0; n < dim; n++) {
int x = (n + offset) % p->width;
int y = n % p->height;
2019-11-10 14:25:30 +01:00
2019-11-10 19:50:40 +01:00
byte pixel = get_pixel(p, x, y);
set_pixel(p, x, y, !pixel);
2019-11-10 14:25:30 +01:00
}
offset++;
2019-11-10 19:50:40 +01:00
}
2019-11-10 14:25:30 +01:00
2019-11-10 19:50:40 +01:00
bool read_header(EthernetClient cli, source_t* src) {
// number of bytes already read from header
2019-11-10 14:25:30 +01:00
static int offset = 0;
2019-11-10 19:50:40 +01:00
// flag set, if header is complete
bool complete = false;
while (offset < 6) {
byte value = cli.read();
if (value == -1) {
break;
}
switch (offset) {
case 0:
src->width = (value << 8);
break;
case 1:
src->width |= value;
break;
case 2:
src->height = (value << 8);
break;
case 3:
src->height |= value;
break;
case 4:
src->delay = (value << 8);
break;
case 5:
src->delay |= value;
break;
}
offset++;
}
if (offset > 5) {
offset = 0;
complete = true;
2019-11-10 14:25:30 +01:00
}
2019-11-10 19:50:40 +01:00
return complete;
}
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;
while (true) {
byte value = cli.read();
if (value == -1) {
return false;
}
set_pixel(img, x, y, value);
x++;
if (x >= img->width) {
x = 0;
y++;
if (y >= img->height) {
y = 0;
return true;
}
2019-11-10 14:25:30 +01:00
}
}
2019-11-10 19:50:40 +01:00
}
2019-11-10 14:25:30 +01:00
2019-11-10 19:50:40 +01:00
void loop() {
static bool seen_client = false;
static bool in_header = true;
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client) {
if (in_header == true) {
if (read_header(client, &source) == true) {
Serial.print("width=");
Serial.print(source.width);
Serial.print(" height=");
Serial.print(source.height);
Serial.print(" delay=");
Serial.println(source.delay);
in_header = false;
}
} else {
if (read_pixels(client, &source, &image) == true) {
Serial.println("pixels complete");
in_header = true;
send_image(&image);
}
}
seen_client = true;
} else {
if (seen_client == false) {
default_image(&image);
send_image(&image);
}
}
2019-11-10 14:25:30 +01:00
}