/
2048.c
537 lines (508 loc) · 19.2 KB
/
2048.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
const unsigned char FONT8x16[97][16] = {
0x08,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x08,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x3C,0x3C,0x3C,0x18,0x18,0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00,
0x00,0x63,0x63,0x63,0x22,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x36,0x36,0x7F,0x36,0x36,0x36,0x7F,0x36,0x36,0x00,0x00,0x00,0x00,
0x0C,0x0C,0x3E,0x63,0x61,0x60,0x3E,0x03,0x03,0x43,0x63,0x3E,0x0C,0x0C,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x61,0x63,0x06,0x0C,0x18,0x33,0x63,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x1C,0x36,0x36,0x1C,0x3B,0x6E,0x66,0x66,0x3B,0x00,0x00,0x00,0x00,
0x00,0x30,0x30,0x30,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0C,0x18,0x18,0x30,0x30,0x30,0x30,0x18,0x18,0x0C,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x0C,0x0C,0x06,0x06,0x06,0x06,0x0C,0x0C,0x18,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x42,0x66,0x3C,0xFF,0x3C,0x66,0x42,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x18,0x18,0x18,0xFF,0x18,0x18,0x18,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x18,0x30,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0x03,0x07,0x0E,0x1C,0x38,0x70,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x63,0x63,0x6B,0x6B,0x63,0x63,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x0C,0x1C,0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3F,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x03,0x06,0x0C,0x18,0x30,0x61,0x63,0x7F,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x03,0x03,0x1E,0x03,0x03,0x03,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x06,0x0E,0x1E,0x36,0x66,0x66,0x7F,0x06,0x06,0x0F,0x00,0x00,0x00,0x00,
0x00,0x00,0x7F,0x60,0x60,0x60,0x7E,0x03,0x03,0x63,0x73,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x1C,0x30,0x60,0x60,0x7E,0x63,0x63,0x63,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x7F,0x63,0x03,0x06,0x06,0x0C,0x0C,0x18,0x18,0x18,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x63,0x63,0x3E,0x63,0x63,0x63,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x63,0x63,0x63,0x3F,0x03,0x03,0x06,0x3C,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x18,0x30,0x00,0x00,
0x00,0x00,0x00,0x06,0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x06,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x60,0x30,0x18,0x0C,0x06,0x0C,0x18,0x30,0x60,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x63,0x06,0x0C,0x0C,0x0C,0x00,0x0C,0x0C,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x63,0x6F,0x6B,0x6B,0x6E,0x60,0x60,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x08,0x1C,0x36,0x63,0x63,0x63,0x7F,0x63,0x63,0x63,0x00,0x00,0x00,0x00,
0x00,0x00,0x7E,0x33,0x33,0x33,0x3E,0x33,0x33,0x33,0x33,0x7E,0x00,0x00,0x00,0x00,
0x00,0x00,0x1E,0x33,0x61,0x60,0x60,0x60,0x60,0x61,0x33,0x1E,0x00,0x00,0x00,0x00,
0x00,0x00,0x7C,0x36,0x33,0x33,0x33,0x33,0x33,0x33,0x36,0x7C,0x00,0x00,0x00,0x00,
0x00,0x00,0x7F,0x33,0x31,0x34,0x3C,0x34,0x30,0x31,0x33,0x7F,0x00,0x00,0x00,0x00,
0x00,0x00,0x7F,0x33,0x31,0x34,0x3C,0x34,0x30,0x30,0x30,0x78,0x00,0x00,0x00,0x00,
0x00,0x00,0x1E,0x33,0x61,0x60,0x60,0x6F,0x63,0x63,0x37,0x1D,0x00,0x00,0x00,0x00,
0x00,0x00,0x63,0x63,0x63,0x63,0x7F,0x63,0x63,0x63,0x63,0x63,0x00,0x00,0x00,0x00,
0x00,0x00,0x3C,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00,
0x00,0x00,0x0F,0x06,0x06,0x06,0x06,0x06,0x06,0x66,0x66,0x3C,0x00,0x00,0x00,0x00,
0x00,0x00,0x73,0x33,0x36,0x36,0x3C,0x36,0x36,0x33,0x33,0x73,0x00,0x00,0x00,0x00,
0x00,0x00,0x78,0x30,0x30,0x30,0x30,0x30,0x30,0x31,0x33,0x7F,0x00,0x00,0x00,0x00,
0x00,0x00,0x63,0x77,0x7F,0x6B,0x63,0x63,0x63,0x63,0x63,0x63,0x00,0x00,0x00,0x00,
0x00,0x00,0x63,0x63,0x73,0x7B,0x7F,0x6F,0x67,0x63,0x63,0x63,0x00,0x00,0x00,0x00,
0x00,0x00,0x1C,0x36,0x63,0x63,0x63,0x63,0x63,0x63,0x36,0x1C,0x00,0x00,0x00,0x00,
0x00,0x00,0x7E,0x33,0x33,0x33,0x3E,0x30,0x30,0x30,0x30,0x78,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x63,0x63,0x63,0x63,0x63,0x6B,0x6F,0x3E,0x06,0x07,0x00,0x00,
0x00,0x00,0x7E,0x33,0x33,0x33,0x3E,0x36,0x36,0x33,0x33,0x73,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x63,0x30,0x1C,0x06,0x03,0x63,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0xFF,0xDB,0x99,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00,
0x00,0x00,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x36,0x1C,0x08,0x00,0x00,0x00,0x00,
0x00,0x00,0x63,0x63,0x63,0x63,0x63,0x6B,0x6B,0x7F,0x36,0x36,0x00,0x00,0x00,0x00,
0x00,0x00,0xC3,0xC3,0x66,0x3C,0x18,0x18,0x3C,0x66,0xC3,0xC3,0x00,0x00,0x00,0x00,
0x00,0x00,0xC3,0xC3,0xC3,0x66,0x3C,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00,
0x00,0x00,0x7F,0x63,0x43,0x06,0x0C,0x18,0x30,0x61,0x63,0x7F,0x00,0x00,0x00,0x00,
0x00,0x00,0x3C,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x3C,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0xC0,0xE0,0x70,0x38,0x1C,0x0E,0x07,0x03,0x01,0x00,0x00,0x00,0x00,
0x00,0x00,0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00,0x00,0x00,0x00,
0x08,0x1C,0x36,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
0x18,0x18,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3C,0x46,0x06,0x3E,0x66,0x66,0x3B,0x00,0x00,0x00,0x00,
0x00,0x00,0x70,0x30,0x30,0x3C,0x36,0x33,0x33,0x33,0x33,0x6E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3E,0x63,0x60,0x60,0x60,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x0E,0x06,0x06,0x1E,0x36,0x66,0x66,0x66,0x66,0x3B,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3E,0x63,0x63,0x7E,0x60,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x1C,0x36,0x32,0x30,0x7C,0x30,0x30,0x30,0x30,0x78,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3B,0x66,0x66,0x66,0x66,0x3E,0x06,0x66,0x3C,0x00,0x00,
0x00,0x00,0x70,0x30,0x30,0x36,0x3B,0x33,0x33,0x33,0x33,0x73,0x00,0x00,0x00,0x00,
0x00,0x00,0x0C,0x0C,0x00,0x1C,0x0C,0x0C,0x0C,0x0C,0x0C,0x1E,0x00,0x00,0x00,0x00,
0x00,0x00,0x06,0x06,0x00,0x0E,0x06,0x06,0x06,0x06,0x06,0x66,0x66,0x3C,0x00,0x00,
0x00,0x00,0x70,0x30,0x30,0x33,0x33,0x36,0x3C,0x36,0x33,0x73,0x00,0x00,0x00,0x00,
0x00,0x00,0x1C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x1E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x6E,0x7F,0x6B,0x6B,0x6B,0x6B,0x6B,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x6E,0x33,0x33,0x33,0x33,0x33,0x33,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3E,0x63,0x63,0x63,0x63,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x6E,0x33,0x33,0x33,0x33,0x3E,0x30,0x30,0x78,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3B,0x66,0x66,0x66,0x66,0x3E,0x06,0x06,0x0F,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x6E,0x3B,0x33,0x30,0x30,0x30,0x78,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3E,0x63,0x38,0x0E,0x03,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x08,0x18,0x18,0x7E,0x18,0x18,0x18,0x18,0x1B,0x0E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x66,0x66,0x66,0x66,0x66,0x66,0x3B,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x63,0x63,0x36,0x36,0x1C,0x1C,0x08,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x63,0x63,0x63,0x6B,0x6B,0x7F,0x36,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x63,0x36,0x1C,0x1C,0x1C,0x36,0x63,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x63,0x63,0x63,0x63,0x63,0x3F,0x03,0x06,0x3C,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x7F,0x66,0x0C,0x18,0x30,0x63,0x7F,0x00,0x00,0x00,0x00,
0x00,0x00,0x0E,0x18,0x18,0x18,0x70,0x18,0x18,0x18,0x18,0x0E,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x18,0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x00,
0x00,0x00,0x70,0x18,0x18,0x18,0x0E,0x18,0x18,0x18,0x18,0x70,0x00,0x00,0x00,0x00,
0x00,0x00,0x3B,0x6E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
//*********************************************
// Functions
//*********************************************
void LCDCommand(unsigned char data);
void LCDData(unsigned char data);
void LCDInit(void);
void LCDClear(int color);
void LCDSetPixel(int color, unsigned char x, unsigned char y);
void LCDSetLine(int x0, int y0, int x1, int y1, int color);
void LCDSetRect(int x0, int y0, int x1, int y1, unsigned char fill, int color);
void LCDPutStr(char *pString, int x, int y, int fColor, int bColor);
//*********************************************
// used PCF-8833 Controller Commands
//*********************************************
#define NOP 0x00
#define BSTRON 0x03
#define SLEEPOUT 0x11
#define SETCON 0x25
#define DISPON 0x29
#define CASET 0x2A
#define PASET 0x2B
#define RAMWR 0x2C
#define MADCTL 0x36
#define COLMOD 0x3A /* color mode 12/8 bit*/
//************************************************
// 12-Bit Colors
//************************************************
#define BLACK 0x000
#define NAVY 0x008
#define BLUE 0x00F
#define TEAL 0x088
#define EMERALD 0x0C5
#define GREEN 0x0F0
#define CYAN 0x0FF
#define SLATE 0x244
#define INDIGO 0x408
#define TURQUOISE 0x4ED
#define OLIVE 0x682
#define MAROON 0x800
#define PURPLE 0x808
#define GRAY 0x888
#define SKYBLUE 0x8CE
#define BROWN 0xB22
#define CRIMSON 0xD13
#define ORCHID 0xD7D
#define RED 0xF00
#define MAGENTA 0xF0F
#define ORANGE 0xF40
#define PINK 0xF6A
#define CORAL 0xF75
#define SALMON 0xF87
#define GOLD 0xFD0
#define YELLOW 0xFF0
#define WHITE 0xFFF
// PORT and BIT number for each control pin
#define _USE_ARDUINO
// Arduino duemilanove digital port pins
#define LCD_RES_PIN 9
#define CS_PIN 10
#define DIO_PIN 11
#define SCK_PIN 13
// AVR ATmega328p POTRB pins
#define LCD_RES 1 // D9
#define CS 2 // D10
#define DIO 3 // D11
#define SCK 5 // D13
// AVR ATmega328p pin ports
#define PORT_CS PORTB
#define PORT_SCK PORTB
#define PORT_RES PORTB
#define PORT_DIO PORTB
//*********************************
// Macros sb1, cb0
//*********************************
#define sb1(port, pin) port = port | (uint8_t)(1 << pin)
#define cb0(port, pin) port = port & (uint8_t)~(1 << pin)
//******************************************
// LCD_d.c
//******************************************
#include <string.h>
#include "Arduino.h"
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
//*********************************************
void delay_us(int microSecs) {delayMicroseconds(microSecs);}
//*********************************************
void bytout(char bajt) {
char j;
for (j = 0; j < 8; j++) {
if ((bajt & 0x80) == 0x80) {sb1(PORT_DIO, DIO);}
else{cb0(PORT_DIO, DIO);}
delay_us(1);
cb0(PORT_SCK, SCK); delay_us(1); sb1(PORT_SCK, SCK); // 01 clock pulse
bajt <<= 1;
}}
//*********************************************
// LCDCommand(RAMWR)
//*********************************************
void LCDCommand(unsigned char bajt) {
cb0(PORT_CS, CS); delay_us(1); // chip enable
cb0(PORT_DIO, DIO); delay_us(1); // output 0 (9th bit 0 = command)
cb0(PORT_SCK, SCK); delay_us(1); sb1(PORT_SCK, SCK); // 01 clock pulse
bytout(bajt);
sb1(PORT_CS, CS);
}
//************************************************
// LCDData(RAMWR)
//************************************************
void LCDData(unsigned char bajt) {
cb0(PORT_CS, CS); delay_us(1); // chip enable
sb1(PORT_DIO, DIO); delay_us(1); // output 1 (9th bit 1 = data)
cb0(PORT_SCK, SCK); delay_us(1); sb1(PORT_SCK, SCK); // 01 clock pulse
bytout(bajt);
sb1(PORT_CS, CS);
}
//****************************************
// LCDInit()
//****************************************
void LCDInit(void) {
DDRB = ((1<<CS)|(1<<DIO)|(1<<SCK)|(1<<LCD_RES)); // pins as output
delay(100);
cb0(PORT_SCK, SCK);
cb0(PORT_DIO, DIO); delay_us(10);
sb1(PORT_CS, CS); delay_us(10);
cb0(PORT_RES, LCD_RES); delay(100);
sb1(PORT_RES, LCD_RES); delay(100);
sb1(PORT_SCK, SCK);
sb1(PORT_DIO, DIO); delay_us(10);
LCDCommand(SLEEPOUT); // Sleep out
LCDCommand(BSTRON); // Boost on
LCDCommand(MADCTL); LCDData(0xC0); // Memory Access Control (C8 ?)
LCDCommand(COLMOD); LCDData(0x03); // Set Color Mode 03=12bit (02=8bit)
LCDCommand(SETCON); LCDData(0x30); // Set Contrast
LCDCommand(NOP); delay(100);
LCDCommand(DISPON); // Display on
}
//*******************************************
// LCDClear(GRAY)
//*******************************************
void LCDClear(int color) {
unsigned int i;
LCDCommand(PASET); LCDData(0); LCDData(131);
LCDCommand(CASET); LCDData(0); LCDData(131);
LCDCommand(RAMWR);
/* 3 bytes for 2 pixels */
for (i=0; i < 132*132/2; i++) {
LCDData((color >> 4) & 0x00FF);
LCDData(((color & 0x0F) << 4) | (color >> 8));
LCDData(color & 0x0FF);
}}
//*********************************************
// LCDSetPixel(GOLD, 10, 20)
//*********************************************
void LCDSetPixel(int color, unsigned char x, unsigned char y) {
LCDCommand(PASET); LCDData(y); LCDData(131);
LCDCommand(CASET); LCDData(x); LCDData(131);
LCDCommand(RAMWR);
LCDData((unsigned char)((color >> 4)& 0x00FF));
LCDData((unsigned char)(((color & 0x0F) << 4 ) | 0x00));
}
//****************************************
// LCDSetLine
//****************************************
void LCDSetLine(int x0, int y0, int x1, int y1, int color) {
int dy = y1 - y0; int dx = x1 - x0;
int stepx, stepy;
if (dy < 0) {dy = -dy; stepy = -1;}
else stepy = 1;
if (dx < 0) {dx = -dx; stepx = -1;}
else stepx = 1;
dy <<= 1; dx <<= 1; // dy,dx = 2*dy,dx
LCDSetPixel(color, x0, y0);
//
if (dx > dy) {
int fraction = dy - (dx >> 1);
while (x0 != x1) {
if (fraction >= 0) {y0 += stepy; fraction -= dx;}
x0 += stepx; fraction += dy; LCDSetPixel(color, x0, y0);
}
}
else {
int fraction = dx - (dy >> 1);
while (y0 != y1) {
if (fraction >= 0) {x0 += stepx; fraction -= dy;}
y0 += stepy; fraction += dx; LCDSetPixel(color, x0, y0);
}
}
}
//******************************************
// LCDSetRect
//******************************************
void LCDSetRect(int x0, int y0, int x1, int y1, unsigned char fill, int color) {
if (fill == 1) { // filled rectangle
int yd;
if(y0 > y1) yd = y0 - y1;
else yd = y1 - y0;
while(yd > 0){
LCDSetLine(x0, y0, x1, y0, color);
if(y0 > y1) y0--;
else y0++;
yd--;
}
}
else { // unfilled rectangle
LCDSetLine(x0, y0, x1, y0, color);
LCDSetLine(x1, y0, x1, y1, color);
LCDSetLine(x0, y1, x1, y1, color);
LCDSetLine(x0, y0, x0, y1, color);
}
}
//*****************************************
// putch
//Draws ASCII character c
//x,y = row,column,fC,bC - 12-bit color.
//Original author: James P Lynch. Edited.
//*****************************************
void putch(char c, int x, int y, int fC, int bC) {
int i,j;
unsigned int nCols;
unsigned int nRows;
unsigned int nBytes;
unsigned char PixelRow;
unsigned char Mask;
unsigned int Word0;
unsigned int Word1;
unsigned char *pFont;
unsigned char *pChar;
pFont = (unsigned char *)FONT8x16; // ptr to font table
nCols = *pFont; nRows = *(pFont + 1); nBytes = *(pFont + 2);
// ptr to last byte of the character
pChar = pFont + (nBytes * (c - 0x1F)) + nBytes - 1;
LCDCommand(PASET); LCDData(y); LCDData(y + nRows - 1);
LCDCommand(CASET); LCDData(x); LCDData(x + nCols - 1);
LCDCommand(RAMWR);
// each row from bottom to top
for (i = nRows - 1; i >= 0; i--) {
PixelRow = *pChar++; Mask = 0x80;
// left to right each pixel of row 3 bytes = two pixels each loop
for (j = 0; j < nCols; j += 2) {
// if pixel bit set, use foreground color; else use the background color
// pixel color for two successive pixels
if ((PixelRow & Mask) == 0) Word0 = bC;
else Word0 = fC;
Mask = Mask >> 1;
if ((PixelRow & Mask) == 0) Word1 = bC;
else Word1 = fC;
Mask = Mask >> 1;
// output 3 bytes (2 pixels)
LCDData((Word0 >> 4) & 0xFF);
LCDData(((Word0 & 0xF) << 4) | ((Word1 >> 8) & 0xF));
LCDData(Word1 & 0xFF);
}
}
LCDCommand(NOP);
}
// ****************************************
// LCDPutStr
// outputs a null-terminated char. string
// ****************************************
void LCDPutStr(char *pString, int x, int y, int fColor, int bColor) {
while (*pString != 0x00) { // until null-terminator
putch(*pString++, x, y, fColor, bColor);
x = x + 8;
if (x > 131) break;
}
}
const int LED = 11;
// prawo - 504
// lew ~140
// gora 0
// dol 330
int last_dir = 0;
const int IDL = 0;
const int UP = 1;
const int RIGHT = 2;
const int DOWN = 3;
const int LEFT = 4;
const int last_diod = 1;
int started = 0;
int t[17];
long score = 0;
char *Q,*W="adws",D,x,y,X;
void refresh_display() {
LCDClear(BLACK);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int current = t[i*4+j];
int to_display = current ? 1 : 0;
to_display = to_display << current;
char ss[5];
sprintf(ss, "%d ", to_display);
LCDPutStr(ss, 20+26*j, 20+26*i, WHITE, BLACK);
}
}
char score_str[25];
sprintf(score_str, "Score: %ld", score);
LCDPutStr(score_str, 5, 2, WHITE, BLACK);
}
void loop() {
// Read input
int joystick = analogRead(0);
char c = 'X';
int cur_dir = 0;
if (0 <= joystick && joystick <= 10) {
cur_dir = UP;
c = 'w';
} else if (130 <= joystick && joystick <= 150) {
cur_dir = LEFT;
c = 'a';
} else if (320 <= joystick && joystick <= 350) {
cur_dir = DOWN;
c = 's';
} else if (490 <= joystick && joystick <= 510) {
cur_dir = RIGHT;
c = 'd';
} else {
return;
}
if (!started) {
started = 1;
add_random_field();
add_random_field();
refresh_display();
return;
}
move(c);
}
int m(int x, int y){
return !D?x+y*4:D==1?3-x+y*4:D==2?y+x*4:y+(3-x)*4;
}
int c(){
for(y=0; y<3; ++y)
for(x=0; x<3; ++x){D=t[x+y*4];
if(t[x+1+y*4]==D||t[x+4+y*4]==D)x=y=9;
}
return y>4;
}
int add_random_field(){
while(t[x=(rand()&15)]);
if (rand() % 100 < 10)
return t[x]=2;
return t[x]=1;
}
int f(){
for(x=0; x<17; ++x)
if(X=(t[x]==11))x=32;
return x<18;
}
int b_t[17];
void backup_table() {
for (int i = 0; i < 17; ++i) {
b_t[i] = t[i];
}
}
int table_changed() {
int ret_val = 0;
for (int i = 0; i < 17; ++i) {
ret_val += (t[i] != b_t[i]);
}
return ret_val;
}
void move(char input){
char sss[10];
sprintf(sss, "%c", input);
backup_table();
f();
c();
x=0;
while(!(Q=strchr(W,input)));
D=Q-W;
for(y=0; y<4; ++y) {
for(X=0,x=1; x<4; ++x) {
if(t[m(x,y)]) {
if(t[m(x,y)]==t[m(X,y)]&&t[m(X,y)]++) {
score += 1 << t[m(X,y)];
t[m(x,y)]=0;
}
X=x;
}
}
}
do {
for(y=0; y<4; ++y) {
for(x=0; x<3; ++x) {
if(!t[m(x,y)]&&(X=t[m(x+1,y)])) {
t[m(x,y)]=X;
t[m(x+1,y)]=0;
x=y=9;
}
}
}
} while(y>4);
if (table_changed()) {
add_random_field();
}
refresh_display();
}
void setup() {
for (int i = 0; i < 17; i++) { t[i] = 0; }
pinMode(LED, OUTPUT);
pinMode(3, OUTPUT); digitalWrite(3, HIGH);
LCDInit();
LCDClear(BLACK); delay(100);
char hello[5] = "2048";
char start[17] = "Move joystick...";
LCDPutStr(hello, 50, 50, CYAN, BLACK);
LCDPutStr(start, 5, 75, YELLOW, BLACK);
}