blob: 672153166217835b2178e1e29db353e433f2ce7c (
plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// vim: tabstop=4 shiftwidth=4 noexpandtab
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include "ws2812.h"
#define LED_ARRAY_LENGTH 64 // ws2812 array is 64 LEDs long (adapt it to your needs)
// pin B13 is connected to DIN of ws2812 array:
#define LED_ARRAY_PORT GPIOB
#define LED_ARRAY_PIN GPIO13
#define RCC_GPIO_PORT RCC_GPIOB
#define MAX_BRIGHTNESS 10 // maximum brightness per LED (value from 0 to 255)
void clock_init(void);
void gpio_init(void);
void delay_ms(uint16_t delay);
void animation_full_color(WS2812_ARRAY *leds);
int main(void)
{
WS2812_ARRAY leds; // ws2812 control struct
uint8_t led_buffer[LED_ARRAY_LENGTH*3]; // reserve a uint8_t for each color in each LED
clock_init();
gpio_init();
ws2812_init(&leds, LED_ARRAY_PORT, LED_ARRAY_PIN, (uint8_t *)led_buffer, LED_ARRAY_LENGTH);
while(1)
{
animation_full_color(&leds);
}
return 0;
}
void clock_init(void)
{
rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use external oscillator for 72 MHz sysclock frequency
rcc_periph_clock_enable(RCC_GPIO_PORT); // enable gpio port clock to use LED_ARRAY_PORT
}
void gpio_init(void)
{
gpio_set_mode(LED_ARRAY_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_ARRAY_PIN); // settings for LED_ARRAY_PIN
gpio_clear(LED_ARRAY_PORT, LED_ARRAY_PIN); // set LED_ARRAY_PIN to 0 V
}
void delay_ms(uint16_t delay)
{
uint32_t delay_nops = delay * 4500; // just @ 72 MHz!
for (uint32_t i = 0; i < delay_nops; i++) {
__asm__("nop");
}
}
void animation_full_color(WS2812_ARRAY *leds)
{
// set red
ws2812_clear_buffer(leds);
for (uint32_t i = 0; i < leds->array_length*3; i++)
{
if (i%3 == 0) leds->array_buffer[i] = MAX_BRIGHTNESS;
}
ws2812_write_buffer_to_leds(leds);
delay_ms(500);
// set green
ws2812_clear_buffer(leds);
for (uint32_t i = 0; i < leds->array_length*3; i++)
{
if (i%3 == 1) leds->array_buffer[i] = MAX_BRIGHTNESS;
}
ws2812_write_buffer_to_leds(leds);
delay_ms(500);
// set blue
ws2812_clear_buffer(leds);
for (uint32_t i = 0; i < leds->array_length*3; i++)
{
if (i%3 == 2) leds->array_buffer[i] = MAX_BRIGHTNESS;
}
ws2812_write_buffer_to_leds(leds);
delay_ms(500);
// set white
ws2812_clear_buffer(leds);
for (uint32_t i = 0; i < leds->array_length*3; i++)
{
leds->array_buffer[i] = MAX_BRIGHTNESS;
}
ws2812_write_buffer_to_leds(leds);
delay_ms(500);
// set off
ws2812_clear_buffer(leds);
ws2812_write_buffer_to_leds(leds);
delay_ms(500);
}
|