Control a robot over the internet using UDP and a Ethernet interface.

Dependencies:   EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
apatel336
Date:
Thu Oct 17 13:26:38 2013 +0000
Revision:
0:1496281373a5
Initial Release

Who changed what in which revision?

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