summaryrefslogtreecommitdiff
path: root/libraries/ws2812.c
diff options
context:
space:
mode:
authorxengineering <mail2xengineering@protonmail.com>2021-01-26 16:05:27 +0100
committerxengineering <mail2xengineering@protonmail.com>2021-01-26 16:05:27 +0100
commit60849a62e04769aff854781e6cafa67b7025ed73 (patch)
treeabe07c2472e3b43795fb7dec611f36d5df23f95f /libraries/ws2812.c
parent17857f69f675141a814c80975545afb7c13dc675 (diff)
downloadstm32f103c8-examples-60849a62e04769aff854781e6cafa67b7025ed73.tar
stm32f103c8-examples-60849a62e04769aff854781e6cafa67b7025ed73.tar.zst
stm32f103c8-examples-60849a62e04769aff854781e6cafa67b7025ed73.zip
Update ws2812 Lib and ExampleHEADmain
Diffstat (limited to 'libraries/ws2812.c')
-rw-r--r--libraries/ws2812.c88
1 files changed, 31 insertions, 57 deletions
diff --git a/libraries/ws2812.c b/libraries/ws2812.c
index aa64031..5fbdf6c 100644
--- a/libraries/ws2812.c
+++ b/libraries/ws2812.c
@@ -1,16 +1,8 @@
+// vim: tabstop=4 shiftwidth=4 noexpandtab
/*
-
-# WS2812 Library for libopencm3
-
-Please mind these restrictions:
-
-- Only usable with libopencm3
-- Newer versions of libopencm3 may be not compatibel (libopencm3 api is not stable yet)
-- Just usable with a clock frequency of 72 MHz
-- You have to setup the clock and gpio yourself
-
+ Documentation is in ws2812.h
*/
@@ -21,6 +13,8 @@ Please mind these restrictions:
#define NUMBER_NOPS_SHORT 6 // 0.35 µs = 25.2 cycles @ 72 MHz = 6.3 nops --> 16.7 ns timing error
+// public API functions:
+
void ws2812_init(WS2812_ARRAY *control_struct, uint32_t port, uint16_t pin, uint8_t *buffer_ptr, uint32_t array_length)
{
control_struct->gpio_port = port;
@@ -29,47 +23,65 @@ void ws2812_init(WS2812_ARRAY *control_struct, uint32_t port, uint16_t pin, uint
control_struct->array_length = array_length;
ws2812_clear_buffer(control_struct);
ws2812_send_reset(control_struct);
+ ws2812_write_buffer_to_leds(control_struct);
}
+void ws2812_clear_buffer(WS2812_ARRAY *control_struct)
+{
+ for (uint32_t i=0; i<(control_struct->array_length*3); i++){
+ control_struct->array_buffer[i] = 0x00;
+ }
+}
+
+void ws2812_write_buffer_to_leds(WS2812_ARRAY *control_struct)
+{
+ for (uint32_t i=0; i<(control_struct->array_length); i++)
+ {
+ ws2812_send_byte(control_struct, control_struct->array_buffer[i*3+1]); // green first
+ ws2812_send_byte(control_struct, control_struct->array_buffer[i*3+0]); // red second
+ ws2812_send_byte(control_struct, control_struct->array_buffer[i*3+2]); // blue third
+ }
+ ws2812_send_reset(control_struct);
+}
+
+
+// internal functions:
void ws2812_send_reset(WS2812_ARRAY *control_struct)
{
gpio_clear(control_struct->gpio_port, control_struct->gpio_pin);
- for (int i = 0; i < NUMBER_NOPS_RESET; i++) {
+ for (uint16_t i = 0; i < NUMBER_NOPS_RESET; i++) {
__asm__("nop");
}
}
-
void ws2812_send_zero(WS2812_ARRAY *control_struct)
{
gpio_set(control_struct->gpio_port, control_struct->gpio_pin);
- for (int i = 0; i < NUMBER_NOPS_SHORT; i++) {
+ for (uint8_t i = 0; i < NUMBER_NOPS_SHORT; i++) {
__asm__("nop");
}
gpio_clear(control_struct->gpio_port, control_struct->gpio_pin);
- for (int i = 0; i < NUMBER_NOPS_LONG; i++) {
+ for (uint8_t i = 0; i < NUMBER_NOPS_LONG; i++) {
__asm__("nop");
}
}
-
void ws2812_send_one(WS2812_ARRAY *control_struct)
{
gpio_set(control_struct->gpio_port, control_struct->gpio_pin);
- for (int i = 0; i < NUMBER_NOPS_LONG; i++) {
+ for (uint8_t i = 0; i < NUMBER_NOPS_LONG; i++) {
__asm__("nop");
}
gpio_clear(control_struct->gpio_port, control_struct->gpio_pin);
- for (int i = 0; i < NUMBER_NOPS_SHORT; i++) {
+ for (uint8_t i = 0; i < NUMBER_NOPS_SHORT; i++) {
__asm__("nop");
}
}
-
void ws2812_send_byte(WS2812_ARRAY *control_struct, uint8_t data)
{
- for (int i=7; i>=0; i--){
+ for (int8_t i=7; i>=0; i--){
if ( (data >> i) & 0x01 ){
ws2812_send_one(control_struct);
}
@@ -78,41 +90,3 @@ void ws2812_send_byte(WS2812_ARRAY *control_struct, uint8_t data)
}
}
}
-
-
-void ws2812_send_led(WS2812_ARRAY *control_struct, uint8_t red, uint8_t green, uint8_t blue)
-{
- ws2812_send_byte(control_struct, green);
- ws2812_send_byte(control_struct, red);
- ws2812_send_byte(control_struct, blue);
-}
-
-
-void ws2812_write_leds(WS2812_ARRAY *control_struct)
-{
- for(uint32_t i=0; i<(control_struct->array_length); i++){
- ws2812_send_led(
- control_struct,
- control_struct->array_buffer[i * WS2812_NUMBER_OF_COLORS + 0], // red
- control_struct->array_buffer[i * WS2812_NUMBER_OF_COLORS + 1], // green
- control_struct->array_buffer[i * WS2812_NUMBER_OF_COLORS + 2] // blue
- );
- }
- ws2812_send_reset(control_struct);
-}
-
-
-void ws2812_set_array_led(WS2812_ARRAY *control_struct, uint32_t index, uint8_t red, uint8_t green, uint8_t blue)
-{
- control_struct->array_buffer[index * WS2812_NUMBER_OF_COLORS + 0] = red;
- control_struct->array_buffer[index * WS2812_NUMBER_OF_COLORS + 1] = green;
- control_struct->array_buffer[index * WS2812_NUMBER_OF_COLORS + 2] = blue;
-}
-
-
-void ws2812_clear_buffer(WS2812_ARRAY *control_struct)
-{
- for (uint32_t i=0; i<control_struct->array_length; i++){
- ws2812_set_array_led(control_struct, i, 0, 0, 0);
- }
-}