mirror of
https://github.com/eclipse/upm.git
synced 2025-07-03 10:21:19 +03:00
i2clcd: use the mraa C++ API instead of the C API
Rewrite the i2c module to be based around the MRAA C++ API, since this makes resource management easier inside of the UPM C++ classes. i2clcd.{h,cxx}: remove the close() function. This now automatically gets called when the object goes out of scope, inside the destructor. examples/i2clcd: fix C++/Python/Javascript examples that explicitly called the close function. The I2c context now gets called by the destructor of the sensor class. This happens when the object goes out of scope or when it gets deleted, if the object was created using the new keyword, as is the case here. Signed-off-by: Wouter van Verre <wouter.van.verre@intel.com> Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:

committed by
Mihai Tudor Panu

parent
53b58225a4
commit
31c4f470fe
@ -31,22 +31,26 @@
|
||||
|
||||
using namespace upm;
|
||||
|
||||
Jhd1313m1::Jhd1313m1(int bus, int lcdAddress, int rgbAddress) : I2CLcd(bus, lcdAddress)
|
||||
Jhd1313m1::Jhd1313m1(int bus, int lcdAddress, int rgbAddress)
|
||||
: I2CLcd(bus, lcdAddress), m_i2c_lcd_rgb(bus)
|
||||
{
|
||||
m_rgb_address = rgbAddress;
|
||||
m_i2c_lcd_rgb = mraa_i2c_init(m_bus);
|
||||
|
||||
mraa_result_t ret = mraa_i2c_address(m_i2c_lcd_rgb, m_rgb_address);
|
||||
mraa_result_t ret = m_i2c_lcd_rgb.address(m_rgb_address);
|
||||
if (ret != MRAA_SUCCESS) {
|
||||
fprintf(stderr, "Messed up i2c bus\n");
|
||||
}
|
||||
|
||||
usleep(50000);
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_control, LCD_FUNCTIONSET | LCD_2LINE, LCD_CMD);
|
||||
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
|
||||
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");
|
||||
}
|
||||
|
||||
usleep(100);
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_control, LCD_DISPLAYCONTROL | LCD_DISPLAYON, LCD_CMD);
|
||||
ret = m_i2c_lcd_control.writeReg(LCD_CMD, LCD_DISPLAYCONTROL | LCD_DISPLAYON);
|
||||
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
|
||||
|
||||
usleep(100);
|
||||
@ -54,23 +58,22 @@ Jhd1313m1::Jhd1313m1(int bus, int lcdAddress, int rgbAddress) : I2CLcd(bus, lcdA
|
||||
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
|
||||
|
||||
usleep(2000);
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_control,
|
||||
LCD_ENTRYMODESET | LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT,
|
||||
LCD_CMD);
|
||||
ret =
|
||||
m_i2c_lcd_control.writeReg(LCD_CMD, LCD_ENTRYMODESET | LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT);
|
||||
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the LCD controller");
|
||||
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, 0, 0);
|
||||
ret = m_i2c_lcd_rgb.writeReg(0, 0);
|
||||
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, 0, 1);
|
||||
ret = m_i2c_lcd_rgb.writeReg(1, 0);
|
||||
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, 0xAA, 0x08);
|
||||
ret = m_i2c_lcd_rgb.writeReg(0x08, 0xAA);
|
||||
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
||||
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, 0xFF, 0x04);
|
||||
ret = m_i2c_lcd_rgb.writeReg(0x04, 0xFF);
|
||||
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, 0xFF, 0x03);
|
||||
ret = m_i2c_lcd_rgb.writeReg(0x03, 0xFF);
|
||||
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, 0xFF, 0x02);
|
||||
ret = m_i2c_lcd_rgb.writeReg(0x02, 0xFF);
|
||||
UPM_CHECK_MRAA_SUCCESS(ret, "Unable to initialise the RGB controller");
|
||||
}
|
||||
|
||||
@ -83,18 +86,18 @@ Jhd1313m1::setColor(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
mraa_result_t ret;
|
||||
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, 0, 0);
|
||||
ret = m_i2c_lcd_rgb.writeReg(0, 0);
|
||||
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, 0, 1);
|
||||
ret = m_i2c_lcd_rgb.writeReg(1, 0);
|
||||
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, 0xAA, 0x08);
|
||||
ret = m_i2c_lcd_rgb.writeReg(0x08, 0xAA);
|
||||
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
||||
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, r, 0x04);
|
||||
ret = m_i2c_lcd_rgb.writeReg(0x04, r);
|
||||
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, g, 0x03);
|
||||
ret = m_i2c_lcd_rgb.writeReg(0x03, g);
|
||||
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_rgb, b, 0x02);
|
||||
ret = m_i2c_lcd_rgb.writeReg(0x02, b);
|
||||
|
||||
beach:
|
||||
return ret;
|
||||
@ -104,13 +107,10 @@ mraa_result_t
|
||||
Jhd1313m1::scroll(bool direction)
|
||||
{
|
||||
if (direction) {
|
||||
return mraa_i2c_write_byte_data(m_i2c_lcd_control,
|
||||
LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT,
|
||||
LCD_CMD);
|
||||
return m_i2c_lcd_control.writeReg(LCD_CMD, LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
|
||||
} else {
|
||||
return mraa_i2c_write_byte_data(m_i2c_lcd_control,
|
||||
LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT,
|
||||
LCD_CMD);
|
||||
return m_i2c_lcd_control.writeReg(LCD_CMD,
|
||||
LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,7 +128,7 @@ Jhd1313m1::write(std::string msg)
|
||||
usleep(1000);
|
||||
|
||||
for (std::string::size_type i = 0; i < msg.size(); ++i) {
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_control, msg[i], LCD_DATA);
|
||||
ret = m_i2c_lcd_control.writeReg(LCD_DATA, msg[i]);
|
||||
UPM_GOTO_ON_MRAA_FAIL(ret, beach);
|
||||
}
|
||||
|
||||
@ -144,7 +144,7 @@ Jhd1313m1::setCursor(int row, int column)
|
||||
int row_addr[] = { 0x80, 0xc0, 0x14, 0x54 };
|
||||
uint8_t offset = ((column % 16) + row_addr[row]);
|
||||
|
||||
ret = mraa_i2c_write_byte_data(m_i2c_lcd_control, offset, LCD_CMD);
|
||||
ret = m_i2c_lcd_control.writeReg(LCD_CMD, offset);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@ -152,11 +152,11 @@ Jhd1313m1::setCursor(int row, int column)
|
||||
mraa_result_t
|
||||
Jhd1313m1::clear()
|
||||
{
|
||||
return mraa_i2c_write_byte_data(m_i2c_lcd_control, LCD_CLEARDISPLAY, LCD_CMD);
|
||||
return m_i2c_lcd_control.writeReg(LCD_CMD, LCD_CLEARDISPLAY);
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
Jhd1313m1::home()
|
||||
{
|
||||
return mraa_i2c_write_byte_data(m_i2c_lcd_control, LCD_RETURNHOME, LCD_CMD);
|
||||
return m_i2c_lcd_control.writeReg(LCD_CMD, LCD_RETURNHOME);
|
||||
}
|
||||
|
Reference in New Issue
Block a user