Display text on LCD displays (even on multiple ones). Allow to create windows (frames) on display, and to combine them (split, add, duplicate, scroll). See http://mbed.org/users/hlipka/notebook/lcdwindow/ for more information.

Dependents:   Mbell

Committer:
hlipka
Date:
Tue Feb 22 22:57:44 2011 +0000
Revision:
9:2fe93daa2106
Parent:
8:ba176eea3e40
fixed semaphore handling - should now be really thread safe (can be called from interrupts)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hlipka 6:7ba288afed38 1 /*
hlipka 6:7ba288afed38 2 * mbed LCDWindow library
hlipka 6:7ba288afed38 3 * Copyright (c) 2010 Hendrik Lipka
hlipka 6:7ba288afed38 4 *
hlipka 6:7ba288afed38 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
hlipka 6:7ba288afed38 6 * of this software and associated documentation files (the "Software"), to deal
hlipka 6:7ba288afed38 7 * in the Software without restriction, including without limitation the rights
hlipka 6:7ba288afed38 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hlipka 6:7ba288afed38 9 * copies of the Software, and to permit persons to whom the Software is
hlipka 6:7ba288afed38 10 * furnished to do so, subject to the following conditions:
hlipka 6:7ba288afed38 11 *
hlipka 6:7ba288afed38 12 * The above copyright notice and this permission notice shall be included in
hlipka 6:7ba288afed38 13 * all copies or substantial portions of the Software.
hlipka 6:7ba288afed38 14 *
hlipka 6:7ba288afed38 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hlipka 6:7ba288afed38 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hlipka 6:7ba288afed38 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hlipka 6:7ba288afed38 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hlipka 6:7ba288afed38 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hlipka 6:7ba288afed38 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hlipka 6:7ba288afed38 21 * THE SOFTWARE.
hlipka 6:7ba288afed38 22 */
hlipka 6:7ba288afed38 23
hlipka 6:7ba288afed38 24 #include "sed1335text.h"
hlipka 6:7ba288afed38 25
hlipka 6:7ba288afed38 26 #include "BusOut.h"
hlipka 6:7ba288afed38 27 #include "DigitalOut.h"
hlipka 6:7ba288afed38 28 #include "wait_api.h"
hlipka 6:7ba288afed38 29
hlipka 7:b472970bd8f6 30 #define FIRSTBANK_MEM 0
hlipka 7:b472970bd8f6 31 #define SECONDBANK_MEM 0x1000
hlipka 7:b472970bd8f6 32 #define THIRDBANK_MEM 0x800
hlipka 7:b472970bd8f6 33
hlipka 7:b472970bd8f6 34 #define HIGH(x) ((x&0xff00)>>8)
hlipka 7:b472970bd8f6 35 #define LOW(x) (x&0xff)
hlipka 7:b472970bd8f6 36
hlipka 6:7ba288afed38 37 void SED1335TextLCD::writeText(const unsigned int column, const unsigned int row, const char text[]) {
hlipka 6:7ba288afed38 38 int i=0;
hlipka 6:7ba288afed38 39
hlipka 6:7ba288afed38 40 int pos=row*getColumns()+column;
hlipka 9:2fe93daa2106 41
hlipka 9:2fe93daa2106 42 if (!_guard->take())
hlipka 9:2fe93daa2106 43 return;
hlipka 6:7ba288afed38 44
hlipka 6:7ba288afed38 45 sendCmd(0x46); // set cursor addr
hlipka 7:b472970bd8f6 46 sendData(LOW(pos));
hlipka 7:b472970bd8f6 47 sendData(HIGH(pos));
hlipka 6:7ba288afed38 48
hlipka 6:7ba288afed38 49 sendCmd(0x42);
hlipka 6:7ba288afed38 50 while (text[i]!=0) {
hlipka 6:7ba288afed38 51 sendData(text[i]);
hlipka 6:7ba288afed38 52 i++;
hlipka 6:7ba288afed38 53 }
hlipka 9:2fe93daa2106 54 _guard->release();
hlipka 6:7ba288afed38 55 }
hlipka 6:7ba288afed38 56
hlipka 7:b472970bd8f6 57 void SED1335TextLCD::clearBank(int start, int size, int data) {
hlipka 6:7ba288afed38 58 sendCmd(0x46); // set cursor addr
hlipka 7:b472970bd8f6 59 sendData(LOW(start)); // start of RAM
hlipka 7:b472970bd8f6 60 sendData(HIGH(start));
hlipka 6:7ba288afed38 61 wait_ms(1);
hlipka 6:7ba288afed38 62
hlipka 6:7ba288afed38 63 sendCmd(0x42); // write to memory
hlipka 7:b472970bd8f6 64 for (int i=0;i<size;i++)
hlipka 7:b472970bd8f6 65 sendData(data); // send space
hlipka 6:7ba288afed38 66 wait_ms(1);
hlipka 7:b472970bd8f6 67 }
hlipka 6:7ba288afed38 68
hlipka 7:b472970bd8f6 69 void SED1335TextLCD::clear() {
hlipka 7:b472970bd8f6 70 _guard->take();
hlipka 7:b472970bd8f6 71
hlipka 7:b472970bd8f6 72 clearBank(FIRSTBANK_MEM,getColumns()*getRows(),0x20);
hlipka 7:b472970bd8f6 73 clearBank(SECONDBANK_MEM,getColumns()*getRows()*8,0);
hlipka 7:b472970bd8f6 74 clearBank(THIRDBANK_MEM,getColumns()*getRows(),0);
hlipka 6:7ba288afed38 75
hlipka 6:7ba288afed38 76 _guard->release();
hlipka 6:7ba288afed38 77 }
hlipka 6:7ba288afed38 78
hlipka 6:7ba288afed38 79
hlipka 6:7ba288afed38 80 void SED1335TextLCD::character(int column, int row, int c){
hlipka 6:7ba288afed38 81 _guard->take();
hlipka 6:7ba288afed38 82 _guard->release();
hlipka 6:7ba288afed38 83 }
hlipka 6:7ba288afed38 84
hlipka 6:7ba288afed38 85 SED1335TextLCD::SED1335TextLCD
hlipka 6:7ba288afed38 86 (const unsigned int columns, const unsigned int rows, BusOut *data, const PinName read, const PinName write, const PinName cs, const PinName a0, const PinName reset)
hlipka 6:7ba288afed38 87 :TextLCDBase(columns, rows) {
hlipka 6:7ba288afed38 88 _data=data;
hlipka 6:7ba288afed38 89 _rd=new DigitalOut(read);
hlipka 6:7ba288afed38 90 _wr=new DigitalOut(write);
hlipka 6:7ba288afed38 91 _a0=new DigitalOut(a0);
hlipka 6:7ba288afed38 92 _cs=new DigitalOut(cs);
hlipka 6:7ba288afed38 93 _reset=new DigitalOut(reset);
hlipka 8:ba176eea3e40 94 wait_ms(100);
hlipka 6:7ba288afed38 95 }
hlipka 6:7ba288afed38 96
hlipka 6:7ba288afed38 97 void SED1335TextLCD::init() {
hlipka 8:ba176eea3e40 98 initInternal();
hlipka 8:ba176eea3e40 99 initInternal();
hlipka 8:ba176eea3e40 100 }
hlipka 8:ba176eea3e40 101
hlipka 8:ba176eea3e40 102 void SED1335TextLCD::initInternal() {
hlipka 6:7ba288afed38 103 _reset->write(0);
hlipka 6:7ba288afed38 104 wait_ms(2);
hlipka 6:7ba288afed38 105 _reset->write(1);
hlipka 6:7ba288afed38 106 wait_ms(10);
hlipka 6:7ba288afed38 107
hlipka 6:7ba288afed38 108 sendCmd(0x40); // SYSTEM SET
hlipka 6:7ba288afed38 109 sendData(0x30); // p1 set cg rom
hlipka 6:7ba288afed38 110 sendData(0x87); // p2
hlipka 6:7ba288afed38 111 sendData(0x7); // p3
hlipka 7:b472970bd8f6 112 sendData(getColumns()-1); // p4
hlipka 7:b472970bd8f6 113 sendData(getColumns()+3); // p5
hlipka 6:7ba288afed38 114 sendData(0xef); // p6
hlipka 7:b472970bd8f6 115 sendData(getColumns()); // p7
hlipka 6:7ba288afed38 116 sendData(0x0); // p8
hlipka 6:7ba288afed38 117 wait_ms(1);
hlipka 6:7ba288afed38 118
hlipka 6:7ba288afed38 119 sendCmd(0x44); // SCROLL
hlipka 7:b472970bd8f6 120 sendData(LOW(FIRSTBANK_MEM)); // first block
hlipka 7:b472970bd8f6 121 sendData(HIGH(FIRSTBANK_MEM));
hlipka 7:b472970bd8f6 122 sendData(getRows()*8); // lines
hlipka 7:b472970bd8f6 123 sendData(LOW(SECONDBANK_MEM)); // second block
hlipka 7:b472970bd8f6 124 sendData(HIGH(SECONDBANK_MEM));
hlipka 7:b472970bd8f6 125 sendData(getRows()*8); // lines
hlipka 7:b472970bd8f6 126 sendData(LOW(THIRDBANK_MEM)); // third block at 0x800
hlipka 7:b472970bd8f6 127 sendData(HIGH(THIRDBANK_MEM));
hlipka 6:7ba288afed38 128 wait_ms(1);
hlipka 6:7ba288afed38 129
hlipka 6:7ba288afed38 130 sendCmd(0x5a); // HDOT SCR
hlipka 6:7ba288afed38 131 sendData(0x0);
hlipka 6:7ba288afed38 132
hlipka 6:7ba288afed38 133
hlipka 6:7ba288afed38 134 sendCmd(0x5b); // OVLAY
hlipka 6:7ba288afed38 135 sendData(0x01); // 2 layers, text, x-or
hlipka 6:7ba288afed38 136 wait_ms(1);
hlipka 6:7ba288afed38 137
hlipka 6:7ba288afed38 138 sendCmd(0x59); // DISP on
hlipka 6:7ba288afed38 139 sendData(0x54); // all layers on, cursor off
hlipka 6:7ba288afed38 140 // sendData(0x56); // all layers on, cursor flashing
hlipka 6:7ba288afed38 141 wait_ms(1);
hlipka 6:7ba288afed38 142
hlipka 6:7ba288afed38 143 clear();
hlipka 6:7ba288afed38 144
hlipka 6:7ba288afed38 145 sendCmd(0x5d); // CSR FORM
hlipka 6:7ba288afed38 146 sendData(0x04); // 8 pixels wide
hlipka 6:7ba288afed38 147 sendData(0x86); // 2 pixel high, block cursor
hlipka 6:7ba288afed38 148 wait_ms(1);
hlipka 6:7ba288afed38 149
hlipka 6:7ba288afed38 150 sendCmd(0x4c); // CSR DIR right
hlipka 6:7ba288afed38 151 wait_ms(1);
hlipka 6:7ba288afed38 152
hlipka 6:7ba288afed38 153 }
hlipka 6:7ba288afed38 154
hlipka 6:7ba288afed38 155 void SED1335TextLCD::sendCmd(const unsigned char cmd) {
hlipka 6:7ba288afed38 156 _rd->write(1);
hlipka 6:7ba288afed38 157 _wr->write(1);
hlipka 6:7ba288afed38 158 _a0->write(1);
hlipka 6:7ba288afed38 159 _cs->write(0);
hlipka 6:7ba288afed38 160 // address setup time is 0
hlipka 6:7ba288afed38 161 sendByte(cmd);
hlipka 6:7ba288afed38 162 wait_us(10);
hlipka 6:7ba288afed38 163 }
hlipka 6:7ba288afed38 164
hlipka 6:7ba288afed38 165 void SED1335TextLCD::sendData(const unsigned char cmd) {
hlipka 6:7ba288afed38 166 _rd->write(1);
hlipka 6:7ba288afed38 167 _wr->write(1);
hlipka 6:7ba288afed38 168 _a0->write(0);
hlipka 6:7ba288afed38 169 _cs->write(0);
hlipka 6:7ba288afed38 170 // address setup time is 0
hlipka 6:7ba288afed38 171 sendByte(cmd);
hlipka 6:7ba288afed38 172 wait_us(10);
hlipka 6:7ba288afed38 173 }
hlipka 6:7ba288afed38 174
hlipka 6:7ba288afed38 175 void SED1335TextLCD::sendByte(const unsigned char byte) {
hlipka 6:7ba288afed38 176 // display reads with rising flank of /wr
hlipka 6:7ba288afed38 177 // first, set data, then set /wr low (no timing needed)
hlipka 6:7ba288afed38 178 _data->write(byte);
hlipka 6:7ba288afed38 179 _wr->write(0);
hlipka 6:7ba288afed38 180 // wait for setup
hlipka 6:7ba288afed38 181 wait_us(1);
hlipka 6:7ba288afed38 182 // set /wr back to high
hlipka 6:7ba288afed38 183 _wr->write(1);
hlipka 6:7ba288afed38 184 // address / data hold time is 10 / 5 ns, so its handled by executing normal code :)
hlipka 6:7ba288afed38 185 }