blob: 48f17ae2640e5f29caeb1796163970681c3f5fd7 [file] [log] [blame]
Comran Morshed33ad1692015-12-06 18:53:02 +00001#ifndef FRC971_WPILIB_LPD8806_H_
2#define FRC971_WPILIB_LPD8806_H_
3
4#include <memory>
5#include <atomic>
6
7#include "aos/common/mutex.h"
8
9#include "SPI.h"
10
11namespace frc971 {
12namespace wpilib {
13
14// Handles sending out colors to a string of LPD8806 LED drivers with a chip to
15// gate the clock+data lines attached to SPI CS1.
16//
17// This is currently implemented by sending data for an entire 64 LED strip
18// right after receiving a gyro message to avoid interfering with the SPI
19// transactions for the gyro.
20//
21// This is designed to be passed into ::std::thread's constructor so it will run
22// as a separate thread.
23class LPD8806 {
24 public:
25 // chips is the number of actual chips in the string. There will be twice this
26 // many LEDs.
27 LPD8806(int chips);
28
29 // For ::std::thread to call.
30 //
31 // Loops until Quit() is called writing values out.
32 void operator()();
33
34 void Quit() { run_ = false; }
35
36 int chips() const { return chips_; }
37
38 // Sets the color to be sent out next refresh cycle for a given LED.
39 // red, green, and blue are 0-1 (inclusive).
40 void SetColor(int led, uint32_t hex_color);
41
42 private:
43 struct LED {
44 uint8_t green;
45 uint8_t red;
46 uint8_t blue;
47 };
48
49 enum Type { GREEN, RED, BLUE };
50
51 static_assert(sizeof(LED[4]) == 12, "LED needs to pack into arrays nicely");
52
53 uint8_t TranslateColor(uint32_t hex_color, Type type);
54
55 const int chips_;
56 int next_led_to_send_ = 0;
57
58 ::std::unique_ptr<LED[]> data_;
59 ::aos::Mutex data_mutex_;
60
61 ::std::unique_ptr<SPI> spi_;
62
63 ::std::atomic<bool> run_{true};
64};
65
66} // namespace wpilib
67} // namespace frc971
68
69#endif // FRC971_WPILIB_LPD8806_H_