diff --git a/docs/images/tm1637.jpeg b/docs/images/tm1637.jpeg new file mode 100644 index 00000000..f3b4e719 Binary files /dev/null and b/docs/images/tm1637.jpeg differ diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index 4b3ea7d7..d645ee05 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -108,6 +108,7 @@ add_executable (a110x-intr-example a110x-intr.cxx) add_executable (mhz16-example mhz16.cxx) add_executable (apds9002-example apds9002.cxx) add_executable (waterlevel-example waterlevel.cxx) +add_executable (tm1637-example tm1637.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -195,6 +196,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/flex) include_directories (${PROJECT_SOURCE_DIR}/src/mhz16) include_directories (${PROJECT_SOURCE_DIR}/src/apds9002) include_directories (${PROJECT_SOURCE_DIR}/src/waterlevel) +include_directories (${PROJECT_SOURCE_DIR}/src/tm1637) target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT}) @@ -304,3 +306,4 @@ target_link_libraries (a110x-intr-example a110x ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (mhz16-example mhz16 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (apds9002-example apds9002 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (waterlevel-example waterlevel ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (tm1637-example tm1637 ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/c++/tm1637.cxx b/examples/c++/tm1637.cxx new file mode 100644 index 00000000..50a028f6 --- /dev/null +++ b/examples/c++/tm1637.cxx @@ -0,0 +1,77 @@ +/* + * Author: Mihai Tudor Panu + * 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. + */ + +#include "tm1637.h" +#include +#include +#include +#include + +using namespace std; +using namespace upm; + +bool run = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + run = false; +} + +int +main(int argc, char** argv) +{ + //! [Interesting] + bool point = true; + int timezone = -7; // Your UTC offset + time_t rawtime; + struct tm * gmt; + char myTime[4]; + + fprintf(stdout, "TM1637 Display Example\n"); + signal(SIGINT, sig_handler); + + TM1637 myDisplay = TM1637(0, 1); // TM1637 on pins 0 (clk) and 1 (dio) + myDisplay.write(0x39, 0x09, 0x09); // Start a box using 7-segment encoding + myDisplay.writeAt(3, ']'); // Finish box using writeAt function + sleep(3); // Wait 3 seconds + + while(run) + { + time(&rawtime); // Update raw time + gmt = gmtime(&rawtime); // Get current time + + // Format and store the time in 24 hour format + sprintf(myTime, "%2d%02d", (gmt->tm_hour + timezone + 24) % 24, gmt->tm_min); + + myDisplay.write(myTime); // Write to display as string + myDisplay.setColon(point ^= true); // Toggle the dots on the display + sleep(1); // Only update once every second + } + + //! [Interesting] + return 0; +} + + diff --git a/examples/javascript/tm1637.js b/examples/javascript/tm1637.js new file mode 100644 index 00000000..62200590 --- /dev/null +++ b/examples/javascript/tm1637.js @@ -0,0 +1,66 @@ +/* +* Author: Mihai Tudor Panu +* 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. +*/ + +// Some vars +var colon = true; +var interval; + +// Load display +var tm1637 = require('jsupm_tm1637'); + +// Instantiate on pins 0 (Clk) and 1 (Dio) +var display = new tm1637.TM1637(0, 1); + +// Get the current time +var now = new Date(); +console.log("System time: " + now.getHours() + ":" + ("0" + now.getMinutes()).slice(-2)); +console.log("Time zone can be changed by setting the TZ environment variable."); + +// Display and time update function +function update(){ + now = new Date(); + var time = now.getHours().toString() + ("0" + now.getMinutes().toString()).slice(-2); + display.writeString(time); + display.setColon(colon = !colon); +} + +// Start with a 7-segment encoded box on the display +display.write(0x39, 0x09, 0x09, 0x0f); + +// Start displaying the clock after 3 seconds +setTimeout(function(){ + // And update every second thereafter + interval = setInterval(update, 1000); +}, 3000) + +// Exit handler +process.on('SIGINT', function() +{ + clearInterval(interval); + display = null; + tm1637.cleanUp(); + tm1637 = null; + console.log("Interrupt received, exiting..."); + process.exit(0); +}); diff --git a/examples/python/tm1637.py b/examples/python/tm1637.py new file mode 100644 index 00000000..8c757444 --- /dev/null +++ b/examples/python/tm1637.py @@ -0,0 +1,54 @@ +# Author: Mihai Tudor Panu +# 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. + +import time, signal +import pyupm_tm1637 as tm1637 + +# Register exit handler for normal Ctrl+C exit +def SIGINTHandler(signum, frame): + raise SystemExit +signal.signal(signal.SIGINT, SIGINTHandler) + +# Create a display object on pins 0 CLK and 1 DIO +display = tm1637.TM1637(0, 1) +dots = True + +# Get local time +myTime = time.localtime(time.time()) +print time.strftime("System time: %H:%M", myTime) +print ("You can adjust your time zone by setting the TZ environment variable.") + +# Draw a box for 3 seconds using 7-segment encoding +display.write(0x39, 0x09, 0x09, 0x0f) +time.sleep(3) + +# Loop indefinitely +while True: + # Update and display time + timeString = time.strftime("%H%M", time.localtime(time.time())) + display.write(timeString) + # Toggle colon + display.setColon(dots) + dots = not dots + + # Sleep for 1 s + time.sleep(1) diff --git a/src/tm1637/CMakeLists.txt b/src/tm1637/CMakeLists.txt new file mode 100644 index 00000000..6138d2e2 --- /dev/null +++ b/src/tm1637/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "tm1637") +set (libdescription "C++ API for the TM1637 7-segment display") +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init() diff --git a/src/tm1637/jsupm_tm1637.i b/src/tm1637/jsupm_tm1637.i new file mode 100644 index 00000000..91c9243c --- /dev/null +++ b/src/tm1637/jsupm_tm1637.i @@ -0,0 +1,21 @@ +%module jsupm_tm1637 +%include "../upm.i" +%include "../carrays_uint8_t.i" + +%varargs(4, int digit = 0) write; + +%rename("writeArray") write(uint8_t *digits); +%rename("writeString") write(std::string digits); + +%typemap(in) uint8_t * { + void *argp = 0 ; + int res = SWIG_ConvertPtr($input, &argp, SWIGTYPE_p_uint8Array, 0 | 0); + $1 = (uint8_t *)(argp); +} + +%{ + #include "tm1637.h" +%} + +%include "tm1637.h" + diff --git a/src/tm1637/pyupm_tm1637.i b/src/tm1637/pyupm_tm1637.i new file mode 100644 index 00000000..801adf1f --- /dev/null +++ b/src/tm1637/pyupm_tm1637.i @@ -0,0 +1,16 @@ +%module pyupm_tm1637 +%include "../upm.i" +%include "../carrays_uint8_t.i" + +%varargs(4, int digit = 0) write; + +%typemap(in) uint8_t * { + void *argp = 0 ; + int res = SWIG_ConvertPtr($input, &argp,SWIGTYPE_p_uint8Array, 0 | 0 ); + $1 = reinterpret_cast< uint8_t * >(argp); +} + +%{ + #include "tm1637.h" +%} +%include "tm1637.h" diff --git a/src/tm1637/tm1637.cxx b/src/tm1637/tm1637.cxx new file mode 100644 index 00000000..5b296f7a --- /dev/null +++ b/src/tm1637/tm1637.cxx @@ -0,0 +1,204 @@ +/* + * Author: Mihai Tudor Panu + * 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. + */ + +#include "tm1637.h" +#include + +const uint8_t m_brkt[2] = {0x39, 0x0f}; +const uint8_t m_nums[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, + 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; +const uint8_t m_char[26] = {0x77, 0x7c, 0x39, 0x5e, 0x79, + 0x71, 0x6f, 0x76, 0x30, 0x1e, + 0x00, 0x38, 0x00, 0x00, 0x5c, + 0x73, 0x67, 0x50, 0x5b, 0x78, + 0x3e, 0x1c, 0x00, 0x00, 0x6e, + 0x5b}; + +using namespace std; +using namespace upm; + +upm::TM1637::TM1637(int clk_pin, int dio_pin, int bright, M_FAST_GPIO mmio) { + + if((m_clk = mraa_gpio_init(clk_pin)) == NULL){ + cerr << "TM1637: failed to initialize CLK pin." << endl; + return; + } + + if((m_dio = mraa_gpio_init(dio_pin)) == NULL){ + cerr << "TM1637: failed to initialize DIO pin." << endl; + return; + } + + mraa_gpio_dir(m_clk, MRAA_GPIO_OUT); + mraa_gpio_dir(m_dio, MRAA_GPIO_OUT); + + // Let the resistors pull the lines high + mraa_gpio_mode(m_clk, MRAA_GPIO_PULLUP); + mraa_gpio_mode(m_dio, MRAA_GPIO_PULLUP); + + if(mmio){ + if(mraa_gpio_use_mmaped(m_clk, 1) != MRAA_SUCCESS || + mraa_gpio_use_mmaped(m_dio, 1) != MRAA_SUCCESS){ + cerr << "TM1637: failed to set memory mapped GPIO" << endl; + return; + } + } + + mraa_gpio_write(m_clk, 0); + mraa_gpio_write(m_dio, 0); + + for (int i = 0; i < M_DISPLAY_DIGITS; i++) { + m_digits[i] = 0x00; + } + setBrightness(bright); +} +upm::TM1637::~TM1637() { + for (int i = 0; i < M_DISPLAY_DIGITS; i++) { + m_digits[i] = 0x00; + } + update(); + + mraa_gpio_close(m_clk); + mraa_gpio_close(m_dio); +} +mraa_result_t upm::TM1637::write(uint8_t *digits) { + for (int i = 0; i < M_DISPLAY_DIGITS; i++) { + m_digits[i] = digits[i]; + } + update(); + return MRAA_SUCCESS; +} +mraa_result_t upm::TM1637::write(int d, ...) { + va_list args; + va_start(args, d); + m_digits[0] = (uint8_t)d; + + for (int i = 1; i < M_DISPLAY_DIGITS; i++) { + m_digits[i] = (uint8_t)va_arg(args, int); + d++; + } + va_end(args); + update(); + return MRAA_SUCCESS; +} +mraa_result_t upm::TM1637::writeAt(int index, char symbol) { + if(index < 0 || index >= M_DISPLAY_DIGITS){ + cerr << "TM1637: invalid index in " << __FUNCTION__ << endl; + return MRAA_ERROR_INVALID_PARAMETER; + } + m_digits[index] = encode(symbol); + update(); + return MRAA_SUCCESS; +} +mraa_result_t upm::TM1637::write(std::string digits) { + int len = digits.length(); + if( len > M_DISPLAY_DIGITS){ + len = M_DISPLAY_DIGITS; + } + for (int i = 0; i < len; i++) { + m_digits[i] = encode(digits[i]); + } + update(); + return MRAA_SUCCESS; +} +void upm::TM1637::setColon(bool value) { + if(value){ + m_digits[1] |= 0x80; + } + else{ + m_digits[1] &= 0x7f; + } + update(); +} +void upm::TM1637::setBrightness(int value) { + m_brightness = value & 0x07; + update(); +} +void upm::TM1637::i2c_start() { + mraa_gpio_write(m_clk, 1); + mraa_gpio_write(m_dio, 1); + mraa_gpio_write(m_dio, 0); +} +void upm::TM1637::i2c_stop() { + mraa_gpio_write(m_clk, 0); + mraa_gpio_write(m_dio, 0); + mraa_gpio_write(m_clk, 1); + mraa_gpio_write(m_dio, 1); +} +void upm::TM1637::i2c_writeByte(uint8_t value) { + for(uint8_t i = 0; i < 8; i++) + { + mraa_gpio_write(m_clk, 0); + if(value & 0x01) + mraa_gpio_write(m_dio, 1); + else + mraa_gpio_write(m_dio, 0); + value >>= 1; + mraa_gpio_write(m_clk, 1); + } + + // Ack clock without skew, TM1637 is fast enough + mraa_gpio_write(m_clk, 0); + mraa_gpio_write(m_clk, 1); + mraa_gpio_write(m_clk, 0); +} +void upm::TM1637::update() { + i2c_start(); + i2c_writeByte(TM1637_ADDR); + i2c_stop(); + + i2c_start(); + i2c_writeByte(TM1637_REG); + for (int i = 0; i < M_DISPLAY_DIGITS; i++) { + i2c_writeByte(m_digits[i]); + } + i2c_stop(); + + i2c_start(); + i2c_writeByte(TM1637_CMD | m_brightness); + i2c_stop(); +} +uint8_t upm::TM1637::encode(char c) { + if(c >= '0' && c <= '9') + return m_nums[(int)c - 48]; + if(c >= 'a' && c <= 'z') + return m_char[(int)c - 97]; + if(c >= 'A' && c <= 'Z') + return m_char[(int)c - 65]; + if(c == '[') + return m_brkt[0]; + if(c == ']') + return m_brkt[1]; + if(c == '(' || c == ')') + return m_brkt[(int)c - 40]; + if(c == '-') + return 0x40; + if(c == '_') + return 0x08; + if(c == '}') + return 0x70; + if(c == '{') + return 0x46; + return 0x00; +} diff --git a/src/tm1637/tm1637.h b/src/tm1637/tm1637.h new file mode 100644 index 00000000..97687ef6 --- /dev/null +++ b/src/tm1637/tm1637.h @@ -0,0 +1,149 @@ +/* + * Author: Mihai Tudor Panu + * 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 + +#include +#include +#include +#include + +#include + + +// TM1637 specific register addresses for writing all digits at a time +#define TM1637_ADDR 0x40 +#define TM1637_REG 0xC0 +#define TM1637_CMD 0x88 + +// Display specific values +#define M_DISPLAY_DIGITS 4 + +namespace upm +{ + +/** + * @brief TM1637 7-segment display library + * @defgroup tm1637 libupm-tm1637 + * @ingroup seeed gpio display + */ + +/** + * @library tm1637 + * @sensor tm1637 + * @comname TM1637 7-Segment Display + * @type display + * @man seeed + * @con gpio + * + * @brief C++ API for TM1637 (7-segment display) + * + * The TM1637 is a display controller for LED based 7-segment displays. + * It can be used to address and write data to multiple display digits. This + * driver is based on the Grove version of the TM1637 display which uses 4 + * digits thus making it ideal for clock displays, timers, counters or even + * score display in a two player arcade game. + * + * @image html tm1637.jpeg + * @snippet tm1637.cxx Interesting + */ + + class TM1637 + { + public: + /** + * Enum for memory mapped GPIO + */ + typedef enum { + NO = 0, + YES = 1 + } M_FAST_GPIO; + /** + * TM1637 constructor + * + * @param clk_pin the clock pin the sensor is connected to + * @param dio_pin the data pin the sensor is connected to + * @param bright initial brightness from 0 to 7 dark to bright (default 3) + * @param mmio fast memory mapped gpio writes, default is yes + */ + TM1637(int clk_pin, int dio_pin, int bright = 3, M_FAST_GPIO mmio = YES); + /** + * TM1637 destructor + */ + ~TM1637(); + /** + * Write digits to the display in 7 segment encoding + * + * @param digits an array of digits to send to the display + * @return 0 if the write was successful, error code otherwise + */ + mraa_result_t write(uint8_t *digits); + /** + * Write digits to the display in 7 segment encoding + * + * @param d list of multiple arguments to send to the display + * @return 0 if the write was successful, error code otherwise + */ + mraa_result_t write(int d, ...); + /** + * Write a symbol (digit or letter) to the display at a specified index + * + * @param index 0-based index of the digit to change from the left + * @param symbol the digit or letter to display + * @return 0 if the write was successful, error code otherwise + */ + mraa_result_t writeAt(int index, char symbol); + /** + * Write all the digits or letters to the display as a string + * + * @param digits a string of symbols to display + * @return 0 if the write was successful, error code otherwise + */ + mraa_result_t write(std::string digits); + /** + * Toggles the colon between digits on the display + * + * @param value pass true to turn on the colon, false to turn it off + */ + void setColon(bool value); + /** + * Use to control the brightness of the display + * + * @param value between 0 and 7, darkest to brightest + */ + void setBrightness(int value); + + private: + void i2c_start(); + void i2c_stop(); + void i2c_writeByte(uint8_t value); + void update(); + uint8_t encode(char c); + + mraa_gpio_context m_clk, m_dio; + std::string m_name; + uint8_t m_digits[4]; + uint8_t m_brightness; + }; +}