2018/3/9 s240メインプログラム用
Fork of TextOLED by
TextOLED.cpp@0:4467cf9395fd, 2012-06-21 (annotated)
- Committer:
- okini3939
- Date:
- Thu Jun 21 02:52:20 2012 +0000
- Revision:
- 0:4467cf9395fd
- Child:
- 1:df8722660f48
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:4467cf9395fd | 1 | /* mbed TextOLED Library, for a 4-bit LCD based on Winstar WEH000000 series |
okini3939 | 0:4467cf9395fd | 2 | * Originl: http://mbed.org/users/simon/libraries/TextLCD/latest |
okini3939 | 0:4467cf9395fd | 3 | * Modified by Suga |
okini3939 | 0:4467cf9395fd | 4 | */ |
okini3939 | 0:4467cf9395fd | 5 | /* mbed TextLCD Library, for a 4-bit LCD based on HD44780 |
okini3939 | 0:4467cf9395fd | 6 | * Copyright (c) 2007-2010, sford, http://mbed.org |
okini3939 | 0:4467cf9395fd | 7 | * |
okini3939 | 0:4467cf9395fd | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
okini3939 | 0:4467cf9395fd | 9 | * of this software and associated documentation files (the "Software"), to deal |
okini3939 | 0:4467cf9395fd | 10 | * in the Software without restriction, including without limitation the rights |
okini3939 | 0:4467cf9395fd | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
okini3939 | 0:4467cf9395fd | 12 | * copies of the Software, and to permit persons to whom the Software is |
okini3939 | 0:4467cf9395fd | 13 | * furnished to do so, subject to the following conditions: |
okini3939 | 0:4467cf9395fd | 14 | * |
okini3939 | 0:4467cf9395fd | 15 | * The above copyright notice and this permission notice shall be included in |
okini3939 | 0:4467cf9395fd | 16 | * all copies or substantial portions of the Software. |
okini3939 | 0:4467cf9395fd | 17 | * |
okini3939 | 0:4467cf9395fd | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
okini3939 | 0:4467cf9395fd | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
okini3939 | 0:4467cf9395fd | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
okini3939 | 0:4467cf9395fd | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
okini3939 | 0:4467cf9395fd | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
okini3939 | 0:4467cf9395fd | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
okini3939 | 0:4467cf9395fd | 24 | * THE SOFTWARE. |
okini3939 | 0:4467cf9395fd | 25 | */ |
okini3939 | 0:4467cf9395fd | 26 | |
okini3939 | 0:4467cf9395fd | 27 | #include "TextOLED.h" |
okini3939 | 0:4467cf9395fd | 28 | #include "mbed.h" |
okini3939 | 0:4467cf9395fd | 29 | |
okini3939 | 0:4467cf9395fd | 30 | TextOLED::TextOLED(PinName rs, PinName e, PinName d4, PinName d5, |
okini3939 | 0:4467cf9395fd | 31 | PinName d6, PinName d7, LCDType type) : _rs(rs), |
okini3939 | 0:4467cf9395fd | 32 | _e(e), _d(d4, d5, d6, d7), |
okini3939 | 0:4467cf9395fd | 33 | _type(type) { |
okini3939 | 0:4467cf9395fd | 34 | |
okini3939 | 0:4467cf9395fd | 35 | _e = 1; |
okini3939 | 0:4467cf9395fd | 36 | _rs = 0; // command mode |
okini3939 | 0:4467cf9395fd | 37 | |
okini3939 | 0:4467cf9395fd | 38 | wait_ms(100); // Wait 15ms to ensure powered up |
okini3939 | 0:4467cf9395fd | 39 | |
okini3939 | 0:4467cf9395fd | 40 | write4bit(0x3); // 8-bit mode |
okini3939 | 0:4467cf9395fd | 41 | write4bit(0x0); // 8-bit mode |
okini3939 | 0:4467cf9395fd | 42 | write4bit(0x2); // 4-bit mode |
okini3939 | 0:4467cf9395fd | 43 | |
okini3939 | 0:4467cf9395fd | 44 | writeCommand(0x13); // DCDC off |
okini3939 | 0:4467cf9395fd | 45 | wait_ms(100); |
okini3939 | 0:4467cf9395fd | 46 | |
okini3939 | 0:4467cf9395fd | 47 | writeCommand(0x28); // Function set 001 BW N F - - |
okini3939 | 0:4467cf9395fd | 48 | writeCommand(0x0C); // 0000 1 D C B |
okini3939 | 0:4467cf9395fd | 49 | writeCommand(0x06); // Entry mode : 0000 01 I/D S |
okini3939 | 0:4467cf9395fd | 50 | cls(); |
okini3939 | 0:4467cf9395fd | 51 | writeCommand(0x02); // Home |
okini3939 | 0:4467cf9395fd | 52 | writeCommand(0x10); // Cursor and Display Shift 0001 S/C R/L 0 0 |
okini3939 | 0:4467cf9395fd | 53 | writeCommand(0x17); // DCDC on |
okini3939 | 0:4467cf9395fd | 54 | wait_ms(100); |
okini3939 | 0:4467cf9395fd | 55 | } |
okini3939 | 0:4467cf9395fd | 56 | |
okini3939 | 0:4467cf9395fd | 57 | void TextOLED::character(int column, int row, int c) { |
okini3939 | 0:4467cf9395fd | 58 | int a = address(column, row); |
okini3939 | 0:4467cf9395fd | 59 | writeCommand(a); |
okini3939 | 0:4467cf9395fd | 60 | writeData(c); |
okini3939 | 0:4467cf9395fd | 61 | } |
okini3939 | 0:4467cf9395fd | 62 | |
okini3939 | 0:4467cf9395fd | 63 | void TextOLED::cls() { |
okini3939 | 0:4467cf9395fd | 64 | writeCommand(0x01); // cls, and set cursor to 0 |
okini3939 | 0:4467cf9395fd | 65 | wait_ms(10); // This command takes 1.64 ms |
okini3939 | 0:4467cf9395fd | 66 | locate(0, 0); |
okini3939 | 0:4467cf9395fd | 67 | } |
okini3939 | 0:4467cf9395fd | 68 | |
okini3939 | 0:4467cf9395fd | 69 | void TextOLED::locate(int column, int row) { |
okini3939 | 0:4467cf9395fd | 70 | _column = column; |
okini3939 | 0:4467cf9395fd | 71 | _row = row; |
okini3939 | 0:4467cf9395fd | 72 | } |
okini3939 | 0:4467cf9395fd | 73 | |
okini3939 | 0:4467cf9395fd | 74 | int TextOLED::_putc(int value) { |
okini3939 | 0:4467cf9395fd | 75 | if (value == '\n') { |
okini3939 | 0:4467cf9395fd | 76 | _column = 0; |
okini3939 | 0:4467cf9395fd | 77 | _row++; |
okini3939 | 0:4467cf9395fd | 78 | if (_row >= rows()) { |
okini3939 | 0:4467cf9395fd | 79 | _row = 0; |
okini3939 | 0:4467cf9395fd | 80 | } |
okini3939 | 0:4467cf9395fd | 81 | } else { |
okini3939 | 0:4467cf9395fd | 82 | character(_column, _row, value); |
okini3939 | 0:4467cf9395fd | 83 | _column++; |
okini3939 | 0:4467cf9395fd | 84 | if (_column >= columns()) { |
okini3939 | 0:4467cf9395fd | 85 | _column = 0; |
okini3939 | 0:4467cf9395fd | 86 | _row++; |
okini3939 | 0:4467cf9395fd | 87 | if (_row >= rows()) { |
okini3939 | 0:4467cf9395fd | 88 | _row = 0; |
okini3939 | 0:4467cf9395fd | 89 | } |
okini3939 | 0:4467cf9395fd | 90 | } |
okini3939 | 0:4467cf9395fd | 91 | } |
okini3939 | 0:4467cf9395fd | 92 | return value; |
okini3939 | 0:4467cf9395fd | 93 | } |
okini3939 | 0:4467cf9395fd | 94 | |
okini3939 | 0:4467cf9395fd | 95 | int TextOLED::_getc() { |
okini3939 | 0:4467cf9395fd | 96 | return -1; |
okini3939 | 0:4467cf9395fd | 97 | } |
okini3939 | 0:4467cf9395fd | 98 | |
okini3939 | 0:4467cf9395fd | 99 | void TextOLED::write4bit(int value) { |
okini3939 | 0:4467cf9395fd | 100 | _d = value; |
okini3939 | 0:4467cf9395fd | 101 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 102 | _e = 0; |
okini3939 | 0:4467cf9395fd | 103 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 104 | _e = 1; |
okini3939 | 0:4467cf9395fd | 105 | wait_ms(10); |
okini3939 | 0:4467cf9395fd | 106 | } |
okini3939 | 0:4467cf9395fd | 107 | |
okini3939 | 0:4467cf9395fd | 108 | void TextOLED::writeByte(int value) { |
okini3939 | 0:4467cf9395fd | 109 | _d = value >> 4; |
okini3939 | 0:4467cf9395fd | 110 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 111 | _e = 0; |
okini3939 | 0:4467cf9395fd | 112 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 113 | _e = 1; |
okini3939 | 0:4467cf9395fd | 114 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 115 | _d = value >> 0; |
okini3939 | 0:4467cf9395fd | 116 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 117 | _e = 0; |
okini3939 | 0:4467cf9395fd | 118 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 119 | _e = 1; |
okini3939 | 0:4467cf9395fd | 120 | wait_us(50); |
okini3939 | 0:4467cf9395fd | 121 | } |
okini3939 | 0:4467cf9395fd | 122 | |
okini3939 | 0:4467cf9395fd | 123 | void TextOLED::writeCommand(int command) { |
okini3939 | 0:4467cf9395fd | 124 | _rs = 0; |
okini3939 | 0:4467cf9395fd | 125 | writeByte(command); |
okini3939 | 0:4467cf9395fd | 126 | } |
okini3939 | 0:4467cf9395fd | 127 | |
okini3939 | 0:4467cf9395fd | 128 | void TextOLED::writeData(int data) { |
okini3939 | 0:4467cf9395fd | 129 | _rs = 1; |
okini3939 | 0:4467cf9395fd | 130 | writeByte(data); |
okini3939 | 0:4467cf9395fd | 131 | } |
okini3939 | 0:4467cf9395fd | 132 | |
okini3939 | 0:4467cf9395fd | 133 | int TextOLED::address(int column, int row) { |
okini3939 | 0:4467cf9395fd | 134 | switch (_type) { |
okini3939 | 0:4467cf9395fd | 135 | case LCD20x4: |
okini3939 | 0:4467cf9395fd | 136 | switch (row) { |
okini3939 | 0:4467cf9395fd | 137 | case 0: |
okini3939 | 0:4467cf9395fd | 138 | return 0x80 + column; |
okini3939 | 0:4467cf9395fd | 139 | case 1: |
okini3939 | 0:4467cf9395fd | 140 | return 0xc0 + column; |
okini3939 | 0:4467cf9395fd | 141 | case 2: |
okini3939 | 0:4467cf9395fd | 142 | return 0x94 + column; |
okini3939 | 0:4467cf9395fd | 143 | case 3: |
okini3939 | 0:4467cf9395fd | 144 | return 0xd4 + column; |
okini3939 | 0:4467cf9395fd | 145 | } |
okini3939 | 0:4467cf9395fd | 146 | case LCD16x2B: |
okini3939 | 0:4467cf9395fd | 147 | return 0x80 + (row * 40) + column; |
okini3939 | 0:4467cf9395fd | 148 | case LCD16x2: |
okini3939 | 0:4467cf9395fd | 149 | case LCD20x2: |
okini3939 | 0:4467cf9395fd | 150 | case LCD8x2: |
okini3939 | 0:4467cf9395fd | 151 | default: |
okini3939 | 0:4467cf9395fd | 152 | return 0x80 + (row * 0x40) + column; |
okini3939 | 0:4467cf9395fd | 153 | } |
okini3939 | 0:4467cf9395fd | 154 | } |
okini3939 | 0:4467cf9395fd | 155 | |
okini3939 | 0:4467cf9395fd | 156 | int TextOLED::columns() { |
okini3939 | 0:4467cf9395fd | 157 | switch (_type) { |
okini3939 | 0:4467cf9395fd | 158 | case LCD8x2: |
okini3939 | 0:4467cf9395fd | 159 | return 8; |
okini3939 | 0:4467cf9395fd | 160 | case LCD20x4: |
okini3939 | 0:4467cf9395fd | 161 | case LCD20x2: |
okini3939 | 0:4467cf9395fd | 162 | return 20; |
okini3939 | 0:4467cf9395fd | 163 | case LCD16x2: |
okini3939 | 0:4467cf9395fd | 164 | case LCD16x2B: |
okini3939 | 0:4467cf9395fd | 165 | default: |
okini3939 | 0:4467cf9395fd | 166 | return 16; |
okini3939 | 0:4467cf9395fd | 167 | } |
okini3939 | 0:4467cf9395fd | 168 | } |
okini3939 | 0:4467cf9395fd | 169 | |
okini3939 | 0:4467cf9395fd | 170 | int TextOLED::rows() { |
okini3939 | 0:4467cf9395fd | 171 | switch (_type) { |
okini3939 | 0:4467cf9395fd | 172 | case LCD20x4: |
okini3939 | 0:4467cf9395fd | 173 | return 4; |
okini3939 | 0:4467cf9395fd | 174 | case LCD16x2: |
okini3939 | 0:4467cf9395fd | 175 | case LCD16x2B: |
okini3939 | 0:4467cf9395fd | 176 | case LCD20x2: |
okini3939 | 0:4467cf9395fd | 177 | case LCD8x2: |
okini3939 | 0:4467cf9395fd | 178 | default: |
okini3939 | 0:4467cf9395fd | 179 | return 2; |
okini3939 | 0:4467cf9395fd | 180 | } |
okini3939 | 0:4467cf9395fd | 181 | } |