optimize rerender matrix, do it only if something is changed from the last render

This commit is contained in:
andrea
2026-03-14 23:47:44 +01:00
parent fa47480aef
commit e7f0c3cc1a

View File

@@ -42,6 +42,8 @@ int bar_length= 3;
int p1_score= 0; int p1_score= 0;
int p2_score= 0; int p2_score= 0;
int need_refresh= 1;
int loop_delay= 220; int loop_delay= 220;
long exec_t2= millis(); long exec_t2= millis();
@@ -59,6 +61,8 @@ void setup() {
} }
void render_matrix() { void render_matrix() {
if (!need_refresh) return;
need_refresh= 0;
// clear // clear
for (int x=0; x < 12; x++) { for (int x=0; x < 12; x++) {
for (int y=0; y < 8; y++) { for (int y=0; y < 8; y++) {
@@ -84,30 +88,27 @@ void render_matrix() {
void pong_move_p1() { void pong_move_p1() {
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) { if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
p1_start -= 1; p1_start -= 1;
Serial.print("P1 up: "); need_refresh= 1;
Serial.println(p1_start);
} }
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) { else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
p1_start += 1; p1_start += 1;
Serial.print("P1 down: "); need_refresh= 1;
Serial.println(p1_start);
} }
} }
void pong_move_p2() { void pong_move_p2() {
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) { if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
p2_start -= 1; p2_start -= 1;
Serial.print("P2 up: "); need_refresh= 1;
Serial.println(p2_start);
} }
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) { else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
p2_start += 1; p2_start += 1;
Serial.print("P2 down: "); need_refresh= 1;
Serial.println(p2_start);
} }
} }
void move_ball() { void move_ball() {
need_refresh= 1;
if (ball_move_x == 0 || ball_move_y == 0) { if (ball_move_x == 0 || ball_move_y == 0) {
// extract random number between 0 or 1 to select the directions // extract random number between 0 or 1 to select the directions
if (random(2) == 0) ball_move_x= 1; if (random(2) == 0) ball_move_x= 1;