Sunday, November 21, 2021

Arduino Pro Micro (Atmega32U4) - Can't Upload Sketch - Board Bricked

Problem: 

I recently purchased some pro micro clone boards with HID support. I was able to upload to the first board without a problem, but then I was unable to upload to the same board with a new sketch or any of the other boards I purchased in that bundle. It turned out to be a few things including a timing issue. 

Solution: 

There are really two parts of this solution required to actually make this work and it is a bit of a pain if you have to do it often, but it worked for me and hopefully will help you. 

Problem 1 & Solution: The com port while the board was plugged in was always different then the one used while the bootloader was running. For example, when the board sketch was running it would show up as COM9.


When the board was first plugged in and the bootloader was reset, it would show up as COM8. Since we want to upload a new sketch to the board, we need to do this while the board is on the bootloader serial port COM8. First, let's get the bootloader to reset and stall for ~8 seconds so we can determine the bootloader serial port (your ports are likely different than mine). To do this with my board and unplugged from the USB, I had to connect RST and GND together with a wire, and then plug in the USB cable. After a moment, pull the wire out of either pin and then immediately in the Arduino IDE go to the Tools -> Port menu and choose the bootloader port now showing: 


Once you have done this, Arduino IDE should remember that serial port for next time. If you waited too long, you won't see the Arduino connected to any other port, so make sure you are quick. 

Problem 2 & Solution: The second part of the problem is that we need to compile and upload the new sketch before the bootloader times out. This is a challenge if you are trying to manage everything above, compile the sketch and upload it at once which is why we break it into parts. With the Arduino IDE still pointing to the bootloader serial port (COM8 for me), we need to again reset the bootloader on the Arduino to allow the new sketch to be uploaded. We do this by unplugging the USB, reconnecting our wire between RST and GND, and then plugging the USB cable back in. While the wire is still connected between RST and GND, begin compile and upload in the Arduino IDE. Once you see the sketch storage information print in the output, 


pull the wire out of either pin (RST or GND). The IDE should see the device connect on port COM8 as the bootloader goes into the ~8 second stall and then begin uploading the already compiled sketch. 

If you are successful, you will see Done uploading and your new sketch is uploaded to the board. 

Good luck!


Sources: 

With a lot of various information online, these were the resources most helpful to me with finding a solution: 

https://forum.arduino.cc/t/solved-completely-resetting-arduino-pro-micro/671721/2

https://learn.sparkfun.com/tutorials/qwiic-pro-micro-usb-c-atmega32u4-hookup-guide#troubleshooting-and-faq

https://learn.sparkfun.com/tutorials/pro-micro--fio-v3-hookup-guide/troubleshooting-and-faq#ts-reset

https://config.qmk.fm/#/test (my sketch was using function keys that are not typically on keyboards and I wanted to test and make sure shorting a pin (a button press) would produce the right keycode)


Friday, November 12, 2021

DaVinci Resolve Scripting Setup - ModuleNotFoundError: No module named 'DaVinciResolveScript'

Problem: 

While working on a project, I had the opportunity to do some coding in Blackmagic's DaVinci Resolve. Resolve supports python 2.7 and python 3.6. Jumping in, I opened and read their documentation on how to get a basic script running. 

First, I added the environment variables to my computer as they listed them: 

RESOLVE_SCRIPT_API="%PROGRAMDATA%\Blackmagic Design\DaVinci Resolve\Support\Developer\Scripting"
RESOLVE_SCRIPT_LIB="C:\Program Files\Blackmagic Design\DaVinci Resolve\fusionscript.dll"
PYTHONPATH="%PYTHONPATH%;%RESOLVE_SCRIPT_API%\Modules\"

I created a new script (test.py) based on their example and placed it in my Utility folder so that it would show up in the application (C:\ProgramData\Blackmagic Design\DaVinci Resolve\Fusion\Scripts\Utility). 

#!/usr/bin/env python
import DaVinciResolveScript as dvr_script
resolve = dvr_script.scriptapp("Resolve")
projectManager = resolve.GetProjectManager()
projectManager.CreateProject("Hello World")

After restarting Resolve, the link displayed fine. 


Once clicked, however, nothing would happen. I opened the Console (from the same Workspace menu) and saw this: ModuleNotFoundError: No module named 'DaVinciResolveScript'

There seems to be a lot of conversation with people having this issue in forums, but very few people with solutions which is why I created this post. 


Solution:

In order to determine what went wrong, we need to start by looking at what python has available to it and make sure it matches our expectation. To do this, we are going to open PowerShell (or Windows Terminal) and launch the python interpreter. Once we see the python input prompt (>>>) we can use sys to see what our path resolutions are for python dependencies. 

import sys
print (sys.path)

The python interpreter printed out several paths include: 

['', 'C:\\Users\\MUser\\%PYTHONPATH%', 'C:\\Users\\MUser\\%RESOLVE_SCRIPT_API%\\Modules', 'C:\\Python27\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']

Looking over this list, I realized python was trying to resolve full paths as being relative from my current directory. (I don't know why exactly, so leave a comment if you know). If you look at the environment variables we listed above, you will see what the full paths of these variables in supposed to be. 

Next, I went back to the Console inside Resolve and repeated the code to import and print sys.path. Resolve indicated to me the same thing, but with paths relative to it's operating directory. 

C:\\Program Files\\Blackmagic Design\\DaVinci Resolve\\%RESOLVE_SCRIPT_API%\\Modules

This explains why DaVinciResolveScript could not be found. To fix this problem for the moment, I decided to use a full path for my PYTHONPATH environment variable and this has for the moment seemed to resolve the problem. 

C:\ProgramData\Blackmagic Design\DaVinci Resolve\Support\Developer\Scripting\Modules

This resolved python finding the right files. Finally, all I needed to do was enable External scripting using Local under Resolve Preferences.


If additional solutions are required, I will try to update this post with that information. 

Good luck!