Control a robot over the internet using UDP and a Wifly module (WiFi).

Dependencies:   Motor TextLCD WiflyInterface mbed-rtos mbed

Committer:
apatel336
Date:
Thu Oct 17 13:27:56 2013 +0000
Revision:
0:c0dc3a76f3d4
Initial Release

Who changed what in which revision?

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