diff --git a/src/lcd/iiclcd.cxx b/src/lcd/iiclcd.cxx index aec56f12..a2d39e42 100644 --- a/src/lcd/iiclcd.cxx +++ b/src/lcd/iiclcd.cxx @@ -51,3 +51,36 @@ maa_result_t IICLcd::close() { return maa_i2c_stop(m_i2c_lcd_control); } + +maa_result_t +IICLcd::i2cReg (maa_i2c_context ctx, int deviceAdress, int addr, uint8_t value) { + maa_result_t error = MAA_SUCCESS; + + uint8_t data[2] = { addr, value }; + error = maa_i2c_address (ctx, deviceAdress); + error = maa_i2c_write (ctx, data, 2); + + return error; +} + +maa_result_t +IICLcd::i2Cmd (maa_i2c_context ctx, uint8_t value) { + maa_result_t error = MAA_SUCCESS; + + uint8_t data[2] = { LCD_CMD, value }; + error = maa_i2c_address (ctx, m_lcd_control_address); + error = maa_i2c_write (ctx, data, 2); + + return error; +} + +maa_result_t +IICLcd::i2cData (maa_i2c_context ctx, uint8_t value) { + maa_result_t error = MAA_SUCCESS; + + uint8_t data[2] = { LCD_DATA, value }; + error = maa_i2c_address (ctx, m_lcd_control_address); + error = maa_i2c_write (ctx, data, 2); + + return error; +} diff --git a/src/lcd/iiclcd.h b/src/lcd/iiclcd.h index c36a9e17..fa12fd58 100644 --- a/src/lcd/iiclcd.h +++ b/src/lcd/iiclcd.h @@ -35,8 +35,8 @@ namespace upm { #define LCD_DISPLAYCONTROL 0x08 #define LCD_CURSORSHIFT 0x10 #define LCD_FUNCTIONSET 0x20 -#define LCD_SETCGRAMADDR 0x40 -#define LCD_SETDDRAMADDR 0x80 +#define LCD_DATA 0x40 +#define LCD_CMD 0x80 #define LCD_BACKLIGHT 0x08 #define LCD_NOBACKLIGHT 0x00 @@ -70,11 +70,16 @@ namespace upm { class IICLcd { public: IICLcd (int bus, int lcdAddress); - virtual maa_result_t write (std::string msg) = 0; maa_result_t write (int x, int y, std::string msg); + + virtual maa_result_t write (std::string msg) = 0; virtual maa_result_t setCursor (int row, int column) = 0; virtual maa_result_t clear () = 0; virtual maa_result_t home () = 0; + virtual maa_result_t i2Cmd (maa_i2c_context ctx, uint8_t value); + virtual maa_result_t i2cReg (maa_i2c_context ctx, int deviceAdress, int addr, uint8_t data); + virtual maa_result_t i2cData (maa_i2c_context ctx, uint8_t value); + maa_result_t close(); std::string name() { diff --git a/src/lcd/jhd1313m1.cxx b/src/lcd/jhd1313m1.cxx index eb114e97..9eed420d 100644 --- a/src/lcd/jhd1313m1.cxx +++ b/src/lcd/jhd1313m1.cxx @@ -41,29 +41,29 @@ Jhd1313m1::Jhd1313m1 (int bus, int lcdAddress, int rgbAddress) : IICLcd(bus, lcd } usleep(50000); - cmd (m_i2c_lcd_control, LCD_FUNCTIONSET | LCD_2LINE); + i2Cmd (m_i2c_lcd_control, LCD_FUNCTIONSET | LCD_2LINE); usleep(4500); - cmd (m_i2c_lcd_control, LCD_FUNCTIONSET | LCD_2LINE); + i2Cmd (m_i2c_lcd_control, LCD_FUNCTIONSET | LCD_2LINE); usleep(4500); - cmd (m_i2c_lcd_control, LCD_FUNCTIONSET | LCD_2LINE); + i2Cmd (m_i2c_lcd_control, LCD_FUNCTIONSET | LCD_2LINE); usleep(4500); - cmd (m_i2c_lcd_control, LCD_FUNCTIONSET | LCD_2LINE); + i2Cmd (m_i2c_lcd_control, LCD_FUNCTIONSET | LCD_2LINE); - cmd (m_i2c_lcd_control, LCD_DISPLAYCONTROL | LCD_DISPLAYON); + i2Cmd (m_i2c_lcd_control, LCD_DISPLAYCONTROL | LCD_DISPLAYON); clear (); usleep(4500); - cmd (m_i2c_lcd_control, LCD_ENTRYMODESET | - LCD_ENTRYLEFT | - LCD_ENTRYSHIFTDECREMENT); + i2Cmd (m_i2c_lcd_control, LCD_ENTRYMODESET | + LCD_ENTRYLEFT | + LCD_ENTRYSHIFTDECREMENT); - setReg (m_i2c_lcd_rgb, m_rgb_address, 0, 0); - setReg (m_i2c_lcd_rgb, m_rgb_address, 1, 0); - setReg (m_i2c_lcd_rgb, m_rgb_address, 0x08, 0xAA); + i2cReg (m_i2c_lcd_rgb, m_rgb_address, 0, 0); + i2cReg (m_i2c_lcd_rgb, m_rgb_address, 1, 0); + i2cReg (m_i2c_lcd_rgb, m_rgb_address, 0x08, 0xAA); - setReg (m_i2c_lcd_rgb, m_rgb_address, 0x04, 255); - setReg (m_i2c_lcd_rgb, m_rgb_address, 0x03, 255); - setReg (m_i2c_lcd_rgb, m_rgb_address, 0x02, 255); + i2cReg (m_i2c_lcd_rgb, m_rgb_address, 0x04, 255); + i2cReg (m_i2c_lcd_rgb, m_rgb_address, 0x03, 255); + i2cReg (m_i2c_lcd_rgb, m_rgb_address, 0x02, 255); } Jhd1313m1::~Jhd1313m1() { @@ -80,9 +80,7 @@ Jhd1313m1::write (std::string msg) { maa_result_t error = MAA_SUCCESS; uint8_t data[2] = {0x40, 0}; for (std::string::size_type i = 0; i < msg.size(); ++i) { - data[1] = msg[i]; - error = maa_i2c_address (m_i2c_lcd_control, m_lcd_control_address); - error = maa_i2c_write (m_i2c_lcd_control, data, 2); + error = i2cData (m_i2c_lcd_control, msg[i]); } return error; @@ -91,50 +89,19 @@ Jhd1313m1::write (std::string msg) { maa_result_t Jhd1313m1::setCursor (int row, int column) { maa_result_t error = MAA_SUCCESS; - int row_addr[] = { 0x80, 0xc0, 0x14, 0x54}; uint8_t offset = ((column % 16) + row_addr[row]); - - uint8_t data[2] = { 0x80, offset }; - error = maa_i2c_address (m_i2c_lcd_control, m_lcd_control_address); - error = maa_i2c_write (m_i2c_lcd_control, data, 2); + error = i2Cmd (m_i2c_lcd_control, offset); return error; } maa_result_t Jhd1313m1::clear () { - return cmd (m_i2c_lcd_control, LCD_CLEARDISPLAY); + return i2Cmd (m_i2c_lcd_control, LCD_CLEARDISPLAY); } maa_result_t Jhd1313m1::home () { - return cmd (m_i2c_lcd_control, LCD_RETURNHOME); -} - -/* - * ************** - * private area - * ************** - */ -maa_result_t -Jhd1313m1::setReg (maa_i2c_context ctx, int deviceAdress, int addr, uint8_t value) { - maa_result_t error = MAA_SUCCESS; - - uint8_t data[2] = { addr, value }; - error = maa_i2c_address (ctx, deviceAdress); - error = maa_i2c_write (ctx, data, 2); - - return error; -} - -maa_result_t -Jhd1313m1::cmd (maa_i2c_context ctx, uint8_t value) { - maa_result_t error = MAA_SUCCESS; - - uint8_t data[2] = { 0x80, value }; - error = maa_i2c_address (ctx, m_lcd_control_address); - error = maa_i2c_write (ctx, data, 2); - - return error; + return i2Cmd (m_i2c_lcd_control, LCD_RETURNHOME); } diff --git a/src/lcd/jhd1313m1.h b/src/lcd/jhd1313m1.h index 436e5c9b..9b60c8fe 100644 --- a/src/lcd/jhd1313m1.h +++ b/src/lcd/jhd1313m1.h @@ -38,9 +38,6 @@ class Jhd1313m1 : public IICLcd { maa_result_t home (); private: - maa_result_t cmd (maa_i2c_context ctx, uint8_t value); - maa_result_t setReg (maa_i2c_context ctx, int deviceAdress, int addr, uint8_t data); - int m_rgb_address; maa_i2c_context m_i2c_lcd_rgb; }; diff --git a/src/lcd/lcm1602.cxx b/src/lcd/lcm1602.cxx index 6ec488b4..fe396687 100644 --- a/src/lcd/lcm1602.cxx +++ b/src/lcd/lcm1602.cxx @@ -85,7 +85,7 @@ Lcm1602::setCursor (int row, int column) { int row_addr[] = { 0x80, 0xc0, 0x14, 0x54}; uint8_t offset = ((column % 16) + row_addr[row]); - return send (LCD_SETDDRAMADDR | offset, 0); + return send (LCD_CMD | offset, 0); } maa_result_t diff --git a/src/lcd/ssd1308.cxx b/src/lcd/ssd1308.cxx index 2456efb8..6aef5ae5 100644 --- a/src/lcd/ssd1308.cxx +++ b/src/lcd/ssd1308.cxx @@ -116,39 +116,6 @@ SSD1308::home () { * private area * ************** */ -maa_result_t -SSD1308::i2cReg (maa_i2c_context ctx, int deviceAdress, int addr, uint8_t value) { - maa_result_t error = MAA_SUCCESS; - - uint8_t data[2] = { addr, value }; - error = maa_i2c_address (ctx, deviceAdress); - error = maa_i2c_write (ctx, data, 2); - - return error; -} - -maa_result_t -SSD1308::i2Cmd (maa_i2c_context ctx, uint8_t value) { - maa_result_t error = MAA_SUCCESS; - - uint8_t data[2] = { 0x80, value }; - error = maa_i2c_address (ctx, m_lcd_control_address); - error = maa_i2c_write (ctx, data, 2); - - return error; -} - -maa_result_t -SSD1308::i2cData (maa_i2c_context ctx, uint8_t value) { - maa_result_t error = MAA_SUCCESS; - - uint8_t data[2] = { 0x40, value }; - error = maa_i2c_address (ctx, m_lcd_control_address); - error = maa_i2c_write (ctx, data, 2); - - return error; -} - maa_result_t SSD1308::writeChar (maa_i2c_context ctx, uint8_t value) { if (value < 0x20 || value > 0x7F) { diff --git a/src/lcd/ssd1308.h b/src/lcd/ssd1308.h index a3f18816..280198ed 100644 --- a/src/lcd/ssd1308.h +++ b/src/lcd/ssd1308.h @@ -156,16 +156,13 @@ class SSD1308 : public IICLcd { ~SSD1308 (); maa_result_t draw(uint8_t *data, int bytes); - // virtual methods + // pure virtual methods maa_result_t write (std::string msg); maa_result_t setCursor (int row, int column); maa_result_t clear (); maa_result_t home (); private: - maa_result_t i2Cmd (maa_i2c_context ctx, uint8_t value); - maa_result_t i2cReg (maa_i2c_context ctx, int deviceAdress, int addr, uint8_t data); - maa_result_t i2cData (maa_i2c_context ctx, uint8_t value); maa_result_t writeChar (maa_i2c_context ctx, uint8_t value); maa_result_t setNormalDisplay (); maa_result_t setAddressingMode (displayAddressingMode mode);