Pico Led Controller 1.0.3
A project to control LEDs using Raspberry Pi Pico W
Loading...
Searching...
No Matches
led_controller.c File Reference
#include "led_controller.h"
#include <stdio.h>
#include <stdlib.h>
#include "urgb.h"
#include "hsv.h"
#include "ws2812b.h"
#include "light_state.h"
#include "wifi_credentials.h"
#include "generated/ws2812.pio.h"
#include "lwipopts.h"
#include "lwip/apps/httpd.h"
#include "ssi.h"
#include "cgi.h"
#include "ntp.h"

Go to the source code of this file.

Functions

void connect_to_wifi ()
 Connect to WiFi.
 
void gpio_button_irq_handler (uint gpio, uint32_t events)
 GPIO button interrupt handler.
 
enum init_result_t init ()
 Init.
 
void run_loop ()
 Run loop.
 

Variables

volatile bool light_state_toggle_request = false
 
volatile bool light_mode_toggle_request = false
 
volatile bool stop_flag = false
 
volatile uint64_t last_press_time = 0
 

Function Documentation

◆ connect_to_wifi()

void connect_to_wifi ( )

Connect to WiFi.

Function connects to WiFi using the SSID and password defined in the wifi_credentials.h file.

Definition at line 24 of file led_controller.c.

25{
27 printf("Connecting to Wi-Fi...\n");
28 while (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 10000) != 0)
29 printf("Attempting to connect...\n");
30 printf("Connected!\n");
31 extern cyw43_t cyw43_state;
32 uint32_t ip_addr = cyw43_state.netif[CYW43_ITF_STA].ip_addr.addr;
33 printf("IP Address: %lu.%lu.%lu.%lu\n", ip_addr & 0xFF, (ip_addr >> 8) & 0xFF, (ip_addr >> 16) & 0xFF, ip_addr >> 24);
35}

References BLINK_CODE_WIFI_CONNECTED, and BLINK_CODE_WIFI_CONNECTING.

Referenced by init(), and main().

◆ gpio_button_irq_handler()

void gpio_button_irq_handler ( uint  gpio,
uint32_t  events 
)

GPIO button interrupt handler.

Function handles button interrupts setting proper flags.

Definition at line 37 of file led_controller.c.

38{
39 uint64_t current_time = time_us_64();
40 if (current_time - last_press_time >= DEBOUNCE_TIME_US)
41 {
42 last_press_time = current_time;
43 switch (gpio)
44 {
47 break;
48 case MODE_BUTTON_PIN:
50 if (!light_state.state)
52 break;
53 case STOP_BUTTON_PIN:
54 stop_flag = true;
55 break;
56 default:
57 break;
58 }
59 }
60}
volatile bool light_state_toggle_request
volatile bool light_mode_toggle_request
volatile bool stop_flag
volatile uint64_t last_press_time
#define DEBOUNCE_TIME_US
#define MODE_BUTTON_PIN
#define LIGHT_TOGGLE_PIN
#define STOP_BUTTON_PIN
volatile struct light_state_t light_state
Light state.
Definition light_state.c:8

References DEBOUNCE_TIME_US, last_press_time, light_mode_toggle_request, light_state, light_state_toggle_request, LIGHT_TOGGLE_PIN, MODE_BUTTON_PIN, light_state_t::state, STOP_BUTTON_PIN, and stop_flag.

Referenced by init().

◆ init()

enum init_result_t init ( )

Init.

Function initializes the LED controller.

Returns
: initialization result

Definition at line 62 of file led_controller.c.

63{
64 if (!stdio_init_all())
65 {
66 printf("Stdio init failed");
67 return STDIO_INIT_FAILURE;
68 }
69
70 // GPIO setup - LED
71 gpio_init(CYW43_WL_GPIO_LED_PIN);
72 gpio_set_dir(CYW43_WL_GPIO_LED_PIN, GPIO_OUT);
73 gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
74
75 // GPIO setup - buttons
76 gpio_init(LIGHT_TOGGLE_PIN);
77 gpio_set_dir(LIGHT_TOGGLE_PIN, GPIO_IN);
78 gpio_pull_up(LIGHT_TOGGLE_PIN);
79
80 gpio_init(MODE_BUTTON_PIN);
81 gpio_set_dir(MODE_BUTTON_PIN, GPIO_IN);
82 gpio_pull_up(MODE_BUTTON_PIN);
83
84 gpio_init(STOP_BUTTON_PIN);
85 gpio_set_dir(STOP_BUTTON_PIN, GPIO_IN);
86 gpio_pull_up(STOP_BUTTON_PIN);
87
88 // GPIO setup - interrupts
89 gpio_set_irq_enabled_with_callback(LIGHT_TOGGLE_PIN, GPIO_IRQ_EDGE_FALL, true, &gpio_button_irq_handler);
90 gpio_set_irq_enabled_with_callback(MODE_BUTTON_PIN, GPIO_IRQ_EDGE_FALL, true, &gpio_button_irq_handler);
91 gpio_set_irq_enabled_with_callback(STOP_BUTTON_PIN, GPIO_IRQ_EDGE_FALL, true, &gpio_button_irq_handler);
92
93 // Wi-Fi setup
94 if (cyw43_arch_init())
95 {
96 printf("Wi-Fi init failed");
97 return WIFI_INIT_FAILURE;
98 }
99 cyw43_wifi_pm(&cyw43_state, cyw43_pm_value(CYW43_NO_POWERSAVE_MODE, 20, 1, 1, 1));
100 cyw43_arch_enable_sta_mode();
102 httpd_init();
103 ssi_init();
104 cgi_init();
107
108 // PIO setup - WS2812B
109 PIO pio = pio0;
110 int sm = 0;
111 uint offset = pio_add_program(pio, &ws2812_program);
112 ws2812_program_init(pio, sm, offset, WS2812_PIN, 800000, IS_RGBW);
113 printf("WS2812 LED Control, using pin %d\n", WS2812_PIN);
114
116 return INIT_SUCCESS;
117}
void cgi_init()
Initialize CGI handlers.
Definition cgi.c:289
void gpio_button_irq_handler(uint gpio, uint32_t events)
GPIO button interrupt handler.
void connect_to_wifi()
Connect to WiFi.
#define IS_RGBW
@ STDIO_INIT_FAILURE
@ WIFI_INIT_FAILURE
@ INIT_SUCCESS
#define WS2812_PIN
void ntp_update_time(void)
NTP update time.
Definition ntp.c:123
void ssi_init()
SSI init.
Definition ssi.c:138

References BLINK_CODE_INIT_SUCCESS, BLINK_CODE_NETWORK_INIT_SUCCESS, cgi_init(), connect_to_wifi(), gpio_button_irq_handler(), INIT_SUCCESS, IS_RGBW, LIGHT_TOGGLE_PIN, MODE_BUTTON_PIN, ntp_update_time(), ssi_init(), STDIO_INIT_FAILURE, STOP_BUTTON_PIN, WIFI_INIT_FAILURE, and WS2812_PIN.

Referenced by main().

◆ run_loop()

void run_loop ( )

Run loop.

Function runs the main loop of the LED controller.

Definition at line 119 of file led_controller.c.

120{
121 uint16_t base_hue = 0;
122 uint8_t speed_factor_wheel = 4;
123 uint8_t speed_factor_cycle = 1;
124 uint8_t speed_factor_breathing = 3;
125 uint8_t density_factor = 3;
126 uint8_t breathing_brightness = 15;
127 bool breathing_up = true;
128 while (true) // [ ] Remove bug - cannot change mode after multiple changes
129 { // [ ] Add restart button from web interface
131 {
134 }
136 {
139 }
140 if (stop_flag)
141 {
143 break;
144 }
145 switch (light_state.light_mode)
146 {
148 if (light_state.state)
149 apply_rainbow_wheel_effect(NUM_PIXELS, &base_hue, &speed_factor_wheel, &density_factor, &light_state.brightness);
150 break;
152 if (light_state.state)
153 apply_rainbow_cycle_effect(NUM_PIXELS, &base_hue, &speed_factor_cycle, &light_state.brightness);
154 break;
155 case MODE_BREATHING:
156 if (light_state.state)
157 {
158 uint32_t current_color = get_current_color();
159 apply_breathing_effect(NUM_PIXELS, &speed_factor_breathing, &current_color, &light_state.brightness, &breathing_brightness, &breathing_up);
160 }
161 break;
162 case MODE_FLASHING:
163 if (light_state.state)
164 {
165 uint32_t current_color = get_current_color();
166 apply_flashing_effect(NUM_PIXELS, &current_color);
167 }
168 break;
169 // [ ] Implement the rest of dynamic modes
170 default:
171 break;
172 }
173 sleep_ms(40);
174 }
175}
uint32_t get_current_color()
Get current color.
void toggle_light_state()
Toggle light state.
Definition light_state.c:10
#define NUM_PIXELS
Definition light_state.h:4
void toggle_light_mode()
Toggle light mode.
Definition light_state.c:19
@ MODE_RAINBOW_CYCLE
Definition light_state.h:20
@ MODE_FLASHING
Definition light_state.h:23
@ MODE_BREATHING
Definition light_state.h:22
@ MODE_RAINBOW_WHEEL
Definition light_state.h:19
uint8_t brightness
Definition light_state.h:59
enum light_modes light_mode
Definition light_state.h:61
void apply_flashing_effect(uint32_t len, uint32_t *color)
Apply flashing effect.
Definition ws2812b.c:214
void apply_breathing_effect(uint32_t len, uint8_t *speed_factor, uint32_t *color, volatile uint8_t *base_brightness, uint8_t *brightness, bool *breathing_up)
Apply breathing effect.
Definition ws2812b.c:184
void turn_off_all(uint32_t len)
Turn off all leds.
Definition ws2812b.c:23
void apply_rainbow_wheel_effect(uint32_t len, uint16_t *base_hue, uint8_t *speed_factor, uint8_t *density_factor, volatile uint8_t *brightness)
Apply rainbow wheel effect.
Definition ws2812b.c:160
void apply_rainbow_cycle_effect(uint32_t len, uint16_t *hue, uint8_t *speed_factor, volatile uint8_t *brightness)
Apply rainbow cycle effect.
Definition ws2812b.c:174

References apply_breathing_effect(), apply_flashing_effect(), apply_rainbow_cycle_effect(), apply_rainbow_wheel_effect(), light_state_t::brightness, get_current_color(), light_state_t::light_mode, light_mode_toggle_request, light_state, light_state_toggle_request, MODE_BREATHING, MODE_FLASHING, MODE_RAINBOW_CYCLE, MODE_RAINBOW_WHEEL, NUM_PIXELS, light_state_t::state, stop_flag, toggle_light_mode(), toggle_light_state(), and turn_off_all().

Referenced by main().

Variable Documentation

◆ last_press_time

volatile uint64_t last_press_time = 0

Definition at line 22 of file led_controller.c.

Referenced by gpio_button_irq_handler().

◆ light_mode_toggle_request

volatile bool light_mode_toggle_request = false

Definition at line 20 of file led_controller.c.

Referenced by gpio_button_irq_handler(), and run_loop().

◆ light_state_toggle_request

volatile bool light_state_toggle_request = false

Definition at line 19 of file led_controller.c.

Referenced by gpio_button_irq_handler(), and run_loop().

◆ stop_flag

volatile bool stop_flag = false

Definition at line 21 of file led_controller.c.

Referenced by gpio_button_irq_handler(), main(), and run_loop().