a vim plugin that displays stuff on an led matrix
1#include <stdlib.h> 2 3#include <cstring> 4 5#include "pico/multicore.h" 6#include "pico/stdlib.h" 7 8// Define GPIO pins for rows and columns 9const uint ROW_PINS[4] = {12, 13, 14, 15}; 10const uint COL_PINS[6] = {17, 18, 19, 20, 21, 22}; 11 12#define ROWS 4 13#define COLS 6 14 15// LED state matrix 16uint8_t led_states[ROWS][COLS] = {0}; 17 18void setup_pins() { 19 // Set up row pins 20 for (int i = 0; i < ROWS; i++) { 21 gpio_init(ROW_PINS[i]); 22 gpio_set_dir(ROW_PINS[i], GPIO_OUT); 23 gpio_put(ROW_PINS[i], 0); // Start with LEDs off 24 } 25 26 // Set up column pins 27 for (int j = 0; j < COLS; j++) { 28 gpio_init(COL_PINS[j]); 29 gpio_set_dir(COL_PINS[j], GPIO_OUT); 30 gpio_put(COL_PINS[j], 1); // Start with LEDs off 31 } 32} 33 34// Function to update the display at variable frequencies 35void update_led_display(uint32_t refresh_rate_hz) { 36 static uint32_t last_update = 0; 37 uint32_t current_time = to_ms_since_boot(get_absolute_time()); 38 uint32_t update_interval = 1000 / refresh_rate_hz; 39 40 // Only update at the specified frequency 41 if (current_time - last_update < update_interval) { 42 return; 43 } 44 45 last_update = current_time; 46 47 // Display the current state 48 for (int row = 0; row < ROWS; row++) { 49 // Set row active 50 gpio_put(ROW_PINS[row], 1); 51 52 // Set column states for this row 53 for (int col = 0; col < COLS; col++) { 54 // LED on = column LOW, LED off = column HIGH 55 gpio_put(COL_PINS[col], led_states[row][col] ? 0 : 1); 56 } 57 58 // Small delay for this row to be visible 59 sleep_us(100); 60 61 // Deactivate row 62 gpio_put(ROW_PINS[row], 0); 63 } 64} 65 66// Function to set a specific LED state 67void set_led(int row, int col, bool state) { 68 if (row >= 0 && row < ROWS && col >= 0 && col < COLS) { 69 led_states[row][col] = state ? 1 : 0; 70 } 71} 72 73// Function to set the entire matrix state 74void set_matrix_state(const uint8_t new_state[ROWS][COLS]) { 75 memcpy(led_states, new_state, sizeof(led_states)); 76} 77 78void random_state(float density) { 79 // clamp 80 if (density > 1.0f) 81 density = 1.0f; 82 if (density < 0.0f) 83 density = 0.0f; 84 85 for (int row = 0; row < ROWS; row++) { 86 for (int col = 0; col < COLS; col++) { 87 float r = (float)rand() / (float)RAND_MAX; 88 led_states[row][col] = (r < density) ? 1 : 0; 89 } 90 } 91} 92 93const uint8_t ALL_ON[ROWS][COLS] = {{1, 1, 1, 1, 1, 1}, 94 {1, 1, 1, 1, 1, 1}, 95 {1, 1, 1, 1, 1, 1}, 96 {1, 1, 1, 1, 1, 1}}; 97 98const uint8_t ALL_OFF[ROWS][COLS] = {{0, 0, 0, 0, 0, 0}, 99 {0, 0, 0, 0, 0, 0}, 100 {0, 0, 0, 0, 0, 0}, 101 {0, 0, 0, 0, 0, 0}}; 102 103void core1_main() { 104 while (true) { 105 update_led_display(1000); 106 } 107} 108 109int main() { 110 stdio_init_all(); 111 setup_pins(); 112 113 multicore_launch_core1(core1_main); 114 115 while (true) { 116 random_state(0.5); // Turn random LEDs ON 117 // set_matrix_state(ALL_ON); 118 sleep_ms(200); 119 // set_matrix_state(ALL_OFF); 120 // sleep_ms(500); 121 } 122}