Tiny-WS2812  1.0.0
A tiny cross-platform WS2812 LED Strip driver
Macros | Functions | Variables
blink_cpp.cpp File Reference

C++ Wrapper implementation of blink_array.c
More...

#include <Arduino.h>
#include <ws2812_cpp.h>
Include dependency graph for blink_cpp.cpp:

Go to the source code of this file.

Macros

#define N_LEDS   8
 Number of LEDs on your WS2812 device(s)
 
#define DATA_PINS   {8, 9}
 Arduino pin(s) used to program the WS2812 device(s). Must share same port! (See https://www.arduino.cc/en/Reference/PortManipulation)
 
#define RESET_TIME   50
 Reset time in microseconds (50us recommended by datasheet)
 
#define COLOR_ORDER   grb
 Color order of your WS2812 LEDs (Typically grb or rgb)
 

Functions

void setup ()
 
void loop ()
 

Variables

uint8_t pins [] = DATA_PINS
 Data pins.
 
ws2812_rgb leds [N_LEDS]
 RGB array which represents the LEDs.
 
ws2812_cpp * ws2812_dev
 Device object.
 

Detailed Description

C++ Wrapper implementation of blink_array.c

Author
Patrick Pedersen
Date
2021-04-09

This example showcases the blink array example on AVR platforms which support the Arduino Framework, using the C++ wrapper of the Tiny-WS2812 driver. The purpose of this example is to merely showcase the differences between the C++ wrapper and the C interface. For a more detailed description of the functionality, consult the blink array example written in the C interface.

Note
Please ensure that the WS2812_TARGET_PLATFORM_ARDUINO_AVR macro is defined during compilation. This can either be done by specifying -DWS2812_TARGET_PLATFORM_ARDUINO_AVR in the build flags, or by uncommenting the #define WS2812_TARGET_PLATFORM_ARDUINO_AVR directive at the top of this file.

Definition in file blink_cpp.cpp.

Function Documentation

◆ loop()

void loop ( )

Continously blinks the entire WS2812 device in white.

Definition at line 79 of file blink_cpp.cpp.

80 {
81  // Program all LEDs to white
82  for (unsigned int i = 0; i < N_LEDS; i++) {
83  leds[i].r = 255;
84  leds[i].g = 255;
85  leds[i].b = 255;
86  }
87 
88  ws2812_dev->prep_tx(); // Prepare to transmit data
89  ws2812_dev->tx(leds, N_LEDS); // Transmit array of rgb values to the device
90  ws2812_dev->close_tx(); // Close transmission
91 
92  // Wait 500ms
93  delay(500);
94 
95  // Program all LEDs to black (off)
96  for (unsigned int i = 0; i < N_LEDS; i++) {
97  leds[i].r = 0;
98  leds[i].g = 0;
99  leds[i].b = 0;
100  }
101 
102  ws2812_dev->prep_tx(); // Prepare to transmit data
103  ws2812_dev->tx(leds, N_LEDS); // Transmit array of rgb values to the device
104  ws2812_dev->close_tx(); // Close transmission
105 
106  // Wait 500ms
107  delay(500);
108 }

◆ setup()

void setup ( )

Configures a WS2812 device struct.

Definition at line 57 of file blink_cpp.cpp.

58 {
59  ws2812_cfg cfg; // Device config
60 
61  // Configure the WS2812 device struct
62  cfg.pins = pins;
63  cfg.rst_time_us = RESET_TIME;
64  cfg.order = COLOR_ORDER;
65  cfg.n_dev = sizeof(pins); // Number of devices driven by this struct
66 
67  uint8_t ret;
68  ws2812_dev = new ws2812_cpp(&cfg, &ret); // Initialize the device object
69 
70  if (ret != 0) {
71  // HANDLE ERROR HERE
72  void;
73  }
74 }
ALL PLATFORMS: Data structure to configure a WS2812 device struct.
Definition: ws2812_avr.h:64
uint8_t n_dev
Number of WS2812 device to drive.
Definition: ws2812_avr.h:78
uint8_t rst_time_us
Time required for the WS2812 device(s) to reset in us.
Definition: ws2812_avr.h:76
ws2812_order order
CoColor order of the WS2812 device(s) (ex. rgb, grb, bgr...)
Definition: ws2812_avr.h:77
uint8_t * pins
Array of pins used to program WS2812 devices (Must share the same PORT! (ex. PB0, PB1,...
Definition: ws2812_avr.h:69