···
#include "pico/multicore.h"
// 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};
14
+
const uint ROW_PINS[4] = {12, 13, 14, 15};
15
+
const uint COL_PINS[6] = {17, 18, 19, 20, 21, 22};
uint8_t led_states[ROWS][COLS] = {0};
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
20
+
// brightness control
21
+
uint8_t brightness = 100;
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
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;
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;
···
last_update = current_time;
47
-
// Display the current state
48
-
for (int row = 0; row < ROWS; row++) {
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;
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);
53
+
if (!fading && time_since_last_char > inactivity_timeout_ms) {
56
+
fade_start_time = get_absolute_time();
58
-
// Small delay for this row to be visible
60
+
uint32_t fade_elapsed_ms = absolute_time_diff_us(fade_start_time, get_absolute_time()) / 1000;
62
-
gpio_put(ROW_PINS[row], 0);
62
+
if (fade_elapsed_ms >= fade_duration_ms) {
65
+
brightness = 100 - ((fade_elapsed_ms * 100) / fade_duration_ms);
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);
75
+
for (int j = 0; j < COLS; j++) {
76
+
gpio_put(COL_PINS[j], 1);
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));
82
+
// Software PWM cycle
83
+
for (uint8_t pwm_cycle = 0; pwm_cycle < brightness; pwm_cycle++) {
85
+
for (int row = 0; row < ROWS; row++) {
87
+
gpio_put(ROW_PINS[row], 1);
78
-
void random_state(float density) {
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);
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
98
+
gpio_put(ROW_PINS[row], 0);
93
-
const uint8_t ALL_ON[ROWS][COLS] = {{1, 1, 1, 1, 1, 1},
96
-
{1, 1, 1, 1, 1, 1}};
102
+
void setup_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
98
-
const uint8_t ALL_OFF[ROWS][COLS] = {{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
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
···
129
+
// seed the random number generator
130
+
srand(time_us_32());
132
+
last_char_time = get_absolute_time();
multicore_launch_core1(core1_main);
116
-
random_state(0.5); // Turn random LEDs ON
117
-
// set_matrix_state(ALL_ON);
119
-
// set_matrix_state(ALL_OFF);
137
+
// check if there are characters available from the input
138
+
int c = getchar_timeout_us(0);
140
+
if (c != PICO_ERROR_TIMEOUT) {
144
+
last_char_time = get_absolute_time();
149
+
randomize_matrix();
151
+
while (getchar_timeout_us(0) != PICO_ERROR_TIMEOUT) {}