Tiny-WS2812  1.0.0
A tiny cross-platform WS2812 LED Strip driver
blink_cpp.cpp
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 
38 // #define WS2812_TARGET_PLATFORM_AVR
39 
40 #include <avr/io.h>
41 #include <util/delay.h>
42 
43 #include <ws2812_cpp.h>
44 
45 // Parameters - ALTER THESE TO CORRESPOND WITH YOUR OWN SETUP!
46 #define N_LEDS 8
47 #define DATA_PINS_PORT PORTB
48 #define DATA_PINS_DDR DDRB
49 #define DATA_PINS {PB0, PB1}
50 #define RESET_TIME 50
51 #define COLOR_ORDER grb
52 
56 int main()
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
C++ Wrapper for the Tiny-WS2812 interface.