Browse Source

lpd8806: Added new module - digital led strip (NOT TESTED)

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 years ago
parent
commit
050c134664

+ 3
- 0
examples/CMakeLists.txt View File

@@ -26,6 +26,7 @@ add_executable (max31723-example max31723.cxx)
26 26
 add_executable (max5487-example max5487.cxx)
27 27
 add_executable (nrf8001-broadcast-example nrf8001_broadcast.cxx)
28 28
 add_executable (nrf8001-helloworld-example nrf8001_helloworld.cxx)
29
+add_executable (lpd8806-example lpd8806-example.cxx)
29 30
 
30 31
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
31 32
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -49,6 +50,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/maxds3231m)
49 50
 include_directories (${PROJECT_SOURCE_DIR}/src/max31723)
50 51
 include_directories (${PROJECT_SOURCE_DIR}/src/max5487)
51 52
 include_directories (${PROJECT_SOURCE_DIR}/src/nrf8001)
53
+include_directories (${PROJECT_SOURCE_DIR}/src/lpd8806)
52 54
 
53 55
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
54 56
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -78,3 +80,4 @@ target_link_libraries (max31723-example max31723 ${CMAKE_THREAD_LIBS_INIT})
78 80
 target_link_libraries (max5487-example max5487 ${CMAKE_THREAD_LIBS_INIT})
79 81
 target_link_libraries (nrf8001-broadcast-example nrf8001 ${CMAKE_THREAD_LIBS_INIT})
80 82
 target_link_libraries (nrf8001-helloworld-example nrf8001 ${CMAKE_THREAD_LIBS_INIT})
83
+target_link_libraries (lpd8806-example lpd8806 ${CMAKE_THREAD_LIBS_INIT})

+ 104
- 0
examples/lpd8806-example.cxx View File

@@ -0,0 +1,104 @@
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 <unistd.h>
26
+#include <iostream>
27
+#include "lpd8806.h"
28
+#include <signal.h>
29
+
30
+void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait);
31
+
32
+int doWork = 0;
33
+upm::LPD8806 *sensor = NULL;
34
+
35
+void
36
+sig_handler(int signo)
37
+{
38
+    printf("got signal\n");
39
+    if (signo == SIGINT) {
40
+        printf("exiting application\n");
41
+        doWork = 1;
42
+    }
43
+}
44
+
45
+int
46
+main(int argc, char **argv)
47
+{
48
+    //! [Interesting]
49
+    sensor = new upm::LPD8806(10, 7);
50
+    usleep (1000000);
51
+
52
+    sensor->show ();
53
+
54
+    while (!doWork) {
55
+        // Back-and-forth lights
56
+        scanner(127, 0, 0, 30);        // red, slow
57
+        scanner(0, 0, 127, 15);        // blue, fast
58
+        usleep (1000000);
59
+    }
60
+    //! [Interesting]
61
+
62
+    std::cout << "exiting application" << std::endl;
63
+
64
+    delete sensor;
65
+
66
+    return 0;
67
+}
68
+
69
+void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) {
70
+    int i, j, pos, dir;
71
+
72
+    pos = 0;
73
+    dir = 1;
74
+
75
+    for(i=0; i < ((sensor->getStripLength() - 1) * 8); i++) {
76
+        // Draw 5 pixels centered on pos.  setPixelColor() will clip
77
+        // any pixels off the ends of the strip, no worries there.
78
+        // we'll make the colors dimmer at the edges for a nice pulse
79
+        // look
80
+        sensor->setPixelColor(pos - 2, r/4, g/4, b/4);
81
+        sensor->setPixelColor(pos - 1, r/2, g/2, b/2);
82
+        sensor->setPixelColor(pos, r, g, b);
83
+        sensor->setPixelColor(pos + 1, r/2, g/2, b/2);
84
+        sensor->setPixelColor(pos + 2, r/4, g/4, b/4);
85
+
86
+        sensor->show();
87
+        usleep (wait * 1000);
88
+        // If we wanted to be sneaky we could erase just the tail end
89
+        // pixel, but it's much easier just to erase the whole thing
90
+        // and draw a new one next time.
91
+        for(j=-2; j<= 2; j++) {
92
+            sensor->setPixelColor(pos+j, 0,0,0);
93
+        }
94
+        // Bounce off ends of strip
95
+        pos += dir;
96
+        if(pos < 0) {
97
+            pos = 1;
98
+            dir = -dir;
99
+        } else if (pos >= sensor->getStripLength()) {
100
+            pos = sensor->getStripLength() - 2;
101
+            dir = -dir;
102
+        }
103
+    }
104
+}

BIN
src/lpd8806/.DS_Store View File


BIN
src/lpd8806/._.DS_Store View File


BIN
src/lpd8806/._CMakeLists.txt View File


BIN
src/lpd8806/._jsupm_lpd8806.i View File


BIN
src/lpd8806/._pyupm_lpd8806.i View File


+ 5
- 0
src/lpd8806/CMakeLists.txt View File

@@ -0,0 +1,5 @@
1
+set (libname "lpd8806")
2
+set (libdescription “Digital RGB LED strip”)
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 8
- 0
src/lpd8806/jsupm_lpd8806.i View File

@@ -0,0 +1,8 @@
1
+%module jsupm_lpd8806
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "lpd8806.h"
6
+%}
7
+
8
+%include "lpd8806.h"

+ 146
- 0
src/lpd8806/lpd8806.cxx View File

@@ -0,0 +1,146 @@
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
+
30
+#include "lpd8806.h"
31
+
32
+using namespace upm;
33
+
34
+struct LPD8806Exception : public std::exception {
35
+    std::string message;
36
+    LPD8806Exception (std::string msg) : message (msg) { }
37
+    ~LPD8806Exception () throw () { }
38
+    const char* what() const throw () { return message.c_str(); }
39
+};
40
+
41
+LPD8806::LPD8806 (uint16_t pixelCount, uint8_t csn) {
42
+    mraa_result_t error = MRAA_SUCCESS;
43
+    m_name = "LPD8806";
44
+
45
+    m_pixels = NULL;
46
+
47
+    m_csnPinCtx = mraa_gpio_init (csn);
48
+    if (m_csnPinCtx == NULL) {
49
+        throw LPD8806Exception ("GPIO failed to initilize");
50
+    }
51
+
52
+    error = mraa_gpio_dir (m_csnPinCtx, MRAA_GPIO_OUT);
53
+    if (error != MRAA_SUCCESS) {
54
+        throw LPD8806Exception ("GPIO failed to initilize");
55
+    }
56
+
57
+    CSOff ();
58
+
59
+    m_spi = mraa_spi_init (0);
60
+    if (m_spi == NULL) {
61
+        throw LPD8806Exception ("SPI failed to initilize");
62
+    }
63
+
64
+    // set spi mode to mode2 (CPOL = 0, CPHA = 0)
65
+    mraa_spi_mode (m_spi, MRAA_SPI_MODE0);
66
+
67
+    CSOn ();
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);
71
+    }
72
+    CSOff ();
73
+
74
+    m_pixelsCount = pixelCount;
75
+
76
+    uint8_t  latchBytes;
77
+    uint16_t dataBytes, totalBytes;
78
+    uint16_t numBytes = 0;
79
+
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
87
+    }
88
+}
89
+
90
+LPD8806::~LPD8806() {
91
+    mraa_result_t error = MRAA_SUCCESS;
92
+
93
+    if (m_pixels) {
94
+        free(m_pixels);
95
+    }
96
+    
97
+    error = mraa_spi_stop(m_spi);
98
+    if (error != MRAA_SUCCESS) {
99
+        mraa_result_print(error);
100
+    }
101
+    error = mraa_gpio_close (m_csnPinCtx);
102
+    if (error != MRAA_SUCCESS) {
103
+        mraa_result_print(error);
104
+    }
105
+}
106
+
107
+void
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"
114
+    }
115
+}
116
+
117
+void
118
+LPD8806::show (void) {
119
+    uint8_t  *ptr   = m_pixels;
120
+    uint16_t byte   = (m_pixelsCount * 3) + ((m_pixelsCount + 31) / 32);
121
+    
122
+    while (byte--) {
123
+        mraa_spi_write (m_spi, *ptr++);
124
+    }
125
+}
126
+
127
+uint16_t
128
+LPD8806::getStripLength (void) {
129
+    return m_pixelsCount;
130
+}
131
+
132
+/*
133
+ * **************
134
+ *  private area
135
+ * **************
136
+ */
137
+
138
+mraa_result_t
139
+LPD8806::CSOn () {
140
+    return mraa_gpio_write (m_csnPinCtx, HIGH);
141
+}
142
+
143
+mraa_result_t
144
+LPD8806::CSOff () {
145
+    return mraa_gpio_write (m_csnPinCtx, LOW);
146
+}

+ 105
- 0
src/lpd8806/lpd8806.h View File

@@ -0,0 +1,105 @@
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
+#pragma once
25
+
26
+#include <string>
27
+#include <mraa/aio.h>
28
+#include <mraa/gpio.h>
29
+#include <mraa/spi.h>
30
+
31
+#define HIGH                    1
32
+#define LOW                     0
33
+
34
+namespace upm {
35
+
36
+/**
37
+ * @brief C++ API for LPD8806 
38
+ *
39
+ * This file defines the LPD8806 C++ interface for liblpd8806
40
+ *
41
+ */
42
+class LPD8806 {
43
+    public:
44
+
45
+        /**
46
+         * Instanciates a LPD8806 object
47
+         *
48
+         * @param pixelCount number of pixels in the strip
49
+         * @param csn chip slect pin
50
+         */
51
+        LPD8806 (uint16_t pixelCount, uint8_t csn);
52
+
53
+        /**
54
+         * LPD8806 object destructor, basicaly it close SPI and the GPIO.
55
+         */
56
+        ~LPD8806 ();
57
+
58
+        /**
59
+         * @param pixelOffset pixel offset in the strip of pixel
60
+         * @param r red led
61
+         * @param g green led
62
+         * @param b blue led
63
+         */
64
+        void setPixelColor (uint16_t pixelOffset, uint8_t r, uint8_t g, uint8_t b);
65
+
66
+        /**
67
+         * Write the data stored in array of pixels to the chip
68
+         */
69
+        void show (void);
70
+
71
+        /**
72
+         * Return length of the led strip
73
+         */
74
+        uint16_t getStripLength (void);
75
+
76
+        /**
77
+         * Return name of the component
78
+         */
79
+        std::string name()
80
+        {
81
+            return m_name;
82
+        }
83
+    private:
84
+        std::string m_name;
85
+        mraa_spi_context        m_spi;
86
+        mraa_gpio_context       m_csnPinCtx;
87
+
88
+        uint8_t*                m_pixels;
89
+        uint8_t                 m_pixelsCount;
90
+
91
+        uint8_t readRegister (uint8_t reg);
92
+        void writeRegister (uint8_t reg, uint8_t data);
93
+
94
+        /**
95
+         * Set chip select pin LOW
96
+         */
97
+        mraa_result_t CSOn ();
98
+
99
+        /**
100
+         * Set chip select pin HIGH
101
+         */
102
+        mraa_result_t CSOff ();
103
+};
104
+
105
+}

+ 9
- 0
src/lpd8806/pyupm_lpd8806.i View File

@@ -0,0 +1,9 @@
1
+%module pyupm_lpd8806
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+%include "lpd8806.h"
7
+%{
8
+    #include "lpd8806.h"
9
+%}