2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 struct LPD8806Exception : public std::exception {
36 LPD8806Exception (std::string msg) : message (msg) { }
37 ~LPD8806Exception () throw () { }
38 const char* what() const throw () { return message.c_str(); }
41 LPD8806::LPD8806 (uint16_t pixelCount, uint8_t csn) {
42 mraa_result_t error = MRAA_SUCCESS;
47 m_csnPinCtx = mraa_gpio_init (csn);
48 if (m_csnPinCtx == NULL) {
49 throw LPD8806Exception ("GPIO failed to initilize");
52 error = mraa_gpio_dir (m_csnPinCtx, MRAA_GPIO_OUT);
53 if (error != MRAA_SUCCESS) {
54 throw LPD8806Exception ("GPIO failed to initilize");
59 m_spi = mraa_spi_init (0);
61 throw LPD8806Exception ("SPI failed to initilize");
64 // set spi mode to mode2 (CPOL = 0, CPHA = 0)
65 mraa_spi_mode (m_spi, MRAA_SPI_MODE0);
68 // issue initial latch/reset to strip:
69 for (uint16_t i = ((pixelCount + 31) / 32); i > 0; i--) {
70 mraa_spi_write (m_spi, 0);
74 m_pixelsCount = pixelCount;
77 uint16_t dataBytes, totalBytes;
78 uint16_t numBytes = 0;
80 dataBytes = m_pixelsCount * 3;
81 latchBytes = (m_pixelsCount + 31) / 32;
82 totalBytes = dataBytes + latchBytes;
83 if ((m_pixels = (uint8_t *) malloc(totalBytes))) {
84 numBytes = totalBytes;
85 memset ( m_pixels , 0x80, dataBytes); // Init to RGB 'off' state
86 memset (&m_pixels[dataBytes], 0 , latchBytes); // Clear latch bytes
91 mraa_result_t error = MRAA_SUCCESS;
97 error = mraa_spi_stop(m_spi);
98 if (error != MRAA_SUCCESS) {
99 mraa_result_print(error);
101 error = mraa_gpio_close (m_csnPinCtx);
102 if (error != MRAA_SUCCESS) {
103 mraa_result_print(error);
108 LPD8806::setPixelColor (uint16_t pixelOffset, uint8_t r, uint8_t g, uint8_t b) {
109 if (pixelOffset < m_pixelsCount) { // Arrays are 0-indexed, thus NOT '<='
110 uint8_t *ptr = &m_pixels[pixelOffset * 3];
111 *ptr++ = g | 0x80; // Strip color order is GRB,
112 *ptr++ = r | 0x80; // not the more common RGB,
113 *ptr++ = b | 0x80; // so the order here is intentional; don't "fix"
118 LPD8806::show (void) {
119 uint8_t *ptr = m_pixels;
120 uint16_t byte = (m_pixelsCount * 3) + ((m_pixelsCount + 31) / 32);
123 mraa_spi_write (m_spi, *ptr++);
128 LPD8806::getStripLength (void) {
129 return m_pixelsCount;
140 return mraa_gpio_write (m_csnPinCtx, HIGH);
145 return mraa_gpio_write (m_csnPinCtx, LOW);