Skip to content

Commit

Permalink
Added neopixel code logic. does not work :(
Browse files Browse the repository at this point in the history
  • Loading branch information
rosie-m-banks committed Jan 11, 2025
1 parent 4ebd53e commit 924d330
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 43 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ file(GLOB top CONFIGURE_DEPENDS "src/*.c")
file(GLOB scheduler CONFIGURE_DEPENDS "src/scheduler/*.c")
file(GLOB state_machine_tasks CONFIGURE_DEPENDS "src/state_machine/tasks/*.c")
file(GLOB state_machine_states CONFIGURE_DEPENDS "src/state_machine/states/*.c")
file(GLOB drivers CONFIGURE_DEPENDS "src/drivers/*.c")
add_executable(samwise ${top} ${scheduler} ${state_machine_tasks}
${state_machine_states})
${state_machine_states} ${drivers})

pico_set_program_name(samwise "samwise")
pico_set_program_version(samwise "0.1")
Expand All @@ -51,7 +52,7 @@ pico_enable_stdio_usb(samwise 1)

# Add the standard library to the build
target_link_libraries(samwise
pico_stdlib)
pico_stdlib hardware_pio)

# Add the standard include files to the build
target_include_directories(samwise PRIVATE
Expand Down
53 changes: 13 additions & 40 deletions src/drivers/neopixel.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/clocks.h"
#include "ws2812.pio.h"
#include "drivers/pio/ws2812.pio.h"
#include "drivers/neopixel.h"

const int PIN_TX = 0;

static inline void put_pixel(uint32_t pixel_grb) {
pio_sm_put_blocking(pio0, 0, pixel_grb << 8u);
static inline void put_pixel(uint32_t pixel_grb, int pin) {
pio_sm_put_blocking(pio0, pin, pixel_grb << 8u);
}

static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) {
Expand All @@ -18,41 +17,15 @@ static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) {
(uint32_t)(b);
}

int main() {
stdio_init_all();

PIO pio = pio0;
int sm = 0;
uint offset = pio_add_program(pio, &ws2812_program);
char str[12];

ws2812_program_init(pio, sm, offset, PIN_TX, 800000, true);
void neopixel_init(int ledPin){
PIO pio = pio0;
int sm = 0;
uint offset = pio_add_program(pio, &ws2812_program);
char str[12];

while (true) {
puts("Hello RGB and other colors!");

// OPTION A: uncomment sleep_ms(500) and view the individual colors
// OPTION B: comment sleep_ms(500) and view the pixels cascading colors
put_pixel(urgb_u32(0xff, 0, 0)); // Red
sleep_ms(500);
put_pixel(urgb_u32(0, 0xff, 0)); // Green
sleep_ms(500);
put_pixel(urgb_u32(0, 0, 0xff)); // Blue
sleep_ms(500);
put_pixel(urgb_u32(0xff, 0xff, 0)); // Purple
sleep_ms(500);
put_pixel(urgb_u32(0, 0xff, 0xff)); // Cyan
sleep_ms(500);
put_pixel(urgb_u32(0xff, 0xff, 0xff)); // White
sleep_ms(500);
put_pixel(urgb_u32(0, 0, 0)); // Black or off
sleep_ms(500);
ws2812_program_init(pio, sm, offset, ledPin, 800000, true);
}

sleep_ms(1000);
// Clear all pixels
for (int i = 0; i <= 60; i++) {
put_pixel(urgb_u32(0, 0, 0)); // Black or off
}
sleep_ms(1000);
}
void neopixel_set_color_rgb(uint8_t r, uint8_t g, uint8_t b, int ledPin){
put_pixel(urgb_u32(r,g,b), ledPin);
}
8 changes: 8 additions & 0 deletions src/drivers/neopixel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Header file created 01/11/2025
Sets neopixel states on Picube
*/
#pragma once

void neopixel_init(int ledPin);
void neopixel_set_color_rgb(uint8_t r, uint8_t g, uint8_t b, int ledPin);
123 changes: 123 additions & 0 deletions src/drivers/pio/ws2812.pio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// -------------------------------------------------- //

Check notice on line 1 in src/drivers/pio/ws2812.pio.h

View workflow job for this annotation

GitHub Actions / Lint-Check

Run clang-format on src/drivers/pio/ws2812.pio.h

File src/drivers/pio/ws2812.pio.h does not conform to Custom style guidelines. (lines 23, 25, 26, 27, 28, 43, 51, 79, 81, 82, 83, 84, 99, 101, 106, 107, 115, 122)
// This file is autogenerated by pioasm; do not edit! //
// -------------------------------------------------- //

#pragma once

#if !PICO_NO_HARDWARE
#include "hardware/pio.h"
#endif

// ------ //
// ws2812 //
// ------ //

#define ws2812_wrap_target 0
#define ws2812_wrap 3
#define ws2812_pio_version 0

#define ws2812_T1 3
#define ws2812_T2 3
#define ws2812_T3 4

static const uint16_t ws2812_program_instructions[] = {
// .wrap_target
0x6321, // 0: out x, 1 side 0 [3]
0x1223, // 1: jmp !x, 3 side 1 [2]
0x1200, // 2: jmp 0 side 1 [2]
0xa242, // 3: nop side 0 [2]
// .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program ws2812_program = {
.instructions = ws2812_program_instructions,
.length = 4,
.origin = -1,
.pio_version = ws2812_pio_version,
#if PICO_PIO_VERSION > 0
.used_gpio_ranges = 0x0
#endif
};

static inline pio_sm_config ws2812_program_get_default_config(uint offset) {
pio_sm_config c = pio_get_default_sm_config();
sm_config_set_wrap(&c, offset + ws2812_wrap_target, offset + ws2812_wrap);
sm_config_set_sideset(&c, 1, false, false);
return c;
}

#include "hardware/clocks.h"
static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin, float freq, bool rgbw) {
pio_gpio_init(pio, pin);
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
pio_sm_config c = ws2812_program_get_default_config(offset);
sm_config_set_sideset_pins(&c, pin);
sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24);
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3;
float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
sm_config_set_clkdiv(&c, div);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}

#endif

// --------------- //
// ws2812_parallel //
// --------------- //

#define ws2812_parallel_wrap_target 0
#define ws2812_parallel_wrap 3
#define ws2812_parallel_pio_version 0

#define ws2812_parallel_T1 3
#define ws2812_parallel_T2 3
#define ws2812_parallel_T3 4

static const uint16_t ws2812_parallel_program_instructions[] = {
// .wrap_target
0x6020, // 0: out x, 32
0xa20b, // 1: mov pins, !null [2]
0xa201, // 2: mov pins, x [2]
0xa203, // 3: mov pins, null [2]
// .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program ws2812_parallel_program = {
.instructions = ws2812_parallel_program_instructions,
.length = 4,
.origin = -1,
.pio_version = ws2812_parallel_pio_version,
#if PICO_PIO_VERSION > 0
.used_gpio_ranges = 0x0
#endif
};

static inline pio_sm_config ws2812_parallel_program_get_default_config(uint offset) {
pio_sm_config c = pio_get_default_sm_config();
sm_config_set_wrap(&c, offset + ws2812_parallel_wrap_target, offset + ws2812_parallel_wrap);
return c;
}

#include "hardware/clocks.h"
static inline void ws2812_parallel_program_init(PIO pio, uint sm, uint offset, uint pin_base, uint pin_count, float freq) {
for(uint i=pin_base; i<pin_base+pin_count; i++) {
pio_gpio_init(pio, i);
}
pio_sm_set_consecutive_pindirs(pio, sm, pin_base, pin_count, true);
pio_sm_config c = ws2812_parallel_program_get_default_config(offset);
sm_config_set_out_shift(&c, true, true, 32);
sm_config_set_out_pins(&c, pin_base, pin_count);
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
int cycles_per_bit = ws2812_parallel_T1 + ws2812_parallel_T2 + ws2812_parallel_T3;
float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
sm_config_set_clkdiv(&c, div);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}

#endif

11 changes: 10 additions & 1 deletion src/state_machine/tasks/blink_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@
#include "macros.h"
#include "pico/stdlib.h"
#include "slate.h"
#include "drivers/neopixel.h"

const int NEOPIXEL_PIN = 0;

void blink_task_init(slate_t *slate)
{
slate->led_state = false;
neopixel_init(NEOPIXEL_PIN);
}

void blink_task_dispatch(slate_t *slate)
{
slate->led_state = !slate->led_state;
gpio_put(PICO_DEFAULT_LED_PIN, slate->led_state);
if (slate->led_state){
neopixel_set_color_rgb(0xff, 0xff, 0, NEOPIXEL_PIN); //yellow
}
else{
neopixel_set_color_rgb(0xff, 0, 0xff, NEOPIXEL_PIN); //purple
}
}

sched_task_t blink_task = {.name = "blink",
Expand Down

0 comments on commit 924d330

Please sign in to comment.