GamepadCodec is a gamepad data encoding and decoding library based on the Robust Frame protocol, providing two core components: Encoder and Decoder. The Encoder is used to pack gamepad data into reliable frames; the Decoder is used to read frame data from a Stream byte stream and parse out the gamepad state.
π‘ About
Stream:Streamis a base class in the Arduino core library for data stream communication. It defines the reading and parsing functionality in Arduino and provides a unified interface for all serial communications. Common classes likeHardwareSerialandSoftwareSerialinherit fromStream, andSerialis a predefined instance ofHardwareSerial. For more details onStream, see the Arduino Stream official documentation.
When transmitting gamepad data over serial or Bluetooth, the receiving end needs to accurately identify the start and end of each frame, correctly handle special bytes that may appear in the data, and also be able to determine whether the received data is complete and correct. To meet these requirements, the Robust Frame protocol provides a complete solution: fixed frame headers and footers mark boundaries, escaping mechanisms prevent conflicts between data and frame identifiers, and CRC8 checksums ensure data integrity. This library further encapsulates the encoding and decoding logic for gamepad data. You simply need to set the gamepad state, and the library automatically handles framing and transmission; on the receiving end, the library automatically performs frame identification, validation, and decoding, then returns the parsed gamepad state to you.
For detailed information about the Robust Frame protocol (including frame structure, escaping mechanism, CRC8 checksum, and parser operation), please refer to the Robust Frame Protocol Documentation.
This library is a pure software library and does not depend on specific hardware platforms. It can run on any platform that supports Arduino Stream objects.
-
Data Encode: Pack gamepad state into reliable frames, supporting both
ByteBufferreturn and directStreamwriting. -
Data Decode: When reading data from
Stream, the library automatically performs frame synchronization, escape restoration, and CRC validation, then returns the parsed gamepad state to you. -
Button Event Detection: Provides detection for three button events: pressed, released, and holding.
-
Joystick Axis Change Detection: Configurable threshold to filter out minor joystick jitter, reporting only significant axis value changes.
-
Open the Arduino IDE Library Manager
-
Menu: Tools β Manage Libraries...
-
Shortcut:
Ctrl+Shift+I(Windows/Linux) orCmd+Shift+I(Mac)
-
-
Search and Install
-
Enter
GamepadCodecin the search box -
Find the GamepadCodec library
-
Make sure to select the latest version from the dropdown
-
Click the INSTALL button
π Note: Screenshots are for reference only. Please always install the latest available version.
-
-
Install Dependencies
- When the dependency installation dialog appears, select INSTALL ALL
β οΈ Important Version Notes
Screenshots in this document may show older versions. Always install the latest versions of the following libraries:
GamepadCodeclibraryByteBufferdependencyGamepadInputdependencyRobustFramedependencyIf you skipped dependency installation, manually install the latest versions of
ByteBuffer,GamepadInput, andRobustFrame. For example, to installByteBuffer:
- Open the Library Manager again
- Search for
ByteBuffer- Select the latest version from the dropdown
- Click Install
Examples are organized by use case in the examples/ directory. Each example directory contains a complete Arduino program demonstrating a specific usage of the GamepadCodec library.
Currently, Receiver Examples are provided: demonstrating how to use the Decoder component to receive data from a Stream and parse out the gamepad state.
Demonstrates how to receive data from a Stream, decode it using the Decoder, and detect button events and joystick changes via the Tracker.
Applicable for scenarios where a gamepad is used via a BLE-to-serial module, supporting two connection schemes:
-
Board with integrated BLE-to-serial module
Applicable for development boards with on-board BLE-to-serial modules (e.g., BLE-UNO). The MCU communicates with the Bluetooth chip via serial, working the same way as "development board + external BLE-to-serial module".
Compatible Boards BLE-UNO BLE-NANO π Note: Currently, for CodexPad series gamepads (CodexPad-S10, CodexPad-C10), only the BLE-UNO board is supported. BLE-NANO is not supported at this time.
-
Development board with external BLE-to-serial module
Applicable for development boards without on-board BLE-to-serial modules (e.g., Arduino UNO, Nano), using an external BLE-to-serial module (e.g., NL-16) to connect the gamepad.
Supported External BLE-to-Serial Modules
BLE-to-Serial Module NL-16 (V1.2+) Supported Hardware Platforms
Supported Hardware Platforms Arduino UNO Arduino NANO
Both BLE-UNO and BLE-NANO boards have an integrated Bluetooth chip, no external BLE-to-serial module is required.
-
Description: Connects to CodexPad via Bluetooth Device Address, periodically queries and prints all button states and joystick values.
-
Location: In Arduino IDE, find this example via File β Examples β GamepadCodec β decoder β ble_serial_module β basic_polling.
-
Description: Connects to CodexPad via Bluetooth Device Address, prints button state and joystick value changes as they occur.
-
Location: In Arduino IDE, find this example via File β Examples β GamepadCodec β decoder β ble_serial_module β inputs_detection.
After running the example program, debug information (such as button events and joystick values) is output through the debug serial port. Since the default hardware serial port (D0/D1) is occupied by the Bluetooth module, you cannot view debug information through it. You need to use an additional debug serial port. Follow these steps:
-
Prepare an external serial tool, such as a USB-to-TTL module.
-
Connect the external serial tool to the corresponding debug serial pins according to the wiring instructions below (default pins are 5 and 6).
Using a USB-to-TTL module as an example:
Board Pin USB-to-TTL Module Pin 5 RXD 6 TXD 3.3V 3V3 GND GND π Note: The pin numbers (5, 6) in the table above are the default configuration in the example code. If you change the debug serial pins in your code, use your actual configuration (
kDebugSerialRxPinandkDebugSerialTxPin). -
Upload the example program to the development board.
-
After uploading, power on the development board and run the program.
-
Plug the USB-to-TTL module into your computer's USB port. The system will recognize a new serial port (COM port).
-
Open any serial debug tool.
-
Select the serial port corresponding to the USB-to-TTL module, and set the baud rate to 115200, data bits 8, stop bits 1, parity None.
-
Click "Open Serial Port" to view debug information in real time.
π Notes:
- The debug serial pin numbers (5/6) and baud rate (115200) shown above are the default values in the example code. If you modify them in your code, use your actual configuration.
- After a successful connection, you will see a
Connectedmessage in the debug serial output. When you press buttons or move the joystick, the corresponding state changes will be printed immediately, e.g.,Button Triangle(Y): pressed,L(X:128, Y:0).
The NL-16 BLE-to-serial module needs to be used with a development board.
The NL-16 Bluetooth transparent module must use firmware version v1.2 or later to ensure compatibility with CodexPad gamepads.
-
Description: Connects to CodexPad via Bluetooth Device Address, periodically queries and prints all button states and joystick values.
-
Location: In Arduino IDE, find this example via File β Examples β GamepadCodec β decoder β ble_serial_module β basic_polling.
-
Description: Connects to CodexPad via Bluetooth Device Address, prints button state and joystick value changes as they occur.
-
Location: In Arduino IDE, find this example via File β Examples β GamepadCodec β decoder β ble_serial_module β inputs_detection.
Using Arduino UNO as an example:
Arduino UNO to NL-16 Bluetooth Module Wiring
| Arduino UNO Pin | NLβ16 Pin |
|---|---|
| 5V | +5V |
| GND | GND |
| RX0 | TXD |
| TX0 | RXD |
| RESET | STAT/D-RST |
π Note: The pin numbers (5, 6) in the table above are the default configuration in the example code. If you change the debug serial pins in your code, use your actual configuration (
kDebugSerialRxPinandkDebugSerialTxPin).
After running the example program, debug information (such as button events and joystick values) is output through the debug serial port. Since the default hardware serial port (D0/D1) is occupied by the Bluetooth module, you cannot view debug information through it. You need to use an additional debug serial port. Follow these steps:
-
Prepare an external serial tool, such as a USB-to-TTL module.
-
Connect the external serial tool to the corresponding debug serial pins according to the wiring instructions below (default pins are 5 and 6).
Using a USB-to-TTL module as an example:
Board Pin USB-to-TTL Module Pin 5 RXD 6 TXD 3.3V 3V3 GND GND π Note: The pin numbers (5, 6) in the table above are the default configuration in the example code. If you change the debug serial pins in your code, use your actual configuration (
kDebugSerialRxPinandkDebugSerialTxPin). -
Upload the example program to the development board.
-
After uploading, power on the development board and run the program.
-
Plug the USB-to-TTL module into your computer's USB port. The system will recognize a new serial port (COM port).
-
Open any serial debug tool.
-
Select the serial port corresponding to the USB-to-TTL module, and set the baud rate to 115200, data bits 8, stop bits 1, parity None.
-
Click "Open Serial Port" to view debug information in real time.
π Notes:
- The debug serial pin numbers (5/6) and baud rate (115200) shown above are the default values in the example code. If you modify them in your code, use your actual configuration.
- After a successful connection, you will see a
Connectedmessage in the debug serial output. When you press buttons or move the joystick, the corresponding state changes will be printed immediately, e.g.,Button Triangle(Y): pressed,L(X:128, Y:0).
Full API documentation: https://codexpad.github.io/gamepad_codec_arduino_lib/html/en/index.html
This project is licensed under the MIT License - see the LICENSE file for details.



