- Include Libraries
- Set Up Macro and Global Variables
- Set Up Private Functions
- Add Initialization Functions
- Implement the Main Function
Step 1: Include Libraries
Start by including the necessary libraries for the SDK. See the code block below:C
Step 2: Setup Macro and Global Variables
The code below defines a macro for the module name used in logging/debugging and sets up the global variables that store configuration and session-related data.C
config: Stores vending machine configuration.session_product: Represents a single vending item.session: Stores the session details.menu: Stores menu-related settings.com_id: Represents a communication ID.
Step 3: Set Up Private Functions
Implement helper functions for debugging, handling events, and processing transactions. The following sections provide more details.Debug Output Function
This function determines the output stream for debug information, supporting different platforms:- Linux/X8: Uses
printf(). - STM32 microcontroller: Uses
drv_usart_tx_data().
C
str: String that contains the debug information.
CPU Timer Tick
This function is called periodically (every 5-10ms) by the platform timer.C
p: Pointer to optional parameter (unused in this function).
VMC Link Events Handler
This function handles link events and logs relevant information.C
event_id: Type of link event.data: Pointer to optional parameter (varies based on event type).
__false as no success/failure is determined.
Vend Requests Handler
This function handles the session and the included items. See the code block below:C
Vending Events Handler
This function handles vend events and logs relevant information.C
event_id: Type of vend event.data: Pointer to optional parameter. Varies based on event type.
__falsein most cases.__trueforvmc_vend_event_on_vend_approvedto indicate a successful vend approval.
Step 4: Setup Initialization Functions
After adding the helper functions, you need to implement the functions to initialize the VMC configuration and the Menu Handler. See the sections below for more details.Initialize VMC
This function initializes the VMC configuration and registers the event handlers. See the code below:C
config.always_idle to __true.
Initialize Menu
Before initializing the menu, add the necessary handler functions. See the code below:C
C
Step 5: Implement the Main Function
Once all other parts are implemented, you can add the program’s main entry point, which initializes the link to the peripheral and the menu, then starts an infinite loop that runs the menu. See the code below:C