An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01 MOD by Zak
Fork of ACM1602NI by
ACM1602NI.cpp@8:661827681a12, 2014-02-15 (annotated)
- Committer:
- takuo
- Date:
- Sat Feb 15 13:48:55 2014 +0000
- Revision:
- 8:661827681a12
- Parent:
- 7:728c03b52b79
- Child:
- 9:1f671c868ce0
Revised the documents
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takuo | 3:bac80efb323e | 1 | /* An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01 |
takuo | 7:728c03b52b79 | 2 | * Copyright 2013, 2014, Takuo WATANABE (wtakuo) |
takuo | 1:84527dcd3fcc | 3 | * |
takuo | 1:84527dcd3fcc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
takuo | 1:84527dcd3fcc | 5 | * you may not use this file except in compliance with the License. |
takuo | 1:84527dcd3fcc | 6 | * You may obtain a copy of the License at |
takuo | 1:84527dcd3fcc | 7 | * |
takuo | 1:84527dcd3fcc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
takuo | 1:84527dcd3fcc | 9 | * |
takuo | 1:84527dcd3fcc | 10 | * Unless required by applicable law or agreed to in writing, software |
takuo | 1:84527dcd3fcc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
takuo | 1:84527dcd3fcc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
takuo | 1:84527dcd3fcc | 13 | * See the License for the specific language governing permissions and |
takuo | 1:84527dcd3fcc | 14 | * limitations under the License. |
takuo | 1:84527dcd3fcc | 15 | */ |
takuo | 1:84527dcd3fcc | 16 | |
takuo | 0:aac87cbfc229 | 17 | #include "mbed.h" |
takuo | 0:aac87cbfc229 | 18 | #include "ACM1602NI.h" |
takuo | 0:aac87cbfc229 | 19 | |
takuo | 4:33a8ca14e0e0 | 20 | #define I2C_SUCCESS 0 |
takuo | 4:33a8ca14e0e0 | 21 | #define I2C_FAILURE 1 |
takuo | 0:aac87cbfc229 | 22 | |
takuo | 8:661827681a12 | 23 | ACM1602NI::ACM1602NI(PinName sda, PinName scl) : _i2c(sda, scl) { |
takuo | 5:c1dbbc2af9dd | 24 | init(); |
takuo | 5:c1dbbc2af9dd | 25 | } |
takuo | 5:c1dbbc2af9dd | 26 | |
takuo | 8:661827681a12 | 27 | ACM1602NI::ACM1602NI(I2C &i2c) : _i2c(i2c) { |
takuo | 0:aac87cbfc229 | 28 | init(); |
takuo | 0:aac87cbfc229 | 29 | } |
takuo | 0:aac87cbfc229 | 30 | |
takuo | 8:661827681a12 | 31 | void ACM1602NI::init() { |
takuo | 0:aac87cbfc229 | 32 | writeCommand(0x01); |
takuo | 0:aac87cbfc229 | 33 | wait_ms(i2c_command_wait_ms); |
takuo | 0:aac87cbfc229 | 34 | writeCommand(0x38); |
takuo | 0:aac87cbfc229 | 35 | wait_ms(i2c_command_wait_ms); |
takuo | 0:aac87cbfc229 | 36 | writeCommand(0x0f); |
takuo | 0:aac87cbfc229 | 37 | wait_ms(i2c_command_wait_ms); |
takuo | 0:aac87cbfc229 | 38 | writeCommand(0x06); |
takuo | 0:aac87cbfc229 | 39 | wait_ms(i2c_command_wait_ms); |
takuo | 0:aac87cbfc229 | 40 | locate(0, 0); |
takuo | 0:aac87cbfc229 | 41 | } |
takuo | 0:aac87cbfc229 | 42 | |
takuo | 8:661827681a12 | 43 | int ACM1602NI::writeBytes(const char *data, int length, bool repeated) { |
takuo | 0:aac87cbfc229 | 44 | wait_us(i2c_bit_wait_us); |
takuo | 0:aac87cbfc229 | 45 | _i2c.start(); |
takuo | 0:aac87cbfc229 | 46 | for (int i = 0; i < length; i++) { |
takuo | 0:aac87cbfc229 | 47 | wait_us(i2c_bit_wait_us); |
takuo | 0:aac87cbfc229 | 48 | if (_i2c.write(data[i]) != 1) { |
takuo | 0:aac87cbfc229 | 49 | wait_us(i2c_bit_wait_us); |
takuo | 0:aac87cbfc229 | 50 | _i2c.stop(); |
takuo | 4:33a8ca14e0e0 | 51 | return I2C_FAILURE; |
takuo | 0:aac87cbfc229 | 52 | } |
takuo | 0:aac87cbfc229 | 53 | } |
takuo | 0:aac87cbfc229 | 54 | if (!repeated) { |
takuo | 0:aac87cbfc229 | 55 | wait_us(i2c_bit_wait_us); |
takuo | 0:aac87cbfc229 | 56 | _i2c.stop(); |
takuo | 0:aac87cbfc229 | 57 | } |
takuo | 4:33a8ca14e0e0 | 58 | return I2C_SUCCESS; |
takuo | 0:aac87cbfc229 | 59 | } |
takuo | 0:aac87cbfc229 | 60 | |
takuo | 8:661827681a12 | 61 | void ACM1602NI::character(int column, int row, int c) { |
takuo | 4:33a8ca14e0e0 | 62 | writeCommand(address(column, row)); |
takuo | 0:aac87cbfc229 | 63 | writeData(c); |
takuo | 0:aac87cbfc229 | 64 | } |
takuo | 0:aac87cbfc229 | 65 | |
takuo | 8:661827681a12 | 66 | void ACM1602NI::cls() { |
takuo | 0:aac87cbfc229 | 67 | writeCommand(0x01); |
takuo | 0:aac87cbfc229 | 68 | wait_ms(i2c_command_wait_ms); |
takuo | 0:aac87cbfc229 | 69 | locate(0, 0); |
takuo | 0:aac87cbfc229 | 70 | } |
takuo | 0:aac87cbfc229 | 71 | |
takuo | 8:661827681a12 | 72 | void ACM1602NI::locate(int column, int row) { |
takuo | 0:aac87cbfc229 | 73 | _column = column; |
takuo | 0:aac87cbfc229 | 74 | _row = row; |
takuo | 0:aac87cbfc229 | 75 | } |
takuo | 0:aac87cbfc229 | 76 | |
takuo | 8:661827681a12 | 77 | int ACM1602NI::_putc(int value) { |
takuo | 0:aac87cbfc229 | 78 | if (value == '\n') { |
takuo | 0:aac87cbfc229 | 79 | _column = 0; |
takuo | 0:aac87cbfc229 | 80 | _row = (_row + 1) % rows(); |
takuo | 8:661827681a12 | 81 | } |
takuo | 8:661827681a12 | 82 | else { |
takuo | 0:aac87cbfc229 | 83 | character(_column, _row, value); |
takuo | 0:aac87cbfc229 | 84 | _column++; |
takuo | 0:aac87cbfc229 | 85 | if (_column >= columns()) { |
takuo | 0:aac87cbfc229 | 86 | _column = 0; |
takuo | 0:aac87cbfc229 | 87 | _row = (_row + 1) % rows(); |
takuo | 0:aac87cbfc229 | 88 | } |
takuo | 0:aac87cbfc229 | 89 | } |
takuo | 0:aac87cbfc229 | 90 | return value; |
takuo | 0:aac87cbfc229 | 91 | } |
takuo | 0:aac87cbfc229 | 92 | |
takuo | 8:661827681a12 | 93 | int ACM1602NI::_getc() { |
takuo | 0:aac87cbfc229 | 94 | return -1; |
takuo | 0:aac87cbfc229 | 95 | } |
takuo | 0:aac87cbfc229 | 96 | |
takuo | 8:661827681a12 | 97 | void ACM1602NI::writeCommand(char command) { |
takuo | 0:aac87cbfc229 | 98 | char bs[3] = { i2c_addr, 0x00, command }; |
takuo | 0:aac87cbfc229 | 99 | writeBytes(bs, 3); |
takuo | 0:aac87cbfc229 | 100 | } |
takuo | 0:aac87cbfc229 | 101 | |
takuo | 8:661827681a12 | 102 | void ACM1602NI::writeData(char data) { |
takuo | 0:aac87cbfc229 | 103 | char bs[3] = { i2c_addr, 0x80, data }; |
takuo | 0:aac87cbfc229 | 104 | writeBytes(bs, 3); |
takuo | 0:aac87cbfc229 | 105 | } |
takuo | 0:aac87cbfc229 | 106 | |
takuo | 8:661827681a12 | 107 | int ACM1602NI::address(int column, int row) { |
takuo | 0:aac87cbfc229 | 108 | return 0x80 + row * 0x40 + column; |
takuo | 0:aac87cbfc229 | 109 | } |
takuo | 0:aac87cbfc229 | 110 | |
takuo | 8:661827681a12 | 111 | int ACM1602NI::columns() { |
takuo | 0:aac87cbfc229 | 112 | return display_columns; |
takuo | 0:aac87cbfc229 | 113 | } |
takuo | 0:aac87cbfc229 | 114 | |
takuo | 8:661827681a12 | 115 | int ACM1602NI::rows() { |
takuo | 0:aac87cbfc229 | 116 | return display_rows; |
takuo | 0:aac87cbfc229 | 117 | } |