PCF2119 library for I/O board
Fork of PCF2119 by
PCF2119.cpp@9:27f430d086f3, 2014-07-09 (annotated)
- Committer:
- okini3939
- Date:
- Wed Jul 09 03:54:42 2014 +0000
- Revision:
- 9:27f430d086f3
- Parent:
- TextLCD.cpp@7:44f34c09bd37
1st build;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 9:27f430d086f3 | 1 | /* PCF2119x based I2C LCD Library |
okini3939 | 9:27f430d086f3 | 2 | * Copyright (c) 2014, Hiroshi Suga |
okini3939 | 9:27f430d086f3 | 3 | */ |
simon | 1:ac48b187213c | 4 | /* mbed TextLCD Library, for a 4-bit LCD based on HD44780 |
simon | 6:e4cb7ddee0d3 | 5 | * Copyright (c) 2007-2010, sford, http://mbed.org |
simon | 1:ac48b187213c | 6 | * |
simon | 1:ac48b187213c | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
simon | 1:ac48b187213c | 8 | * of this software and associated documentation files (the "Software"), to deal |
simon | 1:ac48b187213c | 9 | * in the Software without restriction, including without limitation the rights |
simon | 1:ac48b187213c | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
simon | 1:ac48b187213c | 11 | * copies of the Software, and to permit persons to whom the Software is |
simon | 1:ac48b187213c | 12 | * furnished to do so, subject to the following conditions: |
simon | 1:ac48b187213c | 13 | * |
simon | 1:ac48b187213c | 14 | * The above copyright notice and this permission notice shall be included in |
simon | 1:ac48b187213c | 15 | * all copies or substantial portions of the Software. |
simon | 1:ac48b187213c | 16 | * |
simon | 1:ac48b187213c | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
simon | 1:ac48b187213c | 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
simon | 1:ac48b187213c | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
simon | 1:ac48b187213c | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
simon | 1:ac48b187213c | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
simon | 1:ac48b187213c | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
simon | 1:ac48b187213c | 23 | * THE SOFTWARE. |
simon | 1:ac48b187213c | 24 | */ |
simon | 1:ac48b187213c | 25 | |
okini3939 | 9:27f430d086f3 | 26 | #include "PCF2119.h" |
simon | 1:ac48b187213c | 27 | #include "mbed.h" |
simon | 1:ac48b187213c | 28 | |
okini3939 | 9:27f430d086f3 | 29 | PCF2119::PCF2119(PinName sda, PinName sck, PinName reset, LCDType type, char addr) : |
okini3939 | 9:27f430d086f3 | 30 | _i2c(sda, sck), _reset(reset), _type(type), _addr(addr) { |
okini3939 | 9:27f430d086f3 | 31 | init(); |
okini3939 | 9:27f430d086f3 | 32 | } |
simon | 1:ac48b187213c | 33 | |
okini3939 | 9:27f430d086f3 | 34 | PCF2119::PCF2119(I2C &i2c, PinName reset, LCDType type, char addr) : |
okini3939 | 9:27f430d086f3 | 35 | _i2c(i2c), _reset(reset), _type(type), _addr(addr) { |
okini3939 | 9:27f430d086f3 | 36 | init(); |
okini3939 | 9:27f430d086f3 | 37 | } |
simon | 1:ac48b187213c | 38 | |
okini3939 | 9:27f430d086f3 | 39 | void PCF2119::init() { |
okini3939 | 9:27f430d086f3 | 40 | unsigned char buf[12]; |
okini3939 | 9:27f430d086f3 | 41 | |
okini3939 | 9:27f430d086f3 | 42 | _reset = 1; |
okini3939 | 9:27f430d086f3 | 43 | wait(0.001); |
okini3939 | 9:27f430d086f3 | 44 | _reset = 0; |
simon | 1:ac48b187213c | 45 | wait(0.015); // Wait 15ms to ensure powered up |
simon | 1:ac48b187213c | 46 | |
okini3939 | 9:27f430d086f3 | 47 | raw = 0; |
okini3939 | 9:27f430d086f3 | 48 | buf[0] = 0x00; // CO=0, RS=0 |
okini3939 | 9:27f430d086f3 | 49 | buf[1] = 0x34; // Function_set DL=1, M=1, SL=0, (H=0) |
okini3939 | 9:27f430d086f3 | 50 | buf[2] = 0x0c; // Display_ctl D=1, C=1, B=0 |
okini3939 | 9:27f430d086f3 | 51 | buf[3] = 0x06; // Entry_mode_set ID=1, S=0 |
okini3939 | 9:27f430d086f3 | 52 | buf[4] = 0x35; // Function_set DL=1, M=1, SL=0, (H=1) |
okini3939 | 9:27f430d086f3 | 53 | buf[5] = 0x04; // Disp_conf P=0, Q=0 |
okini3939 | 9:27f430d086f3 | 54 | buf[6] = 0x10; // Temp_ctl TC1=0, TC2=0 |
okini3939 | 9:27f430d086f3 | 55 | buf[7] = 0x42; // HV_gen S1=1, S2=0 |
okini3939 | 9:27f430d086f3 | 56 | buf[8] = 0x9f; // VLCD_set V=0, VA=1f |
okini3939 | 9:27f430d086f3 | 57 | buf[9] = 0x34; // Function_set DL=1, M=1, SL=0, (H=0) |
okini3939 | 9:27f430d086f3 | 58 | buf[10] = 0x80; // set DDRAM |
okini3939 | 9:27f430d086f3 | 59 | buf[11] = 0x02; // Screen_conf L=0 |
okini3939 | 9:27f430d086f3 | 60 | _i2c.write(_addr, (char*)buf, 12); |
simon | 1:ac48b187213c | 61 | cls(); |
simon | 1:ac48b187213c | 62 | } |
simon | 1:ac48b187213c | 63 | |
okini3939 | 9:27f430d086f3 | 64 | void PCF2119::character(int column, int row, int c) { |
okini3939 | 9:27f430d086f3 | 65 | unsigned char buf[2]; |
okini3939 | 9:27f430d086f3 | 66 | buf[0] = 0x00; // CO=0, RS=0 |
okini3939 | 9:27f430d086f3 | 67 | buf[1] = address(column, row); |
okini3939 | 9:27f430d086f3 | 68 | _i2c.write(_addr, (char*)buf, 2); |
simon | 1:ac48b187213c | 69 | writeData(c); |
simon | 1:ac48b187213c | 70 | } |
simon | 1:ac48b187213c | 71 | |
okini3939 | 9:27f430d086f3 | 72 | void PCF2119::cls() { |
okini3939 | 9:27f430d086f3 | 73 | char buf[2]; |
okini3939 | 9:27f430d086f3 | 74 | buf[0] = 0x00; // CO=0, RS=0 |
okini3939 | 9:27f430d086f3 | 75 | buf[1] = 0x01; |
okini3939 | 9:27f430d086f3 | 76 | _i2c.write(_addr, buf, 2); |
simon | 1:ac48b187213c | 77 | locate(0, 0); |
simon | 1:ac48b187213c | 78 | } |
simon | 1:ac48b187213c | 79 | |
okini3939 | 9:27f430d086f3 | 80 | void PCF2119::locate(int column, int row) { |
simon | 1:ac48b187213c | 81 | _column = column; |
simon | 1:ac48b187213c | 82 | _row = row; |
simon | 1:ac48b187213c | 83 | } |
simon | 1:ac48b187213c | 84 | |
okini3939 | 9:27f430d086f3 | 85 | int PCF2119::_putc(int value) { |
simon | 1:ac48b187213c | 86 | if (value == '\n') { |
simon | 1:ac48b187213c | 87 | _column = 0; |
simon | 1:ac48b187213c | 88 | _row++; |
simon | 1:ac48b187213c | 89 | if (_row >= rows()) { |
simon | 1:ac48b187213c | 90 | _row = 0; |
simon | 1:ac48b187213c | 91 | } |
simon | 1:ac48b187213c | 92 | } else { |
okini3939 | 9:27f430d086f3 | 93 | character(_column, _row, ascii2lcd(value)); |
simon | 1:ac48b187213c | 94 | _column++; |
simon | 1:ac48b187213c | 95 | if (_column >= columns()) { |
simon | 1:ac48b187213c | 96 | _column = 0; |
simon | 1:ac48b187213c | 97 | _row++; |
simon | 1:ac48b187213c | 98 | if (_row >= rows()) { |
simon | 1:ac48b187213c | 99 | _row = 0; |
simon | 1:ac48b187213c | 100 | } |
simon | 1:ac48b187213c | 101 | } |
simon | 1:ac48b187213c | 102 | } |
simon | 1:ac48b187213c | 103 | return value; |
simon | 1:ac48b187213c | 104 | } |
simon | 1:ac48b187213c | 105 | |
okini3939 | 9:27f430d086f3 | 106 | int PCF2119::_getc() { |
simon | 1:ac48b187213c | 107 | return -1; |
simon | 1:ac48b187213c | 108 | } |
simon | 1:ac48b187213c | 109 | |
okini3939 | 9:27f430d086f3 | 110 | void PCF2119::writeCommand(int command) { |
okini3939 | 9:27f430d086f3 | 111 | unsigned char buf[2]; |
okini3939 | 9:27f430d086f3 | 112 | buf[0] = 0x00; // CO=0, RS=0 |
okini3939 | 9:27f430d086f3 | 113 | buf[1] = command; |
okini3939 | 9:27f430d086f3 | 114 | _i2c.write(_addr, (char*)buf, 2); |
simon | 1:ac48b187213c | 115 | } |
simon | 1:ac48b187213c | 116 | |
okini3939 | 9:27f430d086f3 | 117 | void PCF2119::writeData(int data) { |
okini3939 | 9:27f430d086f3 | 118 | unsigned char buf[2]; |
okini3939 | 9:27f430d086f3 | 119 | buf[0] = 0x40; // CO=0, RS=1 |
okini3939 | 9:27f430d086f3 | 120 | buf[1] = data; |
okini3939 | 9:27f430d086f3 | 121 | _i2c.write(_addr, (char*)buf, 2); |
simon | 1:ac48b187213c | 122 | } |
simon | 1:ac48b187213c | 123 | |
okini3939 | 9:27f430d086f3 | 124 | int PCF2119::address(int column, int row) { |
simon | 1:ac48b187213c | 125 | switch (_type) { |
simon | 1:ac48b187213c | 126 | case LCD20x4: |
simon | 1:ac48b187213c | 127 | switch (row) { |
simon | 1:ac48b187213c | 128 | case 0: |
simon | 1:ac48b187213c | 129 | return 0x80 + column; |
simon | 1:ac48b187213c | 130 | case 1: |
simon | 1:ac48b187213c | 131 | return 0xc0 + column; |
simon | 1:ac48b187213c | 132 | case 2: |
simon | 1:ac48b187213c | 133 | return 0x94 + column; |
simon | 1:ac48b187213c | 134 | case 3: |
simon | 1:ac48b187213c | 135 | return 0xd4 + column; |
simon | 1:ac48b187213c | 136 | } |
simon | 1:ac48b187213c | 137 | case LCD16x2B: |
simon | 4:bf5b706f8d32 | 138 | return 0x80 + (row * 40) + column; |
simon | 1:ac48b187213c | 139 | case LCD16x2: |
simon | 1:ac48b187213c | 140 | case LCD20x2: |
simon | 1:ac48b187213c | 141 | default: |
simon | 4:bf5b706f8d32 | 142 | return 0x80 + (row * 0x40) + column; |
simon | 1:ac48b187213c | 143 | } |
simon | 1:ac48b187213c | 144 | } |
simon | 1:ac48b187213c | 145 | |
okini3939 | 9:27f430d086f3 | 146 | int PCF2119::columns() { |
simon | 1:ac48b187213c | 147 | switch (_type) { |
simon | 1:ac48b187213c | 148 | case LCD20x4: |
simon | 1:ac48b187213c | 149 | case LCD20x2: |
simon | 1:ac48b187213c | 150 | return 20; |
simon | 1:ac48b187213c | 151 | case LCD16x2: |
simon | 1:ac48b187213c | 152 | case LCD16x2B: |
simon | 1:ac48b187213c | 153 | default: |
simon | 1:ac48b187213c | 154 | return 16; |
simon | 1:ac48b187213c | 155 | } |
simon | 1:ac48b187213c | 156 | } |
simon | 1:ac48b187213c | 157 | |
okini3939 | 9:27f430d086f3 | 158 | int PCF2119::rows() { |
simon | 1:ac48b187213c | 159 | switch (_type) { |
simon | 1:ac48b187213c | 160 | case LCD20x4: |
simon | 1:ac48b187213c | 161 | return 4; |
simon | 1:ac48b187213c | 162 | case LCD16x2: |
simon | 1:ac48b187213c | 163 | case LCD16x2B: |
simon | 1:ac48b187213c | 164 | case LCD20x2: |
simon | 1:ac48b187213c | 165 | default: |
simon | 1:ac48b187213c | 166 | return 2; |
simon | 1:ac48b187213c | 167 | } |
simon | 1:ac48b187213c | 168 | } |
okini3939 | 9:27f430d086f3 | 169 | |
okini3939 | 9:27f430d086f3 | 170 | int PCF2119::ascii2lcd (int c) { |
okini3939 | 9:27f430d086f3 | 171 | if (raw) return c; |
okini3939 | 9:27f430d086f3 | 172 | if (((c >= ' ') && (c <= '?')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) { |
okini3939 | 9:27f430d086f3 | 173 | c |= 0x80; |
okini3939 | 9:27f430d086f3 | 174 | } else |
okini3939 | 9:27f430d086f3 | 175 | if (c >= 0xf0 && c <= 0xff) { |
okini3939 | 9:27f430d086f3 | 176 | c &= 0x0f; |
okini3939 | 9:27f430d086f3 | 177 | } |
okini3939 | 9:27f430d086f3 | 178 | return c; |
okini3939 | 9:27f430d086f3 | 179 | } |
okini3939 | 9:27f430d086f3 | 180 | |
okini3939 | 9:27f430d086f3 | 181 | void PCF2119::cgram(int num, const char *data, int len) { |
okini3939 | 9:27f430d086f3 | 182 | int i; |
okini3939 | 9:27f430d086f3 | 183 | unsigned char buf[1 + len]; |
okini3939 | 9:27f430d086f3 | 184 | buf[0] = 0x00; // CO=0, RS=0 |
okini3939 | 9:27f430d086f3 | 185 | buf[1] = 0x80 | (num >= 8 ? 0x40 : 0); |
okini3939 | 9:27f430d086f3 | 186 | _i2c.write(_addr, (char*)buf, 2); |
okini3939 | 9:27f430d086f3 | 187 | buf[0] = 0x00; // CO=0, RS=0 |
okini3939 | 9:27f430d086f3 | 188 | buf[1] = 0x40 | (8 * (num % 8)); |
okini3939 | 9:27f430d086f3 | 189 | _i2c.write(_addr, (char*)buf, 2); |
okini3939 | 9:27f430d086f3 | 190 | buf[0] = 0x40; // CO=0, RS=1 |
okini3939 | 9:27f430d086f3 | 191 | for (i = 0; i < len; i ++) { |
okini3939 | 9:27f430d086f3 | 192 | buf[1 + i] = data[i]; |
okini3939 | 9:27f430d086f3 | 193 | } |
okini3939 | 9:27f430d086f3 | 194 | _i2c.write(_addr, (char*)buf, 1 + len); |
okini3939 | 9:27f430d086f3 | 195 | } |