David Buck
/
lcdtft_only
main.cpp@0:0751be719bbf, 2009-12-16 (annotated)
- Committer:
- Kaikestu
- Date:
- Wed Dec 16 10:30:10 2009 +0000
- Revision:
- 0:0751be719bbf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kaikestu | 0:0751be719bbf | 1 | #include "mbed.h" |
Kaikestu | 0:0751be719bbf | 2 | |
Kaikestu | 0:0751be719bbf | 3 | #define BLACK 0x0000 |
Kaikestu | 0:0751be719bbf | 4 | #define NAVY 0x000F |
Kaikestu | 0:0751be719bbf | 5 | #define DARK_GREEN 0x03E0 |
Kaikestu | 0:0751be719bbf | 6 | #define DARK_CYAN 0x03EF |
Kaikestu | 0:0751be719bbf | 7 | #define MAROON 0x7800 |
Kaikestu | 0:0751be719bbf | 8 | #define PURPLE 0x780F |
Kaikestu | 0:0751be719bbf | 9 | #define OLIVE 0x7BE0 |
Kaikestu | 0:0751be719bbf | 10 | #define LIGHT_GRAY 0xC618 |
Kaikestu | 0:0751be719bbf | 11 | #define DARK_GRAY 0x7BEF |
Kaikestu | 0:0751be719bbf | 12 | #define BLUE 0x001F |
Kaikestu | 0:0751be719bbf | 13 | #define GREEN 0x07E0 |
Kaikestu | 0:0751be719bbf | 14 | #define CYAN 0x07FF |
Kaikestu | 0:0751be719bbf | 15 | #define RED 0xF800 |
Kaikestu | 0:0751be719bbf | 16 | #define MAGENTA 0xF81F |
Kaikestu | 0:0751be719bbf | 17 | #define YELLOW 0xFFE0 |
Kaikestu | 0:0751be719bbf | 18 | #define WHITE 0xFFFF |
Kaikestu | 0:0751be719bbf | 19 | |
Kaikestu | 0:0751be719bbf | 20 | |
Kaikestu | 0:0751be719bbf | 21 | |
Kaikestu | 0:0751be719bbf | 22 | SPI spi (5,6,7); |
Kaikestu | 0:0751be719bbf | 23 | DigitalOut cs (13); |
Kaikestu | 0:0751be719bbf | 24 | DigitalOut rst (17); |
Kaikestu | 0:0751be719bbf | 25 | DigitalOut tcs (23); |
Kaikestu | 0:0751be719bbf | 26 | |
Kaikestu | 0:0751be719bbf | 27 | |
Kaikestu | 0:0751be719bbf | 28 | |
Kaikestu | 0:0751be719bbf | 29 | int _hwidth = 320; |
Kaikestu | 0:0751be719bbf | 30 | int _hheight = 240; |
Kaikestu | 0:0751be719bbf | 31 | |
Kaikestu | 0:0751be719bbf | 32 | void command(int value); |
Kaikestu | 0:0751be719bbf | 33 | void data(int value); |
Kaikestu | 0:0751be719bbf | 34 | void config(int index, int value); |
Kaikestu | 0:0751be719bbf | 35 | void lcd_movePen(int x, int y); |
Kaikestu | 0:0751be719bbf | 36 | void lcd_fillScreen(int color); |
Kaikestu | 0:0751be719bbf | 37 | |
Kaikestu | 0:0751be719bbf | 38 | void lcd_point(int x, int y, int color); |
Kaikestu | 0:0751be719bbf | 39 | void lcd_circle(int x0, int y0, int r, int color); |
Kaikestu | 0:0751be719bbf | 40 | |
Kaikestu | 0:0751be719bbf | 41 | int main() { |
Kaikestu | 0:0751be719bbf | 42 | |
Kaikestu | 0:0751be719bbf | 43 | spi.frequency(10000000); |
Kaikestu | 0:0751be719bbf | 44 | spi.format(9); |
Kaikestu | 0:0751be719bbf | 45 | |
Kaikestu | 0:0751be719bbf | 46 | |
Kaikestu | 0:0751be719bbf | 47 | cs = 0; |
Kaikestu | 0:0751be719bbf | 48 | |
Kaikestu | 0:0751be719bbf | 49 | rst = 0; |
Kaikestu | 0:0751be719bbf | 50 | wait(0.001); |
Kaikestu | 0:0751be719bbf | 51 | rst = 1; |
Kaikestu | 0:0751be719bbf | 52 | wait(0.001); |
Kaikestu | 0:0751be719bbf | 53 | |
Kaikestu | 0:0751be719bbf | 54 | |
Kaikestu | 0:0751be719bbf | 55 | |
Kaikestu | 0:0751be719bbf | 56 | config(0x07 |
Kaikestu | 0:0751be719bbf | 57 | , 1 << 5 // GON |
Kaikestu | 0:0751be719bbf | 58 | | 0 << 4 // DTE |
Kaikestu | 0:0751be719bbf | 59 | | 0 << 3 // CM |
Kaikestu | 0:0751be719bbf | 60 | | 1 << 0 // D[1:0] = 01 - operate, but disp off |
Kaikestu | 0:0751be719bbf | 61 | ); |
Kaikestu | 0:0751be719bbf | 62 | |
Kaikestu | 0:0751be719bbf | 63 | config(0x00, 0001); |
Kaikestu | 0:0751be719bbf | 64 | |
Kaikestu | 0:0751be719bbf | 65 | config(0x07, 1 << 5 | 1 << 4 | 0 << 3 | 3 << 0 ); |
Kaikestu | 0:0751be719bbf | 66 | |
Kaikestu | 0:0751be719bbf | 67 | config(0x10, 0000); |
Kaikestu | 0:0751be719bbf | 68 | |
Kaikestu | 0:0751be719bbf | 69 | wait(0.030); |
Kaikestu | 0:0751be719bbf | 70 | |
Kaikestu | 0:0751be719bbf | 71 | config(0x02, 0x0600); |
Kaikestu | 0:0751be719bbf | 72 | config(0x01, 0x2b3f); // 1011 |
Kaikestu | 0:0751be719bbf | 73 | config(0x25, 0xa000); // 70Hz freq |
Kaikestu | 0:0751be719bbf | 74 | |
Kaikestu | 0:0751be719bbf | 75 | |
Kaikestu | 0:0751be719bbf | 76 | |
Kaikestu | 0:0751be719bbf | 77 | |
Kaikestu | 0:0751be719bbf | 78 | |
Kaikestu | 0:0751be719bbf | 79 | while (1) { |
Kaikestu | 0:0751be719bbf | 80 | |
Kaikestu | 0:0751be719bbf | 81 | lcd_fillScreen(WHITE); |
Kaikestu | 0:0751be719bbf | 82 | |
Kaikestu | 0:0751be719bbf | 83 | lcd_fillScreen(BLUE); |
Kaikestu | 0:0751be719bbf | 84 | lcd_fillScreen(GREEN); |
Kaikestu | 0:0751be719bbf | 85 | |
Kaikestu | 0:0751be719bbf | 86 | } |
Kaikestu | 0:0751be719bbf | 87 | } |
Kaikestu | 0:0751be719bbf | 88 | |
Kaikestu | 0:0751be719bbf | 89 | |
Kaikestu | 0:0751be719bbf | 90 | void command(int value) { |
Kaikestu | 0:0751be719bbf | 91 | spi.write(value & 0xFF); |
Kaikestu | 0:0751be719bbf | 92 | } |
Kaikestu | 0:0751be719bbf | 93 | |
Kaikestu | 0:0751be719bbf | 94 | void data(int value) { |
Kaikestu | 0:0751be719bbf | 95 | spi.write(value | 0x100); |
Kaikestu | 0:0751be719bbf | 96 | } |
Kaikestu | 0:0751be719bbf | 97 | |
Kaikestu | 0:0751be719bbf | 98 | void config(int index, int value) { |
Kaikestu | 0:0751be719bbf | 99 | command(0); |
Kaikestu | 0:0751be719bbf | 100 | command(index); |
Kaikestu | 0:0751be719bbf | 101 | data(value >> 8); |
Kaikestu | 0:0751be719bbf | 102 | data(value); |
Kaikestu | 0:0751be719bbf | 103 | } |
Kaikestu | 0:0751be719bbf | 104 | |
Kaikestu | 0:0751be719bbf | 105 | |
Kaikestu | 0:0751be719bbf | 106 | void lcd_movePen(int x, int y) { |
Kaikestu | 0:0751be719bbf | 107 | config(0x4e, x & 0x00ff); |
Kaikestu | 0:0751be719bbf | 108 | config(0x4f, y & 0x01ff); |
Kaikestu | 0:0751be719bbf | 109 | command(0); |
Kaikestu | 0:0751be719bbf | 110 | command(0x22); |
Kaikestu | 0:0751be719bbf | 111 | } |
Kaikestu | 0:0751be719bbf | 112 | |
Kaikestu | 0:0751be719bbf | 113 | void lcd_fillScreen(int color) { |
Kaikestu | 0:0751be719bbf | 114 | int i = 0; |
Kaikestu | 0:0751be719bbf | 115 | int j = 0; |
Kaikestu | 0:0751be719bbf | 116 | |
Kaikestu | 0:0751be719bbf | 117 | lcd_movePen(0, 0); |
Kaikestu | 0:0751be719bbf | 118 | |
Kaikestu | 0:0751be719bbf | 119 | for (i=0; i < _hwidth; i++) { |
Kaikestu | 0:0751be719bbf | 120 | for (j=0; j< _hheight; j++) { |
Kaikestu | 0:0751be719bbf | 121 | data(color >> 8); |
Kaikestu | 0:0751be719bbf | 122 | data(color); |
Kaikestu | 0:0751be719bbf | 123 | } |
Kaikestu | 0:0751be719bbf | 124 | } |
Kaikestu | 0:0751be719bbf | 125 | } |
Kaikestu | 0:0751be719bbf | 126 | void lcd_point(int x, int y, int color) { |
Kaikestu | 0:0751be719bbf | 127 | data(color >> 8); |
Kaikestu | 0:0751be719bbf | 128 | data(color); |
Kaikestu | 0:0751be719bbf | 129 | |
Kaikestu | 0:0751be719bbf | 130 | } |
Kaikestu | 0:0751be719bbf | 131 | |
Kaikestu | 0:0751be719bbf | 132 | void lcd_circle(int x0, int y0, int r, int color) { |
Kaikestu | 0:0751be719bbf | 133 | int draw_x0, draw_y0; |
Kaikestu | 0:0751be719bbf | 134 | int draw_x1, draw_y1; |
Kaikestu | 0:0751be719bbf | 135 | int draw_x2, draw_y2; |
Kaikestu | 0:0751be719bbf | 136 | int draw_x3, draw_y3; |
Kaikestu | 0:0751be719bbf | 137 | int draw_x4, draw_y4; |
Kaikestu | 0:0751be719bbf | 138 | int draw_x5, draw_y5; |
Kaikestu | 0:0751be719bbf | 139 | int draw_x6, draw_y6; |
Kaikestu | 0:0751be719bbf | 140 | int draw_x7, draw_y7; |
Kaikestu | 0:0751be719bbf | 141 | int xx, yy; |
Kaikestu | 0:0751be719bbf | 142 | int di; |
Kaikestu | 0:0751be719bbf | 143 | |
Kaikestu | 0:0751be719bbf | 144 | |
Kaikestu | 0:0751be719bbf | 145 | draw_x0 = draw_x1 = x0; |
Kaikestu | 0:0751be719bbf | 146 | draw_y0 = draw_y1 = y0 + r; |
Kaikestu | 0:0751be719bbf | 147 | if (draw_y0 < _hheight) { |
Kaikestu | 0:0751be719bbf | 148 | lcd_point(draw_x0, draw_y0, color); /* 90 degree */ |
Kaikestu | 0:0751be719bbf | 149 | } |
Kaikestu | 0:0751be719bbf | 150 | |
Kaikestu | 0:0751be719bbf | 151 | draw_x2 = draw_x3 = x0; |
Kaikestu | 0:0751be719bbf | 152 | draw_y2 = draw_y3 = y0 - r; |
Kaikestu | 0:0751be719bbf | 153 | if (draw_y2 >= 0) { |
Kaikestu | 0:0751be719bbf | 154 | lcd_point(draw_x2, draw_y2, color); /* 270 degree */ |
Kaikestu | 0:0751be719bbf | 155 | } |
Kaikestu | 0:0751be719bbf | 156 | |
Kaikestu | 0:0751be719bbf | 157 | draw_x4 = draw_x6 = x0 + r; |
Kaikestu | 0:0751be719bbf | 158 | draw_y4 = draw_y6 = y0; |
Kaikestu | 0:0751be719bbf | 159 | if (draw_x4 < _hwidth) { |
Kaikestu | 0:0751be719bbf | 160 | lcd_point(draw_x4, draw_y4, color); /* 0 degree */ |
Kaikestu | 0:0751be719bbf | 161 | } |
Kaikestu | 0:0751be719bbf | 162 | |
Kaikestu | 0:0751be719bbf | 163 | draw_x5 = draw_x7 = x0 - r; |
Kaikestu | 0:0751be719bbf | 164 | draw_y5 = draw_y7 = y0; |
Kaikestu | 0:0751be719bbf | 165 | if (draw_x5>=0) { |
Kaikestu | 0:0751be719bbf | 166 | lcd_point(draw_x5, draw_y5, color); /* 180 degree */ |
Kaikestu | 0:0751be719bbf | 167 | } |
Kaikestu | 0:0751be719bbf | 168 | |
Kaikestu | 0:0751be719bbf | 169 | if (r == 1) { |
Kaikestu | 0:0751be719bbf | 170 | return; |
Kaikestu | 0:0751be719bbf | 171 | } |
Kaikestu | 0:0751be719bbf | 172 | |
Kaikestu | 0:0751be719bbf | 173 | di = 3 - 2*r; |
Kaikestu | 0:0751be719bbf | 174 | xx = 0; |
Kaikestu | 0:0751be719bbf | 175 | yy = r; |
Kaikestu | 0:0751be719bbf | 176 | while (xx < yy) { |
Kaikestu | 0:0751be719bbf | 177 | if (di < 0) { |
Kaikestu | 0:0751be719bbf | 178 | di += 4*xx + 6; |
Kaikestu | 0:0751be719bbf | 179 | } else { |
Kaikestu | 0:0751be719bbf | 180 | di += 4*(xx - yy) + 10; |
Kaikestu | 0:0751be719bbf | 181 | yy--; |
Kaikestu | 0:0751be719bbf | 182 | draw_y0--; |
Kaikestu | 0:0751be719bbf | 183 | draw_y1--; |
Kaikestu | 0:0751be719bbf | 184 | draw_y2++; |
Kaikestu | 0:0751be719bbf | 185 | draw_y3++; |
Kaikestu | 0:0751be719bbf | 186 | draw_x4--; |
Kaikestu | 0:0751be719bbf | 187 | draw_x5++; |
Kaikestu | 0:0751be719bbf | 188 | draw_x6--; |
Kaikestu | 0:0751be719bbf | 189 | draw_x7++; |
Kaikestu | 0:0751be719bbf | 190 | } |
Kaikestu | 0:0751be719bbf | 191 | xx++; |
Kaikestu | 0:0751be719bbf | 192 | draw_x0++; |
Kaikestu | 0:0751be719bbf | 193 | draw_x1--; |
Kaikestu | 0:0751be719bbf | 194 | draw_x2++; |
Kaikestu | 0:0751be719bbf | 195 | draw_x3--; |
Kaikestu | 0:0751be719bbf | 196 | draw_y4++; |
Kaikestu | 0:0751be719bbf | 197 | draw_y5++; |
Kaikestu | 0:0751be719bbf | 198 | draw_y6--; |
Kaikestu | 0:0751be719bbf | 199 | draw_y7--; |
Kaikestu | 0:0751be719bbf | 200 | |
Kaikestu | 0:0751be719bbf | 201 | if ( (draw_x0 <= _hwidth) && (draw_y0>=0) ) { |
Kaikestu | 0:0751be719bbf | 202 | lcd_point(draw_x0, draw_y0, color); |
Kaikestu | 0:0751be719bbf | 203 | } |
Kaikestu | 0:0751be719bbf | 204 | |
Kaikestu | 0:0751be719bbf | 205 | if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) { |
Kaikestu | 0:0751be719bbf | 206 | lcd_point(draw_x1, draw_y1, color); |
Kaikestu | 0:0751be719bbf | 207 | } |
Kaikestu | 0:0751be719bbf | 208 | |
Kaikestu | 0:0751be719bbf | 209 | if ( (draw_x2 <= _hwidth) && (draw_y2 <= _hheight) ) { |
Kaikestu | 0:0751be719bbf | 210 | lcd_point(draw_x2, draw_y2, color); |
Kaikestu | 0:0751be719bbf | 211 | } |
Kaikestu | 0:0751be719bbf | 212 | |
Kaikestu | 0:0751be719bbf | 213 | if ( (draw_x3 >=0 ) && (draw_y3 <= _hheight) ) { |
Kaikestu | 0:0751be719bbf | 214 | lcd_point(draw_x3, draw_y3, color); |
Kaikestu | 0:0751be719bbf | 215 | } |
Kaikestu | 0:0751be719bbf | 216 | |
Kaikestu | 0:0751be719bbf | 217 | if ( (draw_x4 <= _hheight) && (draw_y4 >= 0) ) { |
Kaikestu | 0:0751be719bbf | 218 | lcd_point(draw_x4, draw_y4, color); |
Kaikestu | 0:0751be719bbf | 219 | } |
Kaikestu | 0:0751be719bbf | 220 | |
Kaikestu | 0:0751be719bbf | 221 | if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) { |
Kaikestu | 0:0751be719bbf | 222 | lcd_point(draw_x5, draw_y5, color); |
Kaikestu | 0:0751be719bbf | 223 | } |
Kaikestu | 0:0751be719bbf | 224 | if ( (draw_x6 <= _hwidth) && (draw_y6 <= _hheight) ) { |
Kaikestu | 0:0751be719bbf | 225 | lcd_point(draw_x6, draw_y6, color); |
Kaikestu | 0:0751be719bbf | 226 | } |
Kaikestu | 0:0751be719bbf | 227 | if ( (draw_x7 >= 0) && (draw_y7 <= _hheight) ) { |
Kaikestu | 0:0751be719bbf | 228 | lcd_point(draw_x7, draw_y7, color); |
Kaikestu | 0:0751be719bbf | 229 | } |
Kaikestu | 0:0751be719bbf | 230 | } |
Kaikestu | 0:0751be719bbf | 231 | } |
Kaikestu | 0:0751be719bbf | 232 | |
Kaikestu | 0:0751be719bbf | 233 | |
Kaikestu | 0:0751be719bbf | 234 |