-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
94 lines (78 loc) · 3.28 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Copyright (C) 2021 Patrick Pedersen
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/**
* @file blink_loop.c
* @author Patrick Pedersen
* @date 2021-04-09
*
* @brief Blinks one or more WS2812 devices using a more memory efficient method than the blink_array.c example.
*
* 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.
*/
// #define WS2812_TARGET_PLATFORM_AVR
#include <avr/io.h>
#include <util/delay.h>
#include <ws2812.h>
// Parameters - ALTER THESE TO CORRESPOND WITH YOUR OWN SETUP!
#define N_LEDS 17 ///< 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} ///< Pin(s) used to communicate with the WS2812 device(s)
#define RESET_TIME 70 ///< Reset time in microseconds (50us recommended by datasheet)
#define COLOR_ORDER grb ///< Color order of your WS2812 LEDs (Typically grb or rgb)
// Colors
ws2812_rgb green = {0,255,0};
/**
* Blinks one or more WS2812 device(s)
*/
int main()
{
uint8_t pins[] = DATA_PINS; // Data pins
ws2812_cfg cfg; // Device configurationn
ws2812 ws2812_dev; // Device struct
// Configure the WS2812 device struct
cfg.port = &DATA_PINS_PORT;
cfg.ddr = &DATA_PINS_DDR;
cfg.pins = pins;
cfg.n_dev = sizeof(pins); // Number of devices driven by this struct
cfg.rst_time_us = RESET_TIME;
cfg.order = grb;
if (ws2812_config(&ws2812_dev, &cfg) != 0) {
// HANDLE ERROR...
void;
};
// Prepare to transmit data
ws2812_prep_tx(&ws2812_dev);
// Program all LEDs to white
for (unsigned int i = 0; i < N_LEDS; i++) {
ws2812_tx(&ws2812_dev, &green, sizeof(green)/sizeof(ws2812_rgb));
// THIS LOOP NEEDS TO RUN UNINTERRUPTED!
}
// Close transmission
ws2812_close_tx(&ws2812_dev);
return 0;
}