lcd: jhd1313m1: rewrite to use Lcm1602 as a base class

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Jon Trulson 2015-07-27 16:30:22 -06:00 committed by Mihai Tudor Panu
parent cb62f06197
commit f133b6eb4c
6 changed files with 36 additions and 124 deletions

View File

@ -2,6 +2,8 @@
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com> * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* Copyright (c) 2014 Intel Corporation. * Copyright (c) 2014 Intel Corporation.
* *
* Contributions: Jon Trulson <jtrulson@ics.com>
*
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including * "Software"), to deal in the Software without restriction, including
@ -33,33 +35,27 @@
using namespace upm; using namespace upm;
Jhd1313m1::Jhd1313m1(int bus, int lcdAddress, int rgbAddress) Jhd1313m1::Jhd1313m1(int bus, int lcdAddress, int rgbAddress)
: m_i2c_lcd_rgb(bus), m_i2c_lcd_control(bus) : m_i2c_lcd_rgb(bus), Lcm1602(bus, lcdAddress, false)
{ {
m_rgb_address = rgbAddress; m_rgb_address = rgbAddress;
m_name = "Jhd1313m1"; m_name = "Jhd1313m1";
m_lcd_control_address = lcdAddress;
mraa_result_t ret = m_i2c_lcd_rgb.address(m_rgb_address); mraa_result_t ret = m_i2c_lcd_rgb.address(m_rgb_address);
if (ret != MRAA_SUCCESS) { if (ret != MRAA_SUCCESS) {
fprintf(stderr, "Messed up i2c bus\n"); fprintf(stderr, "Messed up i2c bus\n");
} }
ret = m_i2c_lcd_control.address(m_lcd_control_address);
if (ret != MRAA_SUCCESS) {
fprintf(stderr, "Messed up i2c bus\n");
}
usleep(50000); usleep(50000);
ret = m_i2c_lcd_control.writeReg(LCD_CMD, LCD_FUNCTIONSET | LCD_2LINE); ret = command(LCD_FUNCTIONSET | LCD_2LINE);
if (!ret) { if (!ret) {
ret = m_i2c_lcd_control.writeReg(LCD_CMD, LCD_FUNCTIONSET | LCD_2LINE); ret = command(LCD_FUNCTIONSET | LCD_2LINE);
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller"); UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
} }
usleep(100); usleep(100);
ret = m_i2c_lcd_control.writeReg(LCD_CMD, LCD_DISPLAYCONTROL | LCD_DISPLAYON); ret = displayOn();
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller"); UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
usleep(100); usleep(100);
@ -67,8 +63,7 @@ Jhd1313m1::Jhd1313m1(int bus, int lcdAddress, int rgbAddress)
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller"); UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
usleep(2000); usleep(2000);
ret = ret = command(LCD_ENTRYMODESET | LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT);
m_i2c_lcd_control.writeReg(LCD_CMD, LCD_ENTRYMODESET | LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT);
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller"); UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
ret = m_i2c_lcd_rgb.writeReg(0, 0); ret = m_i2c_lcd_rgb.writeReg(0, 0);
@ -118,71 +113,18 @@ mraa_result_t
Jhd1313m1::scroll(bool direction) Jhd1313m1::scroll(bool direction)
{ {
if (direction) { if (direction) {
return m_i2c_lcd_control.writeReg(LCD_CMD, LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); return scrollDisplayLeft();
} else { } else {
return m_i2c_lcd_control.writeReg(LCD_CMD, return scrollDisplayRight();
LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
} }
} }
mraa_result_t mraa_result_t Jhd1313m1::command(uint8_t cmd)
Jhd1313m1::createChar(uint8_t charSlot, uint8_t charData[])
{ {
mraa_result_t error = MRAA_SUCCESS; return m_i2c_lcd_control->writeReg(LCD_CMD, cmd);
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;
} }
mraa_result_t Jhd1313m1::data(uint8_t cmd)
/*
* **************
* virtual area
* **************
*/
mraa_result_t
Jhd1313m1::write(std::string msg)
{ {
mraa_result_t ret = MRAA_SUCCESS; return m_i2c_lcd_control->writeReg(LCD_DATA, cmd);
// This usleep fixes an odd bug where the clear function doesn't always work properly
usleep(1000);
for (std::string::size_type i = 0; i < msg.size(); ++i) {
ret = m_i2c_lcd_control.writeReg(LCD_DATA, msg[i]);
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
}
beach:
return ret;
}
mraa_result_t
Jhd1313m1::setCursor(int row, int column)
{
mraa_result_t ret;
int row_addr[] = { 0x80, 0xc0, 0x14, 0x54 };
uint8_t offset = ((column % 16) + row_addr[row]);
ret = m_i2c_lcd_control.writeReg(LCD_CMD, offset);
return ret;
}
mraa_result_t
Jhd1313m1::clear()
{
return m_i2c_lcd_control.writeReg(LCD_CMD, LCD_CLEARDISPLAY);
}
mraa_result_t
Jhd1313m1::home()
{
return m_i2c_lcd_control.writeReg(LCD_CMD, LCD_RETURNHOME);
} }

View File

@ -2,6 +2,8 @@
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com> * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* Copyright (c) 2014 Intel Corporation. * Copyright (c) 2014 Intel Corporation.
* *
* Contributions: Jon Trulson <jtrulson@ics.com>
*
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including * "Software"), to deal in the Software without restriction, including
@ -24,8 +26,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <mraa/i2c.hpp> #include "lcm1602.h"
#include "lcd.h"
namespace upm namespace upm
{ {
@ -51,7 +52,7 @@ namespace upm
* @image html grovergblcd.jpg * @image html grovergblcd.jpg
* @snippet jhd1313m1-lcd.cxx Interesting * @snippet jhd1313m1-lcd.cxx Interesting
*/ */
class Jhd1313m1 : public LCD class Jhd1313m1 : public Lcm1602
{ {
public: public:
/** /**
@ -82,48 +83,13 @@ class Jhd1313m1 : public LCD
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t setColor(uint8_t r, uint8_t g, uint8_t b); mraa_result_t setColor(uint8_t r, uint8_t g, uint8_t b);
/**
* Write a string to LCD
*
* @param msg The std::string to write to display, note only ascii
* chars are supported
* @return Result of operation
*/
mraa_result_t write(std::string msg);
/**
* Set cursor to a coordinate
*
* @param row The row to set cursor to
* @param column The column to set cursor to
* @return Result of operation
*/
mraa_result_t setCursor(int row, int column);
/**
* Clear display from characters
*
* @return Result of operatio
*/
mraa_result_t clear();
/**
* Return to coordinate 0,0
*
* @return Result of operation
*/
mraa_result_t home();
/** protected:
* Create a custom character virtual mraa_result_t command(uint8_t cmd);
* virtual mraa_result_t data(uint8_t data);
* @param charSlot the character slot to write, only 8 are available
* @param charData The character data (8 bytes) making up the character
* @return Result of operation
*/
mraa_result_t createChar(uint8_t charSlot, uint8_t charData[]);
private: private:
int m_rgb_address; int m_rgb_address;
mraa::I2c m_i2c_lcd_rgb; mraa::I2c m_i2c_lcd_rgb;
int m_lcd_control_address;
mraa::I2c m_i2c_lcd_control;
}; };
} }

View File

@ -14,16 +14,16 @@
#include "lcd.h" #include "lcd.h"
%} %}
%include "jhd1313m1.h"
%{
#include "jhd1313m1.h"
%}
%include "lcm1602.h" %include "lcm1602.h"
%{ %{
#include "lcm1602.h" #include "lcm1602.h"
%} %}
%include "jhd1313m1.h"
%{
#include "jhd1313m1.h"
%}
%include "ssd1327.h" %include "ssd1327.h"
%{ %{
#include "ssd1327.h" #include "ssd1327.h"

View File

@ -55,6 +55,9 @@ Lcm1602::Lcm1602(int bus_in, int addr_in, bool isExpander) :
return; return;
} }
// default display control
m_displayControl = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
// if we are not dealing with an expander (say via a derived class // if we are not dealing with an expander (say via a derived class
// like Jhd1313m1), then we do not want to execute the rest of the // like Jhd1313m1), then we do not want to execute the rest of the
// code below. Rather, the derived class's constructor should // code below. Rather, the derived class's constructor should

View File

@ -230,10 +230,11 @@ class Lcm1602 : public LCD
virtual mraa_result_t command(uint8_t cmd); virtual mraa_result_t command(uint8_t cmd);
virtual mraa_result_t data(uint8_t data); virtual mraa_result_t data(uint8_t data);
private:
int m_lcd_control_address; int m_lcd_control_address;
mraa::I2c* m_i2c_lcd_control; mraa::I2c* m_i2c_lcd_control;
private:
// true if using i2c, false otherwise (gpio) // true if using i2c, false otherwise (gpio)
bool m_isI2C; bool m_isI2C;

View File

@ -17,16 +17,16 @@
#include "lcd.h" #include "lcd.h"
%} %}
%include "jhd1313m1.h"
%{
#include "jhd1313m1.h"
%}
%include "lcm1602.h" %include "lcm1602.h"
%{ %{
#include "lcm1602.h" #include "lcm1602.h"
%} %}
%include "jhd1313m1.h"
%{
#include "jhd1313m1.h"
%}
%include "ssd1327.h" %include "ssd1327.h"
%{ %{
#include "ssd1327.h" #include "ssd1327.h"