Rotating images in micropython

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
jwrm22
Posts: 2
Joined: Wed Apr 15, 2020 7:34 pm

Rotating images in micropython

Post by jwrm22 » Sun Oct 18, 2020 5:54 pm

Hi there,

I did not find a location to introduce myself so I'll quickly do this here.
I'm an electronics engineer, mostly hardware and a bit of C programming. Recently I got into embedded security and found a job there.
For me I just want to the ESP32 as a tool and I don't feel like learning it for the sake of learning.
Micro python is interesting to me as the chips get more powerful there is no real reason any more to write code in assembly or C.

Early 2020 I purchased an M5stack and a couple of ESP32 and gave a few 'easy' projects a try.
I'd wanted to read out a SPI sensor but found quickly that there is no easy way to do so. In M5stack they removed SPI from the machine library. They've told me it's because the SPI implementation does not support multiple sensors on the bus and it's used for the display.
That project failed quickly and I was discouraged.

A month ago I've bought the same sensor but on I2C instead. It's a 12bit magnetic encoder.
For the project I want to measure angles accurately, that succeeded. Now I want to visualise this data, preferably not as a number.
What i came up with was to have a virtual (safe) dial on the screen. I've generated a picture but quickly found rotating images is not trivial.

Something like this:
Image

Others solve this by using sprites, as many angles as you need.
For me this would be 360 sprites, one for every degree of rotation making it non trivial.
I've seen a very cool implementation where someone draws an old radio interface on an OLED.
It looked like they would just draw an image partially outside of the screen area. But it appears they generate it on the fly.
link: https://tj-lab.org/2019/02/17/vfo5/

So my question comes down to:
Does a micro python game or graphics engine exists that allows me to rotate sprites arbitrarily?
What other ways are there to solve this problem?

Thanks in advance.

User avatar
pythoncoder
Posts: 4571
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Rotating images in micropython

Post by pythoncoder » Mon Oct 19, 2020 5:52 am

As far as I know this has not been done. I can virtually guarantee that this would need to be done in C to have acceptable performance.

You haven't told us what display you are using. Some displays such as OLED's have drivers using a class derived from framebuf. In these cases there is a nano-gui class which can display angles in a compass-like format. It would be relatively easy to add your own widget to do a display like that in your radio example. The only issue might be the rotated fonts: there is currently no way to do that with my Python font files.

Note that nano-gui, like my GUI's for other types of display, use widgets based on graphics primitives rather than on bitmapped icons. This is for scalability and for performance as Python code.
Peter Hinch

jwrm22
Posts: 2
Joined: Wed Apr 15, 2020 7:34 pm

Re: Rotating images in micropython

Post by jwrm22 » Mon Oct 19, 2020 6:06 pm

Thank you for the answer. i expected as much.
Nano-Gui is a very cool project. I'll definitely take a look at it.

The screen of the M5 stack is an 2 inch, 320x240 Colorful TFT LCD.

At this moment I'll just have to put the time into it and generate the 400 sprites for the demo.
In Inkscape this is reasonably quick and it's worth the effort.
I'll update once I have some results.

User avatar
pythoncoder
Posts: 4571
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Rotating images in micropython

Post by pythoncoder » Tue Oct 20, 2020 5:30 am

Keep an eye on storage space. From a quick glance at M5 Stack the display appears to use 24 bit colour. If you're creating 400 full screen images I make it nearly 88MiB:

Code: Select all

>>> 320*240*400*3/(1024**2)
87.890625
>>> 
Peter Hinch

Post Reply