2014-05-30 05:50:31 -07:00
|
|
|
/*
|
|
|
|
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
|
|
|
* Copyright (c) 2014 Intel Corporation.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
2015-04-09 17:38:29 +01:00
|
|
|
#include <stdexcept>
|
2014-05-30 05:50:31 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-07-09 12:25:04 -06:00
|
|
|
#include "lcd_private.h"
|
2015-07-09 13:08:37 -06:00
|
|
|
#include "hd44780_bits.h"
|
2014-05-30 05:50:31 -07:00
|
|
|
#include "jhd1313m1.h"
|
|
|
|
|
|
|
|
using namespace upm;
|
|
|
|
|
2015-04-20 14:15:41 +01:00
|
|
|
Jhd1313m1::Jhd1313m1(int bus, int lcdAddress, int rgbAddress)
|
2015-07-09 13:54:49 -06:00
|
|
|
: m_i2c_lcd_rgb(bus), m_i2c_lcd_control(bus)
|
2015-04-08 14:06:49 +01:00
|
|
|
{
|
2014-06-03 09:12:47 +00:00
|
|
|
m_rgb_address = rgbAddress;
|
2015-07-09 13:54:49 -06:00
|
|
|
m_name = "Jhd1313m1";
|
|
|
|
|
|
|
|
m_lcd_control_address = lcdAddress;
|
2014-05-30 05:50:31 -07:00
|
|
|
|
2015-04-20 14:15:41 +01:00
|
|
|
mraa_result_t ret = m_i2c_lcd_rgb.address(m_rgb_address);
|
2014-06-25 10:05:27 +01:00
|
|
|
if (ret != MRAA_SUCCESS) {
|
2014-05-30 05:50:31 -07:00
|
|
|
fprintf(stderr, "Messed up i2c bus\n");
|
|
|
|
}
|
|
|
|
|
2015-07-09 13:54:49 -06:00
|
|
|
ret = m_i2c_lcd_control.address(m_lcd_control_address);
|
|
|
|
if (ret != MRAA_SUCCESS) {
|
|
|
|
fprintf(stderr, "Messed up i2c bus\n");
|
|
|
|
}
|
|
|
|
|
2014-06-03 09:12:47 +00:00
|
|
|
usleep(50000);
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_control.writeReg(LCD_CMD, LCD_FUNCTIONSET | LCD_2LINE);
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
ret = m_i2c_lcd_control.writeReg(LCD_CMD, LCD_FUNCTIONSET | LCD_2LINE);
|
|
|
|
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
|
|
|
|
}
|
2014-06-03 09:12:47 +00:00
|
|
|
|
2015-04-09 17:41:54 +01:00
|
|
|
usleep(100);
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_control.writeReg(LCD_CMD, LCD_DISPLAYCONTROL | LCD_DISPLAYON);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
|
2014-07-29 21:46:48 +04:00
|
|
|
|
2015-04-09 17:41:54 +01:00
|
|
|
usleep(100);
|
2015-04-09 17:38:29 +01:00
|
|
|
ret = clear();
|
|
|
|
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
|
2014-06-03 09:12:47 +00:00
|
|
|
|
2015-04-09 17:41:54 +01:00
|
|
|
usleep(2000);
|
2015-04-20 14:15:41 +01:00
|
|
|
ret =
|
|
|
|
m_i2c_lcd_control.writeReg(LCD_CMD, LCD_ENTRYMODESET | LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
|
|
|
|
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(0, 0);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(1, 0);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(0x08, 0xAA);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
|
|
|
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(0x04, 0xFF);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(0x03, 0xFF);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(0x02, 0xFF);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
2014-05-30 05:50:31 -07:00
|
|
|
}
|
|
|
|
|
2015-04-08 14:06:49 +01:00
|
|
|
Jhd1313m1::~Jhd1313m1()
|
|
|
|
{
|
2015-06-16 17:02:21 -07:00
|
|
|
clear();
|
|
|
|
setColor(0x00, 0x00, 0x00);
|
2014-05-30 05:50:31 -07:00
|
|
|
}
|
|
|
|
|
2014-07-14 10:48:55 +01:00
|
|
|
mraa_result_t
|
|
|
|
Jhd1313m1::setColor(uint8_t r, uint8_t g, uint8_t b)
|
|
|
|
{
|
2015-04-09 17:38:29 +01:00
|
|
|
mraa_result_t ret;
|
|
|
|
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(0, 0);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(1, 0);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(0x08, 0xAA);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
|
|
|
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(0x04, r);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(0x03, g);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_rgb.writeReg(0x02, b);
|
2015-04-09 17:38:29 +01:00
|
|
|
|
|
|
|
beach:
|
|
|
|
return ret;
|
2014-07-14 10:48:55 +01:00
|
|
|
}
|
|
|
|
|
2014-07-14 15:25:12 +01:00
|
|
|
mraa_result_t
|
|
|
|
Jhd1313m1::scroll(bool direction)
|
|
|
|
{
|
2015-04-13 11:58:03 +01:00
|
|
|
if (direction) {
|
2015-04-20 14:15:41 +01:00
|
|
|
return m_i2c_lcd_control.writeReg(LCD_CMD, LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
|
2015-04-13 11:58:03 +01:00
|
|
|
} else {
|
2015-04-20 14:15:41 +01:00
|
|
|
return m_i2c_lcd_control.writeReg(LCD_CMD,
|
|
|
|
LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
|
2015-04-13 11:58:03 +01:00
|
|
|
}
|
2014-07-14 15:25:12 +01:00
|
|
|
}
|
|
|
|
|
2015-07-09 14:12:20 -06:00
|
|
|
mraa_result_t
|
|
|
|
Jhd1313m1::createChar(uint8_t charSlot, uint8_t charData[])
|
|
|
|
{
|
|
|
|
mraa_result_t error = MRAA_SUCCESS;
|
|
|
|
charSlot &= 0x07; // only have 8 positions we can set
|
|
|
|
error = m_i2c_lcd_control.writeReg(LCD_CMD, LCD_SETCGRAMADDR | (charSlot << 3));
|
|
|
|
if (error == MRAA_SUCCESS) {
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
error = m_i2c_lcd_control.writeReg(LCD_DATA, charData[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2014-05-30 05:50:31 -07:00
|
|
|
/*
|
|
|
|
* **************
|
|
|
|
* virtual area
|
|
|
|
* **************
|
|
|
|
*/
|
2014-06-25 10:05:27 +01:00
|
|
|
mraa_result_t
|
2015-04-08 14:06:49 +01:00
|
|
|
Jhd1313m1::write(std::string msg)
|
|
|
|
{
|
2015-04-09 17:38:29 +01:00
|
|
|
mraa_result_t ret = MRAA_SUCCESS;
|
|
|
|
|
2015-04-09 17:39:42 +01:00
|
|
|
// This usleep fixes an odd bug where the clear function doesn't always work properly
|
2015-02-10 11:09:52 -08:00
|
|
|
usleep(1000);
|
2015-04-09 17:38:29 +01:00
|
|
|
|
2014-06-03 09:12:47 +00:00
|
|
|
for (std::string::size_type i = 0; i < msg.size(); ++i) {
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_control.writeReg(LCD_DATA, msg[i]);
|
2015-04-09 17:38:29 +01:00
|
|
|
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
2014-05-30 05:50:31 -07:00
|
|
|
}
|
|
|
|
|
2015-04-09 17:38:29 +01:00
|
|
|
beach:
|
|
|
|
return ret;
|
2014-05-30 05:50:31 -07:00
|
|
|
}
|
|
|
|
|
2014-06-25 10:05:27 +01:00
|
|
|
mraa_result_t
|
2015-04-08 14:06:49 +01:00
|
|
|
Jhd1313m1::setCursor(int row, int column)
|
|
|
|
{
|
2015-04-09 17:38:29 +01:00
|
|
|
mraa_result_t ret;
|
|
|
|
|
2015-04-08 14:06:49 +01:00
|
|
|
int row_addr[] = { 0x80, 0xc0, 0x14, 0x54 };
|
2014-06-03 09:12:47 +00:00
|
|
|
uint8_t offset = ((column % 16) + row_addr[row]);
|
2014-05-30 05:50:31 -07:00
|
|
|
|
2015-04-20 14:15:41 +01:00
|
|
|
ret = m_i2c_lcd_control.writeReg(LCD_CMD, offset);
|
2015-04-09 17:38:29 +01:00
|
|
|
|
|
|
|
return ret;
|
2014-05-30 05:50:31 -07:00
|
|
|
}
|
|
|
|
|
2014-06-25 10:05:27 +01:00
|
|
|
mraa_result_t
|
2015-04-08 14:06:49 +01:00
|
|
|
Jhd1313m1::clear()
|
|
|
|
{
|
2015-04-20 14:15:41 +01:00
|
|
|
return m_i2c_lcd_control.writeReg(LCD_CMD, LCD_CLEARDISPLAY);
|
2014-05-30 05:50:31 -07:00
|
|
|
}
|
|
|
|
|
2014-06-25 10:05:27 +01:00
|
|
|
mraa_result_t
|
2015-04-08 14:06:49 +01:00
|
|
|
Jhd1313m1::home()
|
|
|
|
{
|
2015-04-20 14:15:41 +01:00
|
|
|
return m_i2c_lcd_control.writeReg(LCD_CMD, LCD_RETURNHOME);
|
2014-05-30 05:50:31 -07:00
|
|
|
}
|