SBK_BarDrive Library 2.2.0
LED bar meter control and queued animations for Arduino
Loading...
Searching...
No Matches
customMappingDemo.ino
Go to the documentation of this file.
1/**
2 * @file customMappingDemo.ino
3 * @brief Demonstrates using SBK_BarDrive with a custom row/column mapping.
4 *
5 * Requirements:
6 * - Supported driver with complatible library (SBK_MAX72xx or SBK_HT16K33 libraries)
7 * - Bar meter display or leds array wired to driver
8 *
9 * @author
10 * Samuel Barabé (Smart Builds & Kits)
11 *
12 * @version 2.2.0
13 * @license MIT
14 */
15
16#include <Arduino.h>
17
18// ──────────────────────────────────────────────
19// SBK BarDrive Configuration Flags
20// ──────────────────────────────────────────────
21#define SBK_BARDRIVE_WITH_ANIM
22
23// ──────────────────────────────────────────────
24// Bar Meter Custom Mapping Construction
25// ──────────────────────────────────────────────
26// Custom mapping format: {device, row, col} = {device index, anode, cathode}
27//
28// This defines the physical LED wiring for each logical segment in the bar meter.
29//
30// Each segment corresponds to one LED connected between:
31// - a driver’s row pin (anode, V+)
32// - a driver’s column pin (cathode, GND)
33//
34// Supported drivers (SBK-compatible):
35// - HT16K33: rows = Rn (anode), cols = Cn (cathode)
36// - MAX72xx: rows = SEGn (anode), cols = DIGn (cathode)
37//
38// ✅ Mapping is in top-to-bottom order within each column,
39// progressing left-to-right across the display.
40//
41// ✅ You can freely assign segments to multiple devices
42// by varying the `device` index (first field).
43// This is useful when your LED matrix spans across multiple HT16K33 or MAX72xx ICs.
44//
45// Example layout shown below is a 4-row × 7-column matrix
46// fully driven by device 0, equivalent to MatrixPreset::BL28_3005SK.
47//
48// ┌─────────────── Logical Mapping ───────────────┐
49// │ Column → 0 1 2 3 4 5 6 │
50// │ Row ↓ │
51// │ [0,0,0][0,0,1][0,0,2]... ← top row │
52// │ [0,1,0][0,1,1][0,1,2]... │
53// │ [0,2,0][0,2,1][0,2,2]... │
54// │ [0,3,0][0,3,1][0,3,2]... ← bottom row │
55// └───────────────────────────────────────────────┘
56//
57// 💡 To split across devices, just change the `device` index in each entry.
58// For example:
59// {0, 0, 0}, {0, 1, 0}, ..., {1, 0, 4}, {1, 1, 4}, ...
60// This would place the first few columns on device 0,
61// and the next ones on device 1.
62//
63// IMPORTANT: Ensure your bar meter segments are wired accordingly for this mapping to work.
64//
65// This example defines a 4-row × 7-column matrix on device 0 — equivalent to using MatrixPreset::BL28_3005SK.
66//
67// {dev, row, col} = {dev, anode, cathode} layout, top-to-bottom, then left-to-right
68const uint8_t customMapping[28][3] = {
69 {0, 0, 0}, {0, 1, 0}, {0, 2, 0}, {0, 3, 0}, // Column 0
70 {0, 0, 1}, {0, 1, 1}, {0, 2, 1}, {0, 3, 1}, // Column 1
71 {0, 0, 2}, {0, 1, 2}, {0, 2, 2}, {0, 3, 2}, // Column 2
72 {0, 0, 3}, {0, 1, 3}, {0, 2, 3}, {0, 3, 3}, // Column 3
73 {0, 0, 4}, {0, 1, 4}, {0, 2, 4}, {0, 3, 4}, // Column 4
74 {0, 0, 5}, {0, 1, 5}, {0, 2, 5}, {0, 3, 5}, // Column 5
75 {0, 0, 6}, {0, 1, 6}, {0, 2, 6}, {0, 3, 6} // Column 6
76};
77
78// ──────────────────────────────────────────────
79// SELECT YOUR DRIVER SETUP
80// Uncomment one of the following driver configurations
81// ──────────────────────────────────────────────
82
83/* === [A] Using MAX7219/MAX7221 via SOFTWARE SPI (any 3 digital pins) === */
84// #define DIN_PIN A4 // Define software SPI Data In pin
85// #define CLK_PIN A5 // Define software SPI Clock pin
86// #define CS_PIN A3 // Define SPI Chip Select pin
87// #include <SBK_MAX72xxSoft.h>
88// SBK_MAX72xxSoft driver(DIN_PIN, CLK_PIN, CS_PIN, 1); // Construct MAX72xx software SPI driver instance for 1 device : (DataIn pin, Clock pin, Chip Select pin, devices number)
89// #include <SBK_BarDrive.h>
90// SBK_BarDrive<SBK_MAX72xxSoft> bar(&driver, 0, customMapping); // (driver, device index, custom mapping)
91
92/* === [B] Using MAX7219/MAX7221 via HARDWARE SPI (dedicated MCU SPI pins) === */
93// #define CS_PIN A3
94// #include <SBK_MAX72xxHard.h>
95// SBK_MAX72xxHard driver(CS_PIN, 1); // Construct MAX72xx hardware SPI driver instance for 1 device : (Chip Select pin, devices number)
96// #include <SBK_BarDrive.h>
97// SBK_BarDrive<SBK_MAX72xxHard> bar(&driver, 0, customMapping); // (driver, device index, custom mapping)
98
99/* === [C] Using HT16K33 via I2C === */
100#include <SBK_HT16K33.h>
101const uint8_t NUM_DEV = 1; // Only one device : DEV0
102const uint8_t DEV0_IDX = 0; // Device DEV0 index
103const uint8_t DEV0_ADD = 0x70; // I2C Address (typically 0x70–0x77)
104const uint8_t DEV0_NUM_ROWS = 8; // 20-SOP HT16K33 with only 8 rows, 24-SOP has 12 rows, 28-SOP has 16 rows
105SBK_HT16K33 driver(NUM_DEV);
106#include <SBK_BarDrive.h>
107SBK_BarDrive<SBK_HT16K33> bar(&driver, 0, customMapping); // (driver, device index, custom mapping)
108
109void setup()
110{
111
112#ifdef SBK_HT16K33_IS_DEFINED
113 // HT16K33 driver instance setup (demo uses a single device)
114 driver.setAddress(DEV0_IDX, DEV0_ADD); // Set I2C address for device 0
115 driver.setDriverRows(DEV0_IDX, DEV0_NUM_ROWS); // Set number of active anode outputs (rows)
116#endif
117
118 driver.begin();
119 driver.setBrightness(0, 10);
120
121 bar.setDirection(BarDirection::FORWARD);
122
123 bar.animations().animInit().bounceFillUpDur(700).loop();
124}
125
126void loop()
127{
128 bar.animations().update();
129 bar.show();
130}