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:
3:e5d5e2fe4bf6
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 0:ae5037e3d6e0 1 /*
hlipka 0:ae5037e3d6e0 2 * mbed LCDWindow library
hlipka 0:ae5037e3d6e0 3 * Copyright (c) 2010 Hendrik Lipka
hlipka 0:ae5037e3d6e0 4 *
hlipka 0:ae5037e3d6e0 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
hlipka 0:ae5037e3d6e0 6 * of this software and associated documentation files (the "Software"), to deal
hlipka 0:ae5037e3d6e0 7 * in the Software without restriction, including without limitation the rights
hlipka 0:ae5037e3d6e0 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hlipka 0:ae5037e3d6e0 9 * copies of the Software, and to permit persons to whom the Software is
hlipka 0:ae5037e3d6e0 10 * furnished to do so, subject to the following conditions:
hlipka 0:ae5037e3d6e0 11 *
hlipka 0:ae5037e3d6e0 12 * The above copyright notice and this permission notice shall be included in
hlipka 0:ae5037e3d6e0 13 * all copies or substantial portions of the Software.
hlipka 0:ae5037e3d6e0 14 *
hlipka 0:ae5037e3d6e0 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hlipka 0:ae5037e3d6e0 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hlipka 0:ae5037e3d6e0 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hlipka 0:ae5037e3d6e0 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hlipka 0:ae5037e3d6e0 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hlipka 0:ae5037e3d6e0 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hlipka 0:ae5037e3d6e0 21 * THE SOFTWARE.
hlipka 0:ae5037e3d6e0 22 */
hlipka 0:ae5037e3d6e0 23
hlipka 0:ae5037e3d6e0 24 #include "hd44780_8bit.h"
hlipka 0:ae5037e3d6e0 25
hlipka 0:ae5037e3d6e0 26 #include "BusOut.h"
hlipka 0:ae5037e3d6e0 27 #include "DigitalOut.h"
hlipka 0:ae5037e3d6e0 28 #include "wait_api.h"
hlipka 0:ae5037e3d6e0 29
hlipka 2:5ac5bab7daaf 30 void HD44780LCD8bit::character(int column, int row, int c)
hlipka 2:5ac5bab7daaf 31 {
hlipka 2:5ac5bab7daaf 32 int address=(row)*0x40+(column);
hlipka 9:2fe93daa2106 33 if (!_guard->take())
hlipka 9:2fe93daa2106 34 return;
hlipka 2:5ac5bab7daaf 35 sendCmd((unsigned char)address|0x80);
hlipka 2:5ac5bab7daaf 36 wait_us(30);
hlipka 2:5ac5bab7daaf 37 sendData(c);
hlipka 3:e5d5e2fe4bf6 38 _guard->release();
hlipka 2:5ac5bab7daaf 39 wait_us(30);
hlipka 2:5ac5bab7daaf 40 }
hlipka 0:ae5037e3d6e0 41
hlipka 2:5ac5bab7daaf 42
hlipka 2:5ac5bab7daaf 43 void HD44780LCD8bit::writeText(const unsigned int column, const unsigned int row, const char text[]) {
hlipka 0:ae5037e3d6e0 44 // printf("print to %d,%d {%s}\n",line,pos,text);
hlipka 2:5ac5bab7daaf 45 int address=row*0x40+column;
hlipka 9:2fe93daa2106 46 if (!_guard->take())
hlipka 9:2fe93daa2106 47 return;
hlipka 0:ae5037e3d6e0 48 sendCmd((unsigned char)address|0x80);
hlipka 0:ae5037e3d6e0 49 wait_us(30);
hlipka 0:ae5037e3d6e0 50
hlipka 0:ae5037e3d6e0 51 int i=0;
hlipka 0:ae5037e3d6e0 52 while (text[i]!=0) {
hlipka 0:ae5037e3d6e0 53 sendData(text[i]);
hlipka 0:ae5037e3d6e0 54 wait_us(30);
hlipka 0:ae5037e3d6e0 55 i++;
hlipka 0:ae5037e3d6e0 56 }
hlipka 3:e5d5e2fe4bf6 57 _guard->release();
hlipka 0:ae5037e3d6e0 58 }
hlipka 0:ae5037e3d6e0 59
hlipka 0:ae5037e3d6e0 60 void HD44780LCD8bit::clear() {
hlipka 9:2fe93daa2106 61 if (!_guard->take())
hlipka 9:2fe93daa2106 62 return;
hlipka 0:ae5037e3d6e0 63 sendCmd(1);
hlipka 3:e5d5e2fe4bf6 64 _guard->release();
hlipka 0:ae5037e3d6e0 65 }
hlipka 0:ae5037e3d6e0 66
hlipka 1:65f72ed914fa 67 void HD44780LCD8bit::sendCmd(const unsigned char cmd) {
hlipka 0:ae5037e3d6e0 68 _rs->write(0);
hlipka 0:ae5037e3d6e0 69 wait_us(1);
hlipka 0:ae5037e3d6e0 70 sendByte(cmd);
hlipka 0:ae5037e3d6e0 71 }
hlipka 0:ae5037e3d6e0 72
hlipka 1:65f72ed914fa 73 void HD44780LCD8bit::sendData(const unsigned char cmd) {
hlipka 0:ae5037e3d6e0 74 _rs->write(1);
hlipka 0:ae5037e3d6e0 75 wait_us(1);
hlipka 0:ae5037e3d6e0 76 sendByte(cmd);
hlipka 0:ae5037e3d6e0 77 }
hlipka 0:ae5037e3d6e0 78
hlipka 0:ae5037e3d6e0 79 HD44780LCD8bit::HD44780LCD8bit
hlipka 2:5ac5bab7daaf 80 (unsigned int columns, unsigned int rows, BusOut *data, PinName enable, PinName rs)
hlipka 2:5ac5bab7daaf 81 :TextLCDBase(columns, rows)
hlipka 0:ae5037e3d6e0 82 {
hlipka 0:ae5037e3d6e0 83 _data=data;
hlipka 0:ae5037e3d6e0 84 _rs=new DigitalOut(rs);
hlipka 0:ae5037e3d6e0 85 _enable=new DigitalOut(enable);
hlipka 0:ae5037e3d6e0 86 _enable->write(0);
hlipka 0:ae5037e3d6e0 87 wait_ms(80);
hlipka 0:ae5037e3d6e0 88 }
hlipka 0:ae5037e3d6e0 89
hlipka 0:ae5037e3d6e0 90 void HD44780LCD8bit::init() {
hlipka 0:ae5037e3d6e0 91 unsigned char initCmd[5]={0x38,0x08,0x01,0x06,0x0c};
hlipka 0:ae5037e3d6e0 92 for (int i=0;i<sizeof(initCmd);i++) {
hlipka 0:ae5037e3d6e0 93 sendCmd(initCmd[i]);
hlipka 0:ae5037e3d6e0 94 wait_ms(50);
hlipka 0:ae5037e3d6e0 95 }
hlipka 0:ae5037e3d6e0 96 }
hlipka 0:ae5037e3d6e0 97
hlipka 1:65f72ed914fa 98 void HD44780LCD8bit::sendByte(const unsigned char byte) {
hlipka 0:ae5037e3d6e0 99 _data->write(byte);
hlipka 0:ae5037e3d6e0 100 _enable->write(1);
hlipka 0:ae5037e3d6e0 101 wait_us(2);
hlipka 0:ae5037e3d6e0 102 _enable->write(0);
hlipka 0:ae5037e3d6e0 103 wait_us(30);
hlipka 0:ae5037e3d6e0 104 }
hlipka 0:ae5037e3d6e0 105