-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGamby.cpp
1656 lines (1415 loc) · 42.3 KB
/
Gamby.cpp
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Arduino.h"
#include "Gamby.h"
// Major to-do items:
// ~~~~~~~~~~~~~~~~~~
// TODO: For porting to other Arduinos (e.g. Leonardo, MEGA), move all direct
// port manipulation of LCD to GambyBase::sendByte() and maybe
// GambyBase::init(); abstract what can't be moved with appropriately named
// macros (as was done for COMMAND_MODE and DATA_MODE) to keep it all
// abstracted.
// TODO: Locate and remove any remaining weird and unnecessary typecasting,
// which was originally put in during testing and forgotten about.
// TODO: Refactor buffer in GambyGraphicsMode/GambyBlockMode to allocate
// memory dynamically, allowing for a smaller display area to save RAM,
// adding a lightweight score display, et cetera. Longer-term.
// TODO: Modify the remaining GambyBase::println() methods to call
// printNumber()/printFloat() directly, reducing the extra stack depth.
// TODO: Identify which little loops would be more efficient if flattened
// into multiple, discrete statements.
// Other items:
// ~~~~~~~~~~~~
// TODO: Fix (and enable) word-wrap in GambyTextMode.
// TODO: Consider doing something to support high-bit ASCII in a reasonable
// way. Possibly do something to skip over symbols, just use letters.
/****************************************************************************
* Macros
****************************************************************************/
/**
* Swap: Used in line drawing, assumes a and b are of XOR-able type
* thanks to http://graphics.stanford.edu/~seander/bithacks.html#SwappingValuesXOR
*/
#define SWAP(a,b) (((a) ^ (b)) && ((b) ^= (a) ^= (b), (a) ^= (b)))
/**
* Macros to put LCD into 'data' or 'command' mode (i.e. setting CS and RS).
* They make the code a bit more readable.
*/
#define COMMAND_MODE() PORTB = PORTB & ~(BIT_RS | BIT_CS);
#define DATA_MODE() PORTB = (PORTB & ~BIT_CS) | BIT_RS;
#define SET_CS_HIGH() PORTB |= BIT_CS;
#define SET_CS_LOW() PORTB = PORTB & ~BIT_CS;
#define CHAR_WIDTH(c) (byte)(pgm_read_dword(&font[c]) & 0x0F);
/****************************************************************************
*
****************************************************************************/
byte GambyBase::inputs = 0;
byte GambyBase::textDraw = TEXT_NORMAL;
/**
* Initialize the GAMBY LCD.
*
*/
void GambyBase::init() {
// Direct port manipulation to set output on pins 8-12.
DDRB = DDRB | B00111101;
/*
pinMode(LCD_SID, OUTPUT);
pinMode(LCD_SCK, OUTPUT);
pinMode(LCD_RS, OUTPUT);
pinMode(LCD_RES, OUTPUT);
pinMode(LCD_CS, OUTPUT);
digitalWrite(LCD_RES, LOW);
digitalWrite(LCD_CS, LOW);
digitalWrite(LCD_RES, HIGH);
digitalWrite(LCD_RS, HIGH);
digitalWrite(LCD_RS, LOW);
*/
PORTB = PORTB & B11000000;
PORTB = PORTB | BIT_RES | BIT_RS;
PORTB = PORTB & ~BIT_RS;
sendCommand(SOFT_RESET);
sendCommand(SET_DUTY_1, SET_DUTY_2);
sendCommand(SET_BIAS);
sendCommand(SHL_SELECT_REVERSE); //1100xXXX
sendCommand(ADC_SELECT);
sendCommand(SET_OSC_ON);
sendCommand(DC_STEP_UP);
sendCommand(REGULATOR_RESISTOR | REGULATOR_RESISTOR_VAL);
sendCommand(SET_EVR_1, SET_EVR_2 | VOLUME_CONTROL_VAL);
sendCommand(SET_BIAS);
sendCommand(POWER_CONTROL);
sendCommand(POWER_SAVE_CLEAR);
sendCommand(DISPLAY_INVERT_OFF);
sendCommand(DISPLAY_POWER_ON);
// Set up inputs
inputs = B00000000;
// digitalWrite(18, LOW);
// digitalWrite(19, LOW);
PORTC = PORTC & B11001111;
// Set Pins 2-8 as input, activate pull-up resistors
DDRD = DDRD & B00000011;
PORTD = PORTD | B11111100;
clearDisplay();
setPos(0,0);
}
/**
* Send a byte to the LCD.
*
* @param data: The byte to send
*/
void GambyBase::sendByte(byte data) {
byte i;
// digitalWrite(LCD_SID, HIGH);
// digitalWrite(LCD_SCK, HIGH);
PORTB = PORTB | BIT_SID | BIT_SCK;
for (i=0; i<8; i++) {
// digitalWrite(LCD_SID, (data & B10000000)); // clock out MSBit of data
// digitalWrite(LCD_SCK, LOW);
// digitalWrite(LCD_SCK, HIGH);
if (data & B10000000)
PORTB = (PORTB & ~BIT_SCK) | BIT_SID;
else
PORTB = PORTB & ~(BIT_SCK | BIT_SID);
PORTB = PORTB | BIT_SCK;
data = data << 0x01;
}
// Clock and data pins idle high
// digitalWrite(LCD_SID, HIGH);
PORTB = PORTB | BIT_SID;
}
/**
* Send a byte to the LCD, least significant bit first
* (i.e. backwards).
*
* @param data: The byte to send
*/
void GambyBase::sendByteLSB(byte data) {
byte i;
PORTB = PORTB | BIT_SID | BIT_SCK;
for (i=0; i<8; i++) {
if (data & 1)
PORTB = (PORTB & ~BIT_SCK) | BIT_SID;
else
PORTB = PORTB & ~(BIT_SCK | BIT_SID);
PORTB = PORTB | BIT_SCK;
data = data >> 0x01;
}
// Clock and data pins idle high
// digitalWrite(LCD_SID, HIGH);
PORTB = PORTB | BIT_SID;
}
/**
* Send a single-byte command to the LCD.
*
* @param command: The command to send, typically one of the constants
* defined in `lcd.h`.
*/
void GambyBase::sendCommand (byte command) {
COMMAND_MODE();
sendByte(command);
SET_CS_HIGH();
}
/**
* Send a two-byte command to the LCD.
*
* @param b1: The first byte
* @param b2: The second byte
*/
void GambyBase::sendCommand(byte b1, byte b2) {
COMMAND_MODE();
sendByte(b1);
sendByte(b2);
SET_CS_HIGH();
}
/**
* Erase the screen contents and place the cursor in the first column of the
* first page. If used in GambyGraphicsMode, only the screen is cleared;
* the contents of the buffer are preserved and will be redrawn when
* `update()` is called.
*
*/
void GambyBase::clearDisplay () {
byte i,j;
for (i=0; i < NUM_PAGES; i++) {
sendCommand(SET_PAGE_ADDR | i);
sendCommand(SET_COLUMN_ADDR_1, SET_COLUMN_ADDR_2);
DATA_MODE();
for (j = 0; j < NUM_COLUMNS; j++) {
sendByte(0);
}
SET_CS_HIGH();
}
currentColumn = 0;
currentPage = 0;
}
/**
* Read the state of the DPad and buttons, then set the object's `inputs`
* variable.
*
*/
void GambyBase::readInputs() {
// Digital pin 19 (A5) INPUT, digital pin 18 (A4) OUTPUT
DDRC = (DDRC & B11001111) | B00010000;
// delay(5); // Not waiting gets inconsistent results.
// Do delay without delay(), which acts weird w/ interrupts.
// TODO: Determine how long is enough. This seems to be okay, still gets
// weird sometimes.
asm("nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop");
asm("nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop");
asm("nop \n nop \n nop \n nop");
inputs = PIND >> 4;
// Digital pin 18 (A4) INPUT, digital pin 19 (A5) OUTPUT
DDRC = (DDRC & B11001111) | B00100000;
// delay(5);
asm("nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop");
asm("nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop");
asm("nop \n nop \n nop \n nop");
inputs = ~((PIND & B11110000) | inputs);
}
/**
* Set the column and 'page' location at which the next data will be
* displayed.
*
* @param col: The horizontal position, 0-97.
* @param line: The vertical 'page,' 0-7.
*/
void GambyBase::setPos(byte col, byte line) {
sendCommand(SET_PAGE_ADDR | line);
sendCommand(SET_COLUMN_ADDR_1 + ((col >> 4) & B00000111),
SET_COLUMN_ADDR_2 | (col & B00001111));
currentColumn = col;
currentPage = line;
}
/**
* Set the horizontal position of the cursor.
*
* @param column The horizontal position on screen (0-95).
*/
void GambyBase::setColumn (byte column) {
sendCommand(SET_COLUMN_ADDR_1 + (column >> 4),
B00001111 & column); // & to mask out high bits
}
/**
* Draw an 8px high icon at the current position on screen. The icon itself
* is stored in PROGMEM.
*
* @param icon: The icon's location in `PROGMEM` (e.g. the name of the
* `PROGMEM` constant).
*/
void GambyBase::drawIcon(const byte *icon) {
DATA_MODE();
byte w = pgm_read_byte_near(icon);
currentColumn += w;
for (; w > 0; w--) {
sendByte(pgm_read_byte_near(++icon));
}
}
/**
* Retrieve the width of an icon.
*
* @param icon The icon's location in `PROGMEM` (e.g. the name of the
* `PROGMEM` constant).
* @return The icon's width in pixels
*/
byte GambyBase::getIconWidth(const byte *icon) {
return pgm_read_byte_near(icon);
}
/**
* Draw an 8px high icon at the current position on screen. The icon itself
* is stored in PROGMEM.
*
* @param icon The icon's location in `PROGMEM` (e.g. the name of the
* `PROGMEM` constant).
* @param frame The frame number, 0 to (total frames)-1
*/
void GambyBase::drawIcon(const byte *icon, byte frame) {
DATA_MODE();
byte w = pgm_read_byte_near(icon);
icon += w * frame;
currentColumn += w;
for (; w > 0; w--) {
sendByte(pgm_read_byte_near(++icon));
}
}
/**
* Draw an 8px high icon at the current position on screen. The icon itself
* is stored in PROGMEM.
*
* @param icon The icon's location in `PROGMEM` (e.g. the name of the
* `PROGMEM` constant).
* @param frame The frame number, 0 to (total frames)-1
* @param transform The transform to apply, `HFLIP` and/or `VFLIP`
*/
void GambyBase::drawIcon(const byte *icon, byte frame, byte transform) {
DATA_MODE();
byte w = pgm_read_byte_near(icon);
currentColumn += w;
// This is fairly long, but it avoids doing conditionals every iteration.
// Program memory is cheaper than either SRAM or CPU cycles.
if (transform & VFLIP) {
if (transform & HFLIP) {
// Vertical and horizontal flips (same as rotate 180)
icon += w * (frame + 1) + 1;
for (; w > 0; w--)
sendByteLSB(pgm_read_byte_near(--icon));
}
else {
// Just vertical flip
icon += w * frame;
for (; w > 0; w--)
sendByteLSB(pgm_read_byte_near(++icon));
}
}
else if (transform & HFLIP) {
// Just horizontal flip
icon += w * (frame + 1) + 1;
for (; w > 0; w--)
sendByte(pgm_read_byte_near(--icon));
}
else {
// No transform. Same as normal drawIcon(), rewritten here to save a
// function call.
icon += w * frame;
for (; w > 0; w--) {
sendByte(pgm_read_byte_near(++icon));
}
}
}
/** Retrieves the width of a given character.
*
* @param idx The actual index into font data (ASCII value - 32)
* @return The character's with in pixels
*/
byte GambyBase::getCharWidth(byte idx) {
return CHAR_WIDTH(idx); //(byte)(pgm_read_dword(&font[idx]) & 0x0F);
}
/**
* Get the vertical offset of a character.
*
* @param idx: The actual index into font data (ASCII value - 32)
* @return: The character's vertical offset
*/
byte GambyBase::getCharBaseline(byte idx) {
return (byte)((pgm_read_dword(&font[idx]) >> 4) & 0x07);
}
/**
* Get the width (in pixels) of a string.
*
* @param s: the string to measure.
*/
int GambyBase::getTextWidth(char* s) {
int width = 0;
for (int i=0; s[i] != '\0'; i++) {
char c = s[i]-32;
if (c >= 0)
width += CHAR_WIDTH(c) + 1; // width + gutter
}
// No gutter after last character, so subtract it.
return width-1;
}
/**
* Get the width (in pixels) of a string in `PROGMEM`.
*
* @param s: the `PROGMEM` address (e.g. the constant's name) of the string
* to measure.
*/
int GambyBase::getTextWidth_P(const char *s) {
int width = 0;
char c = pgm_read_byte_near(s);
while (c != '\0') {
c -= 32;
if (c > 0)
width += CHAR_WIDTH(c) + 1;
c = pgm_read_byte_near(++s);
}
return width-1;
}
/**
* Move the cursor to the first column of the next line, scrolling the
* display if necessary.
*
*/
void GambyBase::newline() {
currentPage++;
if (currentPage >= NUM_PAGES) {
currentPage = 0;
}
sendCommand(SET_PAGE_ADDR | currentPage);
sendCommand(SET_COLUMN_ADDR_1, SET_COLUMN_ADDR_2);
currentColumn = 0;
}
/**
* Draw a character at the current screen position.
*
* @param c: is the ASCII character to draw.
*/
void GambyBase::drawChar(char c) {
// Newline character. Bail.
if (c == '\n') {
newline();
return;
}
DATA_MODE();
if (c == '\t') {
// If the tab will exceed the screen width, new line:
if (currentColumn + 8 > NUM_COLUMNS) {
newline();
return;
}
// else, scootch in.
for (byte t = (((currentColumn + 1) & B11111000) + 8) - currentColumn; t > 0; t--)
sendByte(textDraw);
currentColumn = ((currentColumn + 1) & B11111000) + 8;
return;
}
long d = (long)pgm_read_dword(&font[byte(c) - 32]); // character data
byte w = (byte)(d & 0x0F); // character width
byte b = (byte)((d >> 4) & 0x07); // character baseline offset
byte j;
byte col; // The current column of the character
currentColumn += w + 1;
if (currentColumn > NUM_COLUMNS) {
newline();
DATA_MODE();
currentColumn = w + 1;
}
for (; w; --w) {
col = 0;
// generate column of font bitmap
for (j = 5; j; j--) {
col = (col << 1) | ((d >> 31) & 1);
d = d << 0x01;
}
// fill out top of character.
col = col << (b+1);
sendByte(col ^ textDraw);
}
// Draw gap ('kerning') between letters, 1px wide.
sendByte(textDraw);
SET_CS_HIGH();
}
/**
* Clears the current line from the current column to the right edge of the
* screen.
*
*/
void GambyBase::clearLine () {
byte j;
DATA_MODE();
for (j = currentColumn; j <= LAST_COLUMN; j++) {
sendByte(textDraw);
}
// restore previous column.
sendCommand(SET_COLUMN_ADDR_1 + ((currentColumn >> 4) & B00000111),
B00001111 & currentColumn); // & to mask out high bits
SET_CS_HIGH();
}
/**
*
*
*
*/
void GambyBase::clearLineLeft () {
sendCommand(SET_COLUMN_ADDR_1, SET_COLUMN_ADDR_2);
DATA_MODE();
for (byte j = 0; j <= currentColumn; j++) {
sendByte(textDraw);
}
SET_CS_HIGH();
}
/**
* Write a string to the display.
*
* @param s: the string to draw.
*/
void GambyBase::print (char* s) {
while (*s)
drawChar(*s++);
}
/**
* Write a string to the display, followed by a newline.
* To save a little memory, instead of using both `println()` and
* `print()`, consider using only `print()` and 'manually' end your
* strings with a 'newline' character ('`\n`').
*
* @param s: the string to draw.
*/
void GambyBase::println(char* s) {
while (*s)
drawChar(*s++);
newline();
}
/**
* Write a PROGMEM string to the display.
*
* @param s: the PROGMEM address (e.g. the name of the variable) of the
* string to draw.
*/
void GambyBase::print_P(const char *s) {
char c = pgm_read_byte_near(s);
while (c != '\0') {
drawChar(c);
c = pgm_read_byte_near(++s);
}
}
/**
* Write a PROGMEM string to the display.
* To save a little memory, instead of using both `println_P()` and
* `print_P()`, consider using only `print_P()` and 'manually' end your
* strings with a 'newline' character ('`\n`').
*
* @param s: the PROGMEM address of the string to draw.
*/
void GambyBase::println_P(const char *s) {
print_P(s);
newline();
}
/**
* Print a numeric value
*
* @param n Any whole-number numeric value
* @param base The 'base' to use (`BIN`, `OCT`, `DEC`, or `HEX`)
*/
void GambyBase::print(long n, uint8_t base) {
if (base == 10 && n < 0) {
drawChar('-');
n = -n;
}
printNumber((unsigned long) n, base);
}
void GambyBase::print(unsigned long n, uint8_t base) {
printNumber(n, base);
}
void GambyBase::print(int n, uint8_t base) {
print((long) n, base);
}
void GambyBase::print(unsigned int n, uint8_t base) {
printNumber((unsigned long) n, base);
}
void GambyBase::print(unsigned char n, uint8_t base) {
printNumber((unsigned long) n, base);
}
void GambyBase::print(char c) {
drawChar(c);
}
void GambyBase::print(double number, uint8_t digits) {
printFloat(number, digits);
}
void GambyBase::print(float number, uint8_t digits) {
printFloat((double)number, digits);
}
void GambyBase::println(long n, uint8_t base) {
print(n, base);
newline();
}
void GambyBase::println(unsigned long n, uint8_t base) {
printNumber(n, base);
newline();
}
void GambyBase::println(int n, uint8_t base) {
print(n, base);
newline();
}
void GambyBase::println(unsigned int n, uint8_t base) {
printNumber((unsigned long)n, base);
newline();
}
void GambyBase::println(unsigned char n, uint8_t base) {
printNumber((unsigned long)n, base);
newline();
}
void GambyBase::println(char c) {
drawChar(c);
newline();
}
void GambyBase::println(double number, uint8_t digits) {
printFloat(number, digits);
newline();
}
void GambyBase::println(float number, uint8_t digits) {
printFloat((double)number, digits);
newline();
}
/**
* Private. Do the actual printing of a non-decimal number.
* Separated from print() to avoid ambiguous overloading.
*
*/
void GambyBase::printNumber(unsigned long n, uint8_t base) {
// TODO: The Arduino standard library doesn't use itoa(), see if what it
// does is faster.
char buf[10];
print(itoa(n, buf, base));
}
/**
* Private. Print a decimal number.
* Separated from print() to avoid ambiguous overloading.
*
*/
void GambyBase::printFloat(double number, uint8_t digits) {
// This is based on code lifted from the Arduino's standard library
// hardware/arduino/cores/arduino/Print.cpp
// Comments are (largely) from original code
// Handle negative numbers
if (number < 0.0) {
drawChar('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
printNumber(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
drawChar('.');
// Extract digits from the remainder one at a time
while (digits-- > 0) {
remainder *= 10.0;
int toPrint = int(remainder);
print(toPrint);
remainder -= toPrint;
}
}
/****************************************************************************
*
****************************************************************************/
byte GambyTextMode::scrollMode = SCROLL_NORMAL;
byte GambyTextMode::wrapMode = WRAP_CHAR;
/**
* Constructor.
*
*/
GambyTextMode::GambyTextMode() {
init();
currentPage = 0;
currentColumn = 0;
offset = 0;
wrapMode = WRAP_WORD;
scrollMode = SCROLL_NORMAL;
}
/**
* Scroll the screen up (or down) by one or more lines.
*
* @param s: The number of lines to scroll, positive or negative.
*/
void GambyTextMode::scroll(int s) {
offset += s;
if (offset >= NUM_PAGES)
offset -= NUM_PAGES;
else if (offset < 0)
offset += NUM_PAGES;
sendCommand(SET_INITIAL_COM0_1,
(byte)offset << 3); // shift 3 places to multiply by 8.
}
/**
* Set the cursor's column and line (relative to current scrolling).
*
* @param col The column (0 to 95)
* @param line The vertical 8 pixel 'page' (0 to 7)
*/
void GambyTextMode::setPos(byte col, byte line) {
currentColumn = col;
int l = line - offset;
if (l < 0)
l += NUM_PAGES;
sendCommand(SET_PAGE_ADDR | (byte)l);
sendCommand(SET_COLUMN_ADDR_1 + ((currentColumn >> 4) & B00000111),
SET_COLUMN_ADDR_2 | (currentColumn & B00001111));
}
/**
* Text-specific clear screen; resets scrolling offset.
*
*/
void GambyTextMode::clearScreen() {
offset = 0;
clearDisplay();
}
/**
* Move the cursor to the first column of the next line, scrolling the
* display if necessary.
*
*/
void GambyTextMode::newline() {
currentPage++;
if (currentPage >= NUM_PAGES) {
switch (scrollMode) {
case SCROLL_WRAP:
currentPage = 0;
break;
case SCROLL_NORMAL:
scroll(-1);
default:
// both SCROLL_NORMAL and SCROLL_NONE set the current line to the last.
currentPage = (NUM_PAGES - 1);
}
}
sendCommand(SET_PAGE_ADDR | (currentPage - offset));
sendCommand(SET_COLUMN_ADDR_1, SET_COLUMN_ADDR_2);
currentColumn = 0;
clearLine();
}
/****************************************************************************
*
****************************************************************************/
/**
* Constructor. Runs once when the mode's instance is created, initializing
* the display.
*/
GambyBlockMode::GambyBlockMode() {
byte i,j;
for (i=0; i<NUM_BLOCK_COLUMNS; i++)
for (j=0; j<NUM_PAGES; j++)
offscreen[i][j] = 0;
init();
}
/**
*
*
*/
void GambyBlockMode::clearScreen() {
byte i,j;
for (i=0; i<NUM_BLOCK_COLUMNS; i++)
for (j=0; j<NUM_PAGES; j++)
offscreen[i][j] = 0;
clearDisplay();
}
/**
* Retrieve the index (number) of the block at the given location.
*
* @param x The horizontal position, 0 to 23.
* @param y The vertical position, 0 to 15.
* @return The index of the block (0 to 15) at the given coordinates.
*/
byte GambyBlockMode::getBlock(byte x, byte y) {
if (y & 1)
// odd blocks shifted to the high four bits
return (offscreen[x][y >> 1] >> 4) & B00001111;
return offscreen[x][y >> 1] & B00001111;
}
/**
* Set a block at a given location without immediately updating the screen.
* The change is made to the offscreen buffer; a separate call to update()
* is required for it to show up. Use this if you want to do a lot of drawing
* and only show the end results.
*
* @param x The horizontal position, 0 to 23.
* @param y The vertical position, 0 to 15.
* @param block The index of the block to draw (0 to 15).
*/
void GambyBlockMode::setBlock(byte x, byte y, byte block) {
// each page is two blocks high, each block index is 4 bits
byte evenBlockIdx, oddBlockIdx;
byte page = y >> 1;
// Get the other block in that 8-pixel-high 'page'
// Set the specified block in the offscreen
if (y & 1) {
// odd blocks shifted to the high four bits
oddBlockIdx = block & B00001111;
evenBlockIdx = offscreen[x][page] & B00001111;
offscreen[x][page] = (oddBlockIdx << 4) | evenBlockIdx;
} else {
evenBlockIdx = block;
oddBlockIdx = (offscreen[x][page] & B11110000);
offscreen[x][page] = oddBlockIdx | evenBlockIdx;
}
}
/**
* Draw a block at a given location.
*
* @param x The horizontal position, 0 to 23.
* @param y The vertical position, 0 to 15.
* @param block The index of the block to draw (0 to 15).
*/
void GambyBlockMode::drawBlock(byte x, byte y, byte block) {
// TODO: Optimize this!
// each page is two blocks high, each block index is 4 bits
byte evenBlockIdx, oddBlockIdx;
byte page = y >> 1;
// Get the other block in that 8-pixel-high 'page'
// Set the specified block in the offscreen
if (y & 1) {
// odd blocks shifted to the high four bits
oddBlockIdx = block & B00001111;
evenBlockIdx = offscreen[x][page] & B00001111;
offscreen[x][page] = (oddBlockIdx << 4) | evenBlockIdx;
} else {
evenBlockIdx = block;
oddBlockIdx = (offscreen[x][page] & B11110000);
offscreen[x][page] = oddBlockIdx | evenBlockIdx;
oddBlockIdx = oddBlockIdx >> 4;
}
// Do the drawing
unsigned int oddBlock = pgm_read_word_near(palette + oddBlockIdx);
unsigned int evenBlock = pgm_read_word_near(palette + evenBlockIdx);
byte i;
// Don't send set position command if already in the right place
if ((currentPage != page) || (currentColumn != x)) {
setPos(x<<2, page);
}
DATA_MODE();
for (i=0; i<4; i++) {
// Build a two-block set four bits at a time
byte combo = (((oddBlock & B00001111) << 4) | (evenBlock & B00001111));
sendByte(combo);
oddBlock = oddBlock >> 4;
evenBlock = evenBlock >> 4;
}
currentPage = page;
currentColumn = x + 1;
}
/**
* Redraw the entire screen. In `GambyBlockMode`, this will cause the entire
* display to redraw, showing blocks set via `setBlock()`.
*
*/
void GambyBlockMode::update() {
update(0,0,NUM_BLOCK_COLUMNS-1, NUM_BLOCK_ROWS-1);
}
/**
* Redraw a specific portion of the display. When used with `setBlock()`, you
* can update only the portion you know you have changed, which will be
* faster.
*
* @param x1 The left edge of the region to update, 0 to 23.
* @param y1 The top edge of the region to update, 0 to 15.
* @param x2 The right edge of the region to update, x1 to 24.
* @param y2 The bottom edge of the region to update y1 to 15.
*/
void GambyBlockMode::update(byte x1, byte y1, byte x2, byte y2) {
// TODO: This can probably be further optimized.
byte x, y, i;
unsigned int oddBlock, evenBlock;
for (y1 &= ~1; y1 <= (y2 | 1); y1 += 2) {
y = y1 >> 1;
setPos(x1 << 2, y);
DATA_MODE();
for (x=x1; x<=x2; x++) {
oddBlock = pgm_read_word_near(palette + ((offscreen[x][y] >> 4) & B00001111));
evenBlock = pgm_read_word_near(palette + (offscreen[x][y] & B00001111));
for (i=0; i<4; i++) {
// Build a two-block set four bits at a time
sendByte(((oddBlock & B00001111) << 4) | (evenBlock & B00001111));
oddBlock = oddBlock >> 4;
evenBlock = evenBlock >> 4;
}
}
}
setPos(0,0);
currentPage = 0;
currentColumn = 0;