this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add better software pwm and serial in

+98 -63
+1
firmware/CMakeLists.txt
··· 28 28 pico_stdlib 29 29 hardware_gpio 30 30 hardware_uart 31 + hardware_pwm 31 32 pico_multicore 32 33 ) 33 34
+97 -63
firmware/src/main.cpp
··· 1 1 #include <stdlib.h> 2 2 3 3 #include <cstring> 4 + #include <stdio.h> 4 5 5 6 #include "pico/multicore.h" 6 7 #include "pico/stdlib.h" 8 + #include <time.h> 7 9 8 10 // Define GPIO pins for rows and columns 9 - const uint ROW_PINS[4] = {12, 13, 14, 15}; 10 - const uint COL_PINS[6] = {17, 18, 19, 20, 21, 22}; 11 - 12 11 #define ROWS 4 13 12 #define COLS 6 14 13 14 + const uint ROW_PINS[4] = {12, 13, 14, 15}; 15 + const uint COL_PINS[6] = {17, 18, 19, 20, 21, 22}; 16 + 15 17 // LED state matrix 16 18 uint8_t led_states[ROWS][COLS] = {0}; 17 19 18 - void 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 - } 20 + // brightness control 21 + uint8_t brightness = 100; 25 22 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 23 + // timing vars 24 + bool fading = false; 25 + absolute_time_t last_char_time; 26 + absolute_time_t fade_start_time; 27 + const uint32_t inactivity_timeout_ms = 0; 28 + const uint32_t fade_duration_ms = 1200; 29 + 30 + void randomize_matrix() { 31 + for (int row = 0; row < ROWS; row++) { 32 + for (int col = 0; col < COLS; col++) { 33 + led_states[row][col] = rand() % 2; 34 + } 31 35 } 32 36 } 33 37 ··· 44 48 45 49 last_update = current_time; 46 50 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 + uint32_t time_since_last_char = absolute_time_diff_us(last_char_time, get_absolute_time()) / 1000; 51 52 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 - } 53 + if (!fading && time_since_last_char > inactivity_timeout_ms) { 54 + // start fading 55 + fading = true; 56 + fade_start_time = get_absolute_time(); 57 + } 57 58 58 - // Small delay for this row to be visible 59 - sleep_us(100); 59 + if (fading) { 60 + uint32_t fade_elapsed_ms = absolute_time_diff_us(fade_start_time, get_absolute_time()) / 1000; 60 61 61 - // Deactivate row 62 - gpio_put(ROW_PINS[row], 0); 62 + if (fade_elapsed_ms >= fade_duration_ms) { 63 + brightness = 0; 64 + } else { 65 + brightness = 100 - ((fade_elapsed_ms * 100) / fade_duration_ms); 66 + } 63 67 } 64 - } 65 68 66 - // Function to set a specific LED state 67 - void 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; 69 + // Skip display update if brightness is 0 70 + if (brightness == 0) { 71 + // Turn off all rows and set all columns high to ensure LEDs are off 72 + for (int i = 0; i < ROWS; i++) { 73 + gpio_put(ROW_PINS[i], 0); 74 + } 75 + for (int j = 0; j < COLS; j++) { 76 + gpio_put(COL_PINS[j], 1); 77 + } 78 + 79 + return; 70 80 } 71 - } 72 81 73 - // Function to set the entire matrix state 74 - void set_matrix_state(const uint8_t new_state[ROWS][COLS]) { 75 - memcpy(led_states, new_state, sizeof(led_states)); 76 - } 82 + // Software PWM cycle 83 + for (uint8_t pwm_cycle = 0; pwm_cycle < brightness; pwm_cycle++) { 84 + // For each row 85 + for (int row = 0; row < ROWS; row++) { 86 + // Activate row 87 + gpio_put(ROW_PINS[row], 1); 77 88 78 - void random_state(float density) { 79 - // clamp 80 - if (density > 1.0f) 81 - density = 1.0f; 82 - if (density < 0.0f) 83 - density = 0.0f; 89 + // Set column states for this row 90 + for (int col = 0; col < COLS; col++) { 91 + gpio_put(COL_PINS[col], led_states[row][col] ? 0 : 1); 92 + } 84 93 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; 94 + // Keep row active for a short time 95 + sleep_us(50); 96 + 97 + // Deactivate row 98 + gpio_put(ROW_PINS[row], 0); 89 99 } 90 100 } 91 101 } 92 - 93 - const 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}}; 102 + void setup_pins() { 103 + // Set up row pins 104 + for (int i = 0; i < ROWS; i++) { 105 + gpio_init(ROW_PINS[i]); 106 + gpio_set_dir(ROW_PINS[i], GPIO_OUT); 107 + gpio_put(ROW_PINS[i], 0); // Start with LEDs off 108 + } 97 109 98 - const 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}}; 110 + // Set up column pins 111 + for (int j = 0; j < COLS; j++) { 112 + gpio_init(COL_PINS[j]); 113 + gpio_set_dir(COL_PINS[j], GPIO_OUT); 114 + gpio_put(COL_PINS[j], 1); // Start with LEDs off 115 + } 116 + } 102 117 103 118 void core1_main() { 104 119 while (true) { 105 - update_led_display(1000); 120 + update_led_display(1000); // Update at 60Hz for smoother display 121 + sleep_ms(1); // Small delay to prevent tight loop 106 122 } 107 123 } 108 124 ··· 110 126 stdio_init_all(); 111 127 setup_pins(); 112 128 129 + // seed the random number generator 130 + srand(time_us_32()); 131 + 132 + last_char_time = get_absolute_time(); 133 + 113 134 multicore_launch_core1(core1_main); 114 135 115 136 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); 137 + // check if there are characters available from the input 138 + int c = getchar_timeout_us(0); 139 + 140 + if (c != PICO_ERROR_TIMEOUT) { 141 + // echo char back 142 + printf("%c", c); 143 + 144 + last_char_time = get_absolute_time(); 145 + 146 + fading = false; 147 + brightness = 100; 148 + 149 + randomize_matrix(); 150 + 151 + while (getchar_timeout_us(0) != PICO_ERROR_TIMEOUT) {} 152 + } 153 + 154 + sleep_ms(10); 121 155 } 122 156 }