This is a remake of tetris game for mbed. Please see detail here http://developer.mbed.org/users/sucrelv/notebook/tetris-game-on-mbed

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem mbed-rtos mbed wave_player

Committer:
sucrelv
Date:
Tue Oct 21 15:10:36 2014 +0000
Revision:
0:3b5e97ab5884
initial upload

Who changed what in which revision?

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