-
Notifications
You must be signed in to change notification settings - Fork 0
/
avrgbi.ino
342 lines (316 loc) · 6.83 KB
/
avrgbi.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "avrgbi-videogen.h"
bool debug=0;
unsigned char *scrnptr,cmd,x0,y0,x1,y1,c,chi,cmode,tmp;
unsigned short dtmp;
inline void fill(unsigned char c, unsigned char cmode, unsigned char y0, unsigned char y1) {
c|=(c<<4);
unsigned short ptr=y0*_HBYTES; // start for
dtmp=(y1+1)*_HBYTES; // end for
switch(cmode)
{
case 0: // replace
for(; ptr<dtmp; ptr++)
scrnptr[ptr] = c;
break;
case 1: // add
for(; ptr<dtmp; ptr++)
scrnptr[ptr] |= c;
break;
case 2: // subtract
for(; ptr<dtmp; ptr++)
scrnptr[ptr] &= c;
break;
case 3: // invert
for(; ptr<dtmp; ptr++)
scrnptr[ptr] ^= c;
break;
}
}
inline void pix(unsigned char c, unsigned char x, unsigned char y) {
short i=(x%_HRES)/2+(y%_BUFHEIGHT)*_HBYTES;
switch(c>>4)
{
case 0:
scrnptr[i]=(x%2? (scrnptr[i]&0x0f)|(c<<4) : (scrnptr[i]&0xf0)|(c&0x0f));
break;
case 1:
scrnptr[i]|=(x%2? c<<4 : c&0x0f);
break;
case 2:
scrnptr[i]&=(x%2? (c<<4)|0x0f : c|0xf0);
break;
case 3:
scrnptr[i]^=(x%2? c<<4 : c&0x0f);
break;
}
}
void row(unsigned char c, unsigned char cmode, unsigned char line, unsigned char x0, unsigned char x1)
{
unsigned short i=line*_HBYTES+x0/2;
// left end on odd pixel
if(x0%2)
{
dtmp=line*_HBYTES+x0/2;
switch(cmode)
{
case 0:
scrnptr[dtmp]=(scrnptr[dtmp]&0x0f)|(c<<4);
break;
case 1:
scrnptr[dtmp]|=c<<4;
break;
case 2:
scrnptr[dtmp]&=(c<<4)|0x0f;
break;
case 3:
scrnptr[dtmp]^=c<<4;
break;
}
i++;
}
// right end on even pixel
if(!(x1%2))
{
dtmp=line*_HBYTES+x1/2;
switch(cmode)
{
case 0:
scrnptr[dtmp]=(scrnptr[dtmp]&0xf0)|c;
break;
case 1:
scrnptr[dtmp]|=c;
break;
case 2:
scrnptr[dtmp]&=c|0xf0;
break;
case 3:
scrnptr[dtmp]^=c;
break;
}
}
// in between
dtmp=line*_HBYTES+(x1+1)/2;
c|=c<<4;
switch(cmode)
{
case 0: // replace
for(; i<dtmp; i++)
scrnptr[i] = c;
break;
case 1: // add
for(; i<dtmp; i++)
scrnptr[i] |= c;
break;
case 2: // subtract
for(; i<dtmp; i++)
scrnptr[i] &= c;
break;
case 3: // invert
for(; i<dtmp; i++)
scrnptr[i] ^= c;
break;
}
}
void col_even(unsigned char c, unsigned char cmode, unsigned char col, unsigned char y0, unsigned char y1)
{
unsigned short i=y0*_HBYTES+col; // top
dtmp=y1*_HBYTES+col; // bottom
switch(cmode)
{
case 0:
for(; i<=dtmp; i+=_HBYTES)
scrnptr[i]=(scrnptr[i]&0xf0)|c;
break;
case 1:
for(; i<=dtmp; i+=_HBYTES)
scrnptr[i]|=c;
break;
case 2:
for(; i<=dtmp; i+=_HBYTES)
scrnptr[i]&=c|0xf0;
break;
case 3:
for(; i<=dtmp; i+=_HBYTES)
scrnptr[i]^=c;
break;
}
}
void col_odd(unsigned char c, unsigned char cmode, unsigned char col, unsigned char y0, unsigned char y1)
{
unsigned short i=y0*_HBYTES+col; // top
dtmp=y1*_HBYTES+col; // bottom
c=c<<4;
switch(cmode)
{
case 0:
for(; i<=dtmp; i+=_HBYTES)
scrnptr[i]=(scrnptr[i]&0x0f)|c;
break;
case 1:
for(; i<=dtmp; i+=_HBYTES)
scrnptr[i]|=c;
break;
case 2:
for(; i<=dtmp; i+=_HBYTES)
scrnptr[i]&=c|0x0f;
break;
case 3:
for(; i<=dtmp; i+=_HBYTES)
scrnptr[i]^=c;
break;
}
}
void rect(unsigned char c, unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1)
{
cmode=c>>4;
c&=0x0f;
if(x0>x1) // swap order if needed
{
tmp=x0;
x0=x1;
x1=tmp;
}
if(y0>y1)
{
tmp=y0;
y0=y1;
y1=tmp;
}
if(x0>=_HRES||y0>=_BUFHEIGHT) return; // top left corner out of screen: do nothing
if(x1>=_HRES) x1=_HRES-1; // right edge out of screen: bring it back in
if(y1>=_BUFHEIGHT) y1=_BUFHEIGHT-1; // bottom edge out of screen: bring it back in
if(x0==0 && x1==_HRES-1) // full width
{
fill(c,cmode,y0,y1);
return;
}
if(x0==x1) // no width: column
{
if(x0%2)
col_odd(c,cmode,x0/2,y0,y1);
else
col_even(c,cmode,x0/2,y0,y1);
return;
}
if(y0==y1) // no height: row
{
row(c,cmode,y0,x0,x1);
return;
}
// many columns
tmp=x1/2;
for(unsigned char i=(x0+1)/2; i<=tmp; i++)
col_even(c,cmode,i,y0,y1);
tmp=(x1+1)/2;
for(unsigned char i=x0/2; i<tmp; i++)
col_odd(c,cmode,i,y0,y1);
}
void bmp(unsigned char x, unsigned char ypos, unsigned char width, unsigned char height) {
unsigned char line=ypos, endwhile=ypos+height;
dtmp=(unsigned short)x+width;
while(line<endwhile) // each row
{
for(unsigned short i=x; i<dtmp; i++) // a row
{
while(!Serial.available()); // wait for data
if(line==_BUFHEIGHT||i>=_HBYTES) Serial.read(); // out of screen: discard
else scrnptr[line*_HBYTES+i]=Serial.read(); // in screen: draw
}
line++;
}
}
void setup()
{
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, 0); // disable clock prescaler
// _PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, 3); // uncomment to use external clock (20 MHz quartz oscillator)
VPORTB.DIR |= 1;
VPORTB.OUT=255;
PORTB.PIN0CTRL=128;
PORTB.PIN1CTRL=8;
VPORTD.DIR |= 0b00001111;
#ifdef STEREO
VPORTE.DIR |= 0b00000110;
VPORTE.OUT = 2;
#endif
scrnptr=render_setup();
if (!(PORTB.IN&2)) // mode(PB1) pulled low: test pattern, enable debug
{
rect(0,0,0,_HRES/4-1,_BUFHEIGHT/4-1);
rect(1,_HRES/4,0,_HRES/2-1,_BUFHEIGHT/4-1);
rect(2,_HRES/2,0,_HRES*0.75-1,_BUFHEIGHT/4-1);
rect(3,_HRES*0.75,0,_HRES-1,_BUFHEIGHT/4-1);
rect(4,0,_BUFHEIGHT/4,_HRES/4-1,_BUFHEIGHT/2-1);
rect(5,_HRES/4,_BUFHEIGHT/4,_HRES/2-1,_BUFHEIGHT/2-1);
rect(6,_HRES/2,_BUFHEIGHT/4,_HRES*0.75-1,_BUFHEIGHT/2-1);
rect(7,_HRES*0.75,_BUFHEIGHT/4,_HRES-1,_BUFHEIGHT/2-1);
rect(8,0,_BUFHEIGHT/2,_HRES/4-1,_BUFHEIGHT*0.75-1);
rect(9,_HRES/4,_BUFHEIGHT/2,_HRES/2-1,_BUFHEIGHT*0.75-1);
rect(10,_HRES/2,_BUFHEIGHT/2,_HRES*0.75-1,_BUFHEIGHT*0.75-1);
rect(11,_HRES*0.75,_BUFHEIGHT/2,_HRES-1,_BUFHEIGHT*0.75-1);
rect(12,0,_BUFHEIGHT*0.75,_HRES/4-1,_BUFHEIGHT-1);
rect(13,_HRES/4,_BUFHEIGHT*0.75,_HRES/2-1,_BUFHEIGHT-1);
rect(14,_HRES/2,_BUFHEIGHT*0.75,_HRES*0.75-1,_BUFHEIGHT-1);
rect(15,_HRES*0.75,_BUFHEIGHT*0.75,_HRES-1,_BUFHEIGHT-1);
debug=1;
}
PORTB.PIN1CTRL=0; // mode(PB1) pullup off (to be eco friendly)
Serial.begin(115200);
}
void loop()
{
while(!Serial.available()); // wait for command
cmd=Serial.read();
if(debug)
cmd%=3; // modulo to make sure to react to any number
switch(cmd)
{
case 0: // pixel
while(Serial.available()<3);
c=Serial.read();
x0=Serial.read();
y0=Serial.read();
pix(c,x0,y0);
if(debug)
{
Serial.write(cmd);
Serial.write(c);
Serial.write(x0);
Serial.write(y0);
}
break;
case 1: // rectangle
while(Serial.available()<5);
c=Serial.read();
x0=Serial.read();
y0=Serial.read();
x1=Serial.read();
y1=Serial.read();
rect(c,x0,y0,x1,y1);
if(debug)
{
Serial.write(cmd);
Serial.write(c);
Serial.write(x0);
Serial.write(y0);
Serial.write(x1);
Serial.write(y1);
}
break;
case 2: // bitmap
while(Serial.available()<4);
x0=Serial.read();
y0=Serial.read();
x1=Serial.read();
y1=Serial.read();
bmp(x0,y0,x1,y1);
if(debug)
{
Serial.write(cmd);
Serial.write(x0);
Serial.write(y0);
Serial.write(x1);
Serial.write(y1);
}
break;
}
}