nuchoさんのNokiaライブラリ( NokiaLCD_With_JapaneseFont ) の NokiaLCD.cpp _putp関数をカスタマイズしました

Dependents:   NokiaLCD spxml_WeatherLCD

Fork of NokiaLCD_With_JapaneseFont by nucho

Committer:
sunifu
Date:
Sun Jul 13 11:44:38 2014 +0000
Revision:
1:c505894797b3
Parent:
0:839ab88da656
2014.7.13

Who changed what in which revision?

UserRevisionLine numberNew 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
sunifu 1:c505894797b3 129
nucho 0:839ab88da656 130 break;
nucho 0:839ab88da656 131 }
nucho 0:839ab88da656 132
nucho 0:839ab88da656 133 _cs = 1;
nucho 0:839ab88da656 134
nucho 0:839ab88da656 135 cls();
nucho 0:839ab88da656 136 }
nucho 0:839ab88da656 137
nucho 0:839ab88da656 138 void NokiaLCD::command(int value) {
nucho 0:839ab88da656 139 _spi.write(value & 0xFF);
nucho 0:839ab88da656 140 }
nucho 0:839ab88da656 141
nucho 0:839ab88da656 142 void NokiaLCD::data(int value) {
nucho 0:839ab88da656 143 _spi.write(value | 0x100);
nucho 0:839ab88da656 144 }
nucho 0:839ab88da656 145
nucho 0:839ab88da656 146 void NokiaLCD::_window(int x, int y, int width, int height) {
nucho 0:839ab88da656 147 int x1 = x + 2;
nucho 0:839ab88da656 148 int y1 = y + 0;
nucho 0:839ab88da656 149 int x2 = x1 + width - 1;
nucho 0:839ab88da656 150 int y2 = y1 + height - 1;
nucho 0:839ab88da656 151
nucho 0:839ab88da656 152 switch (_type) {
nucho 0:839ab88da656 153 case LCD6100:
nucho 0:839ab88da656 154 case LCD6610:
nucho 0:839ab88da656 155 case LCD3300:
nucho 0:839ab88da656 156 command(0x15); // column
nucho 0:839ab88da656 157 data(x1);
nucho 0:839ab88da656 158 data(x2);
nucho 0:839ab88da656 159 command(0x75); // row
nucho 0:839ab88da656 160 data(y1);
nucho 0:839ab88da656 161 data(y2);
nucho 0:839ab88da656 162 command(0x5C); // start write to ram
nucho 0:839ab88da656 163 break;
nucho 0:839ab88da656 164 case PCF8833:
nucho 0:839ab88da656 165 command(0x2A); // column
nucho 0:839ab88da656 166 data(x1);
nucho 0:839ab88da656 167 data(x2);
nucho 0:839ab88da656 168 command(0x2B); // row
nucho 0:839ab88da656 169 data(y1);
nucho 0:839ab88da656 170 data(y2);
nucho 0:839ab88da656 171 command(0x2C); // start write to ram
nucho 0:839ab88da656 172 break;
nucho 0:839ab88da656 173 }
nucho 0:839ab88da656 174 }
nucho 0:839ab88da656 175
sunifu 1:c505894797b3 176 void NokiaLCD::_putp(int colour){
sunifu 1:c505894797b3 177
sunifu 1:c505894797b3 178 switch (_type)
sunifu 1:c505894797b3 179 {
sunifu 1:c505894797b3 180 case LCD6100:
sunifu 1:c505894797b3 181 case LCD3300:
sunifu 1:c505894797b3 182 int gr = ((colour >> 20) & 0x0F)
sunifu 1:c505894797b3 183 | ((colour >> 8 ) & 0xF0);
sunifu 1:c505894797b3 184 int nb = ((colour >> 4 ) & 0x0F);
sunifu 1:c505894797b3 185 data(nb);
sunifu 1:c505894797b3 186 data(gr);
sunifu 1:c505894797b3 187 break;
sunifu 1:c505894797b3 188
sunifu 1:c505894797b3 189 case PCF8833:
sunifu 1:c505894797b3 190 int rg = ((colour >> 16) & 0xF8)
sunifu 1:c505894797b3 191 | ((colour >> 13 ) & 0x07);
sunifu 1:c505894797b3 192 int gb = ((colour >> 5 ) & 0xE0)
sunifu 1:c505894797b3 193 | ((colour >> 3 ) & 0x1f);
sunifu 1:c505894797b3 194 data(rg);
sunifu 1:c505894797b3 195 data(gb);
sunifu 1:c505894797b3 196 break;
sunifu 1:c505894797b3 197 }
nucho 0:839ab88da656 198 }
nucho 0:839ab88da656 199
nucho 0:839ab88da656 200
nucho 0:839ab88da656 201 void NokiaLCD::locate(int column, int row) {
nucho 0:839ab88da656 202 _column = column;
nucho 0:839ab88da656 203 _row = row;
nucho 0:839ab88da656 204 }
nucho 0:839ab88da656 205
nucho 0:839ab88da656 206 void NokiaLCD::newline() {
nucho 0:839ab88da656 207 _column = 0;
nucho 0:839ab88da656 208 _row++;
nucho 0:839ab88da656 209 if (_row >= NOKIALCD_ROWS) {
nucho 0:839ab88da656 210 _row = 0;
nucho 0:839ab88da656 211 }
nucho 0:839ab88da656 212 }
nucho 0:839ab88da656 213
nucho 0:839ab88da656 214 unsigned int NokiaLCD::findface(unsigned short c) {
nucho 0:839ab88da656 215 unsigned int p = 0;
nucho 0:839ab88da656 216 int i, sum;
nucho 0:839ab88da656 217 for (sum = i = 0; i < countof(font8table); i++) {
nucho 0:839ab88da656 218 if (font8table[i].start <= c && c <= font8table[i].end) {
nucho 0:839ab88da656 219 p = (sum + c - font8table[i].start);
nucho 0:839ab88da656 220 break;
nucho 0:839ab88da656 221 }
nucho 0:839ab88da656 222 sum += font8table[i].end - font8table[i].start + 1;
nucho 0:839ab88da656 223 }
nucho 0:839ab88da656 224 return p;
nucho 0:839ab88da656 225 }
nucho 0:839ab88da656 226
nucho 0:839ab88da656 227 int NokiaLCD::_putc(int value) {
nucho 0:839ab88da656 228 int x = _column * 8; // FIXME: Char sizes
nucho 0:839ab88da656 229 int y = _row * 8;
nucho 0:839ab88da656 230
nucho 0:839ab88da656 231 if(value == '\n') {
nucho 0:839ab88da656 232 newline();
nucho 0:839ab88da656 233 } else if (kstate) { // 2nd byte of shift-jis
nucho 0:839ab88da656 234 kstate = false;
nucho 0:839ab88da656 235 int p = findface(kbuf << 8 | value);
nucho 0:839ab88da656 236 bitblit(x + 1, y + 1, 8,8, (char*)&(FontLookup[p][0]));
nucho 0:839ab88da656 237
nucho 0:839ab88da656 238 //printf("%x %x",( kbuf << 8 | value),p); //for debug
nucho 0:839ab88da656 239 _column++;
nucho 0:839ab88da656 240 if (_column >= NOKIALCD_COLS) {
nucho 0:839ab88da656 241 _row++;
nucho 0:839ab88da656 242 _column = 0;
nucho 0:839ab88da656 243 }
nucho 0:839ab88da656 244 if (_row >= NOKIALCD_ROWS) {
nucho 0:839ab88da656 245 _row = 0;
nucho 0:839ab88da656 246 }
nucho 0:839ab88da656 247 } else if ((0x81 <= value && value <= 0x9f) || (0xe0 <= value && value <= 0xfc)) { // 1st byte of shift-jis
nucho 0:839ab88da656 248 kstate = true;
nucho 0:839ab88da656 249 kbuf = value;
nucho 0:839ab88da656 250 } else {
nucho 0:839ab88da656 251 bitblit(x + 1, y + 1, 8, 8, (char*)&(FontLookup_ABC[value-32][0]));
nucho 0:839ab88da656 252 _column++;
nucho 0:839ab88da656 253 if (_column >= NOKIALCD_COLS) {
nucho 0:839ab88da656 254 _row++;
nucho 0:839ab88da656 255 _column = 0;
nucho 0:839ab88da656 256 }
nucho 0:839ab88da656 257 if (_row >= NOKIALCD_ROWS) {
nucho 0:839ab88da656 258 _row = 0;
nucho 0:839ab88da656 259 }
nucho 0:839ab88da656 260 }
nucho 0:839ab88da656 261
nucho 0:839ab88da656 262 return value;
nucho 0:839ab88da656 263 }
nucho 0:839ab88da656 264
nucho 0:839ab88da656 265 void NokiaLCD::cls() {
nucho 0:839ab88da656 266 fill(0, 0, NOKIALCD_WIDTH, NOKIALCD_HEIGHT, _background);
nucho 0:839ab88da656 267 _row = 0;
nucho 0:839ab88da656 268 _column = 0;
nucho 0:839ab88da656 269 }
nucho 0:839ab88da656 270
nucho 0:839ab88da656 271
nucho 0:839ab88da656 272 void NokiaLCD::window(int x, int y, int width, int height) {
nucho 0:839ab88da656 273 _cs = 0;
nucho 0:839ab88da656 274 _window(x, y, width, height);
nucho 0:839ab88da656 275 _cs = 1;
nucho 0:839ab88da656 276 }
nucho 0:839ab88da656 277
nucho 0:839ab88da656 278 void NokiaLCD::putp(int colour) {
nucho 0:839ab88da656 279 _cs = 0;
nucho 0:839ab88da656 280 _putp(colour);
nucho 0:839ab88da656 281 _cs = 1;
nucho 0:839ab88da656 282 }
nucho 0:839ab88da656 283
nucho 0:839ab88da656 284 void NokiaLCD::pixel(int x, int y, int colour) {
nucho 0:839ab88da656 285 _cs = 0;
nucho 0:839ab88da656 286 _window(x, y, 1, 1);
nucho 0:839ab88da656 287 _putp(colour);
nucho 0:839ab88da656 288 _cs = 1;
nucho 0:839ab88da656 289 }
nucho 0:839ab88da656 290
nucho 0:839ab88da656 291 void NokiaLCD::fill(int x, int y, int width, int height, int colour) {
nucho 0:839ab88da656 292 _cs = 0;
nucho 0:839ab88da656 293 _window(x, y, width, height);
nucho 0:839ab88da656 294 for (int i=0; i<width*height; i++) {
nucho 0:839ab88da656 295 _putp(colour);
nucho 0:839ab88da656 296 }
nucho 0:839ab88da656 297 _window(0, 0, NOKIALCD_WIDTH, NOKIALCD_HEIGHT);
nucho 0:839ab88da656 298 _cs = 1;
nucho 0:839ab88da656 299 }
nucho 0:839ab88da656 300
nucho 0:839ab88da656 301 void NokiaLCD::blit(int x, int y, int width, int height, const int* colour) {
nucho 0:839ab88da656 302 _cs = 0;
nucho 0:839ab88da656 303 _window(x, y, width, height);
nucho 0:839ab88da656 304 for (int i=0; i<width*height; i++) {
nucho 0:839ab88da656 305 _putp(colour[i]);
nucho 0:839ab88da656 306 }
nucho 0:839ab88da656 307 _window(0, 0, NOKIALCD_WIDTH, NOKIALCD_HEIGHT);
nucho 0:839ab88da656 308 _cs = 1;
nucho 0:839ab88da656 309 }
nucho 0:839ab88da656 310
nucho 0:839ab88da656 311 void NokiaLCD::bitblit(int x, int y, int width, int height, const char* bitstream) {
nucho 0:839ab88da656 312 _cs = 0;
nucho 0:839ab88da656 313 _window(x, y, width, height);
nucho 0:839ab88da656 314 for (int i=0; i<height*width; i++) {
nucho 0:839ab88da656 315 int byte = i / 8;
nucho 0:839ab88da656 316 int bit = i % 8;
nucho 0:839ab88da656 317 int colour = ((bitstream[byte] << bit) & 0x80) ? _foreground : _background;
nucho 0:839ab88da656 318 _putp(colour);
nucho 0:839ab88da656 319 }
nucho 0:839ab88da656 320 _window(0, 0, _width, _height);
nucho 0:839ab88da656 321 _cs = 1;
nucho 0:839ab88da656 322 }
nucho 0:839ab88da656 323
nucho 0:839ab88da656 324
nucho 0:839ab88da656 325
nucho 0:839ab88da656 326
nucho 0:839ab88da656 327 void NokiaLCD::foreground(int c) {
nucho 0:839ab88da656 328 _foreground = c;
nucho 0:839ab88da656 329 }
nucho 0:839ab88da656 330
nucho 0:839ab88da656 331 void NokiaLCD::background(int c) {
nucho 0:839ab88da656 332 _background = c;
nucho 0:839ab88da656 333 }
nucho 0:839ab88da656 334
nucho 0:839ab88da656 335 int NokiaLCD::width() {
nucho 0:839ab88da656 336 return NOKIALCD_WIDTH;
nucho 0:839ab88da656 337 }
nucho 0:839ab88da656 338
nucho 0:839ab88da656 339 int NokiaLCD::height() {
nucho 0:839ab88da656 340 return NOKIALCD_HEIGHT;
nucho 0:839ab88da656 341 }
nucho 0:839ab88da656 342
nucho 0:839ab88da656 343 int NokiaLCD::columns() {
nucho 0:839ab88da656 344 return NOKIALCD_COLS;
nucho 0:839ab88da656 345 }
nucho 0:839ab88da656 346
nucho 0:839ab88da656 347 int NokiaLCD::rows() {
nucho 0:839ab88da656 348 return NOKIALCD_ROWS;
nucho 0:839ab88da656 349 }