BTM library (consecutive reading) DIO input
The BTM library provides a method to determine the state of a button by consecutive reading (determine the change conditional on the value being consecutive).

Initialisation

In the following example, a change is determined when the value is the same for five consecutive times in a judgment every 8ms. The judgment is called from an external interrupt handler (such as TimerTimer).
1
#include "btnMgr.h"
2
3
#define u32PortInputMask ((1UL << 11) | (1UL << 12) | (1UL << 13))
4
static tsBTM_Config sBTM_Config; //!< Button input (state is determined by consecutive reading) management structure
5
static PR_BTM_HANDLER pr_BTM_handler; //!< Event handler for button input (called at the start of TickTimer)
6
7
...
8
// 初期化
9
memset(&sBTM_Config, 0, sizeof(tsBTM_Config));
10
sBTM_Config.bmPortMask = u32PortInputMask;
11
sBTM_Config.u16Tick_ms = 8; // Decision cycle
12
sBTM_Config.u8MaxHistory = 5; // Five times the same value.
13
sBTM_Config.u8DeviceTimer = 0xFF; // Use TickTimer for this purpose.
14
pr_BTM_handler = prBTM_InitExternal(&sBTM_Config);
15
16
// Start BTM processing
17
vBTM_Enable();
Copied!

DIO's consecutive reading judgment

The following example calls pr_BTM_handler in the interrupt handler of TickTimer. It reads the IO status and performs the consecutive judgment. The method to obtain the state when it changes is described later.
Also, the timer cycle is given as an argument of pr_BTM_handler, and each call is calculated internally as if the time had elapsed by this value.
1
PUBLIC uint8 cbToCoNet_u8HwInt(uint32 u32DeviceId, uint32 u32ItemBitmap) {
2
uint8 u8handled = FALSE;
3
4
switch (u32DeviceId) {
5
case E_AHI_DEVICE_TICK_TIMER:
6
// Driving the Button Handler
7
if (pr_BTM_handler) {
8
// Call the handler.
9
(*pr_BTM_handler)(4); // Pass the period [ms] of the TickTimer.
10
}
11
break;
12
}
Copied!

Detection of DIO changes

Execute the bBTM_GetState() function, and if it is TRUE, it detects that there is a change due to consecutive reading.
After calling bBTM_GetState(), the internal state is changed to read. Successive calls will return FALSE (no change) the second time.
The following example shows how to check the change of DIO in the event processing part of the TickTimer event (E_AHI_DEVICE_TICK_TIMER).
1
void cbToCoNet_vHwEvent(uint32 u32DeviceId, uint32 u32ItemBitmap) {
2
case E_AHI_DEVICE_TICK_TIMER: //Relatively frequent processing
3
// Determine the button.
4
_C {
5
uint32 bmPorts, bmChanged, i;
6
if (bBTM_GetState(&bmPorts, &bmChanged)) {
7
// bmPorts shows the pin value.
8
// bmChanged shows the changing pin
9
10
if (bmChanged & (1UL << 11)) {
11
if (bmPorts & (1UL << 11)) {
12
// When there is a change in DIO11 and it is judged to be Lo.
13
// For example, make a wireless transmission request here.
14
}
15
}
16
}
17
}
18
break;
19
Copied!
最終更新 8mo ago