Working WS2812 led.

This commit is contained in:
Martin Brodbeck 2023-01-02 10:35:58 +01:00
parent 3f0c09c0a9
commit d98c72cb9f
4 changed files with 59 additions and 3 deletions

View file

@ -1,6 +1,7 @@
#set(CMAKE_INCLUDE_CURRENT_DIR ON) #set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(SOURCES set(SOURCES
ws2812.cpp
abfall.cpp abfall.cpp
) )
@ -30,6 +31,8 @@ set_target_properties(${CMAKE_PROJECT_NAME}
CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}" CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
) )
pico_generate_pio_header(${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/ws2812.pio)
pico_enable_stdio_uart(${CMAKE_PROJECT_NAME} 1) pico_enable_stdio_uart(${CMAKE_PROJECT_NAME} 1)
pico_enable_stdio_usb(${CMAKE_PROJECT_NAME} 0) pico_enable_stdio_usb(${CMAKE_PROJECT_NAME} 0)

View file

@ -3,7 +3,9 @@
#include <string> #include <string>
#include "hardware/pio.h"
#include "hardware/rtc.h" #include "hardware/rtc.h"
#include "pico/cyw43_arch.h" #include "pico/cyw43_arch.h"
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "pico/util/datetime.h" #include "pico/util/datetime.h"
@ -11,8 +13,12 @@
#include "lwip/apps/http_client.h" #include "lwip/apps/http_client.h"
#include "lwip/apps/sntp.h" #include "lwip/apps/sntp.h"
#include "ws2812.h"
using std::string; using std::string;
#define WS2812_PIN 22
int wifi_setup(uint32_t country, const string &ssid, const string &pw, bool firstTry = true) { int wifi_setup(uint32_t country, const string &ssid, const string &pw, bool firstTry = true) {
if (firstTry) { if (firstTry) {
if (cyw43_arch_init_with_country(country)) { if (cyw43_arch_init_with_country(country)) {
@ -40,7 +46,7 @@ int wifi_setup(uint32_t country, const string &ssid, const string &pw, bool firs
continue; continue;
} }
flashrate = flashrate / (status + 1); flashrate = flashrate / (status + 1);
//printf("Connect status: %d %d\n", status, flashrate); // printf("Connect status: %d %d\n", status, flashrate);
} }
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
sleep_ms(flashrate); sleep_ms(flashrate);
@ -82,7 +88,7 @@ err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, u
} }
err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) { err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) {
//printf("Body\n"); // printf("Body\n");
pbuf_copy_partial(p, myBuffer, p->tot_len, 0); pbuf_copy_partial(p, myBuffer, p->tot_len, 0);
// printf("%s", myBuffer); // printf("%s", myBuffer);
@ -116,6 +122,8 @@ int main() {
stdio_init_all(); stdio_init_all();
rtc_init(); rtc_init();
WS2812 led(WS2812_PIN);
bool firstTry = true; bool firstTry = true;
int res = -1; int res = -1;
do { do {
@ -154,7 +162,8 @@ int main() {
while (true) { while (true) {
rtc_get_datetime(&dt); rtc_get_datetime(&dt);
datetime_to_str(datetime_str, sizeof(datetime_buf), &dt); datetime_to_str(datetime_str, sizeof(datetime_buf), &dt);
//printf("DateTime: %s\n", datetime_str); // printf("DateTime: %s\n", datetime_str);
led.switchColor(Color::RED);
sleep_ms(5000); sleep_ms(5000);
} }

13
src/ws2812.cpp Normal file
View file

@ -0,0 +1,13 @@
#include "ws2812.h"
WS2812::WS2812(uint gpio, PIO pio, uint sm) : m_pio{pio}, m_sm{sm} {
uint offset = pio_add_program(m_pio, &ws2812_program);
ws2812_program_init(m_pio, m_sm, offset, gpio, 800000, true);
switchColor(Color::OFF);
}
void WS2812::switchColor(Color color) {
putPixel(static_cast<uint32_t>(color));
}
void WS2812::putPixel(uint32_t pixel_grb) { pio_sm_put_blocking(m_pio, m_sm, pixel_grb << 8u); }

31
src/ws2812.h Normal file
View file

@ -0,0 +1,31 @@
#pragma once
#include "pico/stdlib.h"
#include "ws2812.pio.h"
enum class Color : uint32_t {
RED = 0xff0000,
GREEN = 0x00ff00,
BLUE = 0x0000ff,
WHITE = 0xffffff,
OFF = 0x000000,
YELLOW = 0xffff00
};
class WS2812 {
public:
WS2812(uint gpio, PIO pio = pio0, uint sm = 0);
void switchColor(Color color);
private:
void putPixel(uint32_t pixel_rgb);
PIO m_pio;
uint m_sm;
};
static inline void put_pixel(uint32_t pixel_grb) { pio_sm_put_blocking(pio0, 0, pixel_grb << 8u); }
// static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) {
// return ((uint32_t)(r) << 8) | ((uint32_t)(g) << 16) | (uint32_t)(b);
// }