CodexPad Arduino Lib 2.1.9
Loading...
Searching...
No Matches
CodexPad Class Reference

CodexPad main class. More...

#include <codex_pad.h>

Public Types

enum class  TxPower : int8_t {
  kMinus16dBm = -16 , kMinus12dBm = -12 , kMinus8dBm = -8 , kMinus5dBm = -5 ,
  kMinus3dBm = -3 , kMinus1dBm = -1 , k0dBm = 0 , k1dBm = 1 ,
  k2dBm = 2 , k3dBm = 3 , k4dBm = 4 , k5dBm = 5 ,
  k6dBm = 6
}
 
enum class  Button : uint32_t {
  kUp = uint32_t{1} << 0 , kDown = uint32_t{1} << 1 , kLeft = uint32_t{1} << 2 , kRight = uint32_t{1} << 3 ,
  kSquareX = uint32_t{1} << 4 , kTriangleY = uint32_t{1} << 5 , kCrossA = uint32_t{1} << 6 , kCircleB = uint32_t{1} << 7 ,
  kL1 = uint32_t{1} << 8 , kL2 = uint32_t{1} << 9 , kL3 = uint32_t{1} << 10 , kR1 = uint32_t{1} << 11 ,
  kR2 = uint32_t{1} << 12 , kR3 = uint32_t{1} << 13 , kSelect = uint32_t{1} << 14 , kStart = uint32_t{1} << 15 ,
  kHome = uint32_t{1} << 16
}
 
enum class  Axis : size_t { kLeftStickX , kLeftStickY , kRightStickX , kRightStickY }
 

Public Member Functions

 CodexPad ()
 Constructor.
 
 ~CodexPad ()
 Destructor.
 
void Init ()
 Initialize.
 
bool Connect (const std::string &bluetooth_device_address, const uint32_t timeout_ms=5000)
 Connect.
 
bool ScanAndConnect (const uint32_t button_mask)
 Scans for nearby CodexPad devices and automatically connects to a device whose button state matches the specified mask.
 
void Disconnect ()
 Disconnect.
 
void Update ()
 Update, need to be called in Loop.
 
bool is_connected () const
 Is connected.
 
bool set_remote_tx_power (const TxPower power)
 Set transmission power, only effective when connected, immediately effective for current connection, effective for next connection.
 
const std::string & remote_device_name () const
 Get model number of the CodexPad.
 
const std::string & remote_model_number () const
 Get model number of the CodexPad.
 
const std::array< uint8_t, 3 > remote_firmware_version () const
 Get firmware version of the CodexPad.
 
bool pressed (const Button button) const
 check if a button is pressed
 
bool released (const Button button) const
 check if a button is released
 
bool holding (const Button button) const
 check if a button is held
 
bool button_state (const Button button) const
 check if a button is pressed or held
 
uint32_t button_states () const
 Get all button states, return a 32-bit unsigned integer where each bit represents the state of a specific button.
 
uint8_t axis_value (const Axis axis) const
 Get axis value.
 
std::array< uint8_t, kAxisValueNumaxis_values () const
 Get current values of all analog axes.
 
bool HasAxisValueChanged (const Axis axis, const uint8_t threshold) const
 check if an axis value has changed
 
NimBLEClient * ble_client () const
 Get the BLE client object.
 

Static Public Member Functions

static constexpr uint32_t ButtonMask ()
 
template<typename FirstButton, typename... RestButtons>
static constexpr uint32_t ButtonMask (FirstButton first, RestButtons... rest)
 Combines multiple button identifiers into a single 32-bit button mask.
 

Static Public Attributes

static constexpr size_t kAxisValueNum = 4
 Number of axis values.
 
static constexpr uint8_t kAxisCenter = 0x80
 Axis center value.
 

Detailed Description

CodexPad main class.

Examples
basic_polling.ino, inputs_detection.ino, and scan_and_connect.ino.

Definition at line 46 of file codex_pad.h.

Member Enumeration Documentation

◆ Axis

enum class CodexPad::Axis : size_t
strong
Enumerator
kLeftStickX 

Left stick X axis.

kLeftStickY 

Left stick Y axis.

kRightStickX 

Right stick X axis.

kRightStickY 

Right stick Y axis.

Definition at line 402 of file codex_pad.h.

◆ Button

enum class CodexPad::Button : uint32_t
strong
Enumerator
kUp uint32_t{1} << 0 

Up.

kDown uint32_t{1} << 1 

Down.

kLeft uint32_t{1} << 2 

Left.

kRight uint32_t{1} << 3 

Right.

kSquareX uint32_t{1} << 4 

Square or X.

kTriangleY uint32_t{1} << 5 

Triangle or Y.

kCrossA uint32_t{1} << 6 

Cross or A.

kCircleB uint32_t{1} << 7 

Circle or B.

kL1 uint32_t{1} << 8 

L1.

kL2 uint32_t{1} << 9 

L2.

kL3 uint32_t{1} << 10 

L3.

kR1 uint32_t{1} << 11 

R1.

kR2 uint32_t{1} << 12 

R2.

kR3 uint32_t{1} << 13 

R3.

kSelect uint32_t{1} << 14 

Select.

kStart uint32_t{1} << 15 

Start.

kHome uint32_t{1} << 16 

Home.

Definition at line 220 of file codex_pad.h.

◆ TxPower

enum class CodexPad::TxPower : int8_t
strong
Enumerator
kMinus16dBm -16 

-16 dBm

kMinus12dBm -12 

-12 dBm

kMinus8dBm -8 

-8 dBm

kMinus5dBm -5 

-5 dBm

kMinus3dBm -3 

-3 dBm

kMinus1dBm -1 

-1 dBm

k0dBm 

0 dBm

k1dBm 

1 dBm

k2dBm 

2 dBm

k3dBm 

3 dBm

k4dBm 

4 dBm

k5dBm 

5 dBm

k6dBm 

6 dBm

Definition at line 78 of file codex_pad.h.

Constructor & Destructor Documentation

◆ CodexPad()

CodexPad::CodexPad ( )

Constructor.

Definition at line 30 of file codex_pad.cpp.

◆ ~CodexPad()

CodexPad::~CodexPad ( )

Destructor.

Definition at line 32 of file codex_pad.cpp.

Member Function Documentation

◆ axis_value()

uint8_t CodexPad::axis_value ( const Axis axis) const

Get axis value.

Parameters
[in]axisAxis
Returns
Axis value, range 0~255

Definition at line 165 of file codex_pad.cpp.

◆ axis_values()

std::array< uint8_t, CodexPad::kAxisValueNum > CodexPad::axis_values ( ) const

Get current values of all analog axes.

Returns
Array of axis values with length kAxisValueNum (4 elements), where each element represents the position of a specific axis in the range 0-255. The array indices correspond to the Axis enumeration:
  • Index 0: Left stick X axis (kLeftStickX)
  • Index 1: Left stick Y axis (kLeftStickY)
  • Index 2: Right stick X axis (kRightStickX)
  • Index 3: Right stick Y axis (kRightStickY)

Typical center position value is 128 (0x80).

Example:

const auto axes = codex_pad.axis_values();
const uint8_t left_stick_x = axes[static_cast<size_t>(CodexPad::Axis::kLeftStickX)]; // Get the value of the left stick X axis
@ kLeftStickX
Left stick X axis.
Definition codex_pad.h:411

Definition at line 167 of file codex_pad.cpp.

◆ ble_client()

NimBLEClient * CodexPad::ble_client ( ) const
inline

Get the BLE client object.

This function returns a pointer to the internal BLE client object. The caller should not try and release/delete it.

Returns
BLE client object
Examples
basic_polling.ino, inputs_detection.ino, and scan_and_connect.ino.

Definition at line 903 of file codex_pad.h.

◆ button_state()

bool CodexPad::button_state ( const Button button) const

check if a button is pressed or held

Return values
trueif the button is pressed or held
falseif the button is not pressed or held

Definition at line 161 of file codex_pad.cpp.

◆ button_states()

uint32_t CodexPad::button_states ( ) const

Get all button states, return a 32-bit unsigned integer where each bit represents the state of a specific button.

Returns
A 32-bit unsigned integer where each bit represents the state of a specific button The bits are from the least significant bit (LSB, bit 0) to the most significant bit (MSB, bit 31). You can use bit-and (&) operator to check specific button states. Example:
const uint32_t button_states = codex_pad.button_states();
// Up button is pressed
}
uint32_t button_states() const
Get all button states, return a 32-bit unsigned integer where each bit represents the state of a spec...

Definition at line 163 of file codex_pad.cpp.

◆ ButtonMask() [1/2]

static constexpr uint32_t CodexPad::ButtonMask ( )
inlinestaticconstexpr

Definition at line 445 of file codex_pad.h.

◆ ButtonMask() [2/2]

template<typename FirstButton, typename... RestButtons>
static constexpr uint32_t CodexPad::ButtonMask ( FirstButton first,
RestButtons... rest )
inlinestaticconstexpr

Combines multiple button identifiers into a single 32-bit button mask.

This is a convenience function for creating a bitmask that represents a combination of specific buttons (e.g., Start + A). The resulting mask can be used as a parameter for methods like ScanAndConnect, or for checking against the combined state returned by button_states.

Parameters
[in]buttonOne or more button identifiers from the Button enumeration.
Returns
A 32-bit unsigned integer where the bits corresponding to the input Button values are set to 1.
Note
This is a constexpr function, allowing the mask to be computed at compile time. Example - Creating a mask for the Start and Cross/A buttons:
// Create a mask representing the Start and Cross/A buttons held simultaneously.
constexpr auto kStartAMask = CodexPad::ButtonMask(CodexPad::Button::kStart, CodexPad::Button::kCrossA);
// Use the mask to scan and connect to a device holding these buttons.
codex_pad.ScanAndConnect(kStartAMask);
// Alternatively, check the current combined button state against the mask.
if ((codex_pad.button_states() & kStartAMask) == kStartAMask) {
// Both Start and Cross/A are currently pressed.
}
@ kCrossA
Cross or A.
Definition codex_pad.h:289

Definition at line 493 of file codex_pad.h.

◆ Connect()

bool CodexPad::Connect ( const std::string & bluetooth_device_address,
const uint32_t timeout_ms = 5000 )

Connect.

Parameters
[in]bluetooth_device_addressThe Bluetooth device address(BD_ADDR) of the CodexPad, formatted as "XX:XX:XX:XX:XX:XX", X is a number or uppercase letter, colon separated
[in]timeout_msTimeout in milliseconds
Return values
trueif connected successfully
falseif connection failed
Examples
basic_polling.ino, and inputs_detection.ino.

Definition at line 40 of file codex_pad.cpp.

◆ Disconnect()

void CodexPad::Disconnect ( )
inline

Disconnect.

Definition at line 658 of file codex_pad.h.

◆ HasAxisValueChanged()

bool CodexPad::HasAxisValueChanged ( const Axis axis,
const uint8_t threshold ) const

check if an axis value has changed

Parameters
[in]axisAxis
[in]thresholdThreshold
Return values
trueif the axis value has changed
falseif the axis value has not changed

Definition at line 175 of file codex_pad.cpp.

◆ holding()

bool CodexPad::holding ( const Button button) const

check if a button is held

Return values
trueif the button is held
falseif the button is not held

Definition at line 157 of file codex_pad.cpp.

◆ Init()

void CodexPad::Init ( )

Initialize.

Definition at line 34 of file codex_pad.cpp.

◆ is_connected()

bool CodexPad::is_connected ( ) const

Is connected.

Return values
trueif connected
falseif not

Definition at line 125 of file codex_pad.cpp.

◆ pressed()

bool CodexPad::pressed ( const Button button) const

check if a button is pressed

Return values
trueif the button is pressed
falseif the button is not pressed

Definition at line 149 of file codex_pad.cpp.

◆ released()

bool CodexPad::released ( const Button button) const

check if a button is released

Return values
trueif the button is released
falseif the button is not released

Definition at line 153 of file codex_pad.cpp.

◆ remote_device_name()

const std::string & CodexPad::remote_device_name ( ) const
inline

Get model number of the CodexPad.

Returns
Model number of the CodexPad
Examples
basic_polling.ino, inputs_detection.ino, and scan_and_connect.ino.

Definition at line 710 of file codex_pad.h.

◆ remote_firmware_version()

const std::array< uint8_t, 3 > CodexPad::remote_firmware_version ( ) const
inline

Get firmware version of the CodexPad.

Returns
Firmware version of the CodexPad
Examples
basic_polling.ino, inputs_detection.ino, and scan_and_connect.ino.

Definition at line 734 of file codex_pad.h.

◆ remote_model_number()

const std::string & CodexPad::remote_model_number ( ) const
inline

Get model number of the CodexPad.

Returns
Model number of the CodexPad
Examples
basic_polling.ino, inputs_detection.ino, and scan_and_connect.ino.

Definition at line 722 of file codex_pad.h.

◆ ScanAndConnect()

bool CodexPad::ScanAndConnect ( const uint32_t button_mask)

Scans for nearby CodexPad devices and automatically connects to a device whose button state matches the specified mask.

This method actively scans for Bluetooth devices. When it discovers one or more CodexPad devices whose current button states exactly match the provided button_mask, it will attempt to establish a connection. If multiple nearby devices match the button mask, the device with the strongest signal (highest RSSI) will be selected for connection.

Parameters
[in]button_maskA 32-bit button mask used to match the target device's button states. Use the ButtonMask() function to combine multiple Button values.
Return values
trueConnection successful (a device matching the button mask was found and connected).
falseConnection failed (no matching device found or connection attempt failed).
Note
This is a blocking call. It will continuously scan until a matching device is found, a connection is established, or the internal timeout is reached.
Warning
DO NOT include Button::kHome in the button mask. Holding the Home button triggers a device reboot, which will interrupt the connection process. Example:
pad.Init();
// Continuously try to connect to a device where the Start and Cross/A buttons are pressed.
// Optional: Add a small delay or other operations between attempts.
delay(100);
}
// Once the loop exits, connection is successful.
Serial.println("CodexPad connected!");
bool ScanAndConnect(const uint32_t button_mask)
Scans for nearby CodexPad devices and automatically connects to a device whose button state matches t...
Definition codex_pad.cpp:51
CodexPad()
Constructor.
Definition codex_pad.cpp:30
void Init()
Initialize.
Definition codex_pad.cpp:34
Examples
scan_and_connect.ino.

Definition at line 51 of file codex_pad.cpp.

◆ set_remote_tx_power()

bool CodexPad::set_remote_tx_power ( const TxPower power)

Set transmission power, only effective when connected, immediately effective for current connection, effective for next connection.

Parameters
[in]powerTransmission power
Return values
trueSuccess
falseFail
Examples
basic_polling.ino, inputs_detection.ino, and scan_and_connect.ino.

Definition at line 127 of file codex_pad.cpp.

◆ Update()

void CodexPad::Update ( )

Update, need to be called in Loop.

Definition at line 104 of file codex_pad.cpp.

Member Data Documentation

◆ kAxisCenter

uint8_t CodexPad::kAxisCenter = 0x80
staticconstexpr

Axis center value.

Definition at line 66 of file codex_pad.h.

◆ kAxisValueNum

size_t CodexPad::kAxisValueNum = 4
staticconstexpr

Number of axis values.

Definition at line 56 of file codex_pad.h.


The documentation for this class was generated from the following files: