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

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

#include <avr/io.h>
#include <util/delay.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_PORT   PORTB
 Port register used to communicate with the WS2812 device(s)
 
#define DATA_PINS_DDR   DDRB
 Data direction register of the pin(s) used to communicate with the WS2812 device(s)
 
#define DATA_PINS   {PB0, PB1}
 Pin(s) used to communicate with the WS2812 device(s)
 
#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

int main ()
 

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 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_AVR macro is defined during compilation. This can either be done by specifying -DWS2812_TARGET_PLATFORM_AVR in the build flags, or by uncommenting the define WS2812_TARGET_PLATFORM_AVR directive below.

Definition in file blink_cpp.cpp.

Function Documentation

◆ main()

int main ( )

Blinks one or more WS2812 device(s)

Definition at line 56 of file blink_cpp.cpp.

57 {
58  uint8_t pins[] = DATA_PINS; // Data pins
59  ws2812_rgb leds[N_LEDS]; // RGB array which represents the LEDs
60  ws2812_cfg cfg; // Device configurationn
61 
62  // Configure the WS2812 device struct
63  cfg.port = &DATA_PINS_PORT;
64  cfg.ddr = &DATA_PINS_DDR;
65  cfg.pins = pins;
66  cfg.n_dev = sizeof(pins); // Number of devices driven by this struct
67  cfg.rst_time_us = RESET_TIME;
68  cfg.order = grb;
69 
70  uint8_t ret;
71  ws2812_cpp ws2812_dev(&cfg, &ret); // Initialize device struct
72 
73  if (ret != 0) {
74  // HANDLE ERROR...
75  void;
76  };
77 
78  // Blink device
79  while (1) {
80  // Program all LEDs to white
81  for (unsigned int i = 0; i < N_LEDS; i++) {
82  leds[i].r = 255;
83  leds[i].g = 255;
84  leds[i].b = 255;
85  }
86 
87  ws2812_dev.prep_tx(); // Prepare to transmit data
88  ws2812_dev.tx(leds, N_LEDS); // Transmit array of rgb values to the device
89  ws2812_dev.close_tx(); // Close transmission
90 
91  // Wait 500ms
92  _delay_ms(500);
93 
94  // Program all LEDs to black (off)
95  for (unsigned int i = 0; i < N_LEDS; i++) {
96  leds[i].r = 0;
97  leds[i].g = 0;
98  leds[i].b = 0;
99  }
100 
101  ws2812_dev.prep_tx(); // Prepare to transmit data
102  ws2812_dev.tx(leds, N_LEDS); // Transmit array of rgb values to the device
103  ws2812_dev.close_tx(); // Close transmission
104 
105  // Wait 500ms
106  _delay_ms(500);
107  }
108 
109  return 0;
110 }
ALL PLATFORMS: Data structure to configure a WS2812 device struct.
Definition: ws2812_avr.h:64
volatile uint8_t * ddr
Data Direction Register (ex. DDRB, DDRC, DDRD...)
Definition: ws2812_avr.h:68
uint8_t n_dev
Number of WS2812 device to drive.
Definition: ws2812_avr.h:78
volatile uint8_t * port
PORT Register (ex. PORTB, PORTC, PORTD...)
Definition: ws2812_avr.h:67
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
Data structure to hold RGB color values.
Definition: ws2812_common.h:67