Note: This example is using BGScript which is no longer supported, starting from BLE SDK version 2.4.0, and therefore this KBA has been marked as deprecated. A C-based example showing how to detect button presses with GPIO interrupts is available in following article:
(Following is valid only up to BLE SDK 2.3.3.)
This example project demonstrates how to use interrupts to detect button presses. The example is written for BGM111 development kit.
The features of the example project are:
The key points in the implementation are summarized below.
When a short press is detected, the script will call following procedure:
This is placeholder for user code, it can be mapped to any functions required by your application. The button number is given as parameter. The code can be easily extended to support more than two buttons.
When a long press is detected, the script will call procedure:
In this example, sending of notifications is controlled by the long button presses.
Custom GATT service for testing
This example uses very simple but generic custom GATT service for testing purposes. The service has two characteristics:
1) control_byte: length 1 byte, write and read supported
2) status_vector: variable length, up to 20 bytes. Notifications supported.
The control_byte is used to change behavior of the script somehow. In this example, we use it to set the number of notifications that is sent after PB1 long press. (Default is 30 but a client can change the value by writing to control_byte).
The status vector is used to send notifications to client. In this example, only one byte is used. If PB0 is pressed, then value 0x01 is sent. If PB1 is pressed, then ascending values 0,1,2.. etc are sent using notifications, until the given limit (30 by default) is reached.
How to test it?
Program the application into a BGM111 development kit. If you have a terminal program connected you can see useful debug prints that are sent to the virtual COM port.
Connect to the device (it is named "BGM111 ButtonDemo") and enable notifications for the status_vector characteristic. Try pressing PB1 and PB0 on the development kit for short (< 1 sec) or longer duration and see what happens. It is useful to have a terminal program open to see in more detail what is going on in the script.