WebSocketServer test

Dependencies:   mbed

Committer:
gtk2k
Date:
Sun Apr 29 03:58:08 2012 +0000
Revision:
0:74be48b504a5
WebSocketServer

Who changed what in which revision?

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