Tiny-WS2812  1.0.0
A tiny cross-platform WS2812 LED Strip driver
blink_array.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Patrick Pedersen
3 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13 
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  *
17  */
18 
41 // #define WS2812_TARGET_PLATFORM_AVR
42 
43 #include <avr/io.h>
44 #include <util/delay.h>
45 
46 #include <ws2812.h>
47 
48 // Parameters - ALTER THESE TO CORRESPOND WITH YOUR OWN SETUP!
49 #define N_LEDS 8
50 #define DATA_PINS_PORT PORTB
51 #define DATA_PINS_DDR DDRB
52 #define DATA_PINS {PB0, PB1}
53 #define RESET_TIME 50
54 #define COLOR_ORDER grb
55 
59 int main()
60 {
61  uint8_t pins[] = DATA_PINS; // Data pins
62  ws2812_rgb leds[N_LEDS]; // RGB array which represents the LEDs
63  ws2812_cfg cfg; // Device configurationn
64  ws2812 ws2812_dev; // Device struct
65 
66  // Configure the WS2812 device struct
67  cfg.port = &DATA_PINS_PORT;
68  cfg.ddr = &DATA_PINS_DDR;
69  cfg.pins = pins;
70  cfg.n_dev = sizeof(pins); // Number of devices driven by this struct
71  cfg.rst_time_us = RESET_TIME;
72  cfg.order = grb;
73 
74  if (ws2812_config(&ws2812_dev, &cfg) != 0) {
75  // HANDLE ERROR...
76  void;
77  };
78 
79  // Blink device
80  while (1) {
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_prep_tx(&ws2812_dev); // Prepare to transmit data
89  ws2812_tx(&ws2812_dev, leds, N_LEDS); // Transmit array of rgb values to the device
90  ws2812_close_tx(&ws2812_dev); // Close transmission
91 
92  // Wait 500ms
93  _delay_ms(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_prep_tx(&ws2812_dev); // Prepare to transmit data
103  ws2812_tx(&ws2812_dev, leds, N_LEDS); // Transmit array of rgb values to the device
104  ws2812_close_tx(&ws2812_dev); // Close transmission
105 
106  // Wait 500ms
107  _delay_ms(500);
108  }
109 
110  return 0;
111 }
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
Exposes the Tiny-WS2812 library interface.
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