SBK_HT16K33 library 2.0.0
Standalone Arduino library to control HT16K33 LED matrices or bar meters via I2C. Can be used directly or as a driver for SBK_BarDrive animations.
Loading...
Searching...
No Matches
SBK_HT16K33.h
Go to the documentation of this file.
1
23
24/**********************************************************************
25 * This library is a modified version of the original HT16K33 Arduino driver
26 * from MikeS11's ProtonPack repository:
27 * https://github.com/MikeS11/ProtonPack/tree/master/Source/Libraries/ht16k33-arduino-master
28 *
29 * Modifications were made specifically to drive vertical bar meters for use in
30 * SBK projects such as the Wrist Blaster.
31 *
32 * All credit for the original implementation goes to Mike S11.
33 **********************************************************************/
34
35#pragma once
36
37#define SBK_HT16K33_IS_DEFINED
38
39#include <Arduino.h>
40#include <Wire.h>
41
42// HT16K33 Command Definitions
43#define HT16K33_CMD_RAM 0x00
44#define HT16K33_CMD_KEYS 0x40
45#define HT16K33_CMD_SETUP 0x80
46#define HT16K33_CMD_ROWINT 0xA0
47#define HT16K33_CMD_DIMMING 0xE0
48
49#define HT16K33_DISPLAY_OFF 0x00
50#define HT16K33_DISPLAY_ON 0x01
51#define HT16K33_BLINK_OFF 0x00
52#define HT16K33_BLINK_1HZ 0x02
53#define HT16K33_BLINK_2HZ 0x04
54#define HT16K33_BLINK_0HZ5 0x06
55
61{
62public:
68 SBK_HT16K33(uint8_t devsNum = 1);
69
74
90 void setDriverRows(uint8_t devIdx, uint8_t rowsCount = 8)
91 {
92 if (devIdx >= 8 || (rowsCount != 8 && rowsCount != 12 && rowsCount != 16))
93 return;
94
95 _maxRows[devIdx] = rowsCount;
96 }
97
114 uint8_t maxRows(uint8_t devIdx) const { return _maxRows[devIdx]; }
115
124 uint8_t maxColumns() const { return _defaultColBufferSize; }
125
144 uint8_t maxSegments(uint8_t devIdx) const { return maxRows(devIdx) * maxColumns(); }
145
156 uint8_t setAddress(uint8_t devIdx, uint8_t addr);
157
161 void begin();
162
171 void clear(uint8_t devIdx);
172
179 void clear();
180
190 void setBrightness(uint8_t devIdx, uint8_t brightness);
191
200 void setBrightness(uint8_t brightness);
201
207 uint8_t devsNum() const { return _devsNum; }
208
224 void setLed(uint8_t devIdx, uint8_t rowIdx, uint8_t colIdx, bool state);
225
241 bool getLed(uint8_t devIdx, uint8_t rowIdx, uint8_t colIdx) const;
242
253 void show(uint8_t devIdx);
254
261 void show();
262
263private:
264 uint8_t _devsNum = 1;
265 uint8_t _i2c_addr[8];
266 uint8_t _maxRows[8];
267 uint16_t *_buffer;
268 static constexpr uint8_t _defaultRowBufferSize = 8; // HT16K33 comes in 3 versions 20SOP, 24SOP and 28SOP: 8, 12 or 16 rows (anodes)
269 static constexpr uint8_t _defaultColBufferSize = 8;
270
271 void _write(uint8_t devIdx);
272 inline uint8_t _colIndex(uint8_t devIdx, uint8_t colIdx) const;
273};
uint8_t maxRows(uint8_t devIdx) const
Returns the number of active row lines (anode outputs) for a specific HT16K33 device.
Definition SBK_HT16K33.h:114
uint8_t maxSegments(uint8_t devIdx) const
Returns the total number of addressable LED segments for the specified device.
Definition SBK_HT16K33.h:144
void setLed(uint8_t devIdx, uint8_t rowIdx, uint8_t colIdx, bool state)
Set the state of an individual LED for a specific device.
Definition SBK_HT16K33.cpp:126
void setBrightness(uint8_t devIdx, uint8_t brightness)
Set the brightness level (0–15) for a specific device.
Definition SBK_HT16K33.cpp:103
SBK_HT16K33(uint8_t devsNum=1)
Construct a new SBK_HT16K33 instance.
Definition SBK_HT16K33.cpp:23
~SBK_HT16K33()
Destructor. Frees allocated buffer memory.
Definition SBK_HT16K33.cpp:34
uint8_t setAddress(uint8_t devIdx, uint8_t addr)
Set the I2C address for a specific HT16K33 device.
Definition SBK_HT16K33.cpp:43
void setDriverRows(uint8_t devIdx, uint8_t rowsCount=8)
Set the number of active rows (anode outputs) for a specific HT16K33 device.
Definition SBK_HT16K33.h:90
uint8_t devsNum() const
Returns the number of active HT16K33 devices managed by this driver instance.
Definition SBK_HT16K33.h:207
bool getLed(uint8_t devIdx, uint8_t rowIdx, uint8_t colIdx) const
Get the current state of an LED from the internal display buffer.
Definition SBK_HT16K33.cpp:148
uint8_t maxColumns() const
Returns the number of active column lines (cathode outputs = C0–C7) configured for this instance.
Definition SBK_HT16K33.h:124
void clear()
Clear the display buffers for all devices.
Definition SBK_HT16K33.cpp:94
void begin()
Initialize the HT16K33 device at a given I2C address.
Definition SBK_HT16K33.cpp:52
void show()
Push the internal display buffer to all devices.
Definition SBK_HT16K33.cpp:180