From 758234f887ec6e6326b97c60765ddcc72b204c11 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Tue, 11 Oct 2016 13:12:01 -0600 Subject: [PATCH] jhd1313m1: Add C++ wrapper Signed-off-by: Jon Trulson --- src/jhd1313m1/CMakeLists.txt | 6 +- src/jhd1313m1/hd44780_bits.h | 82 ---------- src/jhd1313m1/javaupm_jhd1313m1.i | 4 +- src/jhd1313m1/jhd1313m1.cxx | 169 ++++++++++++++++++++ src/jhd1313m1/jhd1313m1.h | 8 + src/jhd1313m1/jhd1313m1.hpp | 249 ++++++++++++++++++++++++++++++ src/jhd1313m1/jsupm_jhd1313m1.i | 1 + src/jhd1313m1/pyupm_jhd1313m1.i | 1 + 8 files changed, 433 insertions(+), 87 deletions(-) delete mode 100644 src/jhd1313m1/hd44780_bits.h create mode 100644 src/jhd1313m1/jhd1313m1.cxx create mode 100644 src/jhd1313m1/jhd1313m1.hpp diff --git a/src/jhd1313m1/CMakeLists.txt b/src/jhd1313m1/CMakeLists.txt index be9dc4df..d525f696 100644 --- a/src/jhd1313m1/CMakeLists.txt +++ b/src/jhd1313m1/CMakeLists.txt @@ -2,10 +2,10 @@ upm_mixed_module_init (NAME jhd1313m1 DESCRIPTION "upm JHD1313M1 RGB LCD display driver" C_HDR jhd1313m1.h C_SRC jhd1313m1.c -# CPP_HDR jhd1313m1.hpp -# CPP_SRC jhd1313m1.cxx + CPP_HDR jhd1313m1.hpp + CPP_SRC jhd1313m1.cxx # FTI_SRC jhd1313m1_fti.c -# CPP_WRAPS_C + CPP_WRAPS_C REQUIRES mraa) # We need hd44780_bits.h from lcm1602 include_directories("../lcm1602") \ No newline at end of file diff --git a/src/jhd1313m1/hd44780_bits.h b/src/jhd1313m1/hd44780_bits.h deleted file mode 100644 index 846c4015..00000000 --- a/src/jhd1313m1/hd44780_bits.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Author: Jon Trulson - * Copyright (c) 2015 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. - */ -#pragma once - -// This file contains bit definitions for the HD44780 and compatible -// LCD controllers. It is used primarily my the lcm1602 driver and -// it's derivatives. -// -// Those values (DATA, CMD) are specific to the implementation of the -// i2C expander in use, so may not be appropriate for inclusion into -// this file. But for now, we will leave them here. - -#include - -// commands -const uint8_t HD44780_CLEARDISPLAY = 0x01; -const uint8_t HD44780_RETURNHOME = 0x02; -const uint8_t HD44780_ENTRYMODESET = 0x04; -const uint8_t HD44780_DISPLAYCONTROL = 0x08; -const uint8_t HD44780_CURSORSHIFT = 0x10; -const uint8_t HD44780_FUNCTIONSET = 0x20; - -// flags for display entry mode -const uint8_t HD44780_ENTRYRIGHT = 0x00; -const uint8_t HD44780_ENTRYLEFT = 0x02; -const uint8_t HD44780_ENTRYSHIFTINCREMENT = 0x01; -const uint8_t HD44780_ENTRYSHIFTDECREMENT = 0x00; - -// flags for display on/off control -const uint8_t HD44780_DISPLAYON = 0x04; -const uint8_t HD44780_DISPLAYOFF = 0x00; -const uint8_t HD44780_CURSORON = 0x02; -const uint8_t HD44780_CURSOROFF = 0x00; -const uint8_t HD44780_BLINKON = 0x01; -const uint8_t HD44780_BLINKOFF = 0x00; - -// flags for display/cursor shift -const uint8_t HD44780_DISPLAYMOVE = 0x08; -const uint8_t HD44780_MOVERIGHT = 0x04; -const uint8_t HD44780_MOVELEFT = 0x00; - -// flags for function set -const uint8_t HD44780_8BITMODE = 0x10; -const uint8_t HD44780_4BITMODE = 0x00; -const uint8_t HD44780_2LINE = 0x08; -const uint8_t HD44780_1LINE = 0x00; -const uint8_t HD44780_5x10DOTS = 0x04; -const uint8_t HD44780_5x8DOTS = 0x00; - -// flags for CGRAM -const uint8_t HD44780_SETCGRAMADDR = 0x40; - -// may be implementation specific -const uint8_t HD44780_EN = 0x04; // Enable bit -const uint8_t HD44780_RW = 0x02; // Read/Write bit -const uint8_t HD44780_RS = 0x01; // Register select bit -const uint8_t HD44780_DATA = 0x40; -const uint8_t HD44780_CMD = 0x80; - -const uint8_t HD44780_BACKLIGHT = 0x08; -const uint8_t HD44780_NOBACKLIGHT = 0x00; diff --git a/src/jhd1313m1/javaupm_jhd1313m1.i b/src/jhd1313m1/javaupm_jhd1313m1.i index b8cba3ff..90da6256 100644 --- a/src/jhd1313m1/javaupm_jhd1313m1.i +++ b/src/jhd1313m1/javaupm_jhd1313m1.i @@ -22,12 +22,12 @@ JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *)$1, 0); } +%include "jhd1313m1.h" +%include "jhd1313m1.hpp" %{ #include "jhd1313m1.hpp" %} -%include "jhd1313m1.hpp" - %pragma(java) jniclasscode=%{ static { try { diff --git a/src/jhd1313m1/jhd1313m1.cxx b/src/jhd1313m1/jhd1313m1.cxx new file mode 100644 index 00000000..85118045 --- /dev/null +++ b/src/jhd1313m1/jhd1313m1.cxx @@ -0,0 +1,169 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * Based on UPM C++ drivers originally developed by: + * Author: Daniel Mosquera + * Copyright (c) 2013 Daniel Mosquera + * + * Author: Thomas Ingleby + * Copyright (c) 2014 Intel Corporation. + * + * Contributions: Sergey Kiselev + * + * 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 +#include +#include + +#include "jhd1313m1.hpp" + +using namespace upm; + +Jhd1313m1::Jhd1313m1(int bus, int lcdAddress, int rgbAddress) : + m_jhd1313m1(jhd1313m1_init(bus, lcdAddress, rgbAddress)) +{ + if (!m_jhd1313m1) + throw std::runtime_error(std::string(__FUNCTION__) + + ": jhd1313m1_init failed"); +} + +Jhd1313m1::~Jhd1313m1() +{ + jhd1313m1_close(m_jhd1313m1); +} + +upm_result_t Jhd1313m1::write(std::string msg) +{ + return jhd1313m1_write(m_jhd1313m1, (char *)msg.data(), msg.size()); +} + +upm_result_t Jhd1313m1::setCursor(int row, int column) +{ + return jhd1313m1_set_cursor(m_jhd1313m1, row, column); +} + +upm_result_t Jhd1313m1::scroll(bool direction) +{ + return jhd1313m1_scroll(m_jhd1313m1, direction); +} + +upm_result_t Jhd1313m1::setColor(uint8_t r, uint8_t g, uint8_t b) +{ + return jhd1313m1_set_color(m_jhd1313m1, r, g, b); +} + +upm_result_t Jhd1313m1::clear() +{ + return jhd1313m1_clear(m_jhd1313m1); +} + +upm_result_t Jhd1313m1::home() +{ + return jhd1313m1_home(m_jhd1313m1); +} + +upm_result_t Jhd1313m1::createChar(uint8_t charSlot, + jhd1313m1_custom_char_t charData) +{ + return jhd1313m1_create_char(m_jhd1313m1, charSlot, charData); +} + +upm_result_t Jhd1313m1::displayOn() +{ + return jhd1313m1_display_on(m_jhd1313m1, true); +} + +upm_result_t Jhd1313m1::displayOff() +{ + return jhd1313m1_display_on(m_jhd1313m1, false); +} + +upm_result_t Jhd1313m1::cursorOn() +{ + return jhd1313m1_cursor_on(m_jhd1313m1, true); +} + +upm_result_t Jhd1313m1::cursorOff() +{ + return jhd1313m1_cursor_on(m_jhd1313m1, false); +} + +upm_result_t Jhd1313m1::cursorBlinkOn() +{ + return jhd1313m1_cursor_blink_on(m_jhd1313m1, true); +} + +upm_result_t Jhd1313m1::cursorBlinkOff() +{ + return jhd1313m1_cursor_blink_on(m_jhd1313m1, false); +} + +upm_result_t Jhd1313m1::backlightOn() +{ + return jhd1313m1_backlight_on(m_jhd1313m1, true); +} + +upm_result_t Jhd1313m1::backlightOff() +{ + return jhd1313m1_backlight_on(m_jhd1313m1, false); +} + +upm_result_t Jhd1313m1::scrollDisplayLeft() +{ + return jhd1313m1_scroll_display_left(m_jhd1313m1); +} + +upm_result_t Jhd1313m1::scrollDisplayRight() +{ + return jhd1313m1_scroll_display_right(m_jhd1313m1); +} + +upm_result_t Jhd1313m1::entryLeftToRight() +{ + return jhd1313m1_entry_left_to_right(m_jhd1313m1, true); +} + +upm_result_t Jhd1313m1::entryRightToLeft() +{ + return jhd1313m1_entry_left_to_right(m_jhd1313m1, false); +} + +upm_result_t Jhd1313m1::autoscrollOn() +{ + return jhd1313m1_autoscroll_on(m_jhd1313m1, true); +} + +upm_result_t Jhd1313m1::autoscrollOff() +{ + return jhd1313m1_autoscroll_on(m_jhd1313m1, false); +} + +upm_result_t Jhd1313m1::command(uint8_t cmd) +{ + return jhd1313m1_command(m_jhd1313m1, cmd); +} + +upm_result_t Jhd1313m1::data(uint8_t data) +{ + return jhd1313m1_data(m_jhd1313m1, data); +} diff --git a/src/jhd1313m1/jhd1313m1.h b/src/jhd1313m1/jhd1313m1.h index c79c1797..48e63eee 100644 --- a/src/jhd1313m1/jhd1313m1.h +++ b/src/jhd1313m1/jhd1313m1.h @@ -186,6 +186,14 @@ extern "C" { */ upm_result_t jhd1313m1_backlight_on(const jhd1313m1_context dev, bool on); + /** + * Makes the LCD scroll text + * + * @param direction True if scrolling to the right, false otherwise + * @return Result of the operation + */ + upm_result_t jhd1313m1_scroll(const jhd1313m1_context dev, bool direction); + /** * Scroll the display left, without changing the character RAM. * diff --git a/src/jhd1313m1/jhd1313m1.hpp b/src/jhd1313m1/jhd1313m1.hpp new file mode 100644 index 00000000..b2451699 --- /dev/null +++ b/src/jhd1313m1/jhd1313m1.hpp @@ -0,0 +1,249 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * Based on UPM C++ drivers originally developed by: + * Author: Daniel Mosquera + * Copyright (c) 2013 Daniel Mosquera + * + * Author: Thomas Ingleby + * Copyright (c) 2014 Intel Corporation. + * + * Contributions: Sergey Kiselev + * + * 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. + */ + +#pragma once + +#include +#include "jhd1313m1.h" + +namespace upm +{ +/** + * @library jhd1313m1 + * @sensor jhd1313m1 + * @comname JHD1313M1 Display + * @altname Grove RGB LCD + * @type display + * @man seeed adafruit sparkfun + * @web http://www.seeedstudio.com/depot/Grove-LCD-RGB-Backlight-p-1643.html?cPath=34_36 + * @con i2c + * @kit gsk + * + * @brief API for the JHD1313M1 I2C controller for HD44780-based displays with + * an RGB backlight, such as a Grove RGB I2C LCD display + * + * JHD1313M1 has two I2C addreses: one belongs to a controller, very + * similar to the upm::Lcm1602 LCD driver, that controls the + * HD44780-based display, and the other controls only the + * backlight. This module was tested with the Seeed Grove LCD RGB + * Backlight v2.0 display that requires 5V to operate. + * + * @image html grovergblcd.jpg + * @snippet jhd1313m1-lcd.cxx Interesting + */ + + class Jhd1313m1 + { + public: + /** + * Jhd1313m1 constructor + * + * @param bus I2C bus to use + * @param address Slave address the LCD is registered on + * @param address Slave address the RGB backlight is registered on + */ + Jhd1313m1(int bus, int lcdAddress = 0x3E, int rgbAddress = 0x62); + + /** + * Jhd1313m1 destructor + */ + ~Jhd1313m1(); + + /** + * Writes a string to the LCD + * + * @param msg std::string to write to the display; note: only ASCII + * characters are supported + * @return Result of the operation + */ + upm_result_t write(std::string msg); + + /** + * Makes the LCD scroll text + * + * @param direction True if scrolling to the right, false otherwise + * @return Result of the operation + */ + upm_result_t scroll(bool direction); + + /** + * Sets the color of the backlight + * Can be used to change the color even when the backlight is off + * + * @param r 0-255 value for red + * @param g 0-255 value for green + * @param b 0-255 value for blue + * @return Result of the operation + */ + upm_result_t setColor(uint8_t r, uint8_t g, uint8_t b); + + /** + * Sets the cursor to specified coordinates + * + * @param row Row to set the cursor to + * @param column Column to set the cursor to + * @return Result of the operation + */ + upm_result_t setCursor(int row, int column); + + /** + * Clears the display of all characters + * + * @return Result of the operation + */ + upm_result_t clear(); + + /** + * Returns to the original coordinates (0,0) + * + * @return Result of the operation + */ + upm_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 + */ + upm_result_t createChar(uint8_t charSlot, + jhd1313m1_custom_char_t charData); + + /** + * Turn the display on + * + * @return Result of operation + */ + upm_result_t displayOn(); + + /** + * Turn the display off + * + * @return Result of operation + */ + upm_result_t displayOff(); + + /** + * Turn the cursor on + * + * @return Result of operation + */ + upm_result_t cursorOn(); + + /** + * Turn the cursor off + * + * @return Result of operation + */ + upm_result_t cursorOff(); + + /** + * Turn cursor blink on + * + * @return Result of operation + */ + upm_result_t cursorBlinkOn(); + + /** + * Turn cursor blink off + * + * @return Result of operation + */ + upm_result_t cursorBlinkOff(); + + /** + * Turn backlight on + * + * @return Result of operation + */ + upm_result_t backlightOn(); + + /** + * Turn backlight off + * + * @return Result of operation + */ + upm_result_t backlightOff(); + + /** + * Scroll the display left, without changing the character RAM + * + * @return Result of operation + */ + upm_result_t scrollDisplayLeft(); + + /** + * Scroll the display right, without changing the character RAM + * + * @return Result of operation + */ + upm_result_t scrollDisplayRight(); + + /** + * set the entry mode so that characters are added left to right + * + * @return Result of operation + */ + upm_result_t entryLeftToRight(); + + /** + * set the entry mode so that characters are added right to left + * + * @return Result of operation + */ + upm_result_t entryRightToLeft(); + + /** + * Right justify text entered from the cursor + * + * @return Result of operation + */ + upm_result_t autoscrollOn(); + + /** + * Left justify text entered from the cursor + * + * @return Result of operation + */ + upm_result_t autoscrollOff(); + + + protected: + jhd1313m1_context m_jhd1313m1; + upm_result_t command(uint8_t cmd); + upm_result_t data(uint8_t data); + + private: + }; +} diff --git a/src/jhd1313m1/jsupm_jhd1313m1.i b/src/jhd1313m1/jsupm_jhd1313m1.i index e4387972..479e3bd8 100644 --- a/src/jhd1313m1/jsupm_jhd1313m1.i +++ b/src/jhd1313m1/jsupm_jhd1313m1.i @@ -2,6 +2,7 @@ %include "../upm.i" %include "../carrays_uint8_t.i" +%include "jhd1313m1.h" %include "jhd1313m1.hpp" %{ #include "jhd1313m1.hpp" diff --git a/src/jhd1313m1/pyupm_jhd1313m1.i b/src/jhd1313m1/pyupm_jhd1313m1.i index b051246d..a1c66dda 100644 --- a/src/jhd1313m1/pyupm_jhd1313m1.i +++ b/src/jhd1313m1/pyupm_jhd1313m1.i @@ -6,6 +6,7 @@ %feature("autodoc", "3"); +%include "jhd1313m1.h" %include "jhd1313m1.hpp" %{ #include "jhd1313m1.hpp"