From d97ce2378b6eab5639dc0dde0b4ec7974f8bec5c Mon Sep 17 00:00:00 2001 From: izard Date: Sat, 6 Sep 2014 12:57:28 +0000 Subject: [PATCH] lol: Added Olimex LoL array support, 13x9 LED matrix Signed-off-by: izard Signed-off-by: Brendan Le Foll Signed-off-by: Thomas Ingleby --- examples/CMakeLists.txt | 3 + examples/lol-example.cxx | 67 ++++++++++++ src/lol/CMakeLists.txt | 5 + src/lol/jsupm_lol.i | 8 ++ src/lol/lol.cxx | 218 +++++++++++++++++++++++++++++++++++++++ src/lol/lol.h | 93 +++++++++++++++++ src/lol/pyupm_lol.i | 11 ++ 7 files changed, 405 insertions(+) create mode 100644 examples/lol-example.cxx create mode 100644 src/lol/CMakeLists.txt create mode 100644 src/lol/jsupm_lol.i create mode 100644 src/lol/lol.cxx create mode 100644 src/lol/lol.h create mode 100644 src/lol/pyupm_lol.i diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index fad4f32a..8f6cb885 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -37,6 +37,7 @@ add_executable (tcs3414cs-example tcs3414cs.cxx) add_executable (th02-example th02.cxx) add_executable (lsm303-example lsm303.cxx) add_executable (joystick12-example joystick12-example.cxx) +add_executable (lol-example lol-example.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -68,6 +69,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/tcs3414cs) include_directories (${PROJECT_SOURCE_DIR}/src/th02) include_directories (${PROJECT_SOURCE_DIR}/src/lsm303) include_directories (${PROJECT_SOURCE_DIR}/src/joystick12) +include_directories (${PROJECT_SOURCE_DIR}/src/lol) target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT}) @@ -108,3 +110,4 @@ target_link_libraries (tcs3414cs-example tcs3414cs ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (th02-example th02 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (lsm303-example lsm303 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (joystick12-example joystick12 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (lol-example lol ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/lol-example.cxx b/examples/lol-example.cxx new file mode 100644 index 00000000..9f2943de --- /dev/null +++ b/examples/lol-example.cxx @@ -0,0 +1,67 @@ +/* + * Author: Alexander Komarov + * 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 +#include "lol.h" +#include +#include + +int is_running = 0; +upm::LoL *sensor = NULL; + +void +sig_handler(int signo) +{ + printf("got signal\n"); + if (signo == SIGINT) { + is_running = 1; + } +} + +//! [Interesting] +int +main(int argc, char **argv) +{ + sensor = new upm::LoL(); + signal(SIGINT, sig_handler); + + unsigned char *buffer; + //buffer = sensor->getFramebuffer(); + int x = 0, y = 0; + while (!is_running) { + sensor->setPixel(x, y, !(sensor->getPixel(x, y))); + if (++x == 13) { x = 0; y++; } + if (y == 9) y = 0; + usleep(10000); + } + + std::cout << "exiting application" << std::endl; + + delete sensor; + + return 0; +} +//! [Interesting] diff --git a/src/lol/CMakeLists.txt b/src/lol/CMakeLists.txt new file mode 100644 index 00000000..d43c02b2 --- /dev/null +++ b/src/lol/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "lol") +set (libdescription "LoL Olimex LoL rev A") +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init() diff --git a/src/lol/jsupm_lol.i b/src/lol/jsupm_lol.i new file mode 100644 index 00000000..73659e89 --- /dev/null +++ b/src/lol/jsupm_lol.i @@ -0,0 +1,8 @@ +%module jsupm_lol +%include "../upm.i" + +%{ + #include "lol.h" +%} + +%include "lol.h" diff --git a/src/lol/lol.cxx b/src/lol/lol.cxx new file mode 100644 index 00000000..05d7c739 --- /dev/null +++ b/src/lol/lol.cxx @@ -0,0 +1,218 @@ +/* + * Author: Alexander Komarov + * 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 +#include +#include +#include "lol.h" + +using namespace upm; + +static mraa_gpio_context *m_Ctx; +static unsigned char *buffer; + +static mraa_gpio_context c1, c2, c3, c4; + +static int charlie_pairs [12][22] = { +{3,124, 4,110, 5,96, 6,82, 7,68, 8,54, 9,40, 10,26, 11,12, -1,-1, -1,-1}, +{3,122, 4,108, 5,94, 6,80, 7,66, 8,52, 9,38, 10,24, 11,10, -1,-1, -1,-1}, +{3,120, 4,106, 5,92, 6,78, 7,64, 8,50, 9,36, 10,22, 11,8, -1,-1, -1,-1}, +{0,125, 1,123, 2,121, 4,98, 5,84, 6,70, 7,56, 8,42, 9,28, 10,14, 11,0}, +{0,111, 1,109, 2,107, 3,112, 5,85, 6,71, 7,57, 8,43, 9,29, 10,15, 11,1}, +{0,97, 1,95, 2,93, 3,113, 4,99, 6,72, 7,58, 8,44, 9,30, 10,16, 11,2}, +{0,83, 1,81, 2,79, 3,114, 4,100,5,86, 7,59, 8,45, 9,31, 10,17, 11,3}, +{0,69, 1,67, 2,65, 3,115, 4,101,5,87, 6,73, 8,46, 9,32, 10,18, 11,4}, +{0,55, 1,53, 2,51, 3,116, 4,102,5,88, 6,74, 7,60, 9,33, 10,19, 11,5}, +{0,41, 1,39, 2,37, 3,117, 4,103,5,89, 6,75, 7,61, 8,47, 10,20, 11,6}, +{0,27, 1,25, 2,23, 3,118, 4,104,5,90, 6,76, 7,62, 8,48, 9,34, 11,7}, +{0,13, 1,11, 2,9, 3,119, 4,105,5,91, 6,77, 7,63, 8,49, 9,35, 10,21} +}; + +//static int gpios_map[] = { +//14,15,28,17,24,27,26,19,16,25,38,39, +//}; + +void clear_gpio(int gpio) +{ +/* FIXME AK hack around Galileo gen1 issue, to remove + if(gpio == 0) + { + mraa_gpio_mode(c1, MRAA_GPIO_HIZ); + mraa_gpio_mode(c2, MRAA_GPIO_HIZ); + } + else if (gpio == 1) { + mraa_gpio_mode(c3, MRAA_GPIO_HIZ); + mraa_gpio_mode(c4, MRAA_GPIO_HIZ); + } else*/ + mraa_gpio_mode(m_Ctx[gpio], MRAA_GPIO_HIZ); + + mraa_gpio_dir(m_Ctx[gpio], MRAA_GPIO_IN); +} + +void clear_prev_cycle(int cycle) +{ + int i; + +// What is prev cycle? + cycle--; + if (cycle == -1) + cycle = 11; + +// Disable all "1"'s + for (i = 0; i < 11; i++) { + if (charlie_pairs[cycle][i*2] == -1) + break; + if(buffer[charlie_pairs[cycle][i*2 + 1]]) + clear_gpio(charlie_pairs[cycle][i*2]); + } + +// Disable "0" + clear_gpio(cycle); +} + +void set_strong_one(int gpio) +{ + mraa_gpio_dir(m_Ctx[gpio], MRAA_GPIO_OUT); +/* FIXME AK hack around Galileo gen1 issue, to remove + if(gpio == 0) + { + mraa_gpio_mode(c1, MRAA_GPIO_STRONG); + mraa_gpio_mode(c2, MRAA_GPIO_STRONG); + } + else if (gpio == 1) { + mraa_gpio_mode(c3, MRAA_GPIO_STRONG); + mraa_gpio_mode(c4, MRAA_GPIO_STRONG); + } else*/ + mraa_gpio_mode(m_Ctx[gpio], MRAA_GPIO_STRONG); + mraa_gpio_write(m_Ctx[gpio], 1); +} + +void set_strong_zero(int gpio) +{ + mraa_gpio_dir(m_Ctx[gpio], MRAA_GPIO_OUT); +/* FIXME AK hack around Galileo gen1 issue, to remove + if(gpio == 0) + { + mraa_gpio_mode(c1, MRAA_GPIO_STRONG); + mraa_gpio_mode(c2, MRAA_GPIO_STRONG); + } + else if (gpio == 1) { + mraa_gpio_mode(c3, MRAA_GPIO_STRONG); + mraa_gpio_mode(c4, MRAA_GPIO_STRONG); + } else*/ + mraa_gpio_mode(m_Ctx[gpio], MRAA_GPIO_STRONG); + mraa_gpio_write(m_Ctx[gpio], 0); +} + + +static void clear_gpios() +{ + int i; + for (i = 0; i < 12; i++) + clear_gpio(i); +} + +void *do_draw(void *arg) +{ +/* c1 = mraa_gpio_init_raw(31); + c2 = mraa_gpio_init_raw(32); + c3 = mraa_gpio_init_raw(30); + c4 = mraa_gpio_init_raw(18); +*/ + + clear_gpios(); + + while (1) { + int i, cur; + uint8_t cycle = 0; +// 12 Cycles of Matrix + for (cycle = 0; cycle < 12; cycle++) + { + if (cycle == 12) cycle = 0; + + clear_prev_cycle(cycle); +// set strong/0 on current cycle line + set_strong_zero(cycle); + +// draw ones from framebuffer + for (i = 0; i < 11; i++) { + cur = charlie_pairs[cycle][i*2]; + if (cur == -1) break; + + if (buffer[charlie_pairs[cycle][i*2 + 1]]) { + set_strong_one(cur); +// printf("cycle %d %d %d %d\n", cycle, i, charlie_pairs[cycle][i*2 + 1], +// buffer[charlie_pairs[cycle][i*2 + 1]]); + } + } + } + + } +} + +LoL::LoL() { + int i = 0; + mraa_result_t error; + for (i = 0; i < 12; i++) + m_LoLCtx[i] = mraa_gpio_init(i+2); + + memset(framebuffer, 0, LOL_X*LOL_Y); + + // I am optimistic and stupid - thread creation + // always works in my world + buffer = (unsigned char*)framebuffer; + m_Ctx = m_LoLCtx; + pthread_create (&drawer_thread, NULL, do_draw, NULL); +} + +LoL::~LoL() { + int i = 0; + mraa_result_t error; + for (i = 0; i < 12; i++) + mraa_gpio_close(m_LoLCtx[i]); +} + +unsigned char* LoL::getFramebuffer() { + return framebuffer; +} + +unsigned char LoL::setPixel(int x, int y, unsigned char pixel) +{ + if (x < 0 || y < 0 || x >= LOL_X || y >= LOL_Y) + return -1; + + framebuffer[x + LOL_X*y] = (pixel == 0) ? 0 : 1; + return 0; +} + +unsigned char LoL::getPixel(int x, int y) +{ + if (x < 0 || y < 0 || x >= LOL_X || y >= LOL_Y) + return -1; + + return (framebuffer[x + LOL_X*y] == 0) ? 0 : 1; +} + diff --git a/src/lol/lol.h b/src/lol/lol.h new file mode 100644 index 00000000..b1f1fb8e --- /dev/null +++ b/src/lol/lol.h @@ -0,0 +1,93 @@ +/* + * Author: Alexander Komarov + * 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 +#include + +namespace upm { + +#define LOL_X 14 +#define LOL_Y 9 +/** + * @brief C++ API for Olimex LoL array + * + * This file defines the LoL API and implemntation for + * a simple framebuffer + * + * @snippet lol-example.cxx Interesting + * + */ +class LoL { + public: + /** + * Instantiates a LoL object + * singleton + */ + LoL(); + + /** + * LoL object destructor + */ + ~LoL(); + + /** + * Get framebuffer pointer + * @return 0 on success + */ + unsigned char *getFramebuffer(); + + /** + * Gets pixel at coordinates + * @param x coordinate x + * @param y coordinate y + * @return 1 if pixel is on, 0 if off, -1 on error + */ + unsigned char getPixel(int x, int y); + + /** + * sets pixel at coordinates + * @param x coordinate x + * @param y coordinate y + * @param pixel 0 is off, 1 is on + * @return 0 on success, -1 on error + */ + unsigned char setPixel(int x, int y, unsigned char pixel); + + /** + * Get framebuffer pointer + * @return 1 if pixel is on, 0 if off, -1 on error + */ + unsigned char getBit(int x, int y); + + + private: + mraa_gpio_context m_LoLCtx[14]; + unsigned char framebuffer[LOL_X*LOL_Y]; + pthread_t drawer_thread; +}; +}; + diff --git a/src/lol/pyupm_lol.i b/src/lol/pyupm_lol.i new file mode 100644 index 00000000..1957ed70 --- /dev/null +++ b/src/lol/pyupm_lol.i @@ -0,0 +1,11 @@ +%module pyupm_lol +%include "../upm.i" + +%include "stdint.i" + +%feature("autodoc", "3"); + +%include "lol.h" +%{ + #include "lol.h" +%}