Tiny-WS2812  1.0.0
A tiny cross-platform WS2812 LED Strip driver
blink_array.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 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 
40 #include <stm8s.h>
41 
42 #include <ws2812.h>
43 
44 #define N_LEDS 8
45 #define DATA_PORT_BASE GPIOD_BaseAddress
46 #define DATA_PINS {GPIO_PIN_4}
47 #define RESET_TIME 30
48 #define COLOR_ORDER grb
49 
50 #define WAIT_LOOPS 1000000
51 
52 uint8_t pins[] = DATA_PINS;
55 
56 void init_16mhz_clk()
57 {
58  CLK_DeInit();
59  CLK_HSICmd(ENABLE);
60  CLK_HSECmd(DISABLE);
61  CLK_LSICmd(DISABLE);
62  CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
63  CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);
64  CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
65 }
66 
67 void main()
68 {
69  // Initialize clock to run at 16Mhz
70  init_16mhz_clk();
71 
72  // Initialize WS2812 device struct
73 
74  ws2812_cfg cfg;
75 
76  cfg.pins = pins;
77  cfg.rst_time_us = RESET_TIME;
78  cfg.order = COLOR_ORDER;
79  cfg.n_dev = sizeof(pins);
81 
82  ws2812_config(&ws2812_dev, &cfg);
83 
84  // Blink strip
85 
86  while(1) {
87  // Fill strip array with red
88  for (unsigned int i = 0; i < N_LEDS; i++) {
89  leds[i].r = 255;
90  leds[i].g = 0;
91  leds[i].b = 0;
92  }
93 
94  // Write to strip
98 
99  // Wait for some time
100  for (unsigned long i = 0; i < WAIT_LOOPS; i++) {
101  __asm__("nop");
102  }
103 
104  // Fill strip array with black
105  for (unsigned int i = 0; i < N_LEDS; i++) {
106  leds[i].r = 0;
107  leds[i].g = 0;
108  leds[i].b = 0;
109  }
110 
111  // Wait for some time
112  for (unsigned long i = 0; i < WAIT_LOOPS; i++) {
113  __asm__("nop");
114  }
115  }
116 }
117 
118 // See: https://community.st.com/s/question/0D50X00009XkhigSAB/what-is-the-purpose-of-define-usefullassert
119 #ifdef USE_FULL_ASSERT
120 void assert_failed(uint8_t* file, uint32_t line)
121 {
122  while (TRUE)
123  {
124  }
125 }
126 #endif
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
uint16_t port_baseaddr
Base address of the port used to drive WS2812 devices (ex. GPIOA_BASE, GPIOB_BASE,...
Definition: ws2812_stm8s.h:69
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
ALL PLATFORMS: WS2812 device struct to drive one or more WS2812 devices.
Definition: ws2812_avr.h:107
Exposes the Tiny-WS2812 library interface.
void ws2812_close_tx(ws2812 *dev)
Closes a WS2812 transmission.
Definition: ws2812_avr.c:275
uint8_t ws2812_config(ws2812 *dev, ws2812_cfg *cfg)
Configures a WS2812 device struct.
Definition: ws2812_avr.c:126
void ws2812_prep_tx(ws2812 *dev)
Prepares the host device for data transmission.
Definition: ws2812_avr.c:159
void ws2812_tx(ws2812 *dev, ws2812_rgb *leds, size_t n_leds)
Transmits RGB values to the provided WS2812 device.
Definition: ws2812_avr.c:264