Yoshihiko Nagoya / Mbed 2 deprecated q9_poker

Dependencies:   mbed

Committer:
Ayokura
Date:
Sun Nov 21 15:13:22 2010 +0000
Revision:
1:f46ed1683d87
Parent:
0:234f286e40c7
v2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ayokura 0:234f286e40c7 1 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
Ayokura 0:234f286e40c7 2 * Copyright (c) 2007-2010, sford
Ayokura 0:234f286e40c7 3 *
Ayokura 0:234f286e40c7 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Ayokura 0:234f286e40c7 5 * of this software and associated documentation files (the "Software"), to deal
Ayokura 0:234f286e40c7 6 * in the Software without restriction, including without limitation the rights
Ayokura 0:234f286e40c7 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Ayokura 0:234f286e40c7 8 * copies of the Software, and to permit persons to whom the Software is
Ayokura 0:234f286e40c7 9 * furnished to do so, subject to the following conditions:
Ayokura 0:234f286e40c7 10 *
Ayokura 0:234f286e40c7 11 * The above copyright notice and this permission notice shall be included in
Ayokura 0:234f286e40c7 12 * all copies or substantial portions of the Software.
Ayokura 0:234f286e40c7 13 *
Ayokura 0:234f286e40c7 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Ayokura 0:234f286e40c7 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Ayokura 0:234f286e40c7 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Ayokura 0:234f286e40c7 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Ayokura 0:234f286e40c7 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Ayokura 0:234f286e40c7 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Ayokura 0:234f286e40c7 20 * THE SOFTWARE.
Ayokura 0:234f286e40c7 21 */
Ayokura 0:234f286e40c7 22
Ayokura 0:234f286e40c7 23 #include "TextLCD.h"
Ayokura 0:234f286e40c7 24 #include "mbed.h"
Ayokura 0:234f286e40c7 25
Ayokura 0:234f286e40c7 26 TextLCD::TextLCD(PinName rs, PinName e, PinName d0, PinName d1,
Ayokura 0:234f286e40c7 27 PinName d2, PinName d3, LCDType type) : _rs(rs),
Ayokura 0:234f286e40c7 28 _e(e), _d(d0, d1, d2, d3),
Ayokura 0:234f286e40c7 29 _type(type) {
Ayokura 0:234f286e40c7 30
Ayokura 0:234f286e40c7 31 _e = 1;
Ayokura 0:234f286e40c7 32 _rs = 0; // command mode
Ayokura 0:234f286e40c7 33
Ayokura 0:234f286e40c7 34 wait(0.015); // Wait 15ms to ensure powered up
Ayokura 0:234f286e40c7 35
Ayokura 0:234f286e40c7 36 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
Ayokura 0:234f286e40c7 37 for (int i=0; i<3; i++) {
Ayokura 0:234f286e40c7 38 writeByte(0x3);
Ayokura 0:234f286e40c7 39 wait(0.00164); // this command takes 1.64ms, so wait for it
Ayokura 0:234f286e40c7 40 }
Ayokura 0:234f286e40c7 41 writeByte(0x2); // 4-bit mode
Ayokura 0:234f286e40c7 42 wait(0.000040f); // most instructions take 40us
Ayokura 0:234f286e40c7 43
Ayokura 0:234f286e40c7 44 writeCommand(0x28); // Function set 001 BW N F - -
Ayokura 0:234f286e40c7 45 writeCommand(0x0C);
Ayokura 0:234f286e40c7 46 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
Ayokura 0:234f286e40c7 47 cls();
Ayokura 0:234f286e40c7 48 }
Ayokura 0:234f286e40c7 49
Ayokura 0:234f286e40c7 50 void TextLCD::character(int column, int row, int c) {
Ayokura 0:234f286e40c7 51 int a = address(column, row);
Ayokura 0:234f286e40c7 52 writeCommand(a);
Ayokura 0:234f286e40c7 53 writeData(c);
Ayokura 0:234f286e40c7 54 }
Ayokura 0:234f286e40c7 55
Ayokura 0:234f286e40c7 56 void TextLCD::cls() {
Ayokura 0:234f286e40c7 57 writeCommand(0x01); // cls, and set cursor to 0
Ayokura 0:234f286e40c7 58 wait(0.00164f); // This command takes 1.64 ms
Ayokura 0:234f286e40c7 59 locate(0, 0);
Ayokura 0:234f286e40c7 60 }
Ayokura 0:234f286e40c7 61
Ayokura 0:234f286e40c7 62 void TextLCD::locate(int column, int row) {
Ayokura 0:234f286e40c7 63 _column = column;
Ayokura 0:234f286e40c7 64 _row = row;
Ayokura 0:234f286e40c7 65 }
Ayokura 0:234f286e40c7 66
Ayokura 0:234f286e40c7 67 int TextLCD::_putc(int value) {
Ayokura 0:234f286e40c7 68 if (value == '\n') {
Ayokura 0:234f286e40c7 69 _column = 0;
Ayokura 0:234f286e40c7 70 _row++;
Ayokura 0:234f286e40c7 71 if (_row >= rows()) {
Ayokura 0:234f286e40c7 72 _row = 0;
Ayokura 0:234f286e40c7 73 }
Ayokura 0:234f286e40c7 74 } else {
Ayokura 0:234f286e40c7 75 character(_column, _row, value);
Ayokura 0:234f286e40c7 76 _column++;
Ayokura 0:234f286e40c7 77 if (_column >= columns()) {
Ayokura 0:234f286e40c7 78 _column = 0;
Ayokura 0:234f286e40c7 79 _row++;
Ayokura 0:234f286e40c7 80 if (_row >= rows()) {
Ayokura 0:234f286e40c7 81 _row = 0;
Ayokura 0:234f286e40c7 82 }
Ayokura 0:234f286e40c7 83 }
Ayokura 0:234f286e40c7 84 }
Ayokura 0:234f286e40c7 85 return value;
Ayokura 0:234f286e40c7 86 }
Ayokura 0:234f286e40c7 87
Ayokura 0:234f286e40c7 88 int TextLCD::_getc() {
Ayokura 0:234f286e40c7 89 return -1;
Ayokura 0:234f286e40c7 90 }
Ayokura 0:234f286e40c7 91
Ayokura 0:234f286e40c7 92 void TextLCD::writeByte(int value) {
Ayokura 0:234f286e40c7 93 _d = value >> 4;
Ayokura 0:234f286e40c7 94 wait(0.000040f); // most instructions take 40us
Ayokura 0:234f286e40c7 95 _e = 0;
Ayokura 0:234f286e40c7 96 wait(0.000040f);
Ayokura 0:234f286e40c7 97 _e = 1;
Ayokura 0:234f286e40c7 98 _d = value >> 0;
Ayokura 0:234f286e40c7 99 wait(0.000040f);
Ayokura 0:234f286e40c7 100 _e = 0;
Ayokura 0:234f286e40c7 101 wait(0.000040f); // most instructions take 40us
Ayokura 0:234f286e40c7 102 _e = 1;
Ayokura 0:234f286e40c7 103 }
Ayokura 0:234f286e40c7 104
Ayokura 0:234f286e40c7 105 void TextLCD::writeCommand(int command) {
Ayokura 0:234f286e40c7 106 _rs = 0;
Ayokura 0:234f286e40c7 107 writeByte(command);
Ayokura 0:234f286e40c7 108 }
Ayokura 0:234f286e40c7 109
Ayokura 0:234f286e40c7 110 void TextLCD::writeData(int data) {
Ayokura 0:234f286e40c7 111 _rs = 1;
Ayokura 0:234f286e40c7 112 writeByte(data);
Ayokura 0:234f286e40c7 113 }
Ayokura 0:234f286e40c7 114
Ayokura 0:234f286e40c7 115 int TextLCD::address(int column, int row) {
Ayokura 0:234f286e40c7 116 switch (_type) {
Ayokura 0:234f286e40c7 117 case LCD20x4:
Ayokura 0:234f286e40c7 118 switch (row) {
Ayokura 0:234f286e40c7 119 case 0:
Ayokura 0:234f286e40c7 120 return 0x80 + column;
Ayokura 0:234f286e40c7 121 case 1:
Ayokura 0:234f286e40c7 122 return 0xc0 + column;
Ayokura 0:234f286e40c7 123 case 2:
Ayokura 0:234f286e40c7 124 return 0x94 + column;
Ayokura 0:234f286e40c7 125 case 3:
Ayokura 0:234f286e40c7 126 return 0xd4 + column;
Ayokura 0:234f286e40c7 127 }
Ayokura 0:234f286e40c7 128 case LCD16x2B:
Ayokura 0:234f286e40c7 129 return 0x80 + (row * 40) + column;
Ayokura 0:234f286e40c7 130 case LCD16x2:
Ayokura 0:234f286e40c7 131 case LCD20x2:
Ayokura 0:234f286e40c7 132 default:
Ayokura 0:234f286e40c7 133 return 0x80 + (row * 0x40) + column;
Ayokura 0:234f286e40c7 134 }
Ayokura 0:234f286e40c7 135 }
Ayokura 0:234f286e40c7 136
Ayokura 0:234f286e40c7 137 int TextLCD::columns() {
Ayokura 0:234f286e40c7 138 switch (_type) {
Ayokura 0:234f286e40c7 139 case LCD20x4:
Ayokura 0:234f286e40c7 140 case LCD20x2:
Ayokura 0:234f286e40c7 141 return 20;
Ayokura 0:234f286e40c7 142 case LCD16x2:
Ayokura 0:234f286e40c7 143 case LCD16x2B:
Ayokura 0:234f286e40c7 144 default:
Ayokura 0:234f286e40c7 145 return 16;
Ayokura 0:234f286e40c7 146 }
Ayokura 0:234f286e40c7 147 }
Ayokura 0:234f286e40c7 148
Ayokura 0:234f286e40c7 149 int TextLCD::rows() {
Ayokura 0:234f286e40c7 150 switch (_type) {
Ayokura 0:234f286e40c7 151 case LCD20x4:
Ayokura 0:234f286e40c7 152 return 4;
Ayokura 0:234f286e40c7 153 case LCD16x2:
Ayokura 0:234f286e40c7 154 case LCD16x2B:
Ayokura 0:234f286e40c7 155 case LCD20x2:
Ayokura 0:234f286e40c7 156 default:
Ayokura 0:234f286e40c7 157 return 2;
Ayokura 0:234f286e40c7 158 }
Ayokura 0:234f286e40c7 159 }