Tuesday, November 15, 2022

Non-blocking Arduino/OLED SSD1306 Animation Manager

Problem:

I'm working on a project that involves a bit of screen usage and I need to organize things in a way that they will work without blocking my event loop. (I plan to share this project once I'm about finished with it. - And it's sure to make you parent of the year!)

While I have done a bit with Arduino and programming in other languages, I don't have a lot of experience in C/C++, so if anything looks wrong (uh hem, like pointers/ref usage...), please help me understand so I can improve the code and myself :). 


Solution:

To really be able to handle playback of animations on the OLED screen, I decided I would code up an Animator class. You register sequences with it (and get a seq ID back), and then call-up and play any stored sequence, looping or not. In addition to play(), pause(), and stop(),  I also wanted to manage the FPS of playback so that if the event loop called too often, the redraws would only occur when necessary, or in the case of fast FPS, would skip frames in an effort to keep up. 

I think there are a lot of cool things that could still be done with this, but for the time being this is where it is at. 


Here is a quick demo using the Animator class (trust me, it's non-blocking) and some example code: 

https://gist.github.com/mcorrigan/cd95230c568503f3917a57c66ef73b55 


Bug Note: There is currently an issue where if you don't Serial->println() the _currentFrameDuration, the framerate won't update properly. This could be a timing issue.


Be sure to check-out the chatGPT code review here!



No comments: