This is the old version, it worked OK enough, but was clearly nothing more than a vague prototype to check the idea worked at all.
This is the new version. As you can see, it’s a real PCB with properly placed components. I found it over on PCBWay as a project and as is the way with that place, I now have four spare boards that will forever sit on a shelf doing nothing.
After building the board I found some ribbon cable that was exactly the right length and contained exactly the right connectors, so the end result looks pretty tidy.
My aim is to build a proper case for my Agon and incorporate the joystick interface into the design. Something like the Console 8 but with a design more like an actual console or computer, and less like a set top box or wifi router.
Programming the interface was unusually confusing. The Agondev C/C++ toolchain now contains a convenient set of routines for reading the GPIO pins relevant to the joystick, but then it’s up to you to check the button presses.
uint16_t getJoystickButtons(void); // Get state for all joy1/joy2 buttons
// BIT JOYSTICK BUTTON
// 0 2 Up
// 1 1 Up
// 2 2 Down
// 3 1 Down
// 4 2 Left
// 5 1 Left
// 6 2 Right
// 7 1 Right
// 8 - -
// 9 - -
// 10 - -
// 11 - -
// 12 2 Button 1
// 13 1 Button 1
// 14 2 Button 2
// 15 1 Button 2
The trick to doing this properly is to have a “previous frame” and “current frame” state of the inputs, and at the start of each next frame, copy the current to the previous before reading the current state again. This then allows simple logic to determine if a button is being pressed, held or released by comparing the state across this frame and the previous one.
It’s the same as what I did with the keyboard, except the keyboard routines give you 16 8 bit ints as bit patterns to check, whereas the joystick gives you one 16 bit int.
#define BIT_CHECK(b,bit_pos) ((b) & (1<<(bit_pos)))
#define IS_KEY_HELD(keycode) (BIT_CHECK(current_keystates[(keycode) / 8], (keycode) % 8))
#define WAS_KEY_HELD(keycode) (BIT_CHECK(previous_keystates[(keycode) / 8], (keycode) % 8))
#define IS_KEY_PRESSED(keycode) (IS_KEY_HELD(keycode) && !WAS_KEY_HELD(keycode))
#define IS_KEY_RELEASED(keycode) (!IS_KEY_HELD(keycode) && WAS_KEY_HELD(keycode))
It was a good little weekend project. I got to build something physical, then plug it into a device and program it. The hardware worked first time, but the software didn’t. So it involved a bit of debugging to keep me interested.