FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
Max7219< N > Class Template Reference
Collaboration diagram for Max7219< N >:

Public Member Functions

 Max7219 (SpiDevice &spi, Gpio *cs)
 
void RefreshThread ()
 
void Initialize ()
 
void SetIntensity (uint8_t value)
 
void Clear ()
 
void DrawPixel (size_t chip_index, uint8_t row, uint8_t col, bool on)
 
void DrawPixelMatrix2 (uint8_t row, uint8_t col, bool on)
 
void Refresh ()
 
void WriteToChip (size_t index, uint8_t addr, uint8_t data)
 
void TestEachChip ()
 
void Lock ()
 
void Unlock ()
 
void SetLight (uint8_t light)
 

Static Public Attributes

static constexpr uint8_t REG_NOOP = 0x00
 
static constexpr uint8_t REG_DIGIT0 = 0x01
 
static constexpr uint8_t REG_DIGIT7 = 0x08
 
static constexpr uint8_t REG_DECODE_MODE = 0x09
 
static constexpr uint8_t REG_INTENSITY = 0x0A
 
static constexpr uint8_t REG_SCAN_LIMIT = 0x0B
 
static constexpr uint8_t REG_SHUTDOWN = 0x0C
 
static constexpr uint8_t REG_DISPLAY_TEST = 0x0F
 

Private Member Functions

void WriteAll (uint8_t addr, uint8_t value)
 
void WriteCommand (uint8_t addr, const std::array< uint8_t, N > &data)
 
void WriteCommandRaw (const std::array< uint8_t, N > &regs, const std::array< uint8_t, N > &data)
 

Private Attributes

SpiDevicespi_
 
Gpiocs_
 
std::array< std::array< uint8_t, 8 >, N > framebuffer_
 
std::thread thread_
 
std::mutex mutex_
 

Detailed Description

template<size_t N>
class Max7219< N >

Definition at line 20 of file max7219.hpp.

Constructor & Destructor Documentation

◆ Max7219()

template<size_t N>
Max7219< N >::Max7219 ( SpiDevice spi,
Gpio cs 
)
inline

Definition at line 33 of file max7219.hpp.

33 : spi_(spi), cs_(cs) {
34 if (!cs_) {
35 std::perror("CS GPIO is not initialized");
36 }
37 cs_->Write(1); /* CS active low, initialize to high */
38 for (auto& chip : framebuffer_) {
39 chip.fill(0); /* Clear frame buffer */
40 }
41
42 thread_ = std::thread(&Max7219::RefreshThread, this);
43
44 std::this_thread::sleep_for(std::chrono::milliseconds(100));
45
46 TestEachChip();
47 }
void Write(int value)
Definition bsp_gpio.hpp:82

Member Function Documentation

◆ Clear()

template<size_t N>
void Max7219< N >::Clear ( )
inline

Definition at line 84 of file max7219.hpp.

84 {
85 for (auto& chip : framebuffer_) {
86 chip.fill(0);
87 }
88 }

◆ DrawPixel()

template<size_t N>
void Max7219< N >::DrawPixel ( size_t  chip_index,
uint8_t  row,
uint8_t  col,
bool  on 
)
inline

Definition at line 94 of file max7219.hpp.

94 {
95 if (chip_index >= N || row >= 8 || col >= 8) {
96 return;
97 }
98 if (on) {
99 framebuffer_[chip_index][7 - row] |= (1 << col);
100 } else {
101 framebuffer_[chip_index][7 - row] &= ~(1 << col);
102 }
103 }

◆ DrawPixelMatrix2()

template<size_t N>
void Max7219< N >::DrawPixelMatrix2 ( uint8_t  row,
uint8_t  col,
bool  on 
)
inline

Definition at line 107 of file max7219.hpp.

107 {
108 if (row >= 16 || col >= 32) {
109 return;
110 }
111
112 /* Serpentine layout chip index mapping */
113 static constexpr int CHIP_INDEX_MAP[] = {0, 2, 1, 3};
114
115 /* Calculate chip position in virtual matrix */
116 size_t chip_index = (row / 8) + (col / 8) * 2;
117 /* Apply physical layout mapping */
118 chip_index = CHIP_INDEX_MAP[chip_index % 4] + (chip_index - chip_index % 4);
119 uint8_t local_row = row % 8;
120 uint8_t local_col = col % 8;
121
122 DrawPixel(chip_index, local_row, local_col, on);
123 }

◆ Initialize()

template<size_t N>
void Max7219< N >::Initialize ( )
inline

Definition at line 59 of file max7219.hpp.

59 {
60 for (size_t i = 0; i < N; ++i) {
61 WriteToChip(i, REG_SHUTDOWN, 0x00); /* Enter shutdown mode */
62 usleep(5); /* Short delay */
63 WriteToChip(i, REG_DISPLAY_TEST, 0x00); /* Normal operation */
64 WriteToChip(i, REG_DECODE_MODE, 0x00); /* Matrix mode (no decoding) */
65 WriteToChip(i, REG_SCAN_LIMIT, 0x07); /* Scan all 8 rows */
66 WriteToChip(i, REG_INTENSITY, 0x03); /* Medium brightness */
67 WriteToChip(i, REG_SHUTDOWN, 0x01); /* Normal operation */
68 }
69 Clear();
70 Refresh();
71 }

◆ Lock()

template<size_t N>
void Max7219< N >::Lock ( )
inline

Definition at line 206 of file max7219.hpp.

206 {
207 mutex_.lock();
208 }

◆ Refresh()

template<size_t N>
void Max7219< N >::Refresh ( )
inline

Definition at line 126 of file max7219.hpp.

126 {
127 mutex_.lock();
128 for (uint8_t row = 0; row < 8; ++row) {
129 std::array<uint8_t, N> regs;
130 std::array<uint8_t, N> data;
131 regs.fill(REG_DIGIT0 + row);
132 for (size_t i = 0; i < N; ++i) {
133 data[i] = framebuffer_[i][row];
134 }
135 WriteCommandRaw(regs, data);
136 }
137 mutex_.unlock();
138 }

◆ RefreshThread()

template<size_t N>
void Max7219< N >::RefreshThread ( )
inline

Definition at line 49 of file max7219.hpp.

49 {
50 Initialize();
51
52 while (true) {
53 Refresh();
54 std::this_thread::sleep_for(std::chrono::milliseconds(5));
55 }
56 }

◆ SetIntensity()

template<size_t N>
void Max7219< N >::SetIntensity ( uint8_t  value)
inline

Definition at line 74 of file max7219.hpp.

74 {
75 mutex_.lock();
76 if (value > 0x0F) {
77 value = 0x0F;
78 }
79 WriteAll(REG_INTENSITY, value);
80 mutex_.unlock();
81 }

◆ SetLight()

template<size_t N>
void Max7219< N >::SetLight ( uint8_t  light)
inline

Definition at line 214 of file max7219.hpp.

214 {
215 SetIntensity(light);
216 }

◆ TestEachChip()

template<size_t N>
void Max7219< N >::TestEachChip ( )
inline

Definition at line 155 of file max7219.hpp.

155 {
156 Clear();
157 /* Draw left border */
158 for (int i = 0; i < 16; ++i) {
159 DrawPixelMatrix2(i, 0, true);
160 std::this_thread::sleep_for(std::chrono::milliseconds(5));
161 }
162
163 /* Draw bottom border */
164 for (int i = 0; i < 16; ++i) {
165 DrawPixelMatrix2(15, i, true);
166 std::this_thread::sleep_for(std::chrono::milliseconds(5));
167 }
168
169 /* Draw middle vertical line */
170 for (int i = 0; i < 16; ++i) {
171 DrawPixelMatrix2(i, 16, true);
172 std::this_thread::sleep_for(std::chrono::milliseconds(5));
173 }
174
175 /* Draw right border */
176 for (int i = 16; i < 32; ++i) {
177 DrawPixelMatrix2(15, i, true);
178 std::this_thread::sleep_for(std::chrono::milliseconds(5));
179 }
180
181 /* Draw right vertical line */
182 for (int i = 15; i > 0; --i) {
183 DrawPixelMatrix2(i, 31, true);
184 std::this_thread::sleep_for(std::chrono::milliseconds(5));
185 }
186
187 /* Draw top right border */
188 for (int i = 31; i > 16; --i) {
189 DrawPixelMatrix2(0, i, true);
190 std::this_thread::sleep_for(std::chrono::milliseconds(5));
191 }
192
193 /* Draw middle horizontal line */
194 for (int i = 15; i > 0; --i) {
195 DrawPixelMatrix2(i, 15, true);
196 std::this_thread::sleep_for(std::chrono::milliseconds(5));
197 }
198
199 /* Draw top left border */
200 for (int i = 15; i > 0; --i) {
201 DrawPixelMatrix2(0, i, true);
202 std::this_thread::sleep_for(std::chrono::milliseconds(5));
203 }
204 }

◆ Unlock()

template<size_t N>
void Max7219< N >::Unlock ( )
inline

Definition at line 210 of file max7219.hpp.

210 {
211 mutex_.unlock();
212 }

◆ WriteAll()

template<size_t N>
void Max7219< N >::WriteAll ( uint8_t  addr,
uint8_t  value 
)
inlineprivate

Definition at line 227 of file max7219.hpp.

227 {
228 std::array<uint8_t, N> data;
229 data.fill(value);
230 WriteCommand(addr, data);
231 }

◆ WriteCommand()

template<size_t N>
void Max7219< N >::WriteCommand ( uint8_t  addr,
const std::array< uint8_t, N > &  data 
)
inlineprivate

Definition at line 234 of file max7219.hpp.

234 {
235 std::array<uint8_t, N> regs;
236 regs.fill(addr);
237 WriteCommandRaw(regs, data);
238 }

◆ WriteCommandRaw()

template<size_t N>
void Max7219< N >::WriteCommandRaw ( const std::array< uint8_t, N > &  regs,
const std::array< uint8_t, N > &  data 
)
inlineprivate

Definition at line 241 of file max7219.hpp.

242 {
243 std::array<uint8_t, N * 2> tx_buf{};
244 for (size_t i = 0; i < N; ++i) {
245 const size_t HW_INDEX = N - 1 - i;
246 tx_buf[i * 2] = regs[HW_INDEX];
247 tx_buf[i * 2 + 1] = data[HW_INDEX];
248 }
249
250 spi_ioc_transfer transfer{};
251 transfer.tx_buf = reinterpret_cast<uint64_t>(tx_buf.data());
252 transfer.len = tx_buf.size();
253 transfer.speed_hz = 1000000;
254 transfer.bits_per_word = 8;
255 transfer.delay_usecs = 10;
256
257 usleep(100);
258 cs_->Write(0);
259 usleep(100);
260#ifndef TEST_BUILD
261 if (ioctl(spi_.Fd(), SPI_IOC_MESSAGE(1), &transfer) < 0) {
262 cs_->Write(1);
263 std::perror("SPI transfer failed");
264 }
265#endif
266
267 usleep(100);
268 cs_->Write(1);
269 usleep(100);
270 }

◆ WriteToChip()

template<size_t N>
void Max7219< N >::WriteToChip ( size_t  index,
uint8_t  addr,
uint8_t  data 
)
inline

Definition at line 141 of file max7219.hpp.

141 {
142 std::array<uint8_t, N> regs;
143 std::array<uint8_t, N> data_all;
144 regs.fill(REG_NOOP);
145 data_all.fill(0x00);
146 if (index >= N) {
147 return;
148 }
149 regs[index] = addr;
150 data_all[index] = data;
151 WriteCommandRaw(regs, data_all);
152 }

Field Documentation

◆ cs_

template<size_t N>
Gpio* Max7219< N >::cs_
private

Definition at line 221 of file max7219.hpp.

◆ framebuffer_

template<size_t N>
std::array<std::array<uint8_t, 8>, N> Max7219< N >::framebuffer_
private

Definition at line 222 of file max7219.hpp.

◆ mutex_

template<size_t N>
std::mutex Max7219< N >::mutex_
private

Definition at line 224 of file max7219.hpp.

◆ REG_DECODE_MODE

template<size_t N>
constexpr uint8_t Max7219< N >::REG_DECODE_MODE = 0x09
staticconstexpr

Definition at line 26 of file max7219.hpp.

◆ REG_DIGIT0

template<size_t N>
constexpr uint8_t Max7219< N >::REG_DIGIT0 = 0x01
staticconstexpr

Definition at line 24 of file max7219.hpp.

◆ REG_DIGIT7

template<size_t N>
constexpr uint8_t Max7219< N >::REG_DIGIT7 = 0x08
staticconstexpr

Definition at line 25 of file max7219.hpp.

◆ REG_DISPLAY_TEST

template<size_t N>
constexpr uint8_t Max7219< N >::REG_DISPLAY_TEST = 0x0F
staticconstexpr

Definition at line 30 of file max7219.hpp.

◆ REG_INTENSITY

template<size_t N>
constexpr uint8_t Max7219< N >::REG_INTENSITY = 0x0A
staticconstexpr

Definition at line 27 of file max7219.hpp.

◆ REG_NOOP

template<size_t N>
constexpr uint8_t Max7219< N >::REG_NOOP = 0x00
staticconstexpr

Definition at line 23 of file max7219.hpp.

◆ REG_SCAN_LIMIT

template<size_t N>
constexpr uint8_t Max7219< N >::REG_SCAN_LIMIT = 0x0B
staticconstexpr

Definition at line 28 of file max7219.hpp.

◆ REG_SHUTDOWN

template<size_t N>
constexpr uint8_t Max7219< N >::REG_SHUTDOWN = 0x0C
staticconstexpr

Definition at line 29 of file max7219.hpp.

◆ spi_

template<size_t N>
SpiDevice& Max7219< N >::spi_
private

Definition at line 220 of file max7219.hpp.

◆ thread_

template<size_t N>
std::thread Max7219< N >::thread_
private

Definition at line 223 of file max7219.hpp.


The documentation for this class was generated from the following file: