An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01

Dependents:   ACM1602NI_Demo ROBOT_v01 ROBOT_v02 Boku-Transmit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ACM1602NI.cpp Source File

ACM1602NI.cpp

00001 /* An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01
00002  * Copyright 2013, 2014, Takuo WATANABE (wtakuo)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "ACM1602NI.h"
00019 
00020 #define I2C_SUCCESS 0
00021 #define I2C_FAILURE 1
00022 
00023 ACM1602NI::ACM1602NI(PinName sda, PinName scl) : _i2c(sda, scl) {
00024     init();
00025 }
00026 
00027 ACM1602NI::ACM1602NI(I2C &i2c) : _i2c(i2c) {
00028     init();
00029 }
00030 
00031 void ACM1602NI::init() {
00032     writeCommand(0x01);
00033     wait_ms(i2c_command_wait_ms);
00034     writeCommand(0x38);
00035     wait_ms(i2c_command_wait_ms);
00036     writeCommand(0x0f);
00037     wait_ms(i2c_command_wait_ms);
00038     writeCommand(0x06);
00039     wait_ms(i2c_command_wait_ms);
00040     locate(0, 0);
00041 }
00042 
00043 int ACM1602NI::writeBytes(const char *data, int length, bool repeated) {
00044     wait_us(i2c_bit_wait_us);
00045     _i2c.start();
00046     for (int i = 0; i < length; i++) {
00047         wait_us(i2c_bit_wait_us);
00048         if (_i2c.write(data[i]) != 1) {
00049             wait_us(i2c_bit_wait_us);
00050             _i2c.stop();
00051             return I2C_FAILURE;
00052         }
00053     }
00054     if (!repeated) {
00055         wait_us(i2c_bit_wait_us);
00056         _i2c.stop();
00057     }
00058     return I2C_SUCCESS;
00059 }
00060 
00061 void ACM1602NI::character(int column, int row, int c) {
00062     writeCommand(address(column, row));
00063     writeData(c);
00064 }
00065 
00066 void ACM1602NI::cls() {
00067     writeCommand(0x01);
00068     wait_ms(i2c_command_wait_ms);
00069     locate(0, 0);
00070 }
00071 
00072 void ACM1602NI::locate(int column, int row) {
00073     _column = column;
00074     _row = row;
00075 }
00076 
00077 int ACM1602NI::_putc(int value) {
00078     if (value == '\n') {
00079         _column = 0;
00080         _row = (_row + 1) % rows();
00081     }
00082     else {
00083         character(_column, _row, value);
00084         _column++;
00085         if (_column >= columns()) {
00086             _column = 0;
00087             _row = (_row + 1) % rows();
00088         }
00089     }
00090     return value;
00091 }
00092 
00093 int ACM1602NI::_getc() {
00094     return -1;
00095 }
00096 
00097 void ACM1602NI::writeCommand(char command) {
00098     char bs[3] = { i2c_addr, 0x00, command };
00099     writeBytes(bs, 3);
00100 }
00101 
00102 void ACM1602NI::writeData(char data) {
00103     char bs[3] = { i2c_addr, 0x80, data };
00104     writeBytes(bs, 3);
00105 }
00106 
00107 int ACM1602NI::address(int column, int row) {
00108     return 0x80 + row * 0x40 + column;
00109 }
00110 
00111 int ACM1602NI::columns() {
00112     return display_columns;
00113 }
00114 
00115 int ACM1602NI::rows() {
00116     return display_rows;
00117 }