eXoCooLd / Waveshare.EPaperDisplay

.Net Core Library to show images on Waveshare E-Paper Displays
MIT License
30 stars 13 forks source link

NuGet packages updated and performance changes #7

Closed thecaptncode closed 3 years ago

thecaptncode commented 3 years ago

6 NuGet packages were updated the the latest release. Bitmap read changed to use LockBits for performance. Display write and clear changed to use buffered write on each image row for performance. Floyd-Steinberg dithering added for black and white display.

thecaptncode commented 3 years ago

I apologize in advance for the complexity of the changes. It is more extensive than just a couple tweaks. This is a bit of a significant change to the IO. Instead of reading the bitmap pixel by pixel, it is using LockBits and Marshal.Copy to read an entire row of pixels. Then it uses the buffered SpiDevice.Write method to write the entire row to the screen instead of putting the entire screen into a byte array first. You should see a significant performance improvement with Epd7in5bc.

The SPI Buffer in Raspbian defaults to 4K. Optionally, you may get even better performance by increasing the buffer size but this is definitely not necessary for proper function:

Create file /etc/modprobe.d/spidev.conf with contents:
options spidev bufsiz=65536 (or preferred buffer size).
OR
Add this to /boot/cmdline.txt:
spidev.bufsiz=65536 (or preferred buffer size).

Reboot and confirm with cat /sys/module/spidev/parameters/bufsiz

Additionally, there is now dithering with the Epd7in5_V2 since it is in Black and White (no grey). This has somewhat negatively impacted its performance but the other IO changes have made up for that and the screen looks much better now. I don't know if you would ever want to implement a version of dithering for Epd7in5bc since it has four colors but if you do, I can help you there.

I am now seeing this during build. Do we need to make a change to the build target?

C:\Program Files\dotnet\sdk\5.0.201\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.EolTargetFrameworks.targets(28,5): warning NETSDK1138: The target framework 'netcoreapp3.0' is out of support and will not receive security updates in the future. Please refer to https://aka.ms/dotnet-core-support for more information about the support policy.

Finally, I came across this issue. It looks like the dotnet IoT team is considering a move away from System.Drawing. If you want me to look into doing the same with the WaveShare driver, just let me know: https://github.com/dotnet/iot/issues/1403

Best Regards, Greg

eXoCooLd commented 3 years ago

Thank you for you work. I review the changes and try to get rid of the Warning. If everything works, I build the new NuGet Package :-)

thecaptncode commented 3 years ago

I'm glad to help. I should have mentioned this in the request. Since I was not able to test the changes on the Epd7in5bc myself, I suggest testing with images that are both smaller and larger than the display size. There is unique code to handle that but the code is untested.

eXoCooLd commented 3 years ago

I refactored the code for the Epd7in5bc device and it works fine now. Please check if you device is also working corrent after my changes. If you got some tim please try to refactor the SendBitmapToDevice method in the Epd7In5_V2, its hard to understand with all the loops and magic numbers :-)

thecaptncode commented 3 years ago

I like the changes that you made. All is working well for my device.

As a friend of mine is fond of saying: It was hard to write so it should be hard to read.

In reality, I do not subscribe to that idea but I still think it is funny and wanted to share it with you.

I will see what I can do with Epd7In5_V2.SendBitmapToDevice. Unfortunately, the Floyd-Steinberg dithering is complicated.

Those magic numbers in the ColorToByte method are well established weights to arrive at a gray with approximately the same visual intensity as a given color. Perhaps I should have commented the code to say that and define them as constants with clear names. Some of the numbers used for dithering are a mystery to me. I did not come up with the algorithm. I just implemented it.

Screen updates are now taking 5.5 seconds on the Epd7In5_V2. The device specifications say update time is 5 seconds so I don't think I can do any better than that. Are you are seeing a clear performance improvement on your device? I know the four color screen requires a lot more data.

Regards, Greg

eXoCooLd commented 3 years ago

Thats nice, the Nuget update is out now.

Yea I know that quote, and after it hits me too oftn in other projects, I try to keep thinks easy readable :-)

Yea some constants and comments for a future update would be nice, to give other devs a change to understand the magic.

I did not messure it, but it feels much faster yes. You did a very good job!

Regards, André