mq5:: added new sensor
[contrib/upm.git] / examples / lpd8806-example.cxx
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 }