I have to admit that I have been very skeptical about AI, and Generative AI in particular. But after reading a recent review of OpenCode, I decided it was time to try it out. I had the perfect small application to test it on too - the Lilygo T-Watch Ultra factory firmware.
The t-watch ultra is a pretty nifty smartwatch: it combines a powerful esp32-s3 processor with bluetooth and wifi, with IMU, GPS, haptic motor, mic, and speaker, and most notably a full LORA radio. With its touchscreen and (admittedly small) keyboard input, it can send and receive LORA messages directly. Pretty cool for a watch.
The problem is that its factory firmware, while open source (arduino), is, to put it politely, unusable. The two main problems are {1} the watch comes with its timezone hard-coded to +8 (Beijing), and (2) the firmware never remembers the wifi ssid and password, so you have to type it in every time. Fortunately, given the source code, these problems are easy to fix.
Source code with full instructions are available in the LilyGoLib github repo. The code is roughly 12k lines of arduino cpp (not counting all the support libraries). Following the instructions will create a ~/Arduino/libraries/LilyGoLib, with the factory source in ~/Arduino/libraries/LilyGoLib/examples/factory. Libraries for all the t-watch hardware and gui interfaces are also installed in ~/Arduino/.libraries. Building the factory code will copy the code to ~/Arduino/factory, and build the copy there. My first step was to change the hard-coded GMT_OFFSET_SECOND in hal_interface.h to my local timezone, so that the t-watch at least worked as a watch. With the code and build environment working, the next step was to try out OpenCode.
I'm on Fedora 44, so installation of OpenCode is simply:
download the Beta OpenCode Desktop from https://opencode.ai/download/stable/linux-x64-rpm
dnf install opencode-desktop-linux-x86_64.rpm
I did not add any external models, using the default "big pickle" model.
That's it!
I ran the OpenCode Desktop GUI application from its gnome icon, and pointed it to the ~/Arduino/factory directory.
Then I entered "how to make GMT_OFFSET_SECOND a preference".
OpenCode responded with:
"Here's how to make it a runtime preference, following the existing NVS-based pattern:
1. Add field to user_setting_params_t in hal_interface.h
Add an int32_t gmtOffset_sec field to the struct around line 241-248.
2. Set default and load/save it in hal_interface.cpp
In the default init block (lines 685-691), add user_setting.gmtOffset_sec = -4*3600;
In hw_get_user_setting (line 723), print it
In hw_set_user_setting (line 733), it gets persisted automatically via putBytes
3. Replace compile-time constant in factory.ino
Replace static const uint64_t gmtOffset_sec = GMT_OFFSET_SECOND; with a runtime fetch
from NVS (call hw_get_user_setting and use user_setting.gmtOffset_sec).
4. Optionally add a UI control in ui_sys.cpp
Would you like me to implement this? If so, should I add a UI control for it in the
settings screen, or just make it programmatically configurable?"
I entered "implement this and add a UI control to the settings screen".
OpenCode modified the code, which built, flashed, and ran perfectly!
Next I entered "how would you save entered wifi ssid and password in preferences?"
OpenCode responded with:
"There are two approaches, following the existing NVS pattern:
Option A: Add fields to user_setting_params_t
Add char wifi_ssid[64] and char wifi_password[128] to the struct. They'd be persisted alongside
the rest via putBytes/getBytes. Simple, but wastes space if empty and the struct gets bulkier.
Option B: Use separate NVS keys via Preferences (my recommendation)
Store them as strings with their own keys in the same "pager" namespace:
// Save
prefs.putString("wifi_ssid", ssid);
prefs.putString("wifi_password", password);
// Load
String ssid = prefs.getString("wifi_ssid", "");
String pw = prefs.getString("wifi_password", "");
This avoids bloating the struct, works for variable-length strings, and is the idiomatic
way to store strings in NVS. You'd add load/save helper functions like hw_load_wifi_creds()
and hw_save_wifi_creds() called from hw_init() and the connect button handler respectively.
Want me to implement Option B?"
I entered "Yes, implement option B".
The modified code built, flashed, and worked perfectly! I now had a working watch!
Still the skeptic, I created a patch (by diffing the new code to the original in the example directory), and looked in detail at the changes.
I was amazed - the changes were clean, and fit the style of the original perfectly.
I have to admit that OpenCode even with its default (free) Big Pickle model was amazing. It took roughly 5 minutes to analyze and code these two fixes, which would likely have taken me two hours to do. Wow!