nspsck / RM67162_Micropython_QSPI

This is a C driver as a user module for the T-AMOLED-S3 using QSPI-protocol.
MIT License
7 stars 4 forks source link

Bitmap function improperly displaying when tft.rotation(1) or (3) #9

Closed 4jakers18 closed 4 months ago

4jakers18 commented 4 months ago

When using the image_bitmap.py example, The logo.py bitmap displays improperly, it seems to be read in incorrect order, leading to it displaying "split" diagonally, as if someone cut the image from (x1,y0) to (x0,y1). I added some gradient lines to help me understand the coordinate system:

import tft_config
import logo
import gc
import time

def main():
    tft = tft_config.config()

    tft.reset()
    tft.init()
    tft.rotation(1)
    tft.bitmap(0, 0, logo.WIDTH, logo.HEIGHT, logo.BITMAP)

#for testing coordinates:
    # Red to yellow gradient on x axis, starting at 0,0
    for i in range(0,240,2):
        color = tft.colorRGB(255, i, 0)  
        tft.pixel(i, 0, color) 

    # Green to cyan gradient on y axis, starting at 0,0
    for i in range(0,240,2):
        color = tft.colorRGB(0, 255, i) 
        tft.pixel(0, i, color)
    time.sleep(0.1)
    del tft

gc.enable()
main()
gc.collect()

That results in:

I then decided to test tft.rotation(0), using an image that should fit properly, a 240x240 picture of a cat. This was also to rule out the original logo.py being improperly formatted.

That worked okay, but when trying tft.rotation(1) with that same image, the image doesn't appear to be read/displayed entirely:

I was a little confused, so I tried adding an offset to the start/end points in different permutations until something worked, the best I got was with tft.bitmap(0, 0, logo.WIDTH, logo.HEIGHT-1, logo.BITMAP), which resulted in a "split" image similar to before with logo.py:

Not sure how I should proceed here. I can probably get around it for now by purposely adding the split in my original images to negate the split when displayed, but having to subtract from y1 still isn't making sense to me. Otherwise thanks for the driver!

nspsck commented 4 months ago

Hi,

Thanks for pointing out the mistake in the example. I have fixed it, and now it should be working properly.

The reason for this issue is due to the fact, of how panel controllers work. Typically, you set a valid area first, then you fill all the content as linear data (a linear byte array) to this area, line by line. By this nature, if you have set an invalid area, the picture is going to shift.

For example: You have a picture of a size 8x8. And the content will be:

b'\x00\x00\x00\x00\x00\x00\x00\x00' \
b'\x11\x11\x11\x11\x11\x11\x11\x11' \
b'\x00\x00\x00\x00\x00\x00\x00\x00' \
b'\x11\x11\x11\x11\x11\x11\x11\x11' \
b'\x00\x00\x00\x00\x00\x00\x00\x00' \
b'\x11\x11\x11\x11\x11\x11\x11\x11' \
b'\x00\x00\x00\x00\x00\x00\x00\x00' \
b'\x11\x11\x11\x11\x11\x11\x11\x11' \

Which is equivalent to:

# This is why I call it linear. 
# And this is how the data will be sent to the display.
b'\x00\x00\x00\x00\x00\x00\x00\x00\x11\x11\x11\x11\x11\x11\x11\x11\x00\x00\x00\x00\x00\x00\x00\x00\x11\x11\x11\x11\x11\x11\x11\x11\x00\x00\x00\x00\x00\x00\x00\x00\x11\x11\x11\x11\x11\x11\x11\x11\x00\x00\x00\x00\x00\x00\x00\x00\x11\x11\x11\x11\x11\x11\x11\x11'

In case you have set the coordinate wrong, e.g. (0,1,8,8). Which is a 9x8 area, (8 - 0 + 1) x (8 - 1 + 1). What actually will be displayed is as the follows, say 0x00=*, 0x11=#.

********#
#######**
******###
#####****
****#####
###******
**#######
#

So the picture is not what you wanted, which would be:

********
########
********
########
********
########
********
########

So basically, you have to calculate the correct value of the start point (x0, y0) to the end point (x1,y1). Note that, there are (x1 - x0 + 1) x (y1 - y0 + 1) pixels in total.

Hopefully this helps. Best regards.

4jakers18 commented 4 months ago

Thank you, I'll give this a shot. This also confirms that my img_to_bitmap.py script is indeed working properly, would that be something you'd be interesting having in the repository? I can make a PR for it.

nspsck commented 4 months ago

If you write the code on your own, or you know the source of the code, you are free to make a PR. Otherwise will be difficult for me to get the license righ.

4jakers18 commented 4 months ago

It's my own, I'll clean it up and PR!