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

Blinks one or more WS2812 devices using a more memory efficient method than the blink_array.c example. More...

#include <avr/io.h>
#include <util/delay.h>
#include <ws2812.h>
Include dependency graph for blink_loop.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_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 ()
 

Variables

ws2812_rgb white = {255,255,255}
 
ws2812_rgb black = {0, 0 ,0}
 

Detailed Description

Blinks one or more WS2812 devices using a more memory efficient method than the blink_array.c example.

Author
Patrick Pedersen
Date
2021-04-09

The following example showcases how the Tiny-WS2812 library can be used on AVR platforms to blink an entire WS2812 device in white. Unlike the other blink example, we do not allocate a memory expensive array for every single LED here, but instead loop the transmission of a single RGB value until all WS2812 LEDs have been set.

The advantage of this method is that we can save allot of memory, the disadvantage is that this method is more prone to programming mistakes.

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

Function Documentation

◆ main()

int main ( )

Blinks one or more WS2812 device(s)

Definition at line 63 of file blink_loop.c.

64 {
65  uint8_t pins[] = DATA_PINS; // Data pins
66  ws2812_cfg cfg; // Device configurationn
67  ws2812 ws2812_dev; // Device struct
68 
69  // Configure the WS2812 device struct
70  cfg.port = &DATA_PINS_PORT;
71  cfg.ddr = &DATA_PINS_DDR;
72  cfg.pins = pins;
73  cfg.n_dev = sizeof(pins); // Number of devices driven by this struct
74  cfg.rst_time_us = RESET_TIME;
75  cfg.order = grb;
76 
77  if (ws2812_config(&ws2812_dev, &cfg) != 0) {
78  // HANDLE ERROR...
79  void;
80  };
81 
82  // Blink device
83  while (1) {
84  // Prepare to transmit data
86 
87  // Program all LEDs to white
88  for (unsigned int i = 0; i < N_LEDS; i++) {
89  ws2812_tx(&ws2812_dev, &white, sizeof(white)/sizeof(ws2812_rgb));
90  // THIS LOOP NEEDS TO RUN UNINTERRUPTED!
91  }
92 
93  // Close transmission
95 
96  // Wait 500ms
97  _delay_ms(500);
98 
99  // Prepare to transmit data
101 
102  // Program all LEDs to black (off)
103  for (unsigned int i = 0; i < N_LEDS; i++) {
104  ws2812_tx(&ws2812_dev, &black, sizeof(black)/sizeof(ws2812_rgb));
105  // THIS LOOP NEEDS TO RUN UNINTERRUPTED!
106  }
107 
108  // Close transmission
110 
111  // Wait 500ms
112  _delay_ms(500);
113  }
114 
115  return 0;
116 }
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
ALL PLATFORMS: WS2812 device struct to drive one or more WS2812 devices.
Definition: ws2812_avr.h:107
void ws2812_close_tx(ws2812 *dev)
Closes a WS2812 transmission.
Definition: ws2812_avr.c:275
uint8_t ws2812_config(ws2812 *dev, ws2812_cfg *cfg)
Configures a WS2812 device struct.
Definition: ws2812_avr.c:126
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