From 8065ed42fd701c233e234c2f7e02bdfcc54307a4 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Thu, 8 May 2014 20:44:07 +0100 Subject: [PATCH] icm1602: Initial Commit, functionality complete. Signed-off-by: Thomas Ingleby --- Doxyfile.in | 1 + examples/CMakeLists.txt | 3 + examples/lcm-lcd.cxx | 40 ++++++++++ src/CMakeLists.txt | 1 + src/lcm1602/CMakeLists.txt | 39 ++++++++++ src/lcm1602/jsupm_lcm1602.i | 7 ++ src/lcm1602/lcm1602.cxx | 177 ++++++++++++++++++++++++++++++++++++++++++++ src/lcm1602/lcm1602.h | 64 ++++++++++++++++ src/lcm1602/pyupm_lcm1602.i | 12 +++ 9 files changed, 344 insertions(+) create mode 100644 examples/lcm-lcd.cxx create mode 100644 src/lcm1602/CMakeLists.txt create mode 100644 src/lcm1602/jsupm_lcm1602.i create mode 100644 src/lcm1602/lcm1602.cxx create mode 100644 src/lcm1602/lcm1602.h create mode 100644 src/lcm1602/pyupm_lcm1602.i diff --git a/Doxyfile.in b/Doxyfile.in index 43e8a0c..73a4e54 100644 --- a/Doxyfile.in +++ b/Doxyfile.in @@ -755,6 +755,7 @@ WARN_LOGFILE = INPUT = @CMAKE_CURRENT_SOURCE_DIR@/src/hmc5883l/hmc5883l.h \ @CMAKE_CURRENT_SOURCE_DIR@/src/grove/grove.h \ + @CMAKE_CURRENT_SOURCE_DIR@/src/lcm1602/lcm1602.h \ @CMAKE_CURRENT_SOURCE_DIR@/README.md # This tag can be used to specify the character encoding of the source files diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 30591ea..6ec50c6 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,10 +1,13 @@ add_executable (compass compass.cxx) add_executable (groveled groveled.cxx) add_executable (grovetemp grovetemp.cxx) +add_executable (lcm-lcd lcm-lcd.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) +include_directories (${PROJECT_SOURCE_DIR}/src/lcm1602) target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (grovetemp grove ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (lcm-lcd lcm1602 ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/lcm-lcd.cxx b/examples/lcm-lcd.cxx new file mode 100644 index 0000000..a5f4c96 --- /dev/null +++ b/examples/lcm-lcd.cxx @@ -0,0 +1,40 @@ +/* + * Author: Thomas Ingleby + * Copyright (c) 2014 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. + */ + +#include "lcm1602.h" + +int +main(int argc, char **argv) +{ + upm::Lcm1602* lcd = new upm::Lcm1602(0, 0x27); + lcd->cursor(0,0); + lcd->write("Hello World"); + lcd->cursor(1,2); + lcd->write("Hello World"); + lcd->cursor(2,4); + lcd->write("Hello World"); + lcd->cursor(3,6); + lcd->write("Hello World"); + lcd->close(); +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f2fc167..81eb5cf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory (hmc5883l) add_subdirectory (grove) +add_subdirectory (lcm1602) diff --git a/src/lcm1602/CMakeLists.txt b/src/lcm1602/CMakeLists.txt new file mode 100644 index 0000000..dbf118e --- /dev/null +++ b/src/lcm1602/CMakeLists.txt @@ -0,0 +1,39 @@ +set (libname "lcm1602") +add_library (lcm1602 SHARED lcm1602.cxx) +include_directories (${MAA_INCLUDE_DIR}) +target_link_libraries (lcm1602 ${MAA_LIBRARIES}) + +if (SWIG_FOUND) + find_package (PythonLibs) + + include_directories ( + ${PYTHON_INCLUDE_PATH} + ${PYTHON_INCLUDE_DIRS} + ${MAA_INCLUDE_DIR} + . + ) + + set_source_files_properties (pyupm_lcm1602.i PROPERTIES CPLUSPLUS ON) + set_source_files_properties (jsupm_lcm1602.i PROPERTIES CPLUSPLUS ON) + set_source_files_properties (jsupm_lcm1602.i PROPERTIES SWIG_FLAGS "-node") + + swig_add_module (pyupm_lcm1602 python pyupm_lcm1602.i lcm1602.cxx) +# swig_add_module (jsupm_lcm1602 javascript jsupm_lcm1602.i lcm1602.cxx) + + swig_link_libraries (pyupm_lcm1602 ${PYTHON_LIBRARIES} ${MAA_LIBRARIES}) +# swig_link_libraries (jsupm_lcm1602 ${MAA_LIBRARIES}) + + if (DOXYGEN_FOUND) + set (CMAKE_SWIG_FLAGS -DDOXYGEN=${DOXYGEN_FOUND}) + add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${libname}_doc.i + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../doxy2swig.py -n + ${CMAKE_BINARY_DIR}/xml/${libname}_8h.xml + ${CMAKE_CURRENT_BINARY_DIR}/${libname}_doc.i + DEPENDS ${CMAKE_BINARY_DIR}/xml/${libname}_8h.xml + ) + add_custom_target (${libname}doc_i DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${libname}_doc.i) + add_dependencies (${libname}doc_i doc) + add_dependencies (${SWIG_MODULE_pyupm_lcm1602_REAL_NAME} ${libname}doc_i) + endif () + +endif () diff --git a/src/lcm1602/jsupm_lcm1602.i b/src/lcm1602/jsupm_lcm1602.i new file mode 100644 index 0000000..da35f72 --- /dev/null +++ b/src/lcm1602/jsupm_lcm1602.i @@ -0,0 +1,7 @@ +%module jsupm_lcm1602 + +%{ + #include "lcm1602.h" +%} + +%include "lcm1602.h" diff --git a/src/lcm1602/lcm1602.cxx b/src/lcm1602/lcm1602.cxx new file mode 100644 index 0000000..31324ad --- /dev/null +++ b/src/lcm1602/lcm1602.cxx @@ -0,0 +1,177 @@ +/* + * The MIT License (MIT) + * + * Author: Daniel Mosquera + * Copyright (c) 2013 Daniel Mosquera + * + * Author: Thomas Ingleby + * Copyright (c) 2014 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. + */ + +#include +#include + +#include "lcm1602.h" + +#define LCD_CLEARDISPLAY 0x01 +#define LCD_RETURNHOME 0x02 +#define LCD_ENTRYMODESET 0x04 +#define LCD_DISPLAYCONTROL 0x08 +#define LCD_CURSORSHIFT 0x10 +#define LCD_FUNCTIONSET 0x20 +#define LCD_SETCGRAMADDR 0x40 +#define LCD_SETDDRAMADDR 0x80 +#define LCD_BACKLIGHT 0x08 +#define LCD_NOBACKLIGHT 0x00 + +#define LCD_ENTRYRIGHT 0x00 +#define LCD_ENTRYLEFT 0x02 +#define LCD_ENTRYSHIFTINCREMENT 0x01 +#define LCD_ENTRYSHIFTDECREMENT 0x00 + +#define LCD_DISPLAYON 0x04 +#define LCD_DISPLAYOFF 0x00 +#define LCD_CURSORON 0x02 +#define LCD_CURSOROFF 0x00 +#define LCD_BLINKON 0x01 +#define LCD_BLINKOFF 0x00 + +#define LCD_8BITMODE 0x10 +#define LCD_4BITMODE 0x00 +#define LCD_2LINE 0x08 +#define LCD_1LINE 0x00 +#define LCD_5x10DOTS 0x04 +#define LCD_5x8DOTS 0x00 + +#define LCD_EN 0x04 // Enable bit +#define LCD_RW 0x02 // Read/Write bit +#define LCD_RS 0x01 // Register select bit + +using namespace upm; + +Lcm1602::Lcm1602(int bus_in, int addr_in) +{ + address = addr_in; + bus = bus_in; + maa_init(); + m_i2c = maa_i2c_init(bus); + + maa_i2c_address(m_i2c, address); + + usleep(50000); + expandWrite(LCD_BACKLIGHT); + usleep(100000); + + write4bits(0x03 << 4); + usleep(4500); + write4bits(0x30); + usleep(4500); + write4bits(0x30); + usleep(150); + + // Put into 4 bit mode + write4bits(0x20); + + // Set numeber of lines + send(LCD_FUNCTIONSET | 0x0f, 0); + send(LCD_DISPLAYCONTROL | LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF, 0); + clear(); + + // Set entry mode. + send(LCD_ENTRYMODESET | LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT, 0); + + home(); + +} + +int +Lcm1602::clear() +{ + return send(LCD_CLEARDISPLAY, 0); +} + +int +Lcm1602::home() +{ + return send(LCD_RETURNHOME, 0); +} + +int +Lcm1602::cursor(int row, int column) +{ + if (row > 3) + return 99; + int row_addr[] = { 0x80, 0xc0, 0x14, 0x54}; + return send(LCD_SETDDRAMADDR | ((column % 16) + row_addr[row]),0); +} + +int +Lcm1602::write(std::string msg) +{ + int ret = 0; + for(std::string::size_type i = 0; i < msg.size(); ++i) { + ret = send(msg[i], LCD_RS); + } + return 0; +} + +int +Lcm1602::close() +{ + return maa_i2c_stop(m_i2c); +} + +int +Lcm1602::send(char value, int mode) +{ + int ret = 0; + char h = value & 0xf0; + char l = (value << 4) & 0xf0; + ret = write4bits(h | mode); + ret = write4bits(l | mode); + return ret; +} + +int +Lcm1602::write4bits(char value) +{ + int ret = 0; + ret = expandWrite(value); + ret = pulseEnable(value); + return 0; +} + +int +Lcm1602::expandWrite(char value) +{ + char buffer = value | LCD_BACKLIGHT; + return maa_i2c_write_byte(m_i2c, buffer); +} + +int +Lcm1602::pulseEnable(char value) +{ + int ret = 0; + ret = expandWrite(value | LCD_EN); + usleep(1); + ret = expandWrite(value & ~LCD_EN); + usleep(50); + return ret; +} diff --git a/src/lcm1602/lcm1602.h b/src/lcm1602/lcm1602.h new file mode 100644 index 0000000..d8fe313 --- /dev/null +++ b/src/lcm1602/lcm1602.h @@ -0,0 +1,64 @@ +/* + * The MIT License (MIT) + * + * Author: Daniel Mosquera + * Copyright (c) 2013 Daniel Mosquera + * + * Author: Thomas Ingleby + * Copyright (c) 2014 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 + +#include +#include + +namespace upm { + +class Lcm1602 { +public: + /** LCM1602 Constructor. + * Calls MAA initialisation functions. + * @param bus i2c bus to use + * @param address the slave address the lcd is registered on. + */ + Lcm1602(int bus, int address); + + // Clear the screen + int clear(); + // Set the cursor to home + int home(); + // change cursor to row,column. + int cursor(int row, int column); + //write a string at the position + int write(std::string msg); + int close(); + +private: + int address; + int bus; + int send(char value, int mode); + int write4bits(char value); + int expandWrite(char value); + int pulseEnable(char value); + maa_i2c_context* m_i2c; +}; + +} diff --git a/src/lcm1602/pyupm_lcm1602.i b/src/lcm1602/pyupm_lcm1602.i new file mode 100644 index 0000000..68d6371 --- /dev/null +++ b/src/lcm1602/pyupm_lcm1602.i @@ -0,0 +1,12 @@ +%module pyupm_lcm1602 + +%feature("autodoc", "3"); + +#ifdef DOXYGEN +%include "lcm1602_doc.i" +#endif + +%include "lcm1602.h" +%{ + #include "lcm1602.h" +%} -- 2.7.4