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(SOURCES
ws2812.cpp
abfall.cpp
)
@ -30,6 +31,8 @@ set_target_properties(${CMAKE_PROJECT_NAME}
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_usb(${CMAKE_PROJECT_NAME} 0)

View file

@ -3,7 +3,9 @@
#include <string>
#include "hardware/pio.h"
#include "hardware/rtc.h"
#include "pico/cyw43_arch.h"
#include "pico/stdlib.h"
#include "pico/util/datetime.h"
@ -11,8 +13,12 @@
#include "lwip/apps/http_client.h"
#include "lwip/apps/sntp.h"
#include "ws2812.h"
using std::string;
#define WS2812_PIN 22
int wifi_setup(uint32_t country, const string &ssid, const string &pw, bool firstTry = true) {
if (firstTry) {
if (cyw43_arch_init_with_country(country)) {
@ -116,6 +122,8 @@ int main() {
stdio_init_all();
rtc_init();
WS2812 led(WS2812_PIN);
bool firstTry = true;
int res = -1;
do {
@ -155,6 +163,7 @@ int main() {
rtc_get_datetime(&dt);
datetime_to_str(datetime_str, sizeof(datetime_buf), &dt);
// printf("DateTime: %s\n", datetime_str);
led.switchColor(Color::RED);
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);
// }