MAX7219_CH32 Library 1.0.0
A lightweight library for controlling MAX7219 7-segment displays
Loading...
Searching...
No Matches
MAX7219_CH32.h
Go to the documentation of this file.
1/*
2 * MAX7219_CH32.h - MAX7219 7-Segment Display Library for Arduino
3 *
4 * A lightweight library for controlling MAX7219 LED driver ICs with 7-segment displays.
5 * Optimized for memory-constrained microcontrollers like CH32V003.
6 *
7 * Features:
8 * - BCD decode mode (hardware handles 0-9 display)
9 * - No-decode mode (software handles numbers, letters, patterns)
10 * - Configurable alignment (left, right, center)
11 * - Animation support (blink, fade, scroll, chase, count)
12 * - Simple display functions for numbers, text, time, hex, binary
13 *
14 * Author: Rithik Krisna M
15 * License: MIT License
16 * Version: 1.0.0
17 * Created: 2025
18 */
19
20#ifndef MAX7219_CH32_H
21#define MAX7219_CH32_H
22
23#include <Arduino.h>
24
25// ========================================
26// Display Modes
27// ========================================
28#define MAX7219_MODE_DECODE 0xFF // BCD decode for all digits (hardware mode)
29#define MAX7219_MODE_NO_DECODE 0x00 // No decode (software handles display)
30
31// ========================================
32// Alignment Options
33// ========================================
34#define MAX7219_ALIGN_LEFT 0 // Left-aligned (position 0 = leftmost)
35#define MAX7219_ALIGN_RIGHT 1 // Right-aligned (position 0 = rightmost)
36#define MAX7219_ALIGN_CENTER 2 // Center-aligned (proportional to content size)
37
38// ========================================
39// MAX7219 Register Addresses
40// ========================================
41#define MAX7219_REG_NOOP 0x00
42#define MAX7219_REG_DIGIT0 0x01
43#define MAX7219_REG_DIGIT1 0x02
44#define MAX7219_REG_DIGIT2 0x03
45#define MAX7219_REG_DIGIT3 0x04
46#define MAX7219_REG_DIGIT4 0x05
47#define MAX7219_REG_DIGIT5 0x06
48#define MAX7219_REG_DIGIT6 0x07
49#define MAX7219_REG_DIGIT7 0x08
50#define MAX7219_REG_DECODE 0x09
51#define MAX7219_REG_INTENSITY 0x0A
52#define MAX7219_REG_SCANLIMIT 0x0B
53#define MAX7219_REG_SHUTDOWN 0x0C
54#define MAX7219_REG_DISPLAYTEST 0x0F
55
56// ========================================
57// MAX7219 Class
58// ========================================
63class MAX7219 {
64public:
72 MAX7219(uint8_t dinPin, uint8_t clkPin, uint8_t csPin, uint8_t numDigits = 8);
73
74 // ========================================
75 // Initialization
76 // ========================================
80 void begin();
81
86 void begin(uint8_t mode);
87
88 // ========================================
89 // Configuration
90 // ========================================
95 void setMode(uint8_t mode);
96
101 uint8_t getMode();
102
107 void setAlignment(uint8_t alignment);
108
113 uint8_t getAlignment();
114
119 void setBrightness(uint8_t level);
120
125 void setPower(bool on);
126
130 void clear();
131
132 // ========================================
133 // Display Functions (Mode-Aware)
134 // ========================================
139 void display(const char* text);
140
145 void display(int number);
146
151 void display(long number);
152
158 void display(double number, uint8_t decimals = 2);
159
166 void displayAt(uint8_t position, uint8_t value, bool dp = false);
167
174 void displayAt(uint8_t position, char character, bool dp = false);
175
176 // ========================================
177 // Special Formats
178 // ========================================
184 void displayTime(uint8_t hours, uint8_t minutes);
185
192 void displayTime(uint8_t hours, uint8_t minutes, uint8_t seconds);
193
198 void displayHex(uint32_t value);
199
204 void displayBinary(uint8_t value);
205
206 // ========================================
207 // Animations
208 // ========================================
214 void blink(uint8_t times = 3, uint16_t delayMs = 500);
215
220 void fadeIn(uint16_t delayMs = 50);
221
226 void fadeOut(uint16_t delayMs = 50);
227
233 void scroll(const char* text, uint16_t delayMs = 200);
234
240 void scroll(long number, uint16_t delayMs = 200);
241
246 void chase(uint16_t delayMs = 100);
247
254 void countUp(long from, long to, uint16_t delayMs = 100);
255
262 void countDown(long from, long to, uint16_t delayMs = 100);
263
264 // ========================================
265 // Low-Level Control
266 // ========================================
272 void setSegments(uint8_t position, uint8_t segments);
273
280 void setRawDigit(uint8_t position, uint8_t value, bool dp = false);
281
282private:
283 // Pin configuration
284 uint8_t _dinPin;
285 uint8_t _clkPin;
286 uint8_t _csPin;
287
288 // Display configuration
289 uint8_t _numDigits;
290 uint8_t _mode;
291 uint8_t _alignment;
292 uint8_t _brightness;
293
294 // Internal functions
295 void sendCommand(uint8_t address, uint8_t data);
296 void sendByte(uint8_t data);
297 void sendBit(uint8_t bit);
298 uint8_t getPhysicalPosition(uint8_t logicalPosition);
299 uint8_t charToSegments(char c);
300 uint8_t digitToSegments(uint8_t digit);
301 void displayNumberInternal(long number, bool leadingZeros = false);
302 void displayFloatInternal(double number, uint8_t decimals);
303 void displayTextInternal(const char* text);
304};
305
306#endif // MAX7219_CH32_H
307
void displayHex(uint32_t value)
Display value in hexadecimal format.
void chase(uint16_t delayMs=100)
Chase animation (digit-by-digit)
void clear()
Clear all digits.
void setPower(bool on)
Turn display on or off.
void setSegments(uint8_t position, uint8_t segments)
Set raw segment pattern at position.
void begin()
Initialize the display with default mode.
void blink(uint8_t times=3, uint16_t delayMs=500)
Blink the display.
void fadeIn(uint16_t delayMs=50)
Fade in animation.
void setBrightness(uint8_t level)
Set display brightness.
uint8_t getMode()
Get current mode.
uint8_t getAlignment()
Get current alignment.
void scroll(const char *text, uint16_t delayMs=200)
Scroll text across display.
void setMode(uint8_t mode)
Set decode mode.
void setRawDigit(uint8_t position, uint8_t value, bool dp=false)
Set raw digit value at position.
void displayAt(uint8_t position, uint8_t value, bool dp=false)
Display value at specific position.
void display(const char *text)
Display text string.
void fadeOut(uint16_t delayMs=50)
Fade out animation.
void displayBinary(uint8_t value)
Display value in binary format.
void countDown(long from, long to, uint16_t delayMs=100)
Count down animation.
void countUp(long from, long to, uint16_t delayMs=100)
Count up animation.
MAX7219(uint8_t dinPin, uint8_t clkPin, uint8_t csPin, uint8_t numDigits=8)
Constructor for MAX7219 display.
void setAlignment(uint8_t alignment)
Set text alignment.
void displayTime(uint8_t hours, uint8_t minutes)
Display time in HH:MM format.