diff --git a/examples/4digitdisplay.cxx b/examples/4digitdisplay.cxx new file mode 100644 index 00000000..f12fe87e --- /dev/null +++ b/examples/4digitdisplay.cxx @@ -0,0 +1,42 @@ +/* + * Author: Yevgeniy Kiveisha + * 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 "tm1637.h" + +int +main(int argc, char **argv) +{ + uint8_t data[] = { 0xaa, 0xff, 0xff, 0xff }; + + upm::TM1637 *display = new upm::TM1637(8, 9); // di - 8, dcki - 9 + display->write ("1981"); + + std::cout << "exiting application" << std::endl; + + delete display; + + return 0; +} diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 6715940d..4f986025 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -5,12 +5,14 @@ add_executable (lcm-lcd lcm-lcd.cxx) add_executable (rgb-lcd rgb-lcd.cxx) add_executable (buzzer-sound buzzer-sound.cxx) add_executable (led-bar led-bar.cxx) +add_executable (seg-lcd 4digitdisplay.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) include_directories (${PROJECT_SOURCE_DIR}/src/lcd) include_directories (${PROJECT_SOURCE_DIR}/src/buzzer) include_directories (${PROJECT_SOURCE_DIR}/src/ledbar) +include_directories (${PROJECT_SOURCE_DIR}/src/4digitdisplay) target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT}) @@ -19,3 +21,4 @@ target_link_libraries (lcm-lcd i2clcd ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (rgb-lcd i2clcd ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (buzzer-sound buzzer ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (led-bar ledbar ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (seg-lcd 4digitdisplay ${CMAKE_THREAD_LIBS_INIT}) diff --git a/src/4digitdisplay/CMakeLists.txt b/src/4digitdisplay/CMakeLists.txt new file mode 100644 index 00000000..dbdaf0ef --- /dev/null +++ b/src/4digitdisplay/CMakeLists.txt @@ -0,0 +1,4 @@ +set (libname "4digitdisplay") +add_library (4digitdisplay SHARED tm1637.cxx) +include_directories (${MAA_INCLUDE_DIR}) +target_link_libraries (4digitdisplay ${MAA_LIBRARIES}) diff --git a/src/4digitdisplay/jsupm_tm1637.i b/src/4digitdisplay/jsupm_tm1637.i new file mode 100644 index 00000000..6a3edeaf --- /dev/null +++ b/src/4digitdisplay/jsupm_tm1637.i @@ -0,0 +1,7 @@ +%module jsupm_tm1637 + +%{ + #include "tm1637.h" +%} + +%include "tm1637.h" diff --git a/src/4digitdisplay/pyupm_tm1637.i b/src/4digitdisplay/pyupm_tm1637.i new file mode 100644 index 00000000..ff6cd6f1 --- /dev/null +++ b/src/4digitdisplay/pyupm_tm1637.i @@ -0,0 +1,8 @@ +%module pyupm_tm1637 + +%feature("autodoc", "3"); + +%include "tm1637.h" +%{ + #include "tm1637.h" +%} diff --git a/src/4digitdisplay/tm1637.cxx b/src/4digitdisplay/tm1637.cxx new file mode 100644 index 00000000..1ddba1ce --- /dev/null +++ b/src/4digitdisplay/tm1637.cxx @@ -0,0 +1,190 @@ +/* + * Author: Yevgeniy Kiveisha + * 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 "tm1637.h" + +using namespace upm; + +const uint8_t digitToSegment[] = { + // XGFEDCBA + 0b00111111, // 0 + 0b00000110, // 1 + 0b01011011, // 2 + 0b01001111, // 3 + 0b01100110, // 4 + 0b01101101, // 5 + 0b01111101, // 6 + 0b00000111, // 7 + 0b01111111, // 8 + 0b01101111, // 9 + 0b01110111, // A + 0b01111100, // B + 0b00111001, // C + 0b01000111, // D + 0b01111001, // E + 0b01110001 // F +}; + +TM1637::TM1637 (uint8_t di, uint8_t dcki) { + maa_result_t error = MAA_SUCCESS; + maa_init(); + + // init clock context + m_clkPinCtx = maa_gpio_init(dcki); + if (m_clkPinCtx == NULL) { + fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", dcki); + exit(1); + } + // init data context + m_dataPinCtx = maa_gpio_init(di); + if (m_dataPinCtx == NULL) { + fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", di); + exit(1); + } + + // set direction (out) + error = maa_gpio_dir(m_clkPinCtx, MAA_GPIO_IN); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } + + // set direction (out) + error = maa_gpio_dir(m_dataPinCtx, MAA_GPIO_IN); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } + + error = maa_gpio_write (m_dataPinCtx, LOW); + error = maa_gpio_write (m_clkPinCtx, LOW); +} + +TM1637::~TM1637() { + maa_result_t error = MAA_SUCCESS; + error = maa_gpio_close (m_dataPinCtx); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } + error = maa_gpio_close (m_clkPinCtx); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } +} + +maa_result_t +TM1637::setBrightness (uint8_t level) { + m_brightness = level; +} + +maa_result_t +TM1637::setSegments (const uint8_t segments[], uint8_t length, uint8_t pos) { + start(); + writeByte(TM1637_I2C_COMM1); + stop(); + + start(); + writeByte(TM1637_I2C_COMM2 + (pos & 0x03)); + for (uint8_t idx = 0; idx < length; idx++) { + writeByte(segments[idx]); + } + stop(); + + start(); + writeByte(TM1637_I2C_COMM3 + (m_brightness & 0x0f)); + stop(); +} + +maa_result_t +TM1637::write (std::string msg) { + char leter = '\0'; + uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 }; + for (uint8_t idx = 0; idx < msg.length(); idx++) { + leter = msg[idx]; + if (idx < 4) { + data[idx] = digitToSegment[strtol(&leter, NULL, 16)]; + } + } + setBrightness (0x0f); + setSegments(data); +} + +maa_result_t +TM1637::pinMode (maa_gpio_context ctx, gpio_dir_t mode) { + maa_result_t error = MAA_SUCCESS; + error = maa_gpio_dir(ctx, mode); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } +} + +maa_result_t +TM1637::start() { + pinMode (m_dataPinCtx, MAA_GPIO_OUT); + usleep(PULSE_LENGTH); +} + +maa_result_t +TM1637::stop() { + pinMode (m_dataPinCtx, MAA_GPIO_OUT); + usleep(PULSE_LENGTH); + pinMode (m_clkPinCtx, MAA_GPIO_IN); + usleep(PULSE_LENGTH); + pinMode (m_dataPinCtx, MAA_GPIO_IN); + usleep(PULSE_LENGTH); +} + +maa_result_t +TM1637::writeByte(uint8_t value) { + for (uint8_t idx = 0; idx < 8; idx++) { + pinMode(m_clkPinCtx, MAA_GPIO_OUT); + usleep(PULSE_LENGTH); + if (value & 0x01) { + pinMode(m_dataPinCtx, MAA_GPIO_IN); + } else { + pinMode(m_dataPinCtx, MAA_GPIO_OUT); + } + usleep(PULSE_LENGTH); + + pinMode(m_clkPinCtx, MAA_GPIO_IN); + usleep(PULSE_LENGTH); + value = value >> 1; + } + + pinMode(m_clkPinCtx, MAA_GPIO_OUT); + pinMode(m_dataPinCtx, MAA_GPIO_IN); + usleep(PULSE_LENGTH); + + pinMode(m_clkPinCtx, MAA_GPIO_IN); + usleep(PULSE_LENGTH); + + uint8_t ack = maa_gpio_read (m_dataPinCtx); + if (ack == 0) { + pinMode(m_dataPinCtx, MAA_GPIO_OUT); + } usleep(PULSE_LENGTH); + + pinMode(m_clkPinCtx, MAA_GPIO_OUT); + usleep(50); +} diff --git a/src/4digitdisplay/tm1637.h b/src/4digitdisplay/tm1637.h new file mode 100644 index 00000000..79c0ef36 --- /dev/null +++ b/src/4digitdisplay/tm1637.h @@ -0,0 +1,82 @@ +/* + * Author: Yevgeniy Kiveisha + * 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 +#include + +// +// A +// --- +// F | | B +// -G- +// E | | C +// --- +// D + +#define SEG_A 0b00000001 +#define SEG_B 0b00000010 +#define SEG_C 0b00000100 +#define SEG_D 0b00001000 +#define SEG_E 0b00010000 +#define SEG_F 0b00100000 +#define SEG_G 0b01000000 + +#define TM1637_I2C_COMM1 0x40 +#define TM1637_I2C_COMM2 0xC0 +#define TM1637_I2C_COMM3 0x80 + +#define PULSE_LENGTH 50 + +#define HIGH 1 +#define LOW 0 + +namespace upm { + +class TM1637 { + public: + TM1637 (uint8_t di, uint8_t dcki); + ~TM1637 (); + maa_result_t setBrightness (uint8_t level); + maa_result_t setSegments (const uint8_t segments[], uint8_t length = 4, uint8_t pos = 0); + maa_result_t write (std::string msg); + + std::string name() + { + return m_name; + } + private: + maa_result_t start(); + maa_result_t stop(); + maa_result_t writeByte (uint8_t value); + maa_result_t pinMode (maa_gpio_context ctx, gpio_dir_t mode); + + std::string m_name; + maa_gpio_context m_clkPinCtx; + maa_gpio_context m_dataPinCtx; + uint8_t m_brightness; +}; + +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 04ca9141..5db9c014 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,3 +3,4 @@ add_subdirectory (grove) add_subdirectory (lcd) add_subdirectory (buzzer) add_subdirectory (ledbar) +add_subdirectory (4digitdisplay)