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 
39 // #define WS2812_TARGET_PLATFORM_ARDUINO_AVR
40 
41 #include <Arduino.h>
42 #include <ws2812_cpp.h>
43 
44 // Parameters - ALTER THESE TO CORRESPOND WITH YOUR OWN SETUP!
45 #define N_LEDS 8
46 #define DATA_PINS {8, 9}
47 #define RESET_TIME 50
48 #define COLOR_ORDER grb
49 
50 uint8_t pins[] = DATA_PINS;
52 ws2812_cpp *ws2812_dev;
53 
57 void setup()
58 {
59  ws2812_cfg cfg; // Device config
60 
61  // Configure the WS2812 device struct
62  cfg.pins = pins;
63  cfg.rst_time_us = RESET_TIME;
64  cfg.order = COLOR_ORDER;
65  cfg.n_dev = sizeof(pins); // Number of devices driven by this struct
66 
67  uint8_t ret;
68  ws2812_dev = new ws2812_cpp(&cfg, &ret); // Initialize the device object
69 
70  if (ret != 0) {
71  // HANDLE ERROR HERE
72  void;
73  }
74 }
75 
79 void loop()
80 {
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_dev->prep_tx(); // Prepare to transmit data
89  ws2812_dev->tx(leds, N_LEDS); // Transmit array of rgb values to the device
90  ws2812_dev->close_tx(); // Close transmission
91 
92  // Wait 500ms
93  delay(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_dev->prep_tx(); // Prepare to transmit data
103  ws2812_dev->tx(leds, N_LEDS); // Transmit array of rgb values to the device
104  ws2812_dev->close_tx(); // Close transmission
105 
106  // Wait 500ms
107  delay(500);
108 }
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
Data structure to hold RGB color values.
Definition: ws2812_common.h:67
C++ Wrapper for the Tiny-WS2812 interface.