Skip to content

Commit c0b0098

Browse files
Added api documentation
1 parent 7e83b8b commit c0b0098

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

libraries/Arduino_LED_Matrix/src/Arduino_LED_Matrix.h

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,23 @@ static uint32_t reverse(uint32_t x)
148148

149149
static uint8_t __attribute__((aligned)) framebuffer[NUM_LEDS / 8];
150150

151+
/**
152+
* @brief Class representing an LED matrix for Arduino.
153+
*
154+
* This class provides methods to control and manage the LED matrix.
155+
*/
151156
class ArduinoLEDMatrix
152157
#ifdef MATRIX_WITH_ARDUINOGRAPHICS
153158
: public ArduinoGraphics
154159
#endif
155160
{
156161

157162
public:
163+
/**
164+
* @brief Constructor for the ArduinoLEDMatrix class.
165+
*
166+
* Initializes the LED matrix and sets up any necessary configurations.
167+
*/
158168
ArduinoLEDMatrix()
159169
#ifdef MATRIX_WITH_ARDUINOGRAPHICS
160170
: ArduinoGraphics(canvasWidth, canvasHeight)
@@ -165,12 +175,33 @@ class ArduinoLEDMatrix
165175
void autoscroll(uint32_t interval_ms) {
166176
_interval = interval_ms;
167177
}
178+
179+
/**
180+
* @brief Turns on the LED at the specified pin.
181+
*
182+
* @param `pin` The pin number where the LED is connected.
183+
*/
168184
void on(size_t pin) {
169185
turnLed(pin, true);
170186
}
187+
188+
/**
189+
* @brief Turns off the LED at the specified pin.
190+
*
191+
* @param `pin` The pin number where the LED is connected.
192+
*/
171193
void off(size_t pin) {
172194
turnLed(pin, false);
173195
}
196+
197+
/**
198+
* @brief Initializes the LED matrix and starts the timer.
199+
*
200+
* This function configures and starts a periodic timer for controlling the LED matrix.
201+
* It searches for an available timer, configures it, and starts it with a default frequency.
202+
*
203+
* @return Returns `1` (true) if initialization succeeds, otherwise `0` (false).
204+
*/
174205
int begin() {
175206
bool rv = true;
176207
uint8_t type;
@@ -185,6 +216,18 @@ class ArduinoLEDMatrix
185216
rv &= _ledTimer.start();
186217
return rv;
187218
}
219+
220+
/**
221+
* @brief Advances to the next frame in the LED matrix animation sequence.
222+
*
223+
* This function updates the frame buffer by fetching the next frame data from
224+
* the stored sequence. If the animation reaches the last frame, it either loops
225+
* or stops based on the loop setting. If a callback function is set, it is
226+
* triggered when the sequence completes.
227+
*
228+
* @note This function also reverses frame data before updating the framebuffer.
229+
*
230+
*/
188231
void next() {
189232
uint32_t frame[3];
190233
frame[0] = reverse(*(_frames+(_currentFrame*4)+0));
@@ -203,6 +246,17 @@ class ArduinoLEDMatrix
203246
}
204247
memcpy(framebuffer, (uint32_t*)frame, sizeof(frame));
205248
}
249+
250+
/**
251+
* @brief Loads a single frame into the LED matrix and updates the display.
252+
*
253+
* This function takes a frame buffer containing LED matrix data and loads it
254+
* as a single-frame animation sequence. It then advances to display the frame
255+
* immediately and ensures the animation interval is set to 0 to prevent looping.
256+
*
257+
* @param `buffer` An array of three 32-bit values representing the frame data.
258+
*
259+
*/
206260
void loadFrame(const uint32_t buffer[3]){
207261
uint32_t tempBuffer[][4] = {{
208262
buffer[0], buffer[1], buffer[2], 0
@@ -211,16 +265,47 @@ class ArduinoLEDMatrix
211265
next();
212266
_interval = 0;
213267
}
268+
269+
/**
270+
* @brief Renders a specific frame from the loaded sequence.
271+
*
272+
* This function sets the current frame index to the specified frame number,
273+
* ensuring it wraps around within the available frame count. It then updates
274+
* the display to show the selected frame and stops any automatic transitions
275+
* by setting the interval to 0.
276+
*
277+
* @param `frameNumber` The index of the frame to be displayed.
278+
*/
214279
void renderFrame(uint8_t frameNumber){
215280
_currentFrame = frameNumber % _framesCount;
216281
next();
217282
_interval = 0;
218283
}
284+
285+
/**
286+
* @brief Starts playing the loaded frame sequence.
287+
*
288+
* This function begins playback of the current sequence, advancing through
289+
* frames automatically. If looping is enabled, the sequence will restart
290+
* after reaching the last frame.
291+
*
292+
* @param `loop` Set to `true` to enable looping; `false` to play the sequence once. Default is `false`.
293+
*/
219294
void play(bool loop = false){
220295
_loop = loop;
221296
_sequenceDone = false;
222297
next();
223298
}
299+
300+
/**
301+
* @brief Checks if the sequence has finished playing.
302+
*
303+
* This function checks if the current frame sequence has completed.
304+
* If the sequence is done, it will reset the `_sequenceDone` flag
305+
* and return `true`, otherwise it returns `false`.
306+
*
307+
* @return `true` if the sequence is done, `false` otherwise.
308+
*/
224309
bool sequenceDone(){
225310
if(_sequenceDone){
226311
_sequenceDone = false;
@@ -229,6 +314,18 @@ class ArduinoLEDMatrix
229314
return false;
230315
}
231316

317+
/**
318+
* @brief Loads pixel data from an array into a buffer.
319+
*
320+
* This function takes a byte array (`arr`) containing pixel data and converts it
321+
* into a `uint32_t` buffer (`dst`). Each byte in the array represents 8 pixels,
322+
* and the data is packed into the 32-bit buffer accordingly. The function processes
323+
* the pixel data byte by byte and stores the packed data into the buffer in chunks of 32 bits.
324+
*
325+
* @param `arr` The input byte array containing pixel data.
326+
* @param `size` The number of elements in the array `arr` to process.
327+
* @param `dst` The destination buffer where the packed pixel data will be stored.
328+
*/
232329
static void loadPixelsToBuffer(uint8_t* arr, size_t size, uint32_t* dst) {
233330
uint32_t partialBuffer = 0;
234331
uint8_t pixelIndex = 0;
@@ -244,11 +341,36 @@ class ArduinoLEDMatrix
244341
}
245342
}
246343

344+
/**
345+
* @brief Loads pixel data into the frame buffer and prepares it for rendering.
346+
*
347+
* This function takes a byte array (`arr`) containing pixel data, converts it
348+
* into a 32-bit buffer using `loadPixelsToBuffer`, and then loads the buffer
349+
* into the frame sequence using `loadFrame`. The function ensures that the pixel
350+
* data is properly formatted and ready for display by converting the data into
351+
* a suitable format and storing it in the internal frame buffer.
352+
*
353+
* @param `arr` The input byte array containing the pixel data to be loaded.
354+
* @param `size` The number of elements in the array `arr` to process.
355+
*/
247356
void loadPixels(uint8_t *arr, size_t size){
248357
loadPixelsToBuffer(arr, size, _frameHolder);
249358
loadFrame(_frameHolder);
250359
};
251360

361+
/**
362+
* @brief Loads a sequence of frames into the frame buffer.
363+
*
364+
* This function accepts a 2D array (`frames`) containing frame data and the
365+
* number of frames (`howMany`). It calculates the total number of frames
366+
* and sets the appropriate frame pointer for future frame rendering. The
367+
* function also resets the current frame index to 0, ensuring that frame
368+
* playback starts from the beginning.
369+
*
370+
* @param `frames` A 2D array containing the frame data to be loaded into the
371+
* frame buffer. Each frame is expected to contain 4 uint32_t values.
372+
* @param `howMany` The total number of frame elements (in uint32_t) in the `frames` array.
373+
*/
252374
void loadWrapper(const uint32_t frames[][4], uint32_t howMany) {
253375
_currentFrame = 0;
254376
_frames = (uint32_t*)frames;
@@ -259,6 +381,10 @@ class ArduinoLEDMatrix
259381
_callBack = callBack;
260382
}
261383

384+
/**
385+
* @brief Clears the LED matrix display and resets the canvas buffer.
386+
*
387+
*/
262388
void clear() {
263389
const uint32_t fullOff[] = {
264390
0x00000000,
@@ -273,6 +399,22 @@ class ArduinoLEDMatrix
273399

274400

275401
#ifdef MATRIX_WITH_ARDUINOGRAPHICS
402+
/**
403+
* @brief Sets a pixel at the specified coordinates with a color.
404+
*
405+
* This function updates the pixel at position `(x, y)` on the canvas. The pixel is
406+
* set to on (1) if any of the red (r), green (g), or blue (b) values are greater
407+
* than 0.
408+
*
409+
* @param `x` The x-coordinate (horizontal position) of the pixel to set.
410+
* Should be between 0 and `canvasWidth - 1`.
411+
* @param `y` The y-coordinate (vertical position) of the pixel to set.
412+
* Should be between 0 and `canvasHeight - 1`.
413+
* @param `r` The red color value (used here for logical presence check).
414+
* @param `g` The green color value (used here for logical presence check).
415+
* @param `b` The blue color value (used here for logical presence check).
416+
*
417+
*/
276418
virtual void set(int x, int y, uint8_t r, uint8_t g, uint8_t b) {
277419
if (y >= canvasHeight || x >= canvasWidth || y < 0 || x < 0) {
278420
return;
@@ -281,6 +423,17 @@ class ArduinoLEDMatrix
281423
_canvasBuffer[y][x] = (r | g | b) > 0 ? 1 : 0;
282424
}
283425

426+
/**
427+
* @brief Ends the text rendering and optionally scrolls.
428+
*
429+
* This function finalizes the text rendering process and, if scrolling is
430+
* requested, applies the scroll.
431+
*
432+
* @param `scrollDirection` The direction to scroll the text after rendering.
433+
* Possible values are:
434+
* - `NO_SCROLL` (default): No scrolling.
435+
* - A positive value indicates scrolling in a direction.
436+
*/
284437
void endText(int scrollDirection = NO_SCROLL) {
285438
ArduinoGraphics::endText(scrollDirection);
286439
renderBitmap(_canvasBuffer, canvasHeight, canvasWidth);
@@ -302,6 +455,22 @@ class ArduinoLEDMatrix
302455
}
303456
}
304457

458+
/**
459+
* @brief Ends the text rendering and captures it into an animation buffer.
460+
*
461+
* @param `scrollDirection` The direction to scroll the text after rendering.
462+
* Typically, the value can specify no scroll or a scroll speed for animation purposes.
463+
*
464+
* @param `frames` A 2D array that holds the captured frames, where each frame is
465+
* represented by a set of 4 32-bit values.
466+
*
467+
* @param `howManyMax` The maximum number of frames that can be captured into the
468+
* `frames` buffer.
469+
*
470+
* @param `howManyUsed` A reference that will hold the number of frames actually
471+
* used to capture the animation.
472+
*
473+
*/
305474
void endTextToAnimationBuffer(int scrollDirection, uint32_t frames[][4], uint32_t howManyMax, uint32_t& howManyUsed) {
306475
captureAnimationFrame = &frames[0][0];
307476
captureAnimationHowManyRemains = howManyMax;
@@ -315,6 +484,13 @@ class ArduinoLEDMatrix
315484
howManyUsed = howManyMax - captureAnimationHowManyRemains;
316485
}
317486

487+
/**
488+
* @brief Sets the scroll speed for the text rendering.
489+
*
490+
* @param `speed` The speed at which the text should scroll, typically in milliseconds
491+
* or as defined by the graphics library. A higher value will make
492+
* the text scroll slower, and a lower value will make it scroll faster.
493+
*/
318494
void textScrollSpeed(unsigned long speed) {
319495
ArduinoGraphics::textScrollSpeed(speed);
320496
_textScrollSpeed = speed;

0 commit comments

Comments
 (0)