2018/2/15変更
Dependents: GPS_Test05 All_system_kai2
Fork of TextOLED by
TextOLED.cpp@3:aeb0dd496d0f, 2018-02-15 (annotated)
- Committer:
- basyo38
- Date:
- Thu Feb 15 05:03:10 2018 +0000
- Revision:
- 3:aeb0dd496d0f
- Parent:
- 2:45788bd06d4c
TextOLED??????????
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 | 2:45788bd06d4c | 34 | int i; |
okini3939 | 0:4467cf9395fd | 35 | |
okini3939 | 2:45788bd06d4c | 36 | _rs = 0; // command mode |
okini3939 | 0:4467cf9395fd | 37 | _e = 1; |
okini3939 | 0:4467cf9395fd | 38 | |
okini3939 | 0:4467cf9395fd | 39 | wait_ms(100); // Wait 15ms to ensure powered up |
okini3939 | 0:4467cf9395fd | 40 | |
okini3939 | 2:45788bd06d4c | 41 | for (i = 0; i < 5; i ++) { |
okini3939 | 2:45788bd06d4c | 42 | write4bit(0); |
okini3939 | 2:45788bd06d4c | 43 | } |
okini3939 | 2:45788bd06d4c | 44 | /* |
okini3939 | 0:4467cf9395fd | 45 | write4bit(0x3); // 8-bit mode |
okini3939 | 2:45788bd06d4c | 46 | write4bit(0x3); // 8-bit mode |
okini3939 | 2:45788bd06d4c | 47 | write4bit(0x0); |
okini3939 | 2:45788bd06d4c | 48 | */ |
okini3939 | 0:4467cf9395fd | 49 | write4bit(0x2); // 4-bit mode |
okini3939 | 0:4467cf9395fd | 50 | |
okini3939 | 0:4467cf9395fd | 51 | writeCommand(0x28); // Function set 001 BW N F - - |
okini3939 | 0:4467cf9395fd | 52 | writeCommand(0x0C); // 0000 1 D C B |
okini3939 | 0:4467cf9395fd | 53 | writeCommand(0x06); // Entry mode : 0000 01 I/D S |
okini3939 | 0:4467cf9395fd | 54 | writeCommand(0x10); // Cursor and Display Shift 0001 S/C R/L 0 0 |
okini3939 | 0:4467cf9395fd | 55 | writeCommand(0x17); // DCDC on |
okini3939 | 2:45788bd06d4c | 56 | wait_ms(10); |
okini3939 | 2:45788bd06d4c | 57 | writeCommand(0x02); // Home |
okini3939 | 2:45788bd06d4c | 58 | cls(); |
okini3939 | 0:4467cf9395fd | 59 | } |
okini3939 | 0:4467cf9395fd | 60 | |
okini3939 | 0:4467cf9395fd | 61 | void TextOLED::character(int column, int row, int c) { |
okini3939 | 0:4467cf9395fd | 62 | int a = address(column, row); |
okini3939 | 0:4467cf9395fd | 63 | writeCommand(a); |
okini3939 | 0:4467cf9395fd | 64 | writeData(c); |
okini3939 | 0:4467cf9395fd | 65 | } |
okini3939 | 0:4467cf9395fd | 66 | |
okini3939 | 0:4467cf9395fd | 67 | void TextOLED::cls() { |
okini3939 | 0:4467cf9395fd | 68 | writeCommand(0x01); // cls, and set cursor to 0 |
okini3939 | 0:4467cf9395fd | 69 | wait_ms(10); // This command takes 1.64 ms |
okini3939 | 0:4467cf9395fd | 70 | locate(0, 0); |
okini3939 | 0:4467cf9395fd | 71 | } |
okini3939 | 0:4467cf9395fd | 72 | |
okini3939 | 0:4467cf9395fd | 73 | void TextOLED::locate(int column, int row) { |
okini3939 | 0:4467cf9395fd | 74 | _column = column; |
okini3939 | 0:4467cf9395fd | 75 | _row = row; |
okini3939 | 0:4467cf9395fd | 76 | } |
okini3939 | 0:4467cf9395fd | 77 | |
okini3939 | 0:4467cf9395fd | 78 | int TextOLED::_putc(int value) { |
okini3939 | 0:4467cf9395fd | 79 | if (value == '\n') { |
okini3939 | 0:4467cf9395fd | 80 | _column = 0; |
okini3939 | 0:4467cf9395fd | 81 | _row++; |
okini3939 | 0:4467cf9395fd | 82 | if (_row >= rows()) { |
okini3939 | 0:4467cf9395fd | 83 | _row = 0; |
okini3939 | 0:4467cf9395fd | 84 | } |
okini3939 | 0:4467cf9395fd | 85 | } else { |
okini3939 | 0:4467cf9395fd | 86 | character(_column, _row, value); |
okini3939 | 0:4467cf9395fd | 87 | _column++; |
okini3939 | 0:4467cf9395fd | 88 | if (_column >= columns()) { |
okini3939 | 0:4467cf9395fd | 89 | _column = 0; |
okini3939 | 0:4467cf9395fd | 90 | _row++; |
okini3939 | 0:4467cf9395fd | 91 | if (_row >= rows()) { |
okini3939 | 0:4467cf9395fd | 92 | _row = 0; |
okini3939 | 0:4467cf9395fd | 93 | } |
okini3939 | 0:4467cf9395fd | 94 | } |
okini3939 | 0:4467cf9395fd | 95 | } |
okini3939 | 0:4467cf9395fd | 96 | return value; |
okini3939 | 0:4467cf9395fd | 97 | } |
okini3939 | 0:4467cf9395fd | 98 | |
okini3939 | 0:4467cf9395fd | 99 | int TextOLED::_getc() { |
okini3939 | 0:4467cf9395fd | 100 | return -1; |
okini3939 | 0:4467cf9395fd | 101 | } |
okini3939 | 0:4467cf9395fd | 102 | |
okini3939 | 0:4467cf9395fd | 103 | void TextOLED::write4bit(int value) { |
okini3939 | 2:45788bd06d4c | 104 | _e = 1; |
okini3939 | 0:4467cf9395fd | 105 | _d = value; |
okini3939 | 0:4467cf9395fd | 106 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 107 | _e = 0; |
okini3939 | 0:4467cf9395fd | 108 | wait_ms(10); |
okini3939 | 0:4467cf9395fd | 109 | } |
okini3939 | 0:4467cf9395fd | 110 | |
okini3939 | 0:4467cf9395fd | 111 | void TextOLED::writeByte(int value) { |
okini3939 | 2:45788bd06d4c | 112 | _e = 1; |
okini3939 | 2:45788bd06d4c | 113 | _d = (value >> 4) & 0x0f; |
okini3939 | 0:4467cf9395fd | 114 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 115 | _e = 0; |
okini3939 | 0:4467cf9395fd | 116 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 117 | _e = 1; |
okini3939 | 2:45788bd06d4c | 118 | _d = value & 0x0f; |
okini3939 | 0:4467cf9395fd | 119 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 120 | _e = 0; |
okini3939 | 0:4467cf9395fd | 121 | wait_us(50); |
okini3939 | 0:4467cf9395fd | 122 | } |
okini3939 | 0:4467cf9395fd | 123 | |
okini3939 | 0:4467cf9395fd | 124 | void TextOLED::writeCommand(int command) { |
okini3939 | 0:4467cf9395fd | 125 | _rs = 0; |
okini3939 | 2:45788bd06d4c | 126 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 127 | writeByte(command); |
okini3939 | 0:4467cf9395fd | 128 | } |
okini3939 | 0:4467cf9395fd | 129 | |
okini3939 | 0:4467cf9395fd | 130 | void TextOLED::writeData(int data) { |
okini3939 | 0:4467cf9395fd | 131 | _rs = 1; |
okini3939 | 2:45788bd06d4c | 132 | wait_us(1); |
okini3939 | 0:4467cf9395fd | 133 | writeByte(data); |
okini3939 | 0:4467cf9395fd | 134 | } |
okini3939 | 0:4467cf9395fd | 135 | |
okini3939 | 0:4467cf9395fd | 136 | int TextOLED::address(int column, int row) { |
okini3939 | 0:4467cf9395fd | 137 | switch (_type) { |
okini3939 | 0:4467cf9395fd | 138 | case LCD20x4: |
okini3939 | 0:4467cf9395fd | 139 | switch (row) { |
okini3939 | 0:4467cf9395fd | 140 | case 0: |
okini3939 | 0:4467cf9395fd | 141 | return 0x80 + column; |
okini3939 | 0:4467cf9395fd | 142 | case 1: |
okini3939 | 0:4467cf9395fd | 143 | return 0xc0 + column; |
okini3939 | 0:4467cf9395fd | 144 | case 2: |
okini3939 | 0:4467cf9395fd | 145 | return 0x94 + column; |
okini3939 | 0:4467cf9395fd | 146 | case 3: |
okini3939 | 0:4467cf9395fd | 147 | return 0xd4 + column; |
okini3939 | 0:4467cf9395fd | 148 | } |
okini3939 | 0:4467cf9395fd | 149 | case LCD16x2B: |
okini3939 | 0:4467cf9395fd | 150 | return 0x80 + (row * 40) + column; |
okini3939 | 0:4467cf9395fd | 151 | case LCD16x2: |
okini3939 | 0:4467cf9395fd | 152 | case LCD20x2: |
okini3939 | 0:4467cf9395fd | 153 | case LCD8x2: |
okini3939 | 0:4467cf9395fd | 154 | default: |
okini3939 | 0:4467cf9395fd | 155 | return 0x80 + (row * 0x40) + column; |
okini3939 | 0:4467cf9395fd | 156 | } |
okini3939 | 0:4467cf9395fd | 157 | } |
okini3939 | 0:4467cf9395fd | 158 | |
okini3939 | 0:4467cf9395fd | 159 | int TextOLED::columns() { |
okini3939 | 0:4467cf9395fd | 160 | switch (_type) { |
okini3939 | 0:4467cf9395fd | 161 | case LCD8x2: |
okini3939 | 0:4467cf9395fd | 162 | return 8; |
okini3939 | 0:4467cf9395fd | 163 | case LCD20x4: |
okini3939 | 0:4467cf9395fd | 164 | case LCD20x2: |
okini3939 | 0:4467cf9395fd | 165 | return 20; |
okini3939 | 0:4467cf9395fd | 166 | case LCD16x2: |
okini3939 | 0:4467cf9395fd | 167 | case LCD16x2B: |
okini3939 | 0:4467cf9395fd | 168 | default: |
okini3939 | 0:4467cf9395fd | 169 | return 16; |
okini3939 | 0:4467cf9395fd | 170 | } |
okini3939 | 0:4467cf9395fd | 171 | } |
okini3939 | 0:4467cf9395fd | 172 | |
okini3939 | 0:4467cf9395fd | 173 | int TextOLED::rows() { |
okini3939 | 0:4467cf9395fd | 174 | switch (_type) { |
okini3939 | 0:4467cf9395fd | 175 | case LCD20x4: |
okini3939 | 0:4467cf9395fd | 176 | return 4; |
okini3939 | 0:4467cf9395fd | 177 | case LCD16x2: |
okini3939 | 0:4467cf9395fd | 178 | case LCD16x2B: |
okini3939 | 0:4467cf9395fd | 179 | case LCD20x2: |
okini3939 | 0:4467cf9395fd | 180 | case LCD8x2: |
okini3939 | 0:4467cf9395fd | 181 | default: |
okini3939 | 0:4467cf9395fd | 182 | return 2; |
okini3939 | 0:4467cf9395fd | 183 | } |
okini3939 | 0:4467cf9395fd | 184 | } |
okini3939 | 1:df8722660f48 | 185 | |
okini3939 | 1:df8722660f48 | 186 | void TextOLED::cursor (bool flg) { |
okini3939 | 1:df8722660f48 | 187 | if (flg) { |
okini3939 | 1:df8722660f48 | 188 | writeCommand(0x0F); // 0000 1 D C B |
okini3939 | 1:df8722660f48 | 189 | } else { |
okini3939 | 1:df8722660f48 | 190 | writeCommand(0x0C); // 0000 1 D C B |
okini3939 | 1:df8722660f48 | 191 | } |
okini3939 | 1:df8722660f48 | 192 | } |