diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 747f39e9..6715940d 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -4,11 +4,13 @@ add_executable (grovetemp grovetemp.cxx) 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) 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) target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT}) @@ -16,3 +18,4 @@ target_link_libraries (grovetemp grove ${CMAKE_THREAD_LIBS_INIT}) 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}) diff --git a/examples/led-bar.cxx b/examples/led-bar.cxx new file mode 100644 index 00000000..02ed4435 --- /dev/null +++ b/examples/led-bar.cxx @@ -0,0 +1,61 @@ +/* + * 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 "my9221.h" +#include + +int running = 0; + +void +sig_handler(int signo) +{ + printf("got signal\n"); + if (signo == SIGINT) { + printf("exiting application\n"); + running = 1; + } +} + +int +main(int argc, char **argv) +{ + upm::MY9221 *bar = new upm::MY9221(8, 9); + + signal(SIGINT, sig_handler); + + while (!running) { + for (int idx = 1; idx < 11; idx++) { + bar->setBarLevel (idx); + usleep(1000); + } + } + + std::cout << "exiting application" << std::endl; + + delete bar; + + return 0; +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8136f7cd..04ca9141 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,3 +2,4 @@ add_subdirectory (hmc5883l) add_subdirectory (grove) add_subdirectory (lcd) add_subdirectory (buzzer) +add_subdirectory (ledbar) diff --git a/src/ledbar/CMakeLists.txt b/src/ledbar/CMakeLists.txt new file mode 100644 index 00000000..b2af8c3d --- /dev/null +++ b/src/ledbar/CMakeLists.txt @@ -0,0 +1,4 @@ +set (libname "ledbar") +add_library (ledbar SHARED my9221.cxx) +include_directories (${MAA_INCLUDE_DIR}) +target_link_libraries (ledbar ${MAA_LIBRARIES}) diff --git a/src/ledbar/jsupm_my9221.i b/src/ledbar/jsupm_my9221.i new file mode 100644 index 00000000..937f9713 --- /dev/null +++ b/src/ledbar/jsupm_my9221.i @@ -0,0 +1,7 @@ +%module jsupm_my9221 + +%{ + #include "my9221.h" +%} + +%include "my9221.h" diff --git a/src/ledbar/my9221.cxx b/src/ledbar/my9221.cxx new file mode 100644 index 00000000..8575ba7e --- /dev/null +++ b/src/ledbar/my9221.cxx @@ -0,0 +1,118 @@ +/* + * 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 "my9221.h" + +using namespace upm; + +MY9221::MY9221 (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_OUT); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } + + // set direction (out) + error = maa_gpio_dir(m_dataPinCtx, MAA_GPIO_OUT); + if (error != MAA_SUCCESS) { + maa_result_print(error); + } +} + +MY9221::~MY9221() { + 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 +MY9221::setBarLevel (uint8_t level) { + if (level > 10) { + return MAA_ERROR_INVALID_PARAMETER; + } + + send16bitBlock (CMDMODE); + for(uint8_t block_idx = 0; block_idx < 12; block_idx++) { + uint32_t state = (block_idx < level) ? BIT_HIGH : BIT_LOW; + send16bitBlock (state); + } + lockData (); +} + +maa_result_t +MY9221::lockData () { + maa_result_t error = MAA_SUCCESS; + error = maa_gpio_write (m_dataPinCtx, LOW); + usleep(100); + + for(int idx = 0; idx < 4; idx++) { + error = maa_gpio_write (m_dataPinCtx, HIGH); + error = maa_gpio_write (m_dataPinCtx, LOW); + } +} + +maa_result_t +MY9221::send16bitBlock (short data) { + maa_result_t error = MAA_SUCCESS; + for (uint8_t bit_idx = 0; bit_idx < MAX_BIT_PER_BLOCK; bit_idx++) { + uint32_t state = (data & 0x8000) ? HIGH : LOW; + error = maa_gpio_write (m_dataPinCtx, state); + state = maa_gpio_read (m_clkPinCtx); + + if (state) { + state = LOW; + } else { + state = HIGH; + } + + error = maa_gpio_write (m_clkPinCtx, state); + + data <<= 1; + } +} diff --git a/src/ledbar/my9221.h b/src/ledbar/my9221.h new file mode 100644 index 00000000..4850275a --- /dev/null +++ b/src/ledbar/my9221.h @@ -0,0 +1,58 @@ +/* + * 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 + +#define MAX_BIT_PER_BLOCK 16 +#define CMDMODE 0x0000 +#define BIT_HIGH 0x00ff +#define BIT_LOW 0x0000 + +#define HIGH 1 +#define LOW 0 + +namespace upm { + +class MY9221 { + public: + MY9221 (uint8_t di, uint8_t dcki); + ~MY9221 (); + maa_result_t setBarLevel (uint8_t level); + std::string name() + { + return m_name; + } + private: + maa_result_t lockData (); + maa_result_t send16bitBlock (short data); + + std::string m_name; + maa_gpio_context m_clkPinCtx; + maa_gpio_context m_dataPinCtx; +}; + +} diff --git a/src/ledbar/pyupm_my9221.i b/src/ledbar/pyupm_my9221.i new file mode 100644 index 00000000..d28bc201 --- /dev/null +++ b/src/ledbar/pyupm_my9221.i @@ -0,0 +1,8 @@ +%module pyupm_my9221 + +%feature("autodoc", "3"); + +%include "my9221.h" +%{ + #include "my9221.h" +%}