From 1647f572fff5bd6723fca732f7aef769cef11d38 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Thu, 9 Jul 2015 13:54:49 -0600 Subject: [PATCH] lcd: remove i2c context from LCD base class This commit removes the I2C context from the LCD base class and moves it into each of the drivers. In addition, the createChar() virtual function was removed from the base class, as it directly used the now non-existant i2c context, and it would not work on anything other than hd44780 based controllers and displays anyway. Also, it is likely the capability itself, as well as the data required to implement it, is going to be different from device to device. createChar() has been added to the Lcm1602 driver which can support it. While doing this work, the LCD base class m_name is now set appropriately in the driver constructors. Signed-off-by: Jon Trulson Signed-off-by: Mihai Tudor Panu --- src/lcd/jhd1313m1.cxx | 10 +++++++++- src/lcd/jhd1313m1.h | 3 +++ src/lcd/lcd.cxx | 27 ++------------------------- src/lcd/lcd.h | 9 ++------- src/lcd/lcm1602.cxx | 26 +++++++++++++++++++++++++- src/lcd/lcm1602.h | 14 ++++++++++++++ src/lcd/ssd1308.cxx | 11 ++++++++++- src/lcd/ssd1308.h | 4 ++++ src/lcd/ssd1327.cxx | 12 +++++++++++- src/lcd/ssd1327.h | 4 ++++ 10 files changed, 84 insertions(+), 36 deletions(-) diff --git a/src/lcd/jhd1313m1.cxx b/src/lcd/jhd1313m1.cxx index 385cb23c..398a8a1a 100644 --- a/src/lcd/jhd1313m1.cxx +++ b/src/lcd/jhd1313m1.cxx @@ -33,15 +33,23 @@ using namespace upm; Jhd1313m1::Jhd1313m1(int bus, int lcdAddress, int rgbAddress) - : LCD(bus, lcdAddress), m_i2c_lcd_rgb(bus) + : m_i2c_lcd_rgb(bus), m_i2c_lcd_control(bus) { m_rgb_address = rgbAddress; + m_name = "Jhd1313m1"; + + m_lcd_control_address = lcdAddress; mraa_result_t ret = m_i2c_lcd_rgb.address(m_rgb_address); if (ret != MRAA_SUCCESS) { 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); ret = m_i2c_lcd_control.writeReg(LCD_CMD, LCD_FUNCTIONSET | LCD_2LINE); diff --git a/src/lcd/jhd1313m1.h b/src/lcd/jhd1313m1.h index b421a8a6..bd8a5f4e 100644 --- a/src/lcd/jhd1313m1.h +++ b/src/lcd/jhd1313m1.h @@ -24,6 +24,7 @@ #pragma once #include +#include #include "lcd.h" namespace upm @@ -113,5 +114,7 @@ class Jhd1313m1 : public LCD private: int m_rgb_address; mraa::I2c m_i2c_lcd_rgb; + int m_lcd_control_address; + mraa::I2c m_i2c_lcd_control; }; } diff --git a/src/lcd/lcd.cxx b/src/lcd/lcd.cxx index b7c11b3f..9219628c 100644 --- a/src/lcd/lcd.cxx +++ b/src/lcd/lcd.cxx @@ -26,20 +26,12 @@ #include #include "lcd.h" -#include "hd44780_bits.h" -#include "lcd_private.h" using namespace upm; -LCD::LCD(int bus, int lcdAddress) : m_i2c_lcd_control(bus) +LCD::LCD() { - m_lcd_control_address = lcdAddress; - m_bus = bus; - - mraa_result_t ret = m_i2c_lcd_control.address(m_lcd_control_address); - if (ret != MRAA_SUCCESS) { - fprintf(stderr, "Messed up i2c bus\n"); - } + m_name = "LCD"; } LCD::~LCD() @@ -53,21 +45,6 @@ LCD::write(int row, int column, std::string msg) return write(msg); } -mraa_result_t -LCD::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; -} - std::string LCD::name() { diff --git a/src/lcd/lcd.h b/src/lcd/lcd.h index b75756c2..271e7e64 100644 --- a/src/lcd/lcd.h +++ b/src/lcd/lcd.h @@ -24,7 +24,7 @@ #pragma once #include -#include +#include namespace upm { @@ -36,7 +36,7 @@ namespace upm class LCD { public: - LCD(int bus, int lcdAddress); + LCD(); virtual ~LCD(); mraa_result_t write(int x, int y, std::string msg); @@ -44,15 +44,10 @@ class LCD virtual mraa_result_t setCursor(int row, int column) = 0; virtual mraa_result_t clear() = 0; virtual mraa_result_t home() = 0; - virtual mraa_result_t createChar(uint8_t charSlot, uint8_t charData[]); std::string name(); protected: std::string m_name; - int m_lcd_control_address; - int m_bus; - - mraa::I2c m_i2c_lcd_control; }; } diff --git a/src/lcd/lcm1602.cxx b/src/lcd/lcm1602.cxx index ced042c0..012b5d46 100644 --- a/src/lcd/lcm1602.cxx +++ b/src/lcd/lcm1602.cxx @@ -33,9 +33,18 @@ using namespace upm; -Lcm1602::Lcm1602(int bus_in, int addr_in) : LCD(bus_in, addr_in) +Lcm1602::Lcm1602(int bus_in, int addr_in) : m_i2c_lcd_control(bus_in) { mraa_result_t error = MRAA_SUCCESS; + m_name = "Lcm1602 (I2C)"; + + m_lcd_control_address = addr_in; + + error = m_i2c_lcd_control.address(m_lcd_control_address); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Failed to initialize i2c bus\n"); + return; + } usleep(50000); expandWrite(LCD_BACKLIGHT); @@ -104,6 +113,21 @@ Lcm1602::home() return send(LCD_RETURNHOME, 0); } +mraa_result_t +Lcm1602::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; +} + /* * ************** * private area diff --git a/src/lcd/lcm1602.h b/src/lcd/lcm1602.h index d904e6bd..e34537bc 100644 --- a/src/lcd/lcm1602.h +++ b/src/lcd/lcm1602.h @@ -28,6 +28,7 @@ #pragma once #include +#include #include "lcd.h" namespace upm @@ -95,10 +96,23 @@ class Lcm1602 : public LCD */ mraa_result_t home(); + /** + * Create a custom character + * + * @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: mraa_result_t send(uint8_t value, int mode); mraa_result_t write4bits(uint8_t value); mraa_result_t expandWrite(uint8_t value); mraa_result_t pulseEnable(uint8_t value); + + int m_lcd_control_address; + mraa::I2c m_i2c_lcd_control; + }; } diff --git a/src/lcd/ssd1308.cxx b/src/lcd/ssd1308.cxx index c007b5e1..50e60b72 100644 --- a/src/lcd/ssd1308.cxx +++ b/src/lcd/ssd1308.cxx @@ -30,8 +30,17 @@ using namespace upm; -SSD1308::SSD1308(int bus_in, int addr_in) : LCD(bus_in, addr_in) +SSD1308::SSD1308(int bus_in, int addr_in) : m_i2c_lcd_control(bus_in) { + m_lcd_control_address = addr_in; + m_name = "SSD1308"; + + mraa_result_t error = m_i2c_lcd_control.address(m_lcd_control_address); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Failed to initialize i2c bus\n"); + return; + } + m_i2c_lcd_control.writeReg(LCD_CMD, DISPLAY_CMD_OFF); // display off usleep(4500); m_i2c_lcd_control.writeReg(LCD_CMD, DISPLAY_CMD_ON); // display on diff --git a/src/lcd/ssd1308.h b/src/lcd/ssd1308.h index 3c47d4aa..078e3cbf 100644 --- a/src/lcd/ssd1308.h +++ b/src/lcd/ssd1308.h @@ -25,6 +25,7 @@ #pragma once #include +#include #include "lcd.h" #include "ssd.h" @@ -108,5 +109,8 @@ class SSD1308 : public LCD mraa_result_t writeChar(uint8_t value); mraa_result_t setNormalDisplay(); mraa_result_t setAddressingMode(displayAddressingMode mode); + + int m_lcd_control_address; + mraa::I2c m_i2c_lcd_control; }; } diff --git a/src/lcd/ssd1327.cxx b/src/lcd/ssd1327.cxx index cc60dff3..3edcc422 100644 --- a/src/lcd/ssd1327.cxx +++ b/src/lcd/ssd1327.cxx @@ -33,9 +33,19 @@ using namespace upm; #define INIT_SLEEP 50000 #define CMD_SLEEP 10000 -SSD1327::SSD1327(int bus_in, int addr_in) : LCD(bus_in, addr_in) +SSD1327::SSD1327(int bus_in, int addr_in) : m_i2c_lcd_control(bus_in) { mraa_result_t error = MRAA_SUCCESS; + + m_lcd_control_address = addr_in; + m_name = "SSD1327"; + + error = m_i2c_lcd_control.address(m_lcd_control_address); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Failed to initialize i2c bus\n"); + return; + } + usleep(INIT_SLEEP); m_i2c_lcd_control.writeReg(LCD_CMD, 0xFD); // Unlock OLED driver IC MCU // interface from entering command. diff --git a/src/lcd/ssd1327.h b/src/lcd/ssd1327.h index ee9e1651..872d8649 100644 --- a/src/lcd/ssd1327.h +++ b/src/lcd/ssd1327.h @@ -25,6 +25,7 @@ #pragma once #include +#include #include "lcd.h" #include "ssd.h" @@ -119,5 +120,8 @@ class SSD1327 : public LCD uint8_t grayHigh; uint8_t grayLow; + + int m_lcd_control_address; + mraa::I2c m_i2c_lcd_control; }; }