nuchoさんのNokiaライブラリ( NokiaLCD_With_JapaneseFont ) の NokiaLCD.cpp _putp関数をカスタマイズしました
Dependents: NokiaLCD spxml_WeatherLCD
Fork of NokiaLCD_With_JapaneseFont by
NokiaLCD.cpp@0:839ab88da656, 2010-10-19 (annotated)
- Committer:
- nucho
- Date:
- Tue Oct 19 23:49:09 2010 +0000
- Revision:
- 0:839ab88da656
- Child:
- 1:c505894797b3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nucho | 0:839ab88da656 | 1 | /* mbed Nokia LCD Library |
nucho | 0:839ab88da656 | 2 | * Copyright (c) 2007-2010, sford |
nucho | 0:839ab88da656 | 3 | */ |
nucho | 0:839ab88da656 | 4 | |
nucho | 0:839ab88da656 | 5 | #include "NokiaLCD.h" |
nucho | 0:839ab88da656 | 6 | #include "small_font.h" |
nucho | 0:839ab88da656 | 7 | #include "mbed.h" |
nucho | 0:839ab88da656 | 8 | |
nucho | 0:839ab88da656 | 9 | #define NOKIALCD_ROWS 16 |
nucho | 0:839ab88da656 | 10 | #define NOKIALCD_COLS 16 |
nucho | 0:839ab88da656 | 11 | #define NOKIALCD_WIDTH 130 |
nucho | 0:839ab88da656 | 12 | #define NOKIALCD_HEIGHT 130 |
nucho | 0:839ab88da656 | 13 | #define NOKIALCD_FREQUENCY 5000000 |
nucho | 0:839ab88da656 | 14 | |
nucho | 0:839ab88da656 | 15 | #define countof(x) ( sizeof(x) / sizeof(x[0]) ) |
nucho | 0:839ab88da656 | 16 | |
nucho | 0:839ab88da656 | 17 | bool kstate = false; |
nucho | 0:839ab88da656 | 18 | unsigned int kbuf; |
nucho | 0:839ab88da656 | 19 | |
nucho | 0:839ab88da656 | 20 | NokiaLCD::NokiaLCD(PinName mosi, PinName sclk, PinName cs, PinName rst, LCDType type) |
nucho | 0:839ab88da656 | 21 | : _spi(mosi, NC, sclk) |
nucho | 0:839ab88da656 | 22 | , _rst(rst) |
nucho | 0:839ab88da656 | 23 | , _cs(cs) { |
nucho | 0:839ab88da656 | 24 | |
nucho | 0:839ab88da656 | 25 | _type = type; |
nucho | 0:839ab88da656 | 26 | |
nucho | 0:839ab88da656 | 27 | _row = 0; |
nucho | 0:839ab88da656 | 28 | _column = 0; |
nucho | 0:839ab88da656 | 29 | _foreground = 0x00FFFFFF; |
nucho | 0:839ab88da656 | 30 | _background = 0x00000000; |
nucho | 0:839ab88da656 | 31 | |
nucho | 0:839ab88da656 | 32 | reset(); |
nucho | 0:839ab88da656 | 33 | } |
nucho | 0:839ab88da656 | 34 | |
nucho | 0:839ab88da656 | 35 | void NokiaLCD::reset() { |
nucho | 0:839ab88da656 | 36 | |
nucho | 0:839ab88da656 | 37 | // setup the SPI interface and bring display out of reset |
nucho | 0:839ab88da656 | 38 | _cs = 1; |
nucho | 0:839ab88da656 | 39 | _rst = 0; |
nucho | 0:839ab88da656 | 40 | _spi.format(9); |
nucho | 0:839ab88da656 | 41 | _spi.frequency(NOKIALCD_FREQUENCY); |
nucho | 0:839ab88da656 | 42 | wait_ms(1); |
nucho | 0:839ab88da656 | 43 | _rst = 1; |
nucho | 0:839ab88da656 | 44 | wait_ms(1); |
nucho | 0:839ab88da656 | 45 | |
nucho | 0:839ab88da656 | 46 | _cs = 0; |
nucho | 0:839ab88da656 | 47 | |
nucho | 0:839ab88da656 | 48 | switch (_type) { |
nucho | 0:839ab88da656 | 49 | case LCD6100: |
nucho | 0:839ab88da656 | 50 | command(0xCA); // display control |
nucho | 0:839ab88da656 | 51 | data(0); |
nucho | 0:839ab88da656 | 52 | data(32); |
nucho | 0:839ab88da656 | 53 | data(0); |
nucho | 0:839ab88da656 | 54 | command(0xBB); |
nucho | 0:839ab88da656 | 55 | data(1); |
nucho | 0:839ab88da656 | 56 | command(0xD1); // oscillator on |
nucho | 0:839ab88da656 | 57 | command(0x94); // sleep out |
nucho | 0:839ab88da656 | 58 | command(0x20); // power control |
nucho | 0:839ab88da656 | 59 | data(0x0F); |
nucho | 0:839ab88da656 | 60 | command(0xA7); // invert display |
nucho | 0:839ab88da656 | 61 | command(0x81); // Voltage control |
nucho | 0:839ab88da656 | 62 | data(39); // contrast setting: 0..63 |
nucho | 0:839ab88da656 | 63 | data(3); // resistance ratio |
nucho | 0:839ab88da656 | 64 | wait_ms(1); |
nucho | 0:839ab88da656 | 65 | command(0xBC); |
nucho | 0:839ab88da656 | 66 | data(0); |
nucho | 0:839ab88da656 | 67 | data(1); |
nucho | 0:839ab88da656 | 68 | data(4); |
nucho | 0:839ab88da656 | 69 | command(0xAF); // turn on the display |
nucho | 0:839ab88da656 | 70 | break; |
nucho | 0:839ab88da656 | 71 | |
nucho | 0:839ab88da656 | 72 | case LCD6610: |
nucho | 0:839ab88da656 | 73 | command(0xCA); // display control |
nucho | 0:839ab88da656 | 74 | data(0); |
nucho | 0:839ab88da656 | 75 | data(31); |
nucho | 0:839ab88da656 | 76 | data(0); |
nucho | 0:839ab88da656 | 77 | command(0xBB); |
nucho | 0:839ab88da656 | 78 | data(1); |
nucho | 0:839ab88da656 | 79 | command(0xD1); // oscillator on |
nucho | 0:839ab88da656 | 80 | command(0x94); // sleep out |
nucho | 0:839ab88da656 | 81 | command(0x20); // power control |
nucho | 0:839ab88da656 | 82 | data(0x0F); |
nucho | 0:839ab88da656 | 83 | command(0xA7); // invert display |
nucho | 0:839ab88da656 | 84 | command(0x81); // Voltage control |
nucho | 0:839ab88da656 | 85 | data(39); // contrast setting: 0..63 |
nucho | 0:839ab88da656 | 86 | data(3); // resistance ratio |
nucho | 0:839ab88da656 | 87 | wait_ms(1); |
nucho | 0:839ab88da656 | 88 | command(0xBC); |
nucho | 0:839ab88da656 | 89 | data(0); |
nucho | 0:839ab88da656 | 90 | data(0); |
nucho | 0:839ab88da656 | 91 | data(2); |
nucho | 0:839ab88da656 | 92 | command(0xAF); // turn on the display |
nucho | 0:839ab88da656 | 93 | break; |
nucho | 0:839ab88da656 | 94 | |
nucho | 0:839ab88da656 | 95 | case LCD3300: |
nucho | 0:839ab88da656 | 96 | command(0xCA); // display control |
nucho | 0:839ab88da656 | 97 | data(0); |
nucho | 0:839ab88da656 | 98 | data(32); |
nucho | 0:839ab88da656 | 99 | data(0); |
nucho | 0:839ab88da656 | 100 | command(0xBB); |
nucho | 0:839ab88da656 | 101 | data(1); |
nucho | 0:839ab88da656 | 102 | command(0xD1); // oscillator on |
nucho | 0:839ab88da656 | 103 | command(0x94); // sleep out |
nucho | 0:839ab88da656 | 104 | command(0x20); // power control |
nucho | 0:839ab88da656 | 105 | data(0x0F); |
nucho | 0:839ab88da656 | 106 | command(0xA7); // invert display |
nucho | 0:839ab88da656 | 107 | command(0x81); // Voltage control |
nucho | 0:839ab88da656 | 108 | data(39); // contrast setting: 0..63 |
nucho | 0:839ab88da656 | 109 | data(3); // resistance ratio |
nucho | 0:839ab88da656 | 110 | wait_ms(1); |
nucho | 0:839ab88da656 | 111 | command(0xBC); |
nucho | 0:839ab88da656 | 112 | data(1); |
nucho | 0:839ab88da656 | 113 | data(0); |
nucho | 0:839ab88da656 | 114 | data(4); |
nucho | 0:839ab88da656 | 115 | command(0xAF); // turn on the display |
nucho | 0:839ab88da656 | 116 | break; |
nucho | 0:839ab88da656 | 117 | |
nucho | 0:839ab88da656 | 118 | case PCF8833: |
nucho | 0:839ab88da656 | 119 | command(0x11); // sleep out |
nucho | 0:839ab88da656 | 120 | command(0x3A); // column mode |
nucho | 0:839ab88da656 | 121 | data(0x05); |
nucho | 0:839ab88da656 | 122 | command(0x36); // madctl |
nucho | 0:839ab88da656 | 123 | data(0x60); // vertical RAM, flip x |
nucho | 0:839ab88da656 | 124 | command(0x25); // setcon |
nucho | 0:839ab88da656 | 125 | data(0x30);// contrast 0x30 |
nucho | 0:839ab88da656 | 126 | wait_ms(2); |
nucho | 0:839ab88da656 | 127 | command(0x29);//DISPON |
nucho | 0:839ab88da656 | 128 | command(0x03);//BSTRON |
nucho | 0:839ab88da656 | 129 | break; |
nucho | 0:839ab88da656 | 130 | } |
nucho | 0:839ab88da656 | 131 | |
nucho | 0:839ab88da656 | 132 | _cs = 1; |
nucho | 0:839ab88da656 | 133 | |
nucho | 0:839ab88da656 | 134 | cls(); |
nucho | 0:839ab88da656 | 135 | } |
nucho | 0:839ab88da656 | 136 | |
nucho | 0:839ab88da656 | 137 | void NokiaLCD::command(int value) { |
nucho | 0:839ab88da656 | 138 | _spi.write(value & 0xFF); |
nucho | 0:839ab88da656 | 139 | } |
nucho | 0:839ab88da656 | 140 | |
nucho | 0:839ab88da656 | 141 | void NokiaLCD::data(int value) { |
nucho | 0:839ab88da656 | 142 | _spi.write(value | 0x100); |
nucho | 0:839ab88da656 | 143 | } |
nucho | 0:839ab88da656 | 144 | |
nucho | 0:839ab88da656 | 145 | void NokiaLCD::_window(int x, int y, int width, int height) { |
nucho | 0:839ab88da656 | 146 | int x1 = x + 2; |
nucho | 0:839ab88da656 | 147 | int y1 = y + 0; |
nucho | 0:839ab88da656 | 148 | int x2 = x1 + width - 1; |
nucho | 0:839ab88da656 | 149 | int y2 = y1 + height - 1; |
nucho | 0:839ab88da656 | 150 | |
nucho | 0:839ab88da656 | 151 | switch (_type) { |
nucho | 0:839ab88da656 | 152 | case LCD6100: |
nucho | 0:839ab88da656 | 153 | case LCD6610: |
nucho | 0:839ab88da656 | 154 | case LCD3300: |
nucho | 0:839ab88da656 | 155 | command(0x15); // column |
nucho | 0:839ab88da656 | 156 | data(x1); |
nucho | 0:839ab88da656 | 157 | data(x2); |
nucho | 0:839ab88da656 | 158 | command(0x75); // row |
nucho | 0:839ab88da656 | 159 | data(y1); |
nucho | 0:839ab88da656 | 160 | data(y2); |
nucho | 0:839ab88da656 | 161 | command(0x5C); // start write to ram |
nucho | 0:839ab88da656 | 162 | break; |
nucho | 0:839ab88da656 | 163 | case PCF8833: |
nucho | 0:839ab88da656 | 164 | command(0x2A); // column |
nucho | 0:839ab88da656 | 165 | data(x1); |
nucho | 0:839ab88da656 | 166 | data(x2); |
nucho | 0:839ab88da656 | 167 | command(0x2B); // row |
nucho | 0:839ab88da656 | 168 | data(y1); |
nucho | 0:839ab88da656 | 169 | data(y2); |
nucho | 0:839ab88da656 | 170 | command(0x2C); // start write to ram |
nucho | 0:839ab88da656 | 171 | break; |
nucho | 0:839ab88da656 | 172 | } |
nucho | 0:839ab88da656 | 173 | } |
nucho | 0:839ab88da656 | 174 | |
nucho | 0:839ab88da656 | 175 | void NokiaLCD::_putp(int colour) { |
nucho | 0:839ab88da656 | 176 | int gr = ((colour >> 20) & 0x0F) |
nucho | 0:839ab88da656 | 177 | | ((colour >> 8 ) & 0xF0); |
nucho | 0:839ab88da656 | 178 | int nb = ((colour >> 4 ) & 0x0F); |
nucho | 0:839ab88da656 | 179 | data(nb); |
nucho | 0:839ab88da656 | 180 | data(gr); |
nucho | 0:839ab88da656 | 181 | } |
nucho | 0:839ab88da656 | 182 | |
nucho | 0:839ab88da656 | 183 | |
nucho | 0:839ab88da656 | 184 | void NokiaLCD::locate(int column, int row) { |
nucho | 0:839ab88da656 | 185 | _column = column; |
nucho | 0:839ab88da656 | 186 | _row = row; |
nucho | 0:839ab88da656 | 187 | } |
nucho | 0:839ab88da656 | 188 | |
nucho | 0:839ab88da656 | 189 | void NokiaLCD::newline() { |
nucho | 0:839ab88da656 | 190 | _column = 0; |
nucho | 0:839ab88da656 | 191 | _row++; |
nucho | 0:839ab88da656 | 192 | if (_row >= NOKIALCD_ROWS) { |
nucho | 0:839ab88da656 | 193 | _row = 0; |
nucho | 0:839ab88da656 | 194 | } |
nucho | 0:839ab88da656 | 195 | } |
nucho | 0:839ab88da656 | 196 | |
nucho | 0:839ab88da656 | 197 | unsigned int NokiaLCD::findface(unsigned short c) { |
nucho | 0:839ab88da656 | 198 | unsigned int p = 0; |
nucho | 0:839ab88da656 | 199 | int i, sum; |
nucho | 0:839ab88da656 | 200 | for (sum = i = 0; i < countof(font8table); i++) { |
nucho | 0:839ab88da656 | 201 | if (font8table[i].start <= c && c <= font8table[i].end) { |
nucho | 0:839ab88da656 | 202 | p = (sum + c - font8table[i].start); |
nucho | 0:839ab88da656 | 203 | break; |
nucho | 0:839ab88da656 | 204 | } |
nucho | 0:839ab88da656 | 205 | sum += font8table[i].end - font8table[i].start + 1; |
nucho | 0:839ab88da656 | 206 | } |
nucho | 0:839ab88da656 | 207 | return p; |
nucho | 0:839ab88da656 | 208 | } |
nucho | 0:839ab88da656 | 209 | |
nucho | 0:839ab88da656 | 210 | int NokiaLCD::_putc(int value) { |
nucho | 0:839ab88da656 | 211 | int x = _column * 8; // FIXME: Char sizes |
nucho | 0:839ab88da656 | 212 | int y = _row * 8; |
nucho | 0:839ab88da656 | 213 | |
nucho | 0:839ab88da656 | 214 | if(value == '\n') { |
nucho | 0:839ab88da656 | 215 | newline(); |
nucho | 0:839ab88da656 | 216 | } else if (kstate) { // 2nd byte of shift-jis |
nucho | 0:839ab88da656 | 217 | kstate = false; |
nucho | 0:839ab88da656 | 218 | int p = findface(kbuf << 8 | value); |
nucho | 0:839ab88da656 | 219 | bitblit(x + 1, y + 1, 8,8, (char*)&(FontLookup[p][0])); |
nucho | 0:839ab88da656 | 220 | |
nucho | 0:839ab88da656 | 221 | //printf("%x %x",( kbuf << 8 | value),p); //for debug |
nucho | 0:839ab88da656 | 222 | _column++; |
nucho | 0:839ab88da656 | 223 | if (_column >= NOKIALCD_COLS) { |
nucho | 0:839ab88da656 | 224 | _row++; |
nucho | 0:839ab88da656 | 225 | _column = 0; |
nucho | 0:839ab88da656 | 226 | } |
nucho | 0:839ab88da656 | 227 | if (_row >= NOKIALCD_ROWS) { |
nucho | 0:839ab88da656 | 228 | _row = 0; |
nucho | 0:839ab88da656 | 229 | } |
nucho | 0:839ab88da656 | 230 | } else if ((0x81 <= value && value <= 0x9f) || (0xe0 <= value && value <= 0xfc)) { // 1st byte of shift-jis |
nucho | 0:839ab88da656 | 231 | kstate = true; |
nucho | 0:839ab88da656 | 232 | kbuf = value; |
nucho | 0:839ab88da656 | 233 | } else { |
nucho | 0:839ab88da656 | 234 | bitblit(x + 1, y + 1, 8, 8, (char*)&(FontLookup_ABC[value-32][0])); |
nucho | 0:839ab88da656 | 235 | _column++; |
nucho | 0:839ab88da656 | 236 | if (_column >= NOKIALCD_COLS) { |
nucho | 0:839ab88da656 | 237 | _row++; |
nucho | 0:839ab88da656 | 238 | _column = 0; |
nucho | 0:839ab88da656 | 239 | } |
nucho | 0:839ab88da656 | 240 | if (_row >= NOKIALCD_ROWS) { |
nucho | 0:839ab88da656 | 241 | _row = 0; |
nucho | 0:839ab88da656 | 242 | } |
nucho | 0:839ab88da656 | 243 | } |
nucho | 0:839ab88da656 | 244 | |
nucho | 0:839ab88da656 | 245 | return value; |
nucho | 0:839ab88da656 | 246 | } |
nucho | 0:839ab88da656 | 247 | |
nucho | 0:839ab88da656 | 248 | void NokiaLCD::cls() { |
nucho | 0:839ab88da656 | 249 | fill(0, 0, NOKIALCD_WIDTH, NOKIALCD_HEIGHT, _background); |
nucho | 0:839ab88da656 | 250 | _row = 0; |
nucho | 0:839ab88da656 | 251 | _column = 0; |
nucho | 0:839ab88da656 | 252 | } |
nucho | 0:839ab88da656 | 253 | |
nucho | 0:839ab88da656 | 254 | |
nucho | 0:839ab88da656 | 255 | void NokiaLCD::window(int x, int y, int width, int height) { |
nucho | 0:839ab88da656 | 256 | _cs = 0; |
nucho | 0:839ab88da656 | 257 | _window(x, y, width, height); |
nucho | 0:839ab88da656 | 258 | _cs = 1; |
nucho | 0:839ab88da656 | 259 | } |
nucho | 0:839ab88da656 | 260 | |
nucho | 0:839ab88da656 | 261 | void NokiaLCD::putp(int colour) { |
nucho | 0:839ab88da656 | 262 | _cs = 0; |
nucho | 0:839ab88da656 | 263 | _putp(colour); |
nucho | 0:839ab88da656 | 264 | _cs = 1; |
nucho | 0:839ab88da656 | 265 | } |
nucho | 0:839ab88da656 | 266 | |
nucho | 0:839ab88da656 | 267 | void NokiaLCD::pixel(int x, int y, int colour) { |
nucho | 0:839ab88da656 | 268 | _cs = 0; |
nucho | 0:839ab88da656 | 269 | _window(x, y, 1, 1); |
nucho | 0:839ab88da656 | 270 | _putp(colour); |
nucho | 0:839ab88da656 | 271 | _cs = 1; |
nucho | 0:839ab88da656 | 272 | } |
nucho | 0:839ab88da656 | 273 | |
nucho | 0:839ab88da656 | 274 | void NokiaLCD::fill(int x, int y, int width, int height, int colour) { |
nucho | 0:839ab88da656 | 275 | _cs = 0; |
nucho | 0:839ab88da656 | 276 | _window(x, y, width, height); |
nucho | 0:839ab88da656 | 277 | for (int i=0; i<width*height; i++) { |
nucho | 0:839ab88da656 | 278 | _putp(colour); |
nucho | 0:839ab88da656 | 279 | } |
nucho | 0:839ab88da656 | 280 | _window(0, 0, NOKIALCD_WIDTH, NOKIALCD_HEIGHT); |
nucho | 0:839ab88da656 | 281 | _cs = 1; |
nucho | 0:839ab88da656 | 282 | } |
nucho | 0:839ab88da656 | 283 | |
nucho | 0:839ab88da656 | 284 | void NokiaLCD::blit(int x, int y, int width, int height, const int* colour) { |
nucho | 0:839ab88da656 | 285 | _cs = 0; |
nucho | 0:839ab88da656 | 286 | _window(x, y, width, height); |
nucho | 0:839ab88da656 | 287 | for (int i=0; i<width*height; i++) { |
nucho | 0:839ab88da656 | 288 | _putp(colour[i]); |
nucho | 0:839ab88da656 | 289 | } |
nucho | 0:839ab88da656 | 290 | _window(0, 0, NOKIALCD_WIDTH, NOKIALCD_HEIGHT); |
nucho | 0:839ab88da656 | 291 | _cs = 1; |
nucho | 0:839ab88da656 | 292 | } |
nucho | 0:839ab88da656 | 293 | |
nucho | 0:839ab88da656 | 294 | void NokiaLCD::bitblit(int x, int y, int width, int height, const char* bitstream) { |
nucho | 0:839ab88da656 | 295 | _cs = 0; |
nucho | 0:839ab88da656 | 296 | _window(x, y, width, height); |
nucho | 0:839ab88da656 | 297 | for (int i=0; i<height*width; i++) { |
nucho | 0:839ab88da656 | 298 | int byte = i / 8; |
nucho | 0:839ab88da656 | 299 | int bit = i % 8; |
nucho | 0:839ab88da656 | 300 | int colour = ((bitstream[byte] << bit) & 0x80) ? _foreground : _background; |
nucho | 0:839ab88da656 | 301 | _putp(colour); |
nucho | 0:839ab88da656 | 302 | } |
nucho | 0:839ab88da656 | 303 | _window(0, 0, _width, _height); |
nucho | 0:839ab88da656 | 304 | _cs = 1; |
nucho | 0:839ab88da656 | 305 | } |
nucho | 0:839ab88da656 | 306 | |
nucho | 0:839ab88da656 | 307 | |
nucho | 0:839ab88da656 | 308 | |
nucho | 0:839ab88da656 | 309 | |
nucho | 0:839ab88da656 | 310 | void NokiaLCD::foreground(int c) { |
nucho | 0:839ab88da656 | 311 | _foreground = c; |
nucho | 0:839ab88da656 | 312 | } |
nucho | 0:839ab88da656 | 313 | |
nucho | 0:839ab88da656 | 314 | void NokiaLCD::background(int c) { |
nucho | 0:839ab88da656 | 315 | _background = c; |
nucho | 0:839ab88da656 | 316 | } |
nucho | 0:839ab88da656 | 317 | |
nucho | 0:839ab88da656 | 318 | int NokiaLCD::width() { |
nucho | 0:839ab88da656 | 319 | return NOKIALCD_WIDTH; |
nucho | 0:839ab88da656 | 320 | } |
nucho | 0:839ab88da656 | 321 | |
nucho | 0:839ab88da656 | 322 | int NokiaLCD::height() { |
nucho | 0:839ab88da656 | 323 | return NOKIALCD_HEIGHT; |
nucho | 0:839ab88da656 | 324 | } |
nucho | 0:839ab88da656 | 325 | |
nucho | 0:839ab88da656 | 326 | int NokiaLCD::columns() { |
nucho | 0:839ab88da656 | 327 | return NOKIALCD_COLS; |
nucho | 0:839ab88da656 | 328 | } |
nucho | 0:839ab88da656 | 329 | |
nucho | 0:839ab88da656 | 330 | int NokiaLCD::rows() { |
nucho | 0:839ab88da656 | 331 | return NOKIALCD_ROWS; |
nucho | 0:839ab88da656 | 332 | } |