/
pages.ino
125 lines (101 loc) · 4.2 KB
/
pages.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
void handleRoot() {
server.send(200, "text/html", "<html><head><title>led-display</title></head><body><link rel=\"stylesheet\" href=\"/style.css\" />"
"<script src=\"/ajax.js\"></script><script src=\"/default.js\"></script></body></html>");
}
void handleClear() {
server.send(200, "application/json", "{\"error\": null}" );
matrix.clearScreen();
}
void handleFill() {
server.send(200, "application/json", "{\"error\": null}" );
matrix.fillScreen();
}
// draw image directly.
void handleDraw() {
String v = server.arg("v");
if (v.length() != SCREEN_BYTES*2) {
server.send(200, "application/json", "{\"error\":\"parameter error.\"}" );
return;
}
uint8_t x = 0, y = 0;
for (uint8_t i=0; i<SCREEN_BYTES; i++){ // 144 byte = 1152 bit = (24 * 3) * 16 pixel
String bytestring = v.substring(i*2, (i*2)+2);
const char *chars = bytestring.c_str();
byte bytes = strtol(chars,NULL,16);
for (uint8_t b = 0; b < 8; b++) {
matrix.drawPixel(x, y, bitRead(bytes, 7-b));
if (++x == LED_COLS) { y++; x=0; }
}
}
matrix.writeScreen();
server.send(200, "application/json", "{\"error\": null}" );
}
// save to memory
void handleSet() {
String m = server.arg("m");
String v = server.arg("v");
if (m.length() == 0 || v.length() != SCREEN_BYTES*2) {
server.send(200, "application/json", "{\"error\":\"parameter error.\"}" );
return;
}
uint8_t k = SCREEN_MEMORY_START + (m.toInt() * SCREEN_BYTES);
for (uint8_t i=0; i<SCREEN_BYTES; i++){ // 144 byte = 1152 bit = (24 * 3) * 16 pixel
String bytestring = v.substring(i*2, (i*2)+2);
const char *chars = bytestring.c_str();
byte bytes = strtol(chars,NULL,16);
EEPROM.write(k++, bytes);
}
EEPROM.commit();
server.send(200, "application/json", "{\"error\": null}" );
}
// display image from memory.
void handleShow() {
String m = server.arg("m");
if (m.length() == 0) {
server.send(200, "application/json", "{\"error\":\"parameter error.\"}" );
return;
}
uint8_t k = SCREEN_MEMORY_START + (m.toInt() * SCREEN_BYTES);
uint8_t x = 0, y = 0;
for (uint8_t i=0; i<SCREEN_BYTES; i++){ // 144 byte = 1152 bit = (24 * 3) * 16 pixel
byte bytes = (byte)EEPROM.read(k++);
for (uint8_t b = 0; b < 8; b++) {
matrix.drawPixel(x, y, bitRead(bytes, 7-b));
if (++x == LED_COLS) { y++; x=0; }
}
}
matrix.writeScreen();
server.send(200, "application/json", "{\"error\": null}" );
}
void handleBright() {
String v = server.arg("v");
if (v.length() == 0) {
server.send(200, "application/json", "{\"error\":\"parameter error.\"}" );
}
matrix.setBrightness(v.toInt());
server.send(200, "application/json", "{\"error\": null}" );
}
void serve_ajax_js() {
server.send(200, "text/javascript", "function ajax(url,cb){var http=new XMLHttpRequest();http.onreadystatechange=function(){\n"
"if(http.readyState==4&&http.status==200){cb(http.responseText);}else{cb(null);}}\n"
"http.open(\"GET\",url,true);http.send();}");
}
void serve_default_css() {
server.send(200, "text/css", "table{border-collapse:collapse;}td{cursor:pointer;border:1px solid black;width:15px;height:18px;}.on{background:red;}.off{background:black;}");
}
void serve_default_js() {
server.send(200, "text/javascript", "var data = new Array(1152);\ndata.fill(0);\nfunction draw() {"
"ajax('/draw?v='+document.getElementById('hex').value, function(r){console.log(r);});}"
"function calc_hex(){var tmp=(data+'').replace(/,/g,'');var result=[];while(tmp.length>0){"
"var chr=parseInt(tmp.substring(0,4),2).toString(16);result.push(chr);tmp=tmp.substring(4);}"
"document.getElementById('hex').value=result.join('');draw();}"
"function toggle(el,pos){data[pos]=data[pos]==1?0:1;el.className=(data[pos]==1)?'on':'off';calc_hex();}"
"document.write('<table>');for(var row=0;row<16;row++){document.write('<tr>');for(var col=0;col<72;col++){"
"document.write(\"<td class='off' onclick='toggle(this,\"+(row*72+col)+\")'></td>\");}document.write('</tr>');}"
"document.write('</table>');document.write(\"<textarea id='hex' cols='200' rows='3'></textarea>\");");
}
// 404 Not Found
void handleNotFound() {
String message = "File Not Found\n\n";
server.send ( 404, "text/plain", message );
}