1 /*
2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
4 *
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:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 #include <iostream>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <cstring>
29 #include <stdexcept>
30
31 #include "lpd8806.h"
32
33 using namespace upm;
34
LPD8806(uint16_t pixelCount,uint8_t csn)35 LPD8806::LPD8806 (uint16_t pixelCount, uint8_t csn) : m_csnPinCtx(csn), m_spi(0) {
36 mraa::Result error = mraa::SUCCESS;
37 m_name = "LPD8806";
38
39 m_pixels = NULL;
40
41 error = m_csnPinCtx.dir (mraa::DIR_OUT);
42 if (error != mraa::SUCCESS) {
43 throw std::invalid_argument(std::string(__FUNCTION__) +
44 ": GPIO failed to set direction");
45 }
46
47 CSOff ();
48
49 // set spi mode to mode2 (CPOL = 0, CPHA = 0)
50 m_spi.mode (mraa::SPI_MODE0);
51
52 CSOn ();
53 // issue initial latch/reset to strip:
54 for (uint16_t i = ((pixelCount + 31) / 32); i > 0; i--) {
55 m_spi.writeByte (0);
56 }
57 CSOff ();
58
59 m_pixelsCount = pixelCount;
60
61 uint8_t latchBytes;
62 uint16_t dataBytes, totalBytes;
63 uint16_t numBytes = 0;
64
65 dataBytes = m_pixelsCount * 3;
66 latchBytes = (m_pixelsCount + 31) / 32;
67 totalBytes = dataBytes + latchBytes;
68 if ((m_pixels = (uint8_t *) malloc(totalBytes))) {
69 numBytes = totalBytes;
70 memset ( m_pixels , 0x80, dataBytes); // Init to RGB 'off' state
71 memset (&m_pixels[dataBytes], 0 , latchBytes); // Clear latch bytes
72 }
73 }
74
~LPD8806()75 LPD8806::~LPD8806() {
76 if (m_pixels) {
77 free(m_pixels);
78 }
79 }
80
81 void
setPixelColor(uint16_t pixelOffset,uint8_t r,uint8_t g,uint8_t b)82 LPD8806::setPixelColor (uint16_t pixelOffset, uint8_t r, uint8_t g, uint8_t b) {
83 if (pixelOffset < m_pixelsCount) { // Arrays are 0-indexed, thus NOT '<='
84 uint8_t *ptr = &m_pixels[pixelOffset * 3];
85 *ptr++ = g | 0x80; // Strip color order is GRB,
86 *ptr++ = r | 0x80; // not the more common RGB,
87 *ptr++ = b | 0x80; // so the order here is intentional; don't "fix"
88 }
89 }
90
91 void
show(void)92 LPD8806::show (void) {
93 uint8_t *ptr = m_pixels;
94 uint16_t byte = (m_pixelsCount * 3) + ((m_pixelsCount + 31) / 32);
95
96 while (byte--) {
97 m_spi.writeByte (*ptr++);
98 }
99 }
100
101 uint16_t
getStripLength(void)102 LPD8806::getStripLength (void) {
103 return m_pixelsCount;
104 }
105
106 /*
107 * **************
108 * private area
109 * **************
110 */
111
112 mraa::Result
CSOn()113 LPD8806::CSOn () {
114 return m_csnPinCtx.write (HIGH);
115 }
116
117 mraa::Result
CSOff()118 LPD8806::CSOff () {
119 return m_csnPinCtx.write (LOW);
120 }
121