2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <vector>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// Local includes
|
2013-11-11 10:00:37 +01:00
|
|
|
#include <utils/ColorRgb.h>
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-12-17 19:50:15 +01:00
|
|
|
#include "../libsrc/leddevice/LedDeviceWs2801.h"
|
2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
void setColor(char* colorStr)
|
|
|
|
{
|
2013-11-11 10:00:37 +01:00
|
|
|
ColorRgb color = ColorRgb::BLACK;
|
2013-07-26 22:38:34 +02:00
|
|
|
std::cout << "Switching all leds to: ";
|
|
|
|
if (strncmp("red", colorStr, 3) == 0)
|
|
|
|
{
|
|
|
|
std::cout << "red";
|
2013-11-11 10:00:37 +01:00
|
|
|
color = ColorRgb::RED;
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
else if (strncmp("green", colorStr, 5) == 0)
|
|
|
|
{
|
|
|
|
std::cout << "green";
|
2013-11-11 10:00:37 +01:00
|
|
|
color = ColorRgb::GREEN;
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
else if (strncmp("blue", colorStr, 5) == 0)
|
|
|
|
{
|
|
|
|
std::cout << "blue";
|
2013-11-11 10:00:37 +01:00
|
|
|
color = ColorRgb::BLUE;
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
else if (strncmp("cyan", colorStr, 5) == 0)
|
|
|
|
{
|
|
|
|
std::cout << "cyan";
|
|
|
|
}
|
|
|
|
else if (strncmp("gray", colorStr, 4) == 0)
|
|
|
|
{
|
|
|
|
std::cout << "gray";
|
|
|
|
}
|
|
|
|
else if (strncmp("white", colorStr, 5) == 0)
|
|
|
|
{
|
|
|
|
std::cout << "white";
|
2013-11-11 10:00:37 +01:00
|
|
|
color = ColorRgb::WHITE;
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
else if (strncmp("black", colorStr, 5) == 0)
|
|
|
|
{
|
|
|
|
std::cout << "black";
|
2013-11-11 10:00:37 +01:00
|
|
|
color = ColorRgb::BLACK;
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
unsigned ledCnt = 50;
|
2013-11-11 10:00:37 +01:00
|
|
|
std::vector<ColorRgb> buff(ledCnt, color);
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-09-09 17:23:44 +02:00
|
|
|
LedDeviceWs2801 ledDevice("/dev/spidev0.0", 40000);
|
2013-07-26 22:38:34 +02:00
|
|
|
ledDevice.open();
|
|
|
|
ledDevice.write(buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _running = true;
|
|
|
|
void doCircle()
|
|
|
|
{
|
2013-11-11 10:00:37 +01:00
|
|
|
ColorRgb color_1 = ColorRgb::RED;
|
|
|
|
ColorRgb color_2 = ColorRgb::YELLOW;
|
2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
unsigned ledCnt = 50;
|
2013-11-11 10:00:37 +01:00
|
|
|
std::vector<ColorRgb> data(ledCnt, ColorRgb::BLACK);
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-09-09 17:23:44 +02:00
|
|
|
LedDeviceWs2801 ledDevice("/dev/spidev0.0", 40000);
|
2013-07-26 22:38:34 +02:00
|
|
|
ledDevice.open();
|
|
|
|
|
|
|
|
timespec loopTime;
|
|
|
|
loopTime.tv_sec = 0;
|
|
|
|
loopTime.tv_nsec = 100000000; // 100 ms
|
|
|
|
|
|
|
|
int curLed_1 = 0;
|
|
|
|
int nextLed_1 = 1;
|
|
|
|
|
|
|
|
int curLed_2 = 49;
|
|
|
|
int nextLed_2 = 48;
|
|
|
|
|
|
|
|
|
|
|
|
while (_running)
|
|
|
|
{
|
2013-11-11 10:00:37 +01:00
|
|
|
data[curLed_1] = ColorRgb::BLACK;
|
|
|
|
data[curLed_2] = ColorRgb::BLACK;
|
2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
// Move the current and the next pointer
|
|
|
|
curLed_1 = nextLed_1;
|
|
|
|
curLed_2 = nextLed_2;
|
|
|
|
++nextLed_1;
|
|
|
|
--nextLed_2;
|
|
|
|
if (nextLed_1 == int(ledCnt))
|
|
|
|
{
|
|
|
|
nextLed_1 = 0;
|
|
|
|
}
|
|
|
|
if (nextLed_2 < 0)
|
|
|
|
{
|
|
|
|
nextLed_2 = 49;
|
|
|
|
}
|
|
|
|
|
|
|
|
data[curLed_1] = color_1;
|
|
|
|
|
|
|
|
data[curLed_2] = color_2;
|
|
|
|
|
|
|
|
ledDevice.write(data);
|
|
|
|
|
|
|
|
nanosleep(&loopTime, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch the current leds off
|
2013-11-11 10:00:37 +01:00
|
|
|
data[curLed_1] = ColorRgb::BLACK;
|
|
|
|
data[curLed_2] = ColorRgb::BLACK;
|
2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
ledDevice.write(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <csignal>
|
|
|
|
|
|
|
|
void signal_handler(int signum)
|
|
|
|
{
|
|
|
|
_running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2013-11-11 10:00:37 +01:00
|
|
|
if (sizeof(ColorRgb) != 3)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-11-11 10:00:37 +01:00
|
|
|
std::cout << "sizeof(ColorRgb) = " << sizeof(ColorRgb) << std::endl;
|
2013-07-26 22:38:34 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install signal handlers to stop loops
|
|
|
|
signal(SIGTERM, &signal_handler);
|
|
|
|
signal(SIGINT, &signal_handler);
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
std::cerr << "Missing argument" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp("fixed", argv[1], 5) == 0)
|
|
|
|
{
|
|
|
|
setColor(argv[2]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (strncmp("circle", argv[1], 6) == 0)
|
|
|
|
{
|
|
|
|
doCircle();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "Unknown option: " << argv[1] << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|