diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f47cd6..0e711ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,10 @@ add_executable(VL53L8X_Gradin VL53L8CX_ULD_API/src/vl53l8cx_plugin_xtalk.c VL53L8_2024.c Platform/platform.c + ws2812.c ) +# generate the header file into the source tree as it is included in the RP2040 datasheet +pico_generate_pio_header(VL53L8X_Gradin ${CMAKE_CURRENT_LIST_DIR}/ws2812.pio) target_include_directories(VL53L8X_Gradin PRIVATE VL53L8CX_ULD_API/inc/) @@ -31,6 +34,7 @@ target_link_libraries(VL53L8X_Gradin hardware_adc hardware_spi hardware_i2c + hardware_pio pico_stdlib pico_multicore ) diff --git a/main.c b/main.c index 4255bf9..a53c923 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,7 @@ #include "Temps.h" #include "VL53L8_2024.h" #include "vl53l8cx_api.h" +#include "ws2812.h" #include #include @@ -73,6 +74,8 @@ void main(void) //tension_batterie_init(); spi_init(spi0, 2000000); + ws2812_init(); + uint32_t temps_ms = Temps_get_temps_ms(); @@ -242,6 +245,12 @@ void gestion_VL53L8CX(void){ echec |= VL53L8_pos_planche_gauche(results_gauche, &gauche_planche_pos_x, &gauche_planche_pos_y); echec |= VL53L8_pos_planche_droit(results_droit, &droit_planche_pos_x, &droit_planche_pos_y); + if(echec){ + ws2812_set(0x0F,0,0); + }else{ + ws2812_set(0,0x0F,0); + } + droit_planche_pos_x = -droit_planche_pos_x + OFFSET_CAPTEUR_DROIT_Y_MM; gauche_planche_pos_x = -gauche_planche_pos_x + OFFSET_CAPTEUR_GAUCHE_X_MM; diff --git a/ws2812.c b/ws2812.c new file mode 100644 index 0000000..cd945cd --- /dev/null +++ b/ws2812.c @@ -0,0 +1,137 @@ +#include "pico/stdlib.h" +#include "hardware/pio.h" +#include "ws2812.h" +#include "ws2812.pio.h" + +#define WS2812_PIN 12 +#define WS2812_POWER_PIN 11 +#define IS_RGBW false + + +uint32_t couleur[13]; +uint32_t buffer_couleur[12]; + +void ws2812_set_buffer(uint32_t couleur, uint8_t index_led); + +void ws2812_init(){ + /*couleur[0]=0x000200; + couleur[1]=0x010200; + couleur[2]=0x020200; + couleur[3]=0x020100; + couleur[4]=0x020000; + couleur[5]=0x020001; + couleur[6]=0x020002; + couleur[7]=0x010002; + couleur[8]=0x000002; + couleur[9]=0x000102; + couleur[10]=0x000202; + couleur[11]=0x000201; + couleur[12]=0x000200;*/ + + couleur[0]=0x002000; + couleur[1]=0x102000; + couleur[2]=0x202000; + couleur[3]=0x201000; + couleur[4]=0x200000; + couleur[5]=0x200010; + couleur[6]=0x200020; + couleur[7]=0x100020; + couleur[8]=0x000020; + couleur[9]=0x001020; + couleur[10]=0x002020; + couleur[11]=0x002010; + couleur[12]=0x002000; + + /*couleur[0]=0x00FF00; + couleur[1]=0x80FF00; + couleur[2]=0xFFFF00; + couleur[3]=0xFF8000; + couleur[4]=0xFF0000; + couleur[5]=0xFF0080; + couleur[6]=0xFF00FF; + couleur[7]=0x8000FF; + couleur[8]=0x0000FF; + couleur[9]=0x0080FF; + couleur[10]=0x00FFFF; + couleur[11]=0x00FF80; + couleur[12]=0x00FF00;*/ + + // initialisation du PIO + PIO pio = pio0; + int sm = 0; + uint offset = pio_add_program(pio, &ws2812_program); + + + gpio_init(WS2812_POWER_PIN); + gpio_set_dir(WS2812_POWER_PIN, 1); // OUT + gpio_put(WS2812_POWER_PIN, 1); + + + ws2812_program_init(pio, sm, offset, WS2812_PIN, 800000, IS_RGBW); + + // Tout rouge ! + for(uint32_t i = 0; i<12; i++){ + ws2812_set_buffer_rgb(0x4, 0x1, 0, i); + } + ws2812_affiche_buffer(); +} + +void ws2812_set(int rouge, int vert, int bleu){ + ws2812_set_buffer_rgb(rouge, vert, bleu, 0); + ws2812_affiche_buffer(); +} + +void ws2812_arc_en_ciel(){ + while(1){ + uint32_t i; + sleep_ms(50); + + // Affichage + for(i = 0; i<12; i++){ + ws2812_set_buffer(couleur[i], i); + } + ws2812_affiche_buffer(); + + // Décalage des couleurs + for(i = 0; i<12; i++){ + couleur[i] = couleur[i+1]; + } + couleur[12]=couleur[0]; + } +} + +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); +} + +void ws2812_affiche_buffer(){ + for(uint32_t i = 0; i<12; i++){ + put_pixel(buffer_couleur[i]); + } +} + +void ws2812_set_buffer_rgb(uint8_t rouge, uint8_t vert, uint8_t bleu, uint8_t index_led){ + ws2812_set_buffer(urgb_u32(rouge, vert,bleu), index_led); +} + +//Bit 7 6 5 4 3 2 1 0 +//Data R R R G G G B B +void ws2812_set_buffer_8bits(uint8_t couleur, uint8_t index_led){ + ws2812_set_buffer(urgb_u32(couleur >> 5, (couleur >> 2) & 0x07, couleur & 0x03), index_led); +} + +/// @brief Rempli le buffer à envoyer au LED, necessite d'appeler la fonction ws2812_affiche_buffer() ensuite +/// @param couleur : couleur en RVB +/// @param index_led : index entre 0 et 11 +void ws2812_set_buffer(uint32_t couleur, uint8_t index_led){ + if(index_led <12){ + buffer_couleur[index_led] = couleur; + } +} \ No newline at end of file diff --git a/ws2812.h b/ws2812.h new file mode 100644 index 0000000..56c0d68 --- /dev/null +++ b/ws2812.h @@ -0,0 +1,8 @@ +#include "pico/stdlib.h" + +void ws2812_init(void); +void ws2812_set(int rouge, int vert, int bleu); +void ws2812_affiche_buffer(void); +void ws2812_set_buffer_rgb(uint8_t rouge, uint8_t vert, uint8_t bleu, uint8_t index_led); +void ws2812_set_buffer_8bits(uint8_t couleur, uint8_t index_led); +void ws2812_arc_en_ciel(void); \ No newline at end of file diff --git a/ws2812.pio b/ws2812.pio new file mode 100644 index 0000000..3c31fd6 --- /dev/null +++ b/ws2812.pio @@ -0,0 +1,85 @@ +; +; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. +; +; SPDX-License-Identifier: BSD-3-Clause +; + +.program ws2812 +.side_set 1 + +.define public T1 2 +.define public T2 5 +.define public T3 3 + +.lang_opt python sideset_init = pico.PIO.OUT_HIGH +.lang_opt python out_init = pico.PIO.OUT_HIGH +.lang_opt python out_shiftdir = 1 + +.wrap_target +bitloop: + out x, 1 side 0 [T3 - 1] ; Side-set still takes place when instruction stalls + jmp !x do_zero side 1 [T1 - 1] ; Branch on the bit we shifted out. Positive pulse +do_one: + jmp bitloop side 1 [T2 - 1] ; Continue driving high, for a long pulse +do_zero: + nop side 0 [T2 - 1] ; Or drive low, for a short pulse +.wrap + +% c-sdk { +#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); +} +%} + +.program ws2812_parallel + +.define public T1 2 +.define public T2 5 +.define public T3 3 + +.wrap_target + out x, 32 + mov pins, !null [T1-1] + mov pins, x [T2-1] + mov pins, null [T3-2] +.wrap + +% c-sdk { +#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