mirror of
https://github.com/eclipse/upm.git
synced 2025-07-02 01:41:12 +03:00
lpd8806: Added new module - digital led strip (NOT TESTED)
Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
This commit is contained in:
BIN
src/lpd8806/.DS_Store
vendored
Normal file
BIN
src/lpd8806/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
src/lpd8806/._.DS_Store
Normal file
BIN
src/lpd8806/._.DS_Store
Normal file
Binary file not shown.
BIN
src/lpd8806/._CMakeLists.txt
Normal file
BIN
src/lpd8806/._CMakeLists.txt
Normal file
Binary file not shown.
BIN
src/lpd8806/._jsupm_lpd8806.i
Normal file
BIN
src/lpd8806/._jsupm_lpd8806.i
Normal file
Binary file not shown.
BIN
src/lpd8806/._pyupm_lpd8806.i
Normal file
BIN
src/lpd8806/._pyupm_lpd8806.i
Normal file
Binary file not shown.
5
src/lpd8806/CMakeLists.txt
Normal file
5
src/lpd8806/CMakeLists.txt
Normal file
@ -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()
|
8
src/lpd8806/jsupm_lpd8806.i
Normal file
8
src/lpd8806/jsupm_lpd8806.i
Normal file
@ -0,0 +1,8 @@
|
||||
%module jsupm_lpd8806
|
||||
%include "../upm.i"
|
||||
|
||||
%{
|
||||
#include "lpd8806.h"
|
||||
%}
|
||||
|
||||
%include "lpd8806.h"
|
146
src/lpd8806/lpd8806.cxx
Normal file
146
src/lpd8806/lpd8806.cxx
Normal file
@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||
* 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 <iostream>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <cstring>
|
||||
|
||||
#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);
|
||||
}
|
105
src/lpd8806/lpd8806.h
Normal file
105
src/lpd8806/lpd8806.h
Normal file
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||
* 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 <string>
|
||||
#include <mraa/aio.h>
|
||||
#include <mraa/gpio.h>
|
||||
#include <mraa/spi.h>
|
||||
|
||||
#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 ();
|
||||
};
|
||||
|
||||
}
|
9
src/lpd8806/pyupm_lpd8806.i
Normal file
9
src/lpd8806/pyupm_lpd8806.i
Normal file
@ -0,0 +1,9 @@
|
||||
%module pyupm_lpd8806
|
||||
%include "../upm.i"
|
||||
|
||||
%feature("autodoc", "3");
|
||||
|
||||
%include "lpd8806.h"
|
||||
%{
|
||||
#include "lpd8806.h"
|
||||
%}
|
Reference in New Issue
Block a user