N3310LCD library demo

Dependencies:   N3310LCD mbed

Fork of N3310LCD by Petras Saduikis

Committer:
snatch59
Date:
Mon Dec 07 17:24:52 2009 +0000
Revision:
0:36fb749b83c7

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
snatch59 0:36fb749b83c7 1 /*
snatch59 0:36fb749b83c7 2 * N3310LCD. A program to interface mbed with the nuelectronics
snatch59 0:36fb749b83c7 3 * Nokia 3310 LCD shield from www.nuelectronics.com. Ported from
snatch59 0:36fb749b83c7 4 * the nuelectronics Arduino code.
snatch59 0:36fb749b83c7 5 *
snatch59 0:36fb749b83c7 6 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
snatch59 0:36fb749b83c7 7 *
snatch59 0:36fb749b83c7 8 * This file is part of N3310LCD.
snatch59 0:36fb749b83c7 9 *
snatch59 0:36fb749b83c7 10 * N3310LCD is free software: you can redistribute it and/or modify
snatch59 0:36fb749b83c7 11 * it under the terms of the GNU General Public License as published by
snatch59 0:36fb749b83c7 12 * the Free Software Foundation, either version 3 of the License, or
snatch59 0:36fb749b83c7 13 * (at your option) any later version.
snatch59 0:36fb749b83c7 14 *
snatch59 0:36fb749b83c7 15 * N3310LCD is distributed in the hope that it will be useful,
snatch59 0:36fb749b83c7 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
snatch59 0:36fb749b83c7 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
snatch59 0:36fb749b83c7 18 * GNU General Public License for more details.
snatch59 0:36fb749b83c7 19 *
snatch59 0:36fb749b83c7 20 * You should have received a copy of the GNU General Public License
snatch59 0:36fb749b83c7 21 * along with N3310LCD. If not, see <http://www.gnu.org/licenses/>.
snatch59 0:36fb749b83c7 22 */
snatch59 0:36fb749b83c7 23
snatch59 0:36fb749b83c7 24 #include "N3310LCD.h"
snatch59 0:36fb749b83c7 25 #include "N3310Fonts.h"
snatch59 0:36fb749b83c7 26
snatch59 0:36fb749b83c7 27 N3310LCD::N3310LCD (PinName mosi, PinName miso, PinName sck,
snatch59 0:36fb749b83c7 28 PinName ce, PinName dat_cmd, PinName lcd_rst, PinName bl_on) :
snatch59 0:36fb749b83c7 29 lcdPort(mosi, miso, sck),
snatch59 0:36fb749b83c7 30 ceWire(ce), dcWire(dat_cmd), rstWire(lcd_rst), blWire(bl_on)
snatch59 0:36fb749b83c7 31 {
snatch59 0:36fb749b83c7 32 }
snatch59 0:36fb749b83c7 33
snatch59 0:36fb749b83c7 34 void N3310LCD::init()
snatch59 0:36fb749b83c7 35 {
snatch59 0:36fb749b83c7 36 // use default SPI format
snatch59 0:36fb749b83c7 37 lcdPort.format(8,0);
snatch59 0:36fb749b83c7 38 lcdPort.frequency(1000000);
snatch59 0:36fb749b83c7 39
snatch59 0:36fb749b83c7 40 // lcd reset
snatch59 0:36fb749b83c7 41 wait_ms(1);
snatch59 0:36fb749b83c7 42 rstWire = 0;
snatch59 0:36fb749b83c7 43 wait_ms(1);
snatch59 0:36fb749b83c7 44 rstWire = 1;
snatch59 0:36fb749b83c7 45
snatch59 0:36fb749b83c7 46 write(0x21, CMD);
snatch59 0:36fb749b83c7 47 write(0xc8, CMD);
snatch59 0:36fb749b83c7 48 write(0x06, CMD);
snatch59 0:36fb749b83c7 49 write(0x13, CMD);
snatch59 0:36fb749b83c7 50 write(0x20, CMD);
snatch59 0:36fb749b83c7 51 cls();
snatch59 0:36fb749b83c7 52 write(0x0c, CMD);
snatch59 0:36fb749b83c7 53 }
snatch59 0:36fb749b83c7 54
snatch59 0:36fb749b83c7 55 void N3310LCD::cls()
snatch59 0:36fb749b83c7 56 {
snatch59 0:36fb749b83c7 57 write(0x0c, CMD);
snatch59 0:36fb749b83c7 58 write(0x80, CMD);
snatch59 0:36fb749b83c7 59
snatch59 0:36fb749b83c7 60 for (int i = 0; i < 504; i++)
snatch59 0:36fb749b83c7 61 {
snatch59 0:36fb749b83c7 62 write(0, DATA);
snatch59 0:36fb749b83c7 63 }
snatch59 0:36fb749b83c7 64 }
snatch59 0:36fb749b83c7 65
snatch59 0:36fb749b83c7 66 void N3310LCD::backlight(eBacklight state)
snatch59 0:36fb749b83c7 67 {
snatch59 0:36fb749b83c7 68 // switch off/on back light
snatch59 0:36fb749b83c7 69 blWire = state;
snatch59 0:36fb749b83c7 70 }
snatch59 0:36fb749b83c7 71
snatch59 0:36fb749b83c7 72 void N3310LCD::write(BYTE data, eRequestType req_type)
snatch59 0:36fb749b83c7 73 {
snatch59 0:36fb749b83c7 74 // bring CS low for write
snatch59 0:36fb749b83c7 75 ceWire = 0;
snatch59 0:36fb749b83c7 76
snatch59 0:36fb749b83c7 77 if (CMD == req_type)
snatch59 0:36fb749b83c7 78 dcWire = 0;
snatch59 0:36fb749b83c7 79 else // DATA
snatch59 0:36fb749b83c7 80 dcWire = 1;
snatch59 0:36fb749b83c7 81
snatch59 0:36fb749b83c7 82 lcdPort.write(data);
snatch59 0:36fb749b83c7 83
snatch59 0:36fb749b83c7 84 // write finished
snatch59 0:36fb749b83c7 85 ceWire = 1;
snatch59 0:36fb749b83c7 86 }
snatch59 0:36fb749b83c7 87
snatch59 0:36fb749b83c7 88 void N3310LCD::locate(BYTE xPos, BYTE yPos)
snatch59 0:36fb749b83c7 89 {
snatch59 0:36fb749b83c7 90 write(0x40 | yPos, CMD); // column
snatch59 0:36fb749b83c7 91 write(0x80 | xPos, CMD); // row
snatch59 0:36fb749b83c7 92 }
snatch59 0:36fb749b83c7 93
snatch59 0:36fb749b83c7 94 void N3310LCD::drawBitmap(BYTE xPos, BYTE yPos, BYTE* bitmap, BYTE bmpXSize, BYTE bmpYSize)
snatch59 0:36fb749b83c7 95 {
snatch59 0:36fb749b83c7 96 BYTE row;
snatch59 0:36fb749b83c7 97
snatch59 0:36fb749b83c7 98 if (0 == bmpYSize % 8)
snatch59 0:36fb749b83c7 99 row = bmpYSize/8;
snatch59 0:36fb749b83c7 100 else
snatch59 0:36fb749b83c7 101 row = bmpYSize/8 + 1;
snatch59 0:36fb749b83c7 102
snatch59 0:36fb749b83c7 103 for (BYTE n = 0; n < row; n++)
snatch59 0:36fb749b83c7 104 {
snatch59 0:36fb749b83c7 105 locate(xPos, yPos);
snatch59 0:36fb749b83c7 106 for(BYTE i = 0; i < bmpXSize; i++)
snatch59 0:36fb749b83c7 107 {
snatch59 0:36fb749b83c7 108 write(bitmap[i + (n * bmpXSize)], DATA);
snatch59 0:36fb749b83c7 109 }
snatch59 0:36fb749b83c7 110 yPos++;
snatch59 0:36fb749b83c7 111 }
snatch59 0:36fb749b83c7 112 }
snatch59 0:36fb749b83c7 113
snatch59 0:36fb749b83c7 114 void N3310LCD::writeString(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode)
snatch59 0:36fb749b83c7 115 {
snatch59 0:36fb749b83c7 116 locate(xPos, yPos);
snatch59 0:36fb749b83c7 117
snatch59 0:36fb749b83c7 118 while (*string)
snatch59 0:36fb749b83c7 119 {
snatch59 0:36fb749b83c7 120 writeChar(*string++, mode);
snatch59 0:36fb749b83c7 121 }
snatch59 0:36fb749b83c7 122 }
snatch59 0:36fb749b83c7 123
snatch59 0:36fb749b83c7 124 void N3310LCD::writeStringBig(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode)
snatch59 0:36fb749b83c7 125 {
snatch59 0:36fb749b83c7 126 while (*string)
snatch59 0:36fb749b83c7 127 {
snatch59 0:36fb749b83c7 128 writeCharBig(xPos, yPos, *string , mode);
snatch59 0:36fb749b83c7 129
snatch59 0:36fb749b83c7 130 if('.' == *string++)
snatch59 0:36fb749b83c7 131 xPos += 5;
snatch59 0:36fb749b83c7 132 else
snatch59 0:36fb749b83c7 133 xPos += 12;
snatch59 0:36fb749b83c7 134 }
snatch59 0:36fb749b83c7 135 }
snatch59 0:36fb749b83c7 136
snatch59 0:36fb749b83c7 137 void N3310LCD::writeChar(BYTE ch, eDisplayMode mode)
snatch59 0:36fb749b83c7 138 {
snatch59 0:36fb749b83c7 139 BYTE sendByte;
snatch59 0:36fb749b83c7 140
snatch59 0:36fb749b83c7 141 unsigned char* pFont = (unsigned char*)font6_8;
snatch59 0:36fb749b83c7 142 ch -= 32;
snatch59 0:36fb749b83c7 143
snatch59 0:36fb749b83c7 144 for (BYTE line = 0; line < 6; line++)
snatch59 0:36fb749b83c7 145 {
snatch59 0:36fb749b83c7 146 sendByte = *(pFont + ch*6 + line);
snatch59 0:36fb749b83c7 147 write((mode == NORMAL)? sendByte: (sendByte ^ 0xff) , DATA);
snatch59 0:36fb749b83c7 148 }
snatch59 0:36fb749b83c7 149 }
snatch59 0:36fb749b83c7 150
snatch59 0:36fb749b83c7 151 void N3310LCD::writeCharBig(BYTE xPos, BYTE yPos, BYTE ch, eDisplayMode mode)
snatch59 0:36fb749b83c7 152 {
snatch59 0:36fb749b83c7 153 BYTE sendByte;
snatch59 0:36fb749b83c7 154
snatch59 0:36fb749b83c7 155 unsigned char* pFont = (unsigned char *) big_number;
snatch59 0:36fb749b83c7 156
snatch59 0:36fb749b83c7 157 if('.' == ch)
snatch59 0:36fb749b83c7 158 ch = 10;
snatch59 0:36fb749b83c7 159 else if ('+' == ch)
snatch59 0:36fb749b83c7 160 ch = 11;
snatch59 0:36fb749b83c7 161 else if ('-' == ch)
snatch59 0:36fb749b83c7 162 ch = 12;
snatch59 0:36fb749b83c7 163 else
snatch59 0:36fb749b83c7 164 ch = ch & 0x0f;
snatch59 0:36fb749b83c7 165
snatch59 0:36fb749b83c7 166 for(BYTE i = 0; i < 3; i++)
snatch59 0:36fb749b83c7 167 {
snatch59 0:36fb749b83c7 168 locate(xPos, yPos + i);
snatch59 0:36fb749b83c7 169
snatch59 0:36fb749b83c7 170 for(BYTE j = 0; j < 16; j++)
snatch59 0:36fb749b83c7 171 {
snatch59 0:36fb749b83c7 172 sendByte = *(pFont + ch*48 + i*16 + j);
snatch59 0:36fb749b83c7 173 write((mode == NORMAL)? sendByte : (sendByte^0xff), DATA);
snatch59 0:36fb749b83c7 174 }
snatch59 0:36fb749b83c7 175 }
snatch59 0:36fb749b83c7 176 }