Skip to content

Conversation

@gustebeast
Copy link

@gustebeast gustebeast commented Jan 22, 2026

Main branch without Tetris
Flash: [======== ] 79.8% (used 1255301 bytes from 1572864 bytes)

Main branch with Tetris (+196kb)
Flash: [========= ] 92.3% (used 1452049 bytes from 1572864 bytes)

This commit with Tetris (+6kb, 97% less flash)
Flash: [======== ] 80.2% (used 1261625 bytes from 1572864 bytes)

Summary by CodeRabbit

  • New Features
    • Visualizes recently cleared cells so they briefly render as white for clearer line-clear feedback.
  • Refactor
    • Simplified piece-queue advancement logic.
    • Removed unused header includes.
    • Streamlined public API by removing an internal queue-enqueue method.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 22, 2026

Walkthrough

Removed unused includes, refactored piece-queue logic and public API, added cleared-pixel tracking to grid color state, and adjusted drawGrid pixel-color selection to prioritize cleared pixels and use local pixel variables. Total changes are header cleanup, queue shift refactor, rendering tweak, and added clearedPixels storage/accessor.

Changes

Cohort / File(s) Summary
Include cleanup
usermods/TetrisAI_v2/gridbw.h, usermods/TetrisAI_v2/pieces.h
Removed unused includes: <iterator> from gridbw.h and <iostream> from pieces.h. No API or behavioral changes.
Refactor & API removal (bag)
usermods/TetrisAI_v2/tetrisbag.h
Replaced std::rotate with an explicit in-place left-shift loop in queuePiece(); removed public overload void queuePiece(uint8_t idx) and the #include <algorithm>. Queue advancement behavior preserved; callers of the removed overload must be updated.
Rendering change
usermods/TetrisAI_v2/TetrisAI_v2.cpp
drawGrid now reads pixel and cleared-pixel into locals, checks clearedPixel > 0 first (renders White), then background/game-over, and computes colorIndex from gridPixel. Control flow restructured but color-selection logic otherwise retained.
Grid color state extension
usermods/TetrisAI_v2/gridcolor.h
Added std::vector<uint8_t> clearedPixels and accessor getClearedPixel(uint8_t x, uint8_t y); constructor and clear() initialize/reset it; cleanupFullLines() marks clearedPixels for cleared lines. Public interface expanded with cleared-pixel access.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main objective of the pull request: significantly reducing the flash size footprint of the TetrisAI_V2 usermod from 196 KB down to 6 KB (97% reduction), while also adding new functionality like a line-cleared flashing effect.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
usermods/TetrisAI_v2/tetrisbag.h (1)

19-19: Self-include should be removed.

This header includes itself, which is unnecessary and likely a copy-paste error. The include guards prevent infinite recursion, but the self-include serves no purpose.

Proposed fix
-#include "tetrisbag.h"
🧹 Nitpick comments (1)
usermods/TetrisAI_v2/tetrisbag.h (1)

89-91: Manual left-shift loop is correct; minor edge-case note.

The replacement of std::rotate with a manual loop is functionally equivalent and achieves the flash size reduction goal. However, if piecesQueue.size() were ever 0, the expression piecesQueue.size() - 1 would underflow (since size() returns unsigned size_t), causing the loop to iterate with out-of-bounds access.

In practice, queueLength should never be 0 for a Tetris game, so this is a low-risk edge case. If you want to be defensive:

Optional defensive check
 void queuePiece()
 {
     //move vector to left
+    if (piecesQueue.empty()) return;
     for (uint8_t i = 0; i < piecesQueue.size() - 1; i++) {
         piecesQueue[i] = piecesQueue[i + 1];
     }
     piecesQueue[piecesQueue.size() - 1] = getNextPiece();
 }

{
//move vector to left
std::rotate(piecesQueue.begin(), piecesQueue.begin() + 1, piecesQueue.end());
for (uint8_t i = 0; i < piecesQueue.size() - 1; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safer and technically "correct":

for (uint8_t i = 1; i < piecesQueue.size(); i++) {
  piecesQueue[i-1] = piecesQueue[i];
}

Will also guard against size of 0.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
usermods/TetrisAI_v2/gridcolor.h (1)

81-107: Removing offset++ breaks line-clear synchronization between visual and occupancy grids.

With the increment commented out, offset stays 0, so pixel rows never shift down. Meanwhile, gridBW.cleanupFullLines() at line 107 still shifts the occupancy bits. This desyncs the color grid from the collision grid. Worse, the UINT8_MAX value set at line 90 is non-zero, so if sync() runs afterward (which rebuilds gridBW from the color pixels), those marked rows will reappear in gridBW since sync() checks if (*getPixel(x, y) != 0) at line 123.

Restore the offset++ increment to shift color pixels in sync with the occupancy grid, or ensure marked rows are cleared before a later sync pass.

Fix: restore offset++ to shift pixels with occupancy
             if (gridBW.isLineFull(y))
             {
-                // offset++;
+                offset++;
                 for (uint8_t x = 0; x < width; x++)
                 {
                     pixels[y * width + x] = UINT8_MAX;
                 }

@gustebeast gustebeast force-pushed the wled-gtb branch 3 times, most recently from c19abe9 to 2578f7d Compare January 23, 2026 19:22
Main branch without Tetris
Flash: [========  ]  79.8% (used 1255301 bytes from 1572864 bytes)

Main branch with Tetris (+196kb)
Flash: [========= ]  92.3% (used 1452049 bytes from 1572864 bytes)

This commit with Tetris (+6kb, 97% less flash)
Flash: [========  ]  80.2% (used 1261625 bytes from 1572864 bytes)
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
usermods/TetrisAI_v2/gridcolor.h (2)

19-19: Remove self-include.

The file gridcolor.h includes itself. This is redundant since the header guards prevent actual issues, but it's clearly unintentional and should be removed.

-#include "gridcolor.h"

84-114: Critical bug: offset is never incremented, breaking row-shift logic.

The offset++ on line 92 is commented out, so offset remains 0 throughout the loop. This means the condition if (offset > 0) on line 101 is never true, and the row-shifting logic (lines 105-109) that moves color pixels down after a line is cleared will never execute.

While gridBW.cleanupFullLines() handles the black/white grid, the pixels array will become desynchronized—cleared rows won't collapse, causing visual corruption.

🐛 Proposed fix
             if (gridBW.isLineFull(y))
             {
-                // offset++;
+                offset++;
                 for (uint8_t x = 0; x < width; x++)
                 {
                     pixels[y * width + x] = 0;
                     clearedPixels[y * width + x] = 1;
                 }
                 continue;
             }
🤖 Fix all issues with AI agents
In `@usermods/TetrisAI_v2/gridcolor.h`:
- Around line 93-97: The flash state in clearedPixels is never reset so flashed
pixels stay white; to fix, reset the clearedPixels buffer at the start of
cleanupFullLines() (or alternatively decrement/clear entries in drawGrid() after
rendering the flash), e.g., iterate the clearedPixels array and set each element
to 0 before processing line clears so subsequent frames no longer treat those
pixels as flashed; locate the clearedPixels and pixels arrays and the
cleanupFullLines(), drawGrid(), and clear() functions to implement the reset.

Comment on lines 93 to 91
for (uint8_t x = 0; x < width; x++)
{
pixels[y * width + x] = 0;
clearedPixels[y * width + x] = 1;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Flash effect persists indefinitely—clearedPixels are never reset.

The clearedPixels are set to 1 when a line is cleared but are only reset to 0 in clear() (called on full reset). After the flash animation frame, these pixels will continue rendering as white forever because there's no code to decrement or clear them.

Consider either:

  1. Decrementing clearedPixels in drawGrid() after rendering the flash, or
  2. Adding a separate method to clear the flash state after a frame, or
  3. Resetting clearedPixels at the start of cleanupFullLines() before processing.
🐛 Proposed fix (option: reset in cleanupFullLines before processing)
     void cleanupFullLines()
     {
         uint8_t offset = 0;
+        // Reset flash state from previous frame
+        for (uint8_t i = 0; i < width * height; i++)
+        {
+            clearedPixels[i] = 0;
+        }
         //from "height - 1" to "0", so from bottom row to top
         for (uint8_t y = height; y-- > 0; )
         {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for (uint8_t x = 0; x < width; x++)
{
pixels[y * width + x] = 0;
clearedPixels[y * width + x] = 1;
}
void cleanupFullLines()
{
uint8_t offset = 0;
// Reset flash state from previous frame
for (uint8_t i = 0; i < width * height; i++)
{
clearedPixels[i] = 0;
}
//from "height - 1" to "0", so from bottom row to top
for (uint8_t y = height; y-- > 0; )
{
// ... rest of the function
}
}
🤖 Prompt for AI Agents
In `@usermods/TetrisAI_v2/gridcolor.h` around lines 93 - 97, The flash state in
clearedPixels is never reset so flashed pixels stay white; to fix, reset the
clearedPixels buffer at the start of cleanupFullLines() (or alternatively
decrement/clear entries in drawGrid() after rendering the flash), e.g., iterate
the clearedPixels array and set each element to 0 before processing line clears
so subsequent frames no longer treat those pixels as flashed; locate the
clearedPixels and pixels arrays and the cleanupFullLines(), drawGrid(), and
clear() functions to implement the reset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants