Fix some dosctrings errors and trailing whitespaces
[contrib/upm.git] / src / lcd / lcm1602.cxx
1 /*
2  * The MIT License (MIT)
3  *
4  * Author: Daniel Mosquera
5  * Copyright (c) 2013 Daniel Mosquera
6  *
7  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
8  * Copyright (c) 2014 Intel Corporation.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy of
11  * this software and associated documentation files (the "Software"), to deal in
12  * the Software without restriction, including without limitation the rights to
13  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
14  * the Software, and to permit persons to whom the Software is furnished to do so,
15  * subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in all
18  * copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
22  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
23  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #include <string>
29 #include <unistd.h>
30
31 #include "lcm1602.h"
32
33 using namespace upm;
34
35 Lcm1602::Lcm1602(int bus_in, int addr_in) : I2CLcd (bus_in, addr_in) {
36     mraa_result_t error = MRAA_SUCCESS;
37
38     usleep(50000);
39     expandWrite(LCD_BACKLIGHT);
40     usleep(100000);
41
42     write4bits(0x03 << 4);
43     usleep(4500);
44     write4bits(0x30);
45     usleep(4500);
46     write4bits(0x30);
47     usleep(150);
48
49     // Put into 4 bit mode
50     write4bits(0x20);
51
52     // Set numeber of lines
53     send(LCD_FUNCTIONSET | 0x0f, 0);
54     send(LCD_DISPLAYCONTROL | LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF, 0);
55     clear();
56
57     // Set entry mode.
58     send(LCD_ENTRYMODESET | LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT, 0);
59
60     home();
61 }
62
63 Lcm1602::~Lcm1602 () {
64
65 }
66
67 /*
68  * **************
69  *  virtual area
70  * **************
71  */
72 mraa_result_t
73 Lcm1602::write (std::string msg) {
74     mraa_result_t error = MRAA_SUCCESS;
75     for (std::string::size_type i = 0; i < msg.size(); ++i) {
76         error = send (msg[i], LCD_RS);
77     }
78     return error;
79 }
80
81 mraa_result_t
82 Lcm1602::setCursor (int row, int column) {
83     mraa_result_t error = MRAA_SUCCESS;
84
85     int row_addr[] = { 0x80, 0xc0, 0x14, 0x54};
86     uint8_t offset = ((column % 16) + row_addr[row]);
87
88     return send (LCD_CMD | offset, 0);
89 }
90
91 mraa_result_t
92 Lcm1602::clear () {
93     return send(LCD_CLEARDISPLAY, 0);
94 }
95
96 mraa_result_t
97 Lcm1602::home () {
98     return send(LCD_RETURNHOME, 0);
99 }
100
101 /*
102  * **************
103  *  private area
104  * **************
105  */
106 mraa_result_t
107 Lcm1602::send (uint8_t value, int mode) {
108     mraa_result_t ret = MRAA_SUCCESS;
109     uint8_t h = value & 0xf0;
110     uint8_t l = (value << 4) & 0xf0;
111     ret = write4bits(h | mode);
112     ret = write4bits(l | mode);
113     return ret;
114 }
115
116 mraa_result_t
117 Lcm1602::write4bits(uint8_t value)
118 {
119     mraa_result_t ret = MRAA_SUCCESS;
120     ret = expandWrite(value);
121     ret = pulseEnable(value);
122     return ret;
123 }
124
125 mraa_result_t
126 Lcm1602::expandWrite(uint8_t value)
127 {
128     uint8_t buffer = value | LCD_BACKLIGHT;
129     return mraa_i2c_write_byte(m_i2c_lcd_control, buffer);
130 }
131
132 mraa_result_t
133 Lcm1602::pulseEnable(uint8_t value)
134 {
135     mraa_result_t ret = MRAA_SUCCESS;
136     ret = expandWrite(value | LCD_EN);
137     usleep(1);
138     ret = expandWrite(value & ~LCD_EN);
139     usleep(50);
140     return ret;
141 }