1. Home
  2. Docs
  3. Code Overview
  4. Using Ports in Hexabitz Modules to Connect Buttons or Switches

Using Ports in Hexabitz Modules to Connect Buttons or Switches

Hexabitz modules support connecting switches to their ports and can detect button presses using dedicated BOS software interrupts that respond to specific event types like Clicked or Released.

 

Key Features

  • Connect any mechanical button/switch to any of the module’s ports.
  • Button configurations save to emulated EEPROM.
  • Automatic initialization on startup.

API Implementation

AddButton() Function

BOS_Status AddButton(uint8_t port, ButtonType_e buttonType, ButtonState_e buttonState);

port: The port number where the button is connected

buttonType: The button type, which can be:

      1. SWITCH_NO: Normally Open switch

      2. SWITCH_NC: Normally Closed switch

buttonState: The specific event to detect. Key interactive states include:

      • CLICKED: Single quick press and release

      • DBL_CLICKED: Two rapid consecutive presses

      • RELEASED: Moment of release after pressing

RemovePortButton() Function

  • Remove buttons and delete their EEPROM definitions using:

BOS_Status RemovePortButton(uint8_t port);

Linking Events to User Callbacks

These events can trigger user callbacks to execute tasks. Add the following to main.c to handle button events:

void buttonClickedCallback(uint8_t port) {
// Your code here
}
void buttonDblClickedCallback(uint8_t port) {
// Your code here
}
void buttonReleasedCallback(uint8_t port) {
// Your code here
}

STM32CubeIDE Implementation Example

/* Private Function Prototypes */
void buttonClickedCallback(uint8_t port) {
IND_ON();
}
void buttonDblClickedCallback(uint8_t port) {
IND_blink(500);
}
void buttonReleasedCallback(uint8_t port) {
IND_OFF();
}
int main(void) {
/* Initialize Module & BitzOS */
Module_Init();
/* Don't place your code here */
for(;;) {
}
}
void UserTask(void *argument) {
AddButton(P3, SWITCH_NO, CLICKED);
AddButton(P4, SWITCH_NO, DBL_CLICKED);
AddButton(P5, SWITCH_NO, RELEASED);
/* put your code here, to run repeatedly. */
while(1) {
}
}

CLI Functionality

Port button/switch functionality is also available through the CLI. Use the if … end if command snippet to link button events to other commands (see “Command Snippets” article for details).

Terminal Execution (MobaXterm):

1️⃣ Add a ‘Normally Open’ button on port P3.
2️⃣ Add a ‘Normally Closed’ button on port P5.
3️⃣ Create a conditional statement for B3 click events:
If button B3 is clicked (single press and release), then execute the ping command.
4️⃣ Remove button B5 from port P5″.

The image below shows the actual execution of these commands in the CLI interface

Terminal screenshot showing button commands execution in Hexabitz CLI

Fig. 1: Terminal execution of button commands (MobaXterm example)

 

How can we help?