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

Blinks one or more WS2812 devices using a RGB array. More...

#include <Arduino.h>
#include <ws2812.h>
Include dependency graph for blink_array.c:

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 ws2812_dev
 Device struct.
 

Detailed Description

Blinks one or more WS2812 devices using a RGB array.

Author
Patrick Pedersen
Date
2021-04-09

The following example showcases how the Tiny-WS2812 library can be used on AVR platforms supporting the Arduino framework to blink an entire WS2812 device in white. In this rather memory expensive example, we achieve this by simply creating a rgb array equal to the number of LEDs on the WS2812 device, whose values we then transmit to the device using the ws2812_tx() function.

For a more memory efficient method, take a look at the blink_loop.c example.

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_array.c.

Function Documentation

◆ loop()

void loop ( )

Continously blinks the entire WS2812 device in white.

Definition at line 80 of file blink_array.c.

81 {
82  // Program all LEDs to white
83  for (unsigned int i = 0; i < N_LEDS; i++) {
84  leds[i].r = 255;
85  leds[i].g = 255;
86  leds[i].b = 255;
87  }
88 
89  ws2812_prep_tx(&ws2812_dev); // Prepare to transmit data
90  ws2812_tx(&ws2812_dev, leds, N_LEDS); // Transmit array of rgb values to the device
91  ws2812_close_tx(&ws2812_dev); // Close transmission
92 
93  // Wait 500ms
94  delay(500);
95 
96  // Program all LEDs to black (off)
97  for (unsigned int i = 0; i < N_LEDS; i++) {
98  leds[i].r = 0;
99  leds[i].g = 0;
100  leds[i].b = 0;
101  }
102 
103  ws2812_prep_tx(&ws2812_dev); // Prepare to transmit data
104  ws2812_tx(&ws2812_dev, leds, N_LEDS); // Transmit array of rgb values to the device
105  ws2812_close_tx(&ws2812_dev); // Close transmission
106 
107  // Wait 500ms
108  delay(500);
109 }
void ws2812_close_tx(ws2812 *dev)
Closes a WS2812 transmission.
Definition: ws2812_avr.c:275
void ws2812_prep_tx(ws2812 *dev)
Prepares the host device for data transmission.
Definition: ws2812_avr.c:159
void ws2812_tx(ws2812 *dev, ws2812_rgb *leds, size_t n_leds)
Transmits RGB values to the provided WS2812 device.
Definition: ws2812_avr.c:264

◆ setup()

void setup ( )

Configures a WS2812 device struct.

Definition at line 61 of file blink_array.c.

62 {
63  ws2812_cfg cfg; // Device config
64 
65  // Configure the WS2812 device struct
66  cfg.pins = pins;
67  cfg.rst_time_us = RESET_TIME;
68  cfg.order = COLOR_ORDER;
69  cfg.n_dev = sizeof(pins); // Number of devices driven by this struct
70 
71  if (ws2812_config(&ws2812_dev, &cfg) != 0) {
72  // HANDLE ERROR HERE
73  void;
74  }
75 }
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
uint8_t ws2812_config(ws2812 *dev, ws2812_cfg *cfg)
Configures a WS2812 device struct.
Definition: ws2812_avr.c:126