summaryrefslogtreecommitdiff
path: root/ws2812/main.c
blob: 323d03cac5943c10f78b26c75866bf582504aa87 (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


#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>

#include "ws2812.h"


#define DELAY 90000  // 18,000,000 nops are one second @ 72 MHz (one nop --> 4 clock cycles)
#define BRIGHTNESS 30
#define LED_ARRAY_LENGTH 3


void clock_init(void);
void gpio_init(void);
void delay(void);


int main(void)
{
	clock_init();
	gpio_init();
	uint8_t led_array_buffer[LED_ARRAY_LENGTH*WS2812_NUMBER_OF_COLORS];
	WS2812_ARRAY led_panel;
	ws2812_init(&led_panel, GPIOB, GPIO13, (uint8_t *)led_array_buffer, LED_ARRAY_LENGTH);

	while(1)
	{
		for (uint16_t i=0; i<=BRIGHTNESS; i++)
		{
			ws2812_set_array_led(&led_panel, 0, i, 0, 0);
			ws2812_set_array_led(&led_panel, 1, 0, i, 0);
			ws2812_set_array_led(&led_panel, 2, 0, 0, i);

			ws2812_write_leds(&led_panel);
			delay();
		}
		for (uint16_t i=BRIGHTNESS; i>0; i--)
		{
			ws2812_set_array_led(&led_panel, 0, i, 0, 0);
			ws2812_set_array_led(&led_panel, 1, 0, i, 0);
			ws2812_set_array_led(&led_panel, 2, 0, 0, i);

			ws2812_write_leds(&led_panel);
			delay();
		}
	}

	return 0;
}


void clock_init(void)
{
	rcc_clock_setup_in_hse_12mhz_out_72mhz();
	rcc_periph_clock_enable(RCC_GPIOB);  // for ws2812
	rcc_periph_clock_enable(RCC_GPIOC);  // for PC13 blinking
}


void gpio_init(void)
{
	// on board led blinking
	gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
	gpio_set(GPIOC, GPIO13);

	// ws2812 output pin
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
	gpio_set(GPIOB, GPIO13);
}


void delay(void)
{
	for (int i = 0; i < DELAY; i++) {
		__asm__("nop");
	}
}