From 5ca7966b4a1644aa23cc1960f689a2b0ba9e3823 Mon Sep 17 00:00:00 2001 From: ebp1g21 <ebp1g21@soton.ac.uk> Date: Sun, 14 May 2023 12:48:08 +0000 Subject: [PATCH] Upload New File --- solution.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 solution.c diff --git a/solution.c b/solution.c new file mode 100644 index 0000000..0ba830c --- /dev/null +++ b/solution.c @@ -0,0 +1,60 @@ +#include "pico/stdlib.h" +#include "hardware/pio.h" +#include "ws2812.pio.h" + +#define IS_RGBW true +#define WS2812_PIN 28 + +#define BUTTON_1_PIN 20 +#define BUTTON_2_PIN 21 +#define BUTTON_3_PIN 22 + +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); +} + +int main() { + stdio_init_all(); + + // Setting up the ws2812 + PIO pio = pio0; + int sm = 0; + uint offset = pio_add_program(pio, &ws2812_program); + + ws2812_program_init(pio, sm, offset, WS2812_PIN, 800000, IS_RGBW); + + // Initializing buttons + gpio_init(BUTTON_1_PIN); + gpio_set_dir(BUTTON_1_PIN, GPIO_IN); + gpio_pull_up(BUTTON_1_PIN); + + gpio_init(BUTTON_2_PIN); + gpio_set_dir(BUTTON_2_PIN, GPIO_IN); + gpio_pull_up(BUTTON_2_PIN); + + gpio_init(BUTTON_3_PIN); + gpio_set_dir(BUTTON_3_PIN, GPIO_IN); + gpio_pull_up(BUTTON_3_PIN); + + // Main loop + while (true) { + if (!gpio_get(BUTTON_1_PIN)) { + put_pixel(urgb_u32(0xff, 0, 0)); + } + if (!gpio_get(BUTTON_2_PIN)) { + put_pixel(urgb_u32(0, 0xff, 0)); + } + if (!gpio_get(BUTTON_3_PIN)) { + put_pixel(urgb_u32(0, 0, 0xff)); + } + } + + return 0; // This will never be reached +} \ No newline at end of file -- GitLab