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  * Contributions: Jon Trulson <jtrulson@ics.com>
11  *                Sergey Kiselev <sergey.kiselev@intel.com>
12  *
13  * Permission is hereby granted, free of uint8_tge, to any person
14  * obtaining a copy of this software and associated documentation
15  * files (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy,
17  * modify, merge, publish, distribute, sublicense, and/or sell copies
18  * of the Software, and to permit persons to whom the Software is
19  * furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #pragma once
35 
36 #include <string>
37 #include <mraa/i2c.hpp>
38 
39 #include <mraa/gpio.hpp>
40 
41 #include "lcd.h"
42 
43 namespace upm
44 {
45 /**
46  * @library i2clcd
47  * @sensor lcm1602
48  * @comname LCM1602 Display
49  * @type display
50  * @man adafruit sparkfun seeed
51  * @web https://www.adafruit.com/datasheets/TC1602A-01T.pdf
52  * @con i2c gpio
53  *
54  * @brief API for the LCM1602 I2C controller for HD44780-based displays
55  *
56  * This supports all sizes of HD44780 displays, from 16x2 to 4x20. The
57  * controller has no idea of the actual display hardware, so it lets you write
58  * farther than you can see. These displays with such controllers are available
59  * from various manufacturers with different I2C addresses. Adafruit*
60  * TC1602A-01T seems to be a well-documented example. The driver also supports
61  * parallel GPIO connections directly to the HD44780 in case you are not using
62  * an I2C expander/backpack.
63  *
64  * @image html lcm1602.jpeg
65  * @snippet lcm1602-lcd.cxx Interesting
66  */
67 class Lcm1602 : public LCD
68 {
69   public:
70     /**
71      * Lcm1602 constructor; calls libmraa initialisation functions
72      *
73      * @param bus I2C bus to use
74      * @param address Slave address the LCD is registered on
75      * @param isExpander True if we are dealing with an I2C expander,
76      * false otherwise. Default is true.
77      */
78   Lcm1602(int bus, int address, bool isExpander=true,
79           uint8_t numColumns = 16, uint8_t numRows = 4);
80 
81     /**
82      * Lcm1602 alternate constructor, used for GPIO based HD44780
83      * controllers supporting RS, Enable, and 4 data pins in 4-bit
84      * mode.
85      *
86      * @param rs Register select pin
87      * @param enable Enable pin
88      * @param d0 Data 0 pin
89      * @param d1 Data 1 pin
90      * @param d2 Data 2 pin
91      * @param d3 Data 3 pin
92      */
93     Lcm1602(uint8_t rs,  uint8_t enable,
94             uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
95             uint8_t numColumns = 16, uint8_t numRows = 4);
96 
97     /**
98      * Lcm1602 destructor
99      */
100     ~Lcm1602();
101     /**
102      * Writes a string to the LCD
103      *
104      * @param msg std::string to write to the display; note: only ASCII
105      * characters are supported
106      * @return Result of the operation
107      */
108     mraa::Result write(std::string msg);
109     /**
110      * Sets the cursor to specified coordinates
111      *
112      * @param row Row to set the cursor to
113      * @param column Column to set the cursor to
114      * @return Result of the operation
115      */
116     mraa::Result setCursor(int row, int column);
117     /**
118      * Clears the display of all characters
119      *
120      * @return Result of the operation
121      */
122     mraa::Result clear();
123     /**
124      * Returns to the original coordinates (0,0)
125      *
126      * @return Result of the operation
127      */
128     mraa::Result home();
129 
130     /**
131      * Create a custom character
132      *
133      * @param charSlot the character slot to write, only 8 are available
134      * @param charData The character data (8 bytes) making up the character
135      * @return Result of operation
136      */
137     mraa::Result createChar(uint8_t charSlot, uint8_t charData[]);
138 
139     /**
140      * Turn the display on
141      *
142      * @return Result of operation
143      */
144     mraa::Result displayOn();
145 
146     /**
147      * Turn the display off
148      *
149      * @return Result of operation
150      */
151     mraa::Result displayOff();
152 
153     /**
154      * Turn the cursor on
155      *
156      * @return Result of operation
157      */
158     mraa::Result cursorOn();
159 
160     /**
161      * Turn the cursor off
162      *
163      * @return Result of operation
164      */
165     mraa::Result cursorOff();
166 
167     /**
168      * Turn cursor blink on
169      *
170      * @return Result of operation
171      */
172     mraa::Result cursorBlinkOn();
173 
174     /**
175      * Turn cursor blink off
176      *
177      * @return Result of operation
178      */
179     mraa::Result cursorBlinkOff();
180 
181     /**
182      * Scroll the display left, without changing the character RAM
183      *
184      * @return Result of operation
185      */
186     mraa::Result scrollDisplayLeft();
187 
188     /**
189      * Scroll the display right, without changing the character RAM
190      *
191      * @return Result of operation
192      */
193     mraa::Result scrollDisplayRight();
194 
195     /**
196      * set the entry mode so that characters are added left to right
197      *
198      * @return Result of operation
199      */
200     mraa::Result entryLeftToRight();
201 
202     /**
203      * set the entry mode so that characters are added right to left
204      *
205      * @return Result of operation
206      */
207     mraa::Result entryRightToLeft();
208 
209     /**
210      * Right justify text entered from the cursor
211      *
212      * @return Result of operation
213      */
214     mraa::Result autoscrollOn();
215 
216     /**
217      * Left justify text entered from the cursor
218      *
219      * @return Result of operation
220      */
221     mraa::Result autoscrollOff();
222 
223 
224   protected:
225     mraa::Result send(uint8_t value, int mode);
226     mraa::Result write4bits(uint8_t value);
227     mraa::Result expandWrite(uint8_t value);
228     mraa::Result pulseEnable(uint8_t value);
229 
230     uint8_t m_displayControl;
231     uint8_t m_entryDisplayMode;
232 
233     // Display size
234     uint8_t m_numColumns;
235     uint8_t m_numRows;
236 
237     // Add a command() and data() virtual member functions, with a
238     // default implementation in lcm1602.  This is expected to be
239     // implemented by derived classes with different needs (Jhd1313m1,
240     // for example).
241     virtual mraa::Result command(uint8_t cmd);
242     virtual mraa::Result data(uint8_t data);
243 
244     int m_lcd_control_address;
245     mraa::I2c* m_i2c_lcd_control;
246 
247   private:
248 
249     // true if using i2c, false otherwise (gpio)
250     bool m_isI2C;
251 
252     // gpio operation
253     mraa::Gpio* m_gpioRS;
254     mraa::Gpio* m_gpioEnable;
255     mraa::Gpio* m_gpioD0;
256     mraa::Gpio* m_gpioD1;
257     mraa::Gpio* m_gpioD2;
258     mraa::Gpio* m_gpioD3;
259 };
260 }
261