Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
MSPM0SpiPixelStrip.hxx
Go to the documentation of this file.
1
35#ifndef _FREERTOS_DRIVERS_TI_MSPM0PIXELSTRIP_HXX_
36#define _FREERTOS_DRIVERS_TI_MSPM0PIXELSTRIP_HXX_
37
38#include <stdint.h>
39#include <ti/devices/msp/msp.h>
40#include <ti/driverlib/driverlib.h>
41
43{
44public:
51 SpiPixelStrip(SPI_Regs *spi, unsigned num_pixels, uint8_t *backing_data)
52 : spi_(spi)
53 , numPixels_(num_pixels)
54 , data_(backing_data)
55 { }
56
58 void hw_init();
59
64 {
66 while (!eof())
67 {
68 uint16_t next_word = 0;
69 uint16_t ofs = 1u;
70 // Computes 16 SPI bits with 5 pulses.
71 while (!eof() && ofs < (1u << 13))
72 {
73 // Appends an 110 or 100 depending opn what the next bit should
74 // be.
75 next_word |= ofs;
76 ofs <<= 1;
77 if (next_bit())
78 next_word |= ofs;
79 ofs <<= 2;
80 }
81 // We leave an extra bit as zero at the end of the word. We hope
82 // that this will not confuse the pixel. They care mostly about the
83 // length of the HIGH pulse.
84 DL_SPI_transmitDataBlocking16(spi_, next_word);
85 // Note that there is no wait here for the transfer to complete.
86 // This is super important, because we want to be computing the next
87 // word while the previous word is emitted by SPI.
88 }
89 // The last bit output is a zero, and leaving the line there is
90 // reasonable, because it matches the latch level.
91 }
92
93private:
97 {
98 currentByte_ = 0;
99 nextBit_ = 0x80;
100 }
102 bool next_bit()
103 {
104 bool ret = data_[currentByte_] & nextBit_;
105 nextBit_ >>= 1;
106 if (!nextBit_)
107 {
108 nextBit_ = 0x80;
109 currentByte_++;
110 }
111 return ret;
112 }
114 bool eof()
115 {
116 return currentByte_ >= numPixels_ * 3;
117 }
118
120 SPI_Regs *spi_;
122 unsigned numPixels_;
124 uint8_t *data_;
125
128 unsigned currentByte_;
131 uint8_t nextBit_;
132};
133
134#endif // _FREERTOS_DRIVERS_TI_MSPM0PIXELSTRIP_HXX_
void update_sync()
Updates the hardware from the backing data.
unsigned currentByte_
Controls iteration over the data sequence when producing the output.
unsigned numPixels_
Number of pixels to drive.
uint8_t * data_
Backing framebuffer to use.
SpiPixelStrip(SPI_Regs *spi, unsigned num_pixels, uint8_t *backing_data)
Initializes the SPI peripheral.
SPI_Regs * spi_
SPI peripheral pointer.
uint8_t nextBit_
Controls iteration over the data sequence when producing the output.
void hw_init()
Opens and initializes the SPI hardware.
void clear_iteration()
Starts a new iteration over the strip.