From 050c13466405d341d81381f8621e80dbee4f7092 Mon Sep 17 00:00:00 2001 From: Kiveisha Yevgeniy Date: Mon, 18 Aug 2014 11:21:48 +0000 Subject: [PATCH] lpd8806: Added new module - digital led strip (NOT TESTED) Signed-off-by: Kiveisha Yevgeniy --- examples/CMakeLists.txt | 3 + examples/lpd8806-example.cxx | 104 ++++++++++++++++++++++++++++++ src/lpd8806/.DS_Store | Bin 0 -> 6148 bytes src/lpd8806/._.DS_Store | Bin 0 -> 4096 bytes src/lpd8806/._CMakeLists.txt | Bin 0 -> 4096 bytes src/lpd8806/._jsupm_lpd8806.i | Bin 0 -> 4096 bytes src/lpd8806/._pyupm_lpd8806.i | Bin 0 -> 4096 bytes src/lpd8806/CMakeLists.txt | 5 ++ src/lpd8806/jsupm_lpd8806.i | 8 +++ src/lpd8806/lpd8806.cxx | 146 ++++++++++++++++++++++++++++++++++++++++++ src/lpd8806/lpd8806.h | 105 ++++++++++++++++++++++++++++++ src/lpd8806/pyupm_lpd8806.i | 9 +++ 12 files changed, 380 insertions(+) create mode 100644 examples/lpd8806-example.cxx create mode 100644 src/lpd8806/.DS_Store create mode 100644 src/lpd8806/._.DS_Store create mode 100644 src/lpd8806/._CMakeLists.txt create mode 100644 src/lpd8806/._jsupm_lpd8806.i create mode 100644 src/lpd8806/._pyupm_lpd8806.i create mode 100644 src/lpd8806/CMakeLists.txt create mode 100644 src/lpd8806/jsupm_lpd8806.i create mode 100644 src/lpd8806/lpd8806.cxx create mode 100644 src/lpd8806/lpd8806.h create mode 100644 src/lpd8806/pyupm_lpd8806.i diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4a57948..b2d3096 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -26,6 +26,7 @@ add_executable (max31723-example max31723.cxx) add_executable (max5487-example max5487.cxx) add_executable (nrf8001-broadcast-example nrf8001_broadcast.cxx) add_executable (nrf8001-helloworld-example nrf8001_helloworld.cxx) +add_executable (lpd8806-example lpd8806-example.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -49,6 +50,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/maxds3231m) include_directories (${PROJECT_SOURCE_DIR}/src/max31723) include_directories (${PROJECT_SOURCE_DIR}/src/max5487) include_directories (${PROJECT_SOURCE_DIR}/src/nrf8001) +include_directories (${PROJECT_SOURCE_DIR}/src/lpd8806) target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT}) @@ -78,3 +80,4 @@ target_link_libraries (max31723-example max31723 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (max5487-example max5487 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (nrf8001-broadcast-example nrf8001 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (nrf8001-helloworld-example nrf8001 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (lpd8806-example lpd8806 ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/lpd8806-example.cxx b/examples/lpd8806-example.cxx new file mode 100644 index 0000000..8828f07 --- /dev/null +++ b/examples/lpd8806-example.cxx @@ -0,0 +1,104 @@ +/* + * 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 "lpd8806.h" +#include + +void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait); + +int doWork = 0; +upm::LPD8806 *sensor = NULL; + +void +sig_handler(int signo) +{ + printf("got signal\n"); + if (signo == SIGINT) { + printf("exiting application\n"); + doWork = 1; + } +} + +int +main(int argc, char **argv) +{ + //! [Interesting] + sensor = new upm::LPD8806(10, 7); + usleep (1000000); + + sensor->show (); + + while (!doWork) { + // Back-and-forth lights + scanner(127, 0, 0, 30); // red, slow + scanner(0, 0, 127, 15); // blue, fast + usleep (1000000); + } + //! [Interesting] + + std::cout << "exiting application" << std::endl; + + delete sensor; + + return 0; +} + +void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) { + int i, j, pos, dir; + + pos = 0; + dir = 1; + + for(i=0; i < ((sensor->getStripLength() - 1) * 8); i++) { + // Draw 5 pixels centered on pos. setPixelColor() will clip + // any pixels off the ends of the strip, no worries there. + // we'll make the colors dimmer at the edges for a nice pulse + // look + sensor->setPixelColor(pos - 2, r/4, g/4, b/4); + sensor->setPixelColor(pos - 1, r/2, g/2, b/2); + sensor->setPixelColor(pos, r, g, b); + sensor->setPixelColor(pos + 1, r/2, g/2, b/2); + sensor->setPixelColor(pos + 2, r/4, g/4, b/4); + + sensor->show(); + usleep (wait * 1000); + // If we wanted to be sneaky we could erase just the tail end + // pixel, but it's much easier just to erase the whole thing + // and draw a new one next time. + for(j=-2; j<= 2; j++) { + sensor->setPixelColor(pos+j, 0,0,0); + } + // Bounce off ends of strip + pos += dir; + if(pos < 0) { + pos = 1; + dir = -dir; + } else if (pos >= sensor->getStripLength()) { + pos = sensor->getStripLength() - 2; + dir = -dir; + } + } +} diff --git a/src/lpd8806/.DS_Store b/src/lpd8806/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..095d5c96f7da1aa75bc22f4fe004887537e0f3f2 GIT binary patch literal 6148 zcmeHK!AiqG6r8Q8wiL8Sk9+h~^dcTZt%9JJ`T=TN!D^%hTd22uh9Bhz_&0Ih?jx8+ z!INNlQ{J1+W+$7>mb@$g*I!PKfG&U*L%7jq`UY}dqK1a{G|4Vp;~tlo;spza;XgPa zYqyJ2T;UE+__}^QuHp)di)=O?WwQ~-$)Dt$alqIq3T9vrk6h~|=6J>pGV%l`{2k&L zJxtfs?^K9-Th*PKV$3;LRh1j6Dp6Itrg~UYe5j&X-sK#xzrIU(#<(aCJ`+Xbl~d(BGS3h$O36{` zbc^Aloc&(MrOJ8aD2LO{htrvz?oeEqo!9qaI9wXpvN>Q5R2|rghh3@vXFu=%t0Frx z2h4$ua=;~n>p`E7q_ws7a8heM$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIk*Y|peR=07!nc$ zlyHUUV5q>VXjE`C1V%$(Gz3ONU^E0qLtr!nMnhmU1V%$(Gz3ONU^E1%90H6$^FSC3 z$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103v0xI!` z=wO%wWb>nGVFdD_;^N8qxq68O1v#mDA*mH5u6fD%DVcfcKn0~GX}T8HhQ=mFhUS(Q zCTK>Caz{g8Gz3ONU^E0qLtr!nMnhmU1V%$(Gz3ONU^E0qLjW}dK%Gqx1_QZ}jLc$% zqSWI2(xT*4g|z&lY=z9clGMDC%>2B>oSaI9oYb@ug`}Lsylh}!7^-VXQ>gxjdqsvp H?*D%P$J8kb literal 0 HcmV?d00001 diff --git a/src/lpd8806/._jsupm_lpd8806.i b/src/lpd8806/._jsupm_lpd8806.i new file mode 100644 index 0000000000000000000000000000000000000000..59e6f403a2453d97fd185cf2d3f17e81a13fb618 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103v0xAJw zkPe19K+KP(g%Kexo}8bnmsn7cld2bzT2bPfmzVLRbWEkZB G{|5l}!zm2_ literal 0 HcmV?d00001 diff --git a/src/lpd8806/._pyupm_lpd8806.i b/src/lpd8806/._pyupm_lpd8806.i new file mode 100644 index 0000000000000000000000000000000000000000..b4ee648c67e5e41e64d2726e3e87287bbb6b8f3a GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103v0xAiG z=wO%wWb>nGVFdD_;^N8qxq68O1v#mDA*mH5u6fD%DVcfcKn0~GX}T8HhQ=mFhUS(Q zCTK>Caz{g8Gz3ONU^E0qLtr!nMnhmU1V%$(Gz3ONU^E0qLjW}dK%Gqx1_QZ}jLc$% zqSWI2(xT*4g|z&lY=z9clGMDC%>2B>oSaI9oYb@ug`}Lsylh}!7^-VXQ>gxjdqsvp H?*D%PCBZ2W literal 0 HcmV?d00001 diff --git a/src/lpd8806/CMakeLists.txt b/src/lpd8806/CMakeLists.txt new file mode 100644 index 0000000..146ce83 --- /dev/null +++ b/src/lpd8806/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "lpd8806") +set (libdescription “Digital RGB LED strip”) +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init() diff --git a/src/lpd8806/jsupm_lpd8806.i b/src/lpd8806/jsupm_lpd8806.i new file mode 100644 index 0000000..279c8fe --- /dev/null +++ b/src/lpd8806/jsupm_lpd8806.i @@ -0,0 +1,8 @@ +%module jsupm_lpd8806 +%include "../upm.i" + +%{ + #include "lpd8806.h" +%} + +%include "lpd8806.h" diff --git a/src/lpd8806/lpd8806.cxx b/src/lpd8806/lpd8806.cxx new file mode 100644 index 0000000..282fd61 --- /dev/null +++ b/src/lpd8806/lpd8806.cxx @@ -0,0 +1,146 @@ +/* + * 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 +#include + +#include "lpd8806.h" + +using namespace upm; + +struct LPD8806Exception : public std::exception { + std::string message; + LPD8806Exception (std::string msg) : message (msg) { } + ~LPD8806Exception () throw () { } + const char* what() const throw () { return message.c_str(); } +}; + +LPD8806::LPD8806 (uint16_t pixelCount, uint8_t csn) { + mraa_result_t error = MRAA_SUCCESS; + m_name = "LPD8806"; + + m_pixels = NULL; + + m_csnPinCtx = mraa_gpio_init (csn); + if (m_csnPinCtx == NULL) { + throw LPD8806Exception ("GPIO failed to initilize"); + } + + error = mraa_gpio_dir (m_csnPinCtx, MRAA_GPIO_OUT); + if (error != MRAA_SUCCESS) { + throw LPD8806Exception ("GPIO failed to initilize"); + } + + CSOff (); + + m_spi = mraa_spi_init (0); + if (m_spi == NULL) { + throw LPD8806Exception ("SPI failed to initilize"); + } + + // set spi mode to mode2 (CPOL = 0, CPHA = 0) + mraa_spi_mode (m_spi, MRAA_SPI_MODE0); + + CSOn (); + // issue initial latch/reset to strip: + for (uint16_t i = ((pixelCount + 31) / 32); i > 0; i--) { + mraa_spi_write (m_spi, 0); + } + CSOff (); + + m_pixelsCount = pixelCount; + + uint8_t latchBytes; + uint16_t dataBytes, totalBytes; + uint16_t numBytes = 0; + + dataBytes = m_pixelsCount * 3; + latchBytes = (m_pixelsCount + 31) / 32; + totalBytes = dataBytes + latchBytes; + if ((m_pixels = (uint8_t *) malloc(totalBytes))) { + numBytes = totalBytes; + memset ( m_pixels , 0x80, dataBytes); // Init to RGB 'off' state + memset (&m_pixels[dataBytes], 0 , latchBytes); // Clear latch bytes + } +} + +LPD8806::~LPD8806() { + mraa_result_t error = MRAA_SUCCESS; + + if (m_pixels) { + free(m_pixels); + } + + error = mraa_spi_stop(m_spi); + if (error != MRAA_SUCCESS) { + mraa_result_print(error); + } + error = mraa_gpio_close (m_csnPinCtx); + if (error != MRAA_SUCCESS) { + mraa_result_print(error); + } +} + +void +LPD8806::setPixelColor (uint16_t pixelOffset, uint8_t r, uint8_t g, uint8_t b) { + if (pixelOffset < m_pixelsCount) { // Arrays are 0-indexed, thus NOT '<=' + uint8_t *ptr = &m_pixels[pixelOffset * 3]; + *ptr++ = g | 0x80; // Strip color order is GRB, + *ptr++ = r | 0x80; // not the more common RGB, + *ptr++ = b | 0x80; // so the order here is intentional; don't "fix" + } +} + +void +LPD8806::show (void) { + uint8_t *ptr = m_pixels; + uint16_t byte = (m_pixelsCount * 3) + ((m_pixelsCount + 31) / 32); + + while (byte--) { + mraa_spi_write (m_spi, *ptr++); + } +} + +uint16_t +LPD8806::getStripLength (void) { + return m_pixelsCount; +} + +/* + * ************** + * private area + * ************** + */ + +mraa_result_t +LPD8806::CSOn () { + return mraa_gpio_write (m_csnPinCtx, HIGH); +} + +mraa_result_t +LPD8806::CSOff () { + return mraa_gpio_write (m_csnPinCtx, LOW); +} diff --git a/src/lpd8806/lpd8806.h b/src/lpd8806/lpd8806.h new file mode 100644 index 0000000..a84b8e1 --- /dev/null +++ b/src/lpd8806/lpd8806.h @@ -0,0 +1,105 @@ +/* + * 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 +#include + +#define HIGH 1 +#define LOW 0 + +namespace upm { + +/** + * @brief C++ API for LPD8806 + * + * This file defines the LPD8806 C++ interface for liblpd8806 + * + */ +class LPD8806 { + public: + + /** + * Instanciates a LPD8806 object + * + * @param pixelCount number of pixels in the strip + * @param csn chip slect pin + */ + LPD8806 (uint16_t pixelCount, uint8_t csn); + + /** + * LPD8806 object destructor, basicaly it close SPI and the GPIO. + */ + ~LPD8806 (); + + /** + * @param pixelOffset pixel offset in the strip of pixel + * @param r red led + * @param g green led + * @param b blue led + */ + void setPixelColor (uint16_t pixelOffset, uint8_t r, uint8_t g, uint8_t b); + + /** + * Write the data stored in array of pixels to the chip + */ + void show (void); + + /** + * Return length of the led strip + */ + uint16_t getStripLength (void); + + /** + * Return name of the component + */ + std::string name() + { + return m_name; + } + private: + std::string m_name; + mraa_spi_context m_spi; + mraa_gpio_context m_csnPinCtx; + + uint8_t* m_pixels; + uint8_t m_pixelsCount; + + uint8_t readRegister (uint8_t reg); + void writeRegister (uint8_t reg, uint8_t data); + + /** + * Set chip select pin LOW + */ + mraa_result_t CSOn (); + + /** + * Set chip select pin HIGH + */ + mraa_result_t CSOff (); +}; + +} diff --git a/src/lpd8806/pyupm_lpd8806.i b/src/lpd8806/pyupm_lpd8806.i new file mode 100644 index 0000000..bf5cf21 --- /dev/null +++ b/src/lpd8806/pyupm_lpd8806.i @@ -0,0 +1,9 @@ +%module pyupm_lpd8806 +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "lpd8806.h" +%{ + #include "lpd8806.h" +%} -- 2.7.4