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!



Monday, November 7, 2022

Addressable LED Strip Flickers with Arduino | Weird Behavior

Problem: 

While working with Arduino and addressable LED strips (a few times now), I tend to wire up everything (with external power since the LEDs can draw a lot of current) and go to test it. The LEDs almost seem like they are working, but then they flicker, are varying brightness, and erratic. 


(The LED strips I have used don't have a second GND)

Solution:

Quite simply this is just me forgetting to tie the ground from the power adapter to the ground of the Arduino so they share the same basis. Once grounded, things seem to work just fine. It turns out the LED strip above was already trying to hint at how to solve the problem... 

If your LED strip doesn't have 2 GND connections, just connect the G from the Arduino directly to the GND rail connected to by the LED DC power supply.






Tuesday, November 1, 2022

Raspberry Pi Camera - Color ASCII Real-time Preview for command line

Problem:

I'm working on a project with my son that uses a Raspberry Pi camera to detect people, identify them, and then shoot them (with a NERF bullet via a turret) if they are not "authorized". 

I'm using an older RPI 3 B+, and like many PIs it has it's limits. To save on resource usage, I decided I would forgo the desktop. After a bit, I realized that there wasn't a great way (I could figure out) to preview my PI camera footage for the purpose of taking pictures of faces to train a face detection model. 


Solution:

In the past, I had seen some ASCII art, and so I thought, "why not build a live camera preview rendering out the frames as ASCII?" shortly after with "and in color!"

This was my first time working with curses, which prove to have it's own little nuances, but isn't terrible. 

Feel free to comment if you have any improvements. The colors are interesting and could probably use some love. Also, I'm sure there are some optimizations that could be done to improve overall performance. 

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

To improve render performance, you can set use_color = False, but you might have a harder time seeing the image. 


Demos:

Compare ASCII color preview with capture

Testing out ASCII color w/ change to night vision