TestSpi.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // STL includes
  2. #include <vector>
  3. #include <ctime>
  4. #include <cstring>
  5. #include <string>
  6. #include <unistd.h>
  7. #include <iostream>
  8. // Local includes
  9. #include <utils/ColorRgb.h>
  10. //QT includes
  11. #include <QJsonObject>
  12. #include "../libsrc/leddevice/dev_spi/LedDeviceWs2801.h"
  13. QJsonObject deviceConfig;
  14. void setColor(char* colorStr)
  15. {
  16. ColorRgb color = ColorRgb::BLACK;
  17. std::cout << "Switching all leds to: ";
  18. if (strncmp("red", colorStr, 3) == 0)
  19. {
  20. std::cout << "red";
  21. color = ColorRgb::RED;
  22. }
  23. else if (strncmp("green", colorStr, 5) == 0)
  24. {
  25. std::cout << "green";
  26. color = ColorRgb::GREEN;
  27. }
  28. else if (strncmp("blue", colorStr, 5) == 0)
  29. {
  30. std::cout << "blue";
  31. color = ColorRgb::BLUE;
  32. }
  33. else if (strncmp("cyan", colorStr, 5) == 0)
  34. {
  35. std::cout << "cyan";
  36. }
  37. else if (strncmp("gray", colorStr, 4) == 0)
  38. {
  39. std::cout << "gray";
  40. }
  41. else if (strncmp("white", colorStr, 5) == 0)
  42. {
  43. std::cout << "white";
  44. color = ColorRgb::WHITE;
  45. }
  46. else if (strncmp("black", colorStr, 5) == 0)
  47. {
  48. std::cout << "black";
  49. color = ColorRgb::BLACK;
  50. }
  51. std::cout << std::endl;
  52. unsigned ledCnt = 50;
  53. std::vector<ColorRgb> buff(ledCnt, color);
  54. LedDeviceWs2801 ledDevice(deviceConfig);
  55. ledDevice.open();
  56. ledDevice.updateLeds(buff);
  57. }
  58. bool _running = true;
  59. void doCircle()
  60. {
  61. ColorRgb color_1 = ColorRgb::RED;
  62. ColorRgb color_2 = ColorRgb::YELLOW;
  63. unsigned ledCnt = 50;
  64. std::vector<ColorRgb> data(ledCnt, ColorRgb::BLACK);
  65. LedDeviceWs2801 ledDevice(deviceConfig);
  66. ledDevice.open();
  67. timespec loopTime;
  68. loopTime.tv_sec = 0;
  69. loopTime.tv_nsec = 100000000; // 100 ms
  70. int curLed_1 = 0;
  71. int nextLed_1 = 1;
  72. int curLed_2 = 49;
  73. int nextLed_2 = 48;
  74. while (_running)
  75. {
  76. data[curLed_1] = ColorRgb::BLACK;
  77. data[curLed_2] = ColorRgb::BLACK;
  78. // Move the current and the next pointer
  79. curLed_1 = nextLed_1;
  80. curLed_2 = nextLed_2;
  81. ++nextLed_1;
  82. --nextLed_2;
  83. if (nextLed_1 == int(ledCnt))
  84. {
  85. nextLed_1 = 0;
  86. }
  87. if (nextLed_2 < 0)
  88. {
  89. nextLed_2 = 49;
  90. }
  91. data[curLed_1] = color_1;
  92. data[curLed_2] = color_2;
  93. ledDevice.updateLeds(data);
  94. nanosleep(&loopTime, NULL);
  95. }
  96. // Switch the current leds off
  97. data[curLed_1] = ColorRgb::BLACK;
  98. data[curLed_2] = ColorRgb::BLACK;
  99. ledDevice.updateLeds(data);
  100. }
  101. #include <csignal>
  102. void signal_handler(int signum)
  103. {
  104. _running = false;
  105. }
  106. int main(int argc, char** argv)
  107. {
  108. if (sizeof(ColorRgb) != 3)
  109. {
  110. std::cout << "sizeof(ColorRgb) = " << sizeof(ColorRgb) << std::endl;
  111. return -1;
  112. }
  113. // Install signal handlers to stop loops
  114. signal(SIGTERM, &signal_handler);
  115. signal(SIGINT, &signal_handler);
  116. if (argc < 2)
  117. {
  118. std::cerr << "Missing argument" << std::endl;
  119. return -1;
  120. }
  121. deviceConfig["output"] = QString("/dev/spidev0.0");
  122. deviceConfig["rate"] = 40000;
  123. deviceConfig["latchtime"] = 500000;
  124. if (strncmp("fixed", argv[1], 5) == 0)
  125. {
  126. setColor(argv[2]);
  127. return 0;
  128. }
  129. else if (strncmp("circle", argv[1], 6) == 0)
  130. {
  131. doCircle();
  132. }
  133. else
  134. {
  135. std::cerr << "Unknown option: " << argv[1] << std::endl;
  136. }
  137. return 0;
  138. }