Serial Wombat Arduino Library
Loading...
Searching...
No Matches
SerialWombatCharlieplex.h
Go to the documentation of this file.
1#pragma once
2/*
3Copyright 2026 Broadwell Consulting Inc.
4
5"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6the United States. See SerialWombat.com for usage guidance.
7
8Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25*/
26
27#include <stdint.h>
28#include "SerialWombat.h"
29
32
53{
54public:
62
63 static const uint8_t MAX_PINS = 8;
64 static const uint8_t MAX_LEDS = 56;
65 static const uint8_t BITMAP_BYTES = 7;
66 static const uint8_t UNUSED_PIN = 0xFF;
67 static const uint8_t UNUSED_LED = 0xFF;
68
74 SerialWombatPin(serialWombat),
76 {
77 }
78
106 int16_t begin(uint8_t pin0,
107 uint8_t pin1,
108 uint8_t pin2,
109 uint8_t pin3,
110 uint8_t pin4,
111 uint8_t pin5 = UNUSED_PIN,
112 uint8_t pin6 = UNUSED_PIN,
113 uint8_t pin7 = UNUSED_PIN,
115 uint8_t scanPeriod_mS = 0)
116 {
117 const uint8_t pins[MAX_PINS] = {
118 pin0,
119 pin1,
120 pin2,
121 pin3,
122 pin4,
123 pin5,
124 pin6,
125 pin7
126 };
127
128 uint8_t numberOfPins = MAX_PINS;
129 for (uint8_t i = 0; i < MAX_PINS; ++i)
130 {
131 if (pins[i] == UNUSED_PIN)
132 {
133 numberOfPins = i;
134 break;
135 }
136 }
137
138 _pin = pin0;
140
141 uint8_t tx0[] = {
143 _pin,
144 _pinMode,
145 numberOfPins,
146 (uint8_t)displayMode,
147 scanPeriod_mS,
148 pin1,
149 pin2
150 };
151
152 int16_t result = _sw.sendPacket(tx0);
153 if (result < 0)
154 {
155 return result;
156 }
157
158 uint8_t tx1[] = {
160 _pin,
161 _pinMode,
162 pin3,
163 pin4,
164 pin5,
165 pin6,
166 pin7
167 };
168
169 return _sw.sendPacket(tx1);
170 }
171
180 int16_t writeLEDArray(const uint8_t ledArray[BITMAP_BYTES])
181 {
182 uint8_t tx2[] = {
184 _pin,
185 _pinMode,
186 ledArray[0],
187 ledArray[1],
188 ledArray[2],
189 ledArray[3],
190 ledArray[4]
191 };
192
193 int16_t result = _sw.sendPacket(tx2);
194 if (result < 0)
195 {
196 return result;
197 }
198
199 uint8_t tx3[] = {
201 _pin,
202 _pinMode,
203 ledArray[5],
204 ledArray[6],
205 0x55,
206 0x55,
207 0x55
208 };
209
210 return _sw.sendPacket(tx3);
211 }
212
228 int16_t setLEDs(uint8_t led0,
229 uint8_t led1 = UNUSED_LED,
230 uint8_t led2 = UNUSED_LED,
231 uint8_t led3 = UNUSED_LED,
232 uint8_t led4 = UNUSED_LED)
233 {
234 return writeLEDCommand(
236 led0, led1, led2, led3, led4);
237 }
238
247 int16_t setLEDs(const uint8_t ledIndexes[], uint8_t count)
248 {
249 return writeLEDCommand(
251 ledIndexes,
252 count);
253 }
254
270 int16_t clearLEDs(uint8_t led0,
271 uint8_t led1 = UNUSED_LED,
272 uint8_t led2 = UNUSED_LED,
273 uint8_t led3 = UNUSED_LED,
274 uint8_t led4 = UNUSED_LED)
275 {
276 return writeLEDCommand(
278 led0, led1, led2, led3, led4);
279 }
280
289 int16_t clearLEDs(const uint8_t ledIndexes[], uint8_t count)
290 {
291 return writeLEDCommand(
293 ledIndexes,
294 count);
295 }
296
302 int16_t clearLEDs()
303 {
304 uint8_t ledArray[BITMAP_BYTES] = {0, 0, 0, 0, 0, 0, 0};
305 return writeLEDArray(ledArray);
306 }
307
322 int16_t writeLookupEntries(uint8_t firstLEDIndex,
323 uint8_t entry0,
324 uint8_t entry1,
325 uint8_t entry2,
326 uint8_t entry3)
327 {
328 uint8_t tx[] = {
330 _pin,
331 _pinMode,
332 firstLEDIndex,
333 entry0,
334 entry1,
335 entry2,
336 entry3
337 };
338 return _sw.sendPacket(tx);
339 }
340
348 int16_t writeLookupTable(const uint8_t lookupTable[MAX_LEDS])
349 {
350 for (uint8_t firstLEDIndex = 0;
351 firstLEDIndex < MAX_LEDS;
352 firstLEDIndex += 4)
353 {
354 int16_t result = writeLookupEntries(
355 firstLEDIndex,
356 lookupTable[firstLEDIndex],
357 lookupTable[firstLEDIndex + 1],
358 lookupTable[firstLEDIndex + 2],
359 lookupTable[firstLEDIndex + 3]);
360
361 if (result < 0)
362 {
363 return result;
364 }
365 }
366 return 0;
367 }
368
375 static uint8_t encodePinPair(uint8_t highPinIndex, uint8_t lowPinIndex)
376 {
377 return (uint8_t)((highPinIndex << 4) | (lowPinIndex & 0x0F));
378 }
379
384 uint8_t pin()
385 {
387 }
388
394 {
396 }
397
398private:
399 int16_t writeLEDCommand(uint8_t command,
400 uint8_t led0,
401 uint8_t led1,
402 uint8_t led2,
403 uint8_t led3,
404 uint8_t led4)
405 {
406 const uint8_t ledIndexes[5] = {led0, led1, led2, led3, led4};
407
408 for (uint8_t i = 0; i < 5; ++i)
409 {
410 if (ledIndexes[i] != UNUSED_LED && ledIndexes[i] >= MAX_LEDS)
411 {
412 return -1;
413 }
414 }
415
416 uint8_t tx[] = {
417 command,
418 _pin,
419 _pinMode,
420 led0,
421 led1,
422 led2,
423 led3,
424 led4
425 };
426
427 return _sw.sendPacket(tx);
428 }
429
430 int16_t writeLEDCommand(uint8_t command,
431 const uint8_t ledIndexes[],
432 uint8_t count)
433 {
434 if (count > 5)
435 {
436 return -1;
437 }
438
439 uint8_t leds[5] = {
445 };
446
447 for (uint8_t i = 0; i < count; ++i)
448 {
449 if (ledIndexes[i] >= MAX_LEDS)
450 {
451 return -1;
452 }
453 leds[i] = ledIndexes[i];
454 }
455
456 return writeLEDCommand(
457 command,
458 leds[0],
459 leds[1],
460 leds[2],
461 leds[3],
462 leds[4]);
463 }
464
465};
@ PIN_MODE_CHARLIEPLEX
(43)
SerialWombatAbstractScaledOutput(SerialWombatChip &sw)
Constructor for the SerialWombatAbstractScaledOutput Class.
@ DISPLAY_MODE_SCALED_SINGLE_LED
Scaled-output value selects one logical LED index.
@ DISPLAY_MODE_PUBLIC_DATA_BITMAP
Controlling pin public data controls LEDs 0 through 15 as a bitfield.
@ DISPLAY_MODE_SCALED_BARGRAPH
Scaled-output value lights LEDs 0 through the selected index.
@ DISPLAY_MODE_BITMAP
Seven-byte protocol bitmap controls up to 56 LEDs.
int16_t setLEDs(uint8_t led0, uint8_t led1=UNUSED_LED, uint8_t led2=UNUSED_LED, uint8_t led3=UNUSED_LED, uint8_t led4=UNUSED_LED)
Set up to five logical LEDs in bitmap display mode.
static uint8_t encodePinPair(uint8_t highPinIndex, uint8_t lowPinIndex)
Encode a logical high pin and logical low pin into one lookup byte.
static const uint8_t BITMAP_BYTES
uint8_t pin()
Fulfills a virtual function requirement of SerialWombatAbstractScaledOutput.
int16_t begin(uint8_t pin0, uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t pin5=UNUSED_PIN, uint8_t pin6=UNUSED_PIN, uint8_t pin7=UNUSED_PIN, displayMode_t displayMode=DISPLAY_MODE_BITMAP, uint8_t scanPeriod_mS=0)
Initialize a Charlieplexed LED display.
SerialWombatCharlieplex(SerialWombatChip &serialWombat)
Constructor for the SerialWombatCharlieplex class.
uint8_t swPinModeNumber()
Fulfills a virtual function requirement of SerialWombatAbstractScaledOutput.
int16_t clearLEDs()
Turn off all LEDs in bitmap display mode.
int16_t clearLEDs(const uint8_t ledIndexes[], uint8_t count)
Clear up to five logical LEDs from an array.
static const uint8_t UNUSED_PIN
int16_t setLEDs(const uint8_t ledIndexes[], uint8_t count)
Set up to five logical LEDs from an array.
int16_t writeLookupEntries(uint8_t firstLEDIndex, uint8_t entry0, uint8_t entry1, uint8_t entry2, uint8_t entry3)
Write four consecutive logical LED lookup entries.
int16_t clearLEDs(uint8_t led0, uint8_t led1=UNUSED_LED, uint8_t led2=UNUSED_LED, uint8_t led3=UNUSED_LED, uint8_t led4=UNUSED_LED)
Clear up to five logical LEDs in bitmap display mode.
static const uint8_t UNUSED_LED
int16_t writeLookupTable(const uint8_t lookupTable[MAX_LEDS])
Write all 56 logical LED lookup entries.
int16_t writeLEDArray(const uint8_t ledArray[BITMAP_BYTES])
Write the complete LED bitmap from an array of bytes.
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
SerialWombatChip & _sw
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.