Sim mbed
/
FontTest3
main.cpp@0:304c871df0a8, 2009-12-06 (annotated)
- Committer:
- Sim
- Date:
- Sun Dec 06 16:28:01 2009 +0000
- Revision:
- 0:304c871df0a8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sim | 0:304c871df0a8 | 1 | #include "mbed.h" |
Sim | 0:304c871df0a8 | 2 | #include "spilcd.h" |
Sim | 0:304c871df0a8 | 3 | #include "kfont8.h" |
Sim | 0:304c871df0a8 | 4 | |
Sim | 0:304c871df0a8 | 5 | #define countof(x) ( sizeof(x) / sizeof(x[0]) ) |
Sim | 0:304c871df0a8 | 6 | |
Sim | 0:304c871df0a8 | 7 | SPILCD lcd(p21, p22, p23, p11, p12, p13); |
Sim | 0:304c871df0a8 | 8 | |
Sim | 0:304c871df0a8 | 9 | int csrx = 0; |
Sim | 0:304c871df0a8 | 10 | int csry = 0; |
Sim | 0:304c871df0a8 | 11 | int offsety = 0; |
Sim | 0:304c871df0a8 | 12 | bool kstate = false; |
Sim | 0:304c871df0a8 | 13 | unsigned char kbuf; |
Sim | 0:304c871df0a8 | 14 | |
Sim | 0:304c871df0a8 | 15 | void locate(int x, int y){ |
Sim | 0:304c871df0a8 | 16 | csrx = x; |
Sim | 0:304c871df0a8 | 17 | csry = y; |
Sim | 0:304c871df0a8 | 18 | if(csrx < 0) csrx = 0; |
Sim | 0:304c871df0a8 | 19 | if(csry < 0) csry = 0; |
Sim | 0:304c871df0a8 | 20 | csrx &= 31; |
Sim | 0:304c871df0a8 | 21 | csry &= 7; |
Sim | 0:304c871df0a8 | 22 | lcd.moveto(csrx << 3, (offsety + csry) & 7); |
Sim | 0:304c871df0a8 | 23 | }; |
Sim | 0:304c871df0a8 | 24 | |
Sim | 0:304c871df0a8 | 25 | void scroll(void){ |
Sim | 0:304c871df0a8 | 26 | int i; |
Sim | 0:304c871df0a8 | 27 | offsety = (offsety + 1) & 7; |
Sim | 0:304c871df0a8 | 28 | lcd.cmd(0x40 | offsety << 3); |
Sim | 0:304c871df0a8 | 29 | lcd.moveto(0, (offsety + 7) & 7); |
Sim | 0:304c871df0a8 | 30 | for(i = 0; i < 128; i++) lcd.write(0); |
Sim | 0:304c871df0a8 | 31 | } |
Sim | 0:304c871df0a8 | 32 | |
Sim | 0:304c871df0a8 | 33 | void newline(void){ |
Sim | 0:304c871df0a8 | 34 | csrx = 0; |
Sim | 0:304c871df0a8 | 35 | if(++csry == 8){ |
Sim | 0:304c871df0a8 | 36 | scroll(); |
Sim | 0:304c871df0a8 | 37 | csry = 7; |
Sim | 0:304c871df0a8 | 38 | } |
Sim | 0:304c871df0a8 | 39 | locate(csrx, csry); |
Sim | 0:304c871df0a8 | 40 | } |
Sim | 0:304c871df0a8 | 41 | |
Sim | 0:304c871df0a8 | 42 | // draw 4x8 font |
Sim | 0:304c871df0a8 | 43 | void drawfont4(unsigned char c){ |
Sim | 0:304c871df0a8 | 44 | const unsigned char *p = &font4[c << 2]; |
Sim | 0:304c871df0a8 | 45 | lcd.write(p[0]); |
Sim | 0:304c871df0a8 | 46 | lcd.write(p[1]); |
Sim | 0:304c871df0a8 | 47 | lcd.write(p[2]); |
Sim | 0:304c871df0a8 | 48 | lcd.write(p[3]); |
Sim | 0:304c871df0a8 | 49 | // cursor control |
Sim | 0:304c871df0a8 | 50 | if(++csrx == 32) newline(); |
Sim | 0:304c871df0a8 | 51 | } |
Sim | 0:304c871df0a8 | 52 | |
Sim | 0:304c871df0a8 | 53 | const unsigned char *findface(unsigned short c){ |
Sim | 0:304c871df0a8 | 54 | const unsigned char *p = NULL; |
Sim | 0:304c871df0a8 | 55 | int i, sum; |
Sim | 0:304c871df0a8 | 56 | for(sum = i = 0; i < countof(font8table); i++){ |
Sim | 0:304c871df0a8 | 57 | if(font8table[i].start <= c && c <= font8table[i].end){ |
Sim | 0:304c871df0a8 | 58 | p = &font8[(sum + c - font8table[i].start) << 3]; |
Sim | 0:304c871df0a8 | 59 | break; |
Sim | 0:304c871df0a8 | 60 | } |
Sim | 0:304c871df0a8 | 61 | sum += font8table[i].end - font8table[i].start + 1; |
Sim | 0:304c871df0a8 | 62 | } |
Sim | 0:304c871df0a8 | 63 | return p; |
Sim | 0:304c871df0a8 | 64 | } |
Sim | 0:304c871df0a8 | 65 | |
Sim | 0:304c871df0a8 | 66 | // draw 8x8 font |
Sim | 0:304c871df0a8 | 67 | void drawkanji(unsigned short c){ |
Sim | 0:304c871df0a8 | 68 | const unsigned char *p = findface(c); |
Sim | 0:304c871df0a8 | 69 | if(p == NULL) return; |
Sim | 0:304c871df0a8 | 70 | |
Sim | 0:304c871df0a8 | 71 | if(csrx >= 31) newline(); |
Sim | 0:304c871df0a8 | 72 | lcd.write(p[0]); |
Sim | 0:304c871df0a8 | 73 | lcd.write(p[1]); |
Sim | 0:304c871df0a8 | 74 | lcd.write(p[2]); |
Sim | 0:304c871df0a8 | 75 | lcd.write(p[3]); |
Sim | 0:304c871df0a8 | 76 | lcd.write(p[4]); |
Sim | 0:304c871df0a8 | 77 | lcd.write(p[5]); |
Sim | 0:304c871df0a8 | 78 | lcd.write(p[6]); |
Sim | 0:304c871df0a8 | 79 | lcd.write(p[7]); |
Sim | 0:304c871df0a8 | 80 | csrx += 2; |
Sim | 0:304c871df0a8 | 81 | if(csrx == 32) newline(); |
Sim | 0:304c871df0a8 | 82 | } |
Sim | 0:304c871df0a8 | 83 | |
Sim | 0:304c871df0a8 | 84 | void drawc(unsigned char c){ |
Sim | 0:304c871df0a8 | 85 | if(kstate){ // 2nd byte of shift-jis |
Sim | 0:304c871df0a8 | 86 | kstate = false; |
Sim | 0:304c871df0a8 | 87 | drawkanji(kbuf << 8 | c); |
Sim | 0:304c871df0a8 | 88 | } else if((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xfc)){ // 1st byte of shift-jis |
Sim | 0:304c871df0a8 | 89 | kstate = true; |
Sim | 0:304c871df0a8 | 90 | kbuf = c; |
Sim | 0:304c871df0a8 | 91 | } else { // 4x8font |
Sim | 0:304c871df0a8 | 92 | drawfont4(c); |
Sim | 0:304c871df0a8 | 93 | } |
Sim | 0:304c871df0a8 | 94 | } |
Sim | 0:304c871df0a8 | 95 | |
Sim | 0:304c871df0a8 | 96 | void draws(const unsigned char *s){ |
Sim | 0:304c871df0a8 | 97 | unsigned char c; |
Sim | 0:304c871df0a8 | 98 | while((c = *s++) != '\0') drawc(c); |
Sim | 0:304c871df0a8 | 99 | } |
Sim | 0:304c871df0a8 | 100 | |
Sim | 0:304c871df0a8 | 101 | Serial sio(USBTX, USBRX); |
Sim | 0:304c871df0a8 | 102 | |
Sim | 0:304c871df0a8 | 103 | int main() { |
Sim | 0:304c871df0a8 | 104 | unsigned char c; |
Sim | 0:304c871df0a8 | 105 | |
Sim | 0:304c871df0a8 | 106 | sio.baud(115200); |
Sim | 0:304c871df0a8 | 107 | while(1){ |
Sim | 0:304c871df0a8 | 108 | c = sio.getc(); |
Sim | 0:304c871df0a8 | 109 | if(c == 0x0d){ |
Sim | 0:304c871df0a8 | 110 | newline(); |
Sim | 0:304c871df0a8 | 111 | } else { |
Sim | 0:304c871df0a8 | 112 | drawc(c); |
Sim | 0:304c871df0a8 | 113 | } |
Sim | 0:304c871df0a8 | 114 | } |
Sim | 0:304c871df0a8 | 115 | } |