1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Contributions: Jon Trulson <jtrulson@ics.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #include <iostream>
28 #include <stdexcept>
29 #include <unistd.h>
30 
31 #include "lcd_private.h"
32 #include "hd44780_bits.h"
33 #include "jhd1313m1.h"
34 
35 using namespace upm;
36 
Jhd1313m1(int bus,int lcdAddress,int rgbAddress)37 Jhd1313m1::Jhd1313m1(int bus, int lcdAddress, int rgbAddress)
38   : m_i2c_lcd_rgb(bus), Lcm1602(bus, lcdAddress, false)
39 {
40     m_rgb_address = rgbAddress;
41     m_name = "Jhd1313m1";
42 
43     mraa::Result ret = m_i2c_lcd_rgb.address(m_rgb_address);
44     if (ret != mraa::SUCCESS) {
45         throw std::invalid_argument(std::string(__FUNCTION__) +
46                                     ": I2c.address() failed");
47     }
48 
49     usleep(50000);
50     ret = command(LCD_FUNCTIONSET | LCD_2LINE);
51 
52     if (!ret) {
53         ret = command(LCD_FUNCTIONSET | LCD_2LINE);
54         UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
55     }
56 
57     usleep(100);
58     ret = displayOn();
59 
60     UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
61 
62     usleep(100);
63     ret = clear();
64     UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
65 
66     usleep(2000);
67     ret = command(LCD_ENTRYMODESET | LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT);
68     UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
69 
70     ret = m_i2c_lcd_rgb.writeReg(0, 0);
71     UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
72     ret = m_i2c_lcd_rgb.writeReg(1, 0);
73     UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
74     ret = m_i2c_lcd_rgb.writeReg(0x08, 0xAA);
75     UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
76 
77     ret = m_i2c_lcd_rgb.writeReg(0x04, 0xFF);
78     UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
79     ret = m_i2c_lcd_rgb.writeReg(0x03, 0xFF);
80     UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
81     ret = m_i2c_lcd_rgb.writeReg(0x02, 0xFF);
82     UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
83 }
84 
~Jhd1313m1()85 Jhd1313m1::~Jhd1313m1()
86 {
87     clear();
88     setColor(0x00, 0x00, 0x00);
89 }
90 
91 mraa::Result
setColor(uint8_t r,uint8_t g,uint8_t b)92 Jhd1313m1::setColor(uint8_t r, uint8_t g, uint8_t b)
93 {
94     mraa::Result ret;
95 
96     ret = m_i2c_lcd_rgb.writeReg(0, 0);
97     UPM_GOTO_ON_MRAA_FAIL(ret, beach);
98     ret = m_i2c_lcd_rgb.writeReg(1, 0);
99     UPM_GOTO_ON_MRAA_FAIL(ret, beach);
100     ret = m_i2c_lcd_rgb.writeReg(0x08, 0xAA);
101     UPM_GOTO_ON_MRAA_FAIL(ret, beach);
102 
103     ret = m_i2c_lcd_rgb.writeReg(0x04, r);
104     UPM_GOTO_ON_MRAA_FAIL(ret, beach);
105     ret = m_i2c_lcd_rgb.writeReg(0x03, g);
106     UPM_GOTO_ON_MRAA_FAIL(ret, beach);
107     ret = m_i2c_lcd_rgb.writeReg(0x02, b);
108 
109 beach:
110     return ret;
111 }
112 
113 mraa::Result
scroll(bool direction)114 Jhd1313m1::scroll(bool direction)
115 {
116     if (direction) {
117         return scrollDisplayLeft();
118     } else {
119         return scrollDisplayRight();
120     }
121 }
122 
command(uint8_t cmd)123 mraa::Result Jhd1313m1::command(uint8_t cmd)
124 {
125   return m_i2c_lcd_control->writeReg(LCD_CMD, cmd);
126 
127 }
data(uint8_t cmd)128 mraa::Result Jhd1313m1::data(uint8_t cmd)
129 {
130   return m_i2c_lcd_control->writeReg(LCD_DATA, cmd);
131 }
132