Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken colors and partial flush for ILI9488 display #2976

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

andreilukichoff
Copy link

@andreilukichoff andreilukichoff commented Jul 4, 2024

Description

Fixed bugs in native Ili9488 display driver:

  • Corrupted colors (incorrect order BGR instead of RGB).
  • Corrupted screen when using partial flush.

How Has This Been Tested?

  • Tested on LILYGO TTGO T-Relay ESP32 Wireless Module board.
  • Colors tested on "Primitives" examples.
  • Test application code snippet for Flush (BitBlt) provided.

Screenshots

Left: corrupted colors, Right: fixed

ili9488fix1

Left: corrupted partial flush, Right: fixed

ili9488fix2

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • Dependencies/declarations (update dependencies or assembly declarations and changes associated, has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

BitBlt test app:

using System.Threading;
using System.Device.Gpio;
using nanoFramework.Hardware.Esp32;
using nanoFramework.UI;
using System.Drawing;

namespace nanoFrameworkIli9844DisplayTestApp
{
    public class Program
    {
        const int backLightPin = 32;
        const int chipSelectPin = 15;
        const int dataCommandPin = 2;
        const int resetPin = 4;
        
        static GpioController gpio = new();
        static SpiConfiguration spi = new SpiConfiguration(1, chipSelectPin, dataCommandPin, resetPin, backLightPin);
        static ScreenConfiguration screen = new ScreenConfiguration();

        public static void Main()
        {
            Configuration.SetPinFunction(19, DeviceFunction.SPI1_MISO);
            Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI);
            Configuration.SetPinFunction(22, DeviceFunction.SPI1_CLOCK);

            DisplayControl.Initialize(spi, screen, 1024 * 1024);
            
            gpio.OpenPin(backLightPin, PinMode.Output).Write(PinValue.High);

            var buffer = DisplayControl.FullScreen;

            // test full flush
            for (int i = 0; i < 5; i++)
            {
                buffer.DrawRectangle(Color.White, 2, 0, 0, 480, 320, 0, 0,
                    Color.White, 0, 0,
                    Color.Black, 480, 320, Bitmap.OpacityOpaque);
                buffer.Flush();
                Thread.Sleep(500);

                buffer.Clear();
                buffer.Flush();
                Thread.Sleep(500);
            }

            buffer.DrawRectangle(Color.White, 2, 0, 0, 480, 320, 0, 0,
                  Color.White, 0, 0,
                  Color.Black, 480, 320, Bitmap.OpacityOpaque);

            // test partial flush
            for (int i = 0; i < 5; i++)
            {
                buffer.DrawRectangle(Color.White, 2, 100, 100, 200, 200, 0, 0,
                    Color.White, 100, 100,
                    Color.Blue, 300, 300, Bitmap.OpacityOpaque);
                buffer.Flush(100, 100, 200, 200);
                Thread.Sleep(500);

                buffer.DrawRectangle(Color.White, 2, 100, 100, 200, 200, 0, 0,
                 Color.Blue, 100, 100,
                 Color.White, 300, 300, Bitmap.OpacityOpaque);
                buffer.Flush(100, 100, 200, 200);
                Thread.Sleep(500);
            }

            Thread.Sleep(Timeout.Infinite);
        }
    }
}

Summary by CodeRabbit

  • Refactor
    • Improved performance and readability of the pixel iteration logic by iterating over x and y coordinates directly, rather than using a single index.
    • Optimized the storage order of color values in the TransferBuffer for better efficiency.

@nfbot nfbot added the Type: bug label Jul 4, 2024
Copy link

coderabbitai bot commented Jul 4, 2024

Walkthrough

The changes involve modifying the BitBlt function in the ILI9488_480x320_SPI.cpp file of the nanoFramework Graphics library. The primary updates include altering the loop structure from using a single index to using nested loops over x and y coordinates, calculating the pixel index accordingly. Additionally, the order of storing g and b values in the TransferBuffer has been swapped to optimize memory management and performance.

Changes

File Change Summary
src/nanoFramework.Graphics/Graphics/Displays/ILI9488_480x320_SPI.cpp - Modified BitBlt function to use nested loops over x and y coordinates instead of a single index i. Calculated i based on x and y.
- Reordered storing of g and b values in TransferBuffer.
- Removed numPixels variable.
- Adjusted TransferBuffer write operations to new order (r, g, b).

Sequence Diagram(s)

sequenceDiagram
    participant ClientApp
    participant DisplayDriver
    participant TransferBuffer

    ClientApp->>DisplayDriver: Call BitBlt(srcX, srcY, width, height)
    loop For each y from srcY to srcY + height
        loop For each x from srcX to srcX + width
            DisplayDriver->>DisplayDriver: Calculate index i
            DisplayDriver->>TransferBuffer: Store r, g, b values in order
        end
    end
    Note over TransferBuffer: Buffer filled with pixel data
    DisplayDriver-->>ClientApp: BitBlt operation complete
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@andreilukichoff
Copy link
Author

@andreilukichoff please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree

Automated fixes for code style.
Copy link

@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

Outside diff range and nitpick comments (1)
src/nanoFramework.Graphics/Graphics/Displays/ILI9488_480x320_SPI.cpp (1)

298-299: Correctness: Ensure proper nesting of loops.

The nested loops should be properly indented for better readability and to avoid confusion.

-    for (uint32_t y = srcY; y < srcY + height; y++)
-    for (uint32_t x = srcX; x < srcX + width; x++)
+    for (uint32_t y = srcY; y < srcY + height; y++) {
+        for (uint32_t x = srcX; x < srcX + width; x++) {
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 84cc870 and adac5aa.

Files selected for processing (1)
  • src/nanoFramework.Graphics/Graphics/Displays/ILI9488_480x320_SPI.cpp (2 hunks)
Additional comments not posted (2)
src/nanoFramework.Graphics/Graphics/Displays/ILI9488_480x320_SPI.cpp (2)

315-317: LGTM! Ensure correct order of storing color values.

The order of storing r, g, and b values in TransferBuffer looks good. Ensure that this order is consistent with the expected format.


301-301: Correctness: Ensure i calculation is accurate.

The index i is calculated based on x and y coordinates. Ensure that Attributes.Width is correctly defined and used.

Verification successful

Verification Successful: The calculation of i is accurate.

The index i is correctly calculated based on the x and y coordinates, with Attributes.Width being properly defined and assigned based on the display orientation.

  • ILI9488_480x320_SPI.cpp:
    • Lines 173, 179, 185, 191: Attributes.Width is assigned based on the display orientation.
    • Line 301: i calculation uses Attributes.Width.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `Attributes.Width` is correctly defined and used.

# Test: Search for the definition and usage of `Attributes.Width`. Expect: Proper definition and usage.
rg --type cpp $'Attributes.Width'

Length of output: 9375


Script:

#!/bin/bash
# Extract lines from ILI9488_480x320_SPI.cpp where Attributes.Width is defined or assigned values
rg --type cpp --line-number 'Attributes.Width' src/nanoFramework.Graphics/Graphics/Displays/ILI9488_480x320_SPI.cpp

Length of output: 720


Script:

#!/bin/bash
# Extract lines around the assignments of Attributes.Width in ILI9488_480x320_SPI.cpp to understand the context
rg --type cpp --context 5 --line-number 'Attributes.Width' src/nanoFramework.Graphics/Graphics/Displays/ILI9488_480x320_SPI.cpp

Length of output: 3361

Copy link
Member

@Ellerbach Ellerbach left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! Looks good all up, you should have a code style PR to merge.

@@ -309,9 +311,9 @@ void DisplayDriver::BitBlt(
g = (g << 2) | (g >> 4);
r = (r << 3) | (r >> 2);

TransferBuffer[count++] = b;
TransferBuffer[count++] = g;
TransferBuffer[count++] = r;
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure about the order? I remember the color order is configurable and depends on the driver.

Copy link
Author

Choose a reason for hiding this comment

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

The driver only supports SPI and Ili9488 only supports 3-byte color in RGB order in SPI mode. I suppose thats why the order was hardcoded initially.

Copy link
Contributor

@alberk8 alberk8 Jul 5, 2024

Choose a reason for hiding this comment

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

Should we expose the RGB and BGR to the managed side so that it can be switched in the hardware which correspond to this in nF .

#define RFLAG_COLOR_RGB       0b00000000     /* RGB */
#define RFLAG_COLOR_BGR       0b00001000     /* BGR */

Copy link
Author

Choose a reason for hiding this comment

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

If I understand correctly, this flag alters the output signal for panels with different order, while the buffer always uses RGB. If this is the case, we should substitute the hardcoded MADCTL_BGR constant in DisplayDriver::ChangeOrientation with a variable that may be either MADCTL_BGR or 0.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you test it out without flipping the RGB values in code?.

Copy link
Author

Choose a reason for hiding this comment

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

I just tested it and it works exactly as I expected. It displays incorrect colors with RGB buffer and 0 value instead of MADCTL_BGR, and correct colors with MADCTL_BGR value. And vice versa with BGR buffer.
So my panel utilizes BGR pixels obviously.
I suppose the buffer should always remain RGB since there is no other option mentioned in the datasheet. See page 122.

Copy link
Contributor

Choose a reason for hiding this comment

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

Different manufacturer uses different panel with same controller. As far as I know there are two types RGB and BGR.

Copy link
Author

Choose a reason for hiding this comment

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

Yes and controller uses RGB for buffer and flag to alter signal for different panels. I don't see why buffer should not be RGB in any case.

Copy link
Contributor

Choose a reason for hiding this comment

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

May be you want to check with this change and if that is inline with this driver.

Copy link

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between adac5aa and 0bae92b.

Files selected for processing (1)
  • src/nanoFramework.Graphics/Graphics/Displays/ILI9488_480x320_SPI.cpp (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/nanoFramework.Graphics/Graphics/Displays/ILI9488_480x320_SPI.cpp

@josesimoes
Copy link
Member

Any further developments on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants