Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit 8ea83dc

Browse files
author
Janne Kiiskilä
committed
Improved button support
Modify the main.cpp to understand the new "abstract" buttons in mbed OS. Prevoius buttons were applicable only to K64F, but since mbed OS has BUTTON1 and BUTTON2, you can check for those in a board independent manner.
1 parent 4655548 commit 8ea83dc

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,17 +378,17 @@ handle_button_click, new value of counter is 1
378378
1. Flash the application.
379379
2. Verify that the registration succeeded. You should see `Registered object successfully!` printed to the serial port.
380380
3. On mbed Device Connector, go to [My devices > Connected devices](https://connector.mbed.com/#endpoints). Your device should be listed here.
381-
4. Press the **SW2** button on the device a number of times (make a note of how many times you did that).
381+
4. Press the **BUTTON1** button on the device a number of times (make a note of how many times you did that).
382382
5. Go to [Device Connector > API Console](https://connector.mbed.com/#console).
383383
6. Click the **Endpoint directory lookups** drop down menu.
384384
![](/docs/img/ep_lookup.PNG)
385385
7. In the menu, click **GET** next to **Endpoint's resource representation**. Select your _endpoint_ and _resource-path_. For example, the _endpoint_ is the identifier of your endpoint that can be found in the `security.h` file as `MBED_ENDPOINT_NAME`. Select `3200/0/5501`as a resource path and click **TEST API**.
386-
8. The number of times you pressed **SW2** is shown.
387-
9. Press the **SW3** button to unregister from mbed Device Connector. You should see `Unregistered Object Successfully` printed to the serial port and the LED starts blinking. This will also stop your application. Press the **Reset** button to run the program again.
386+
8. The number of times you pressed **BUTTON1** is shown.
387+
9. Press the **BUTTON2** button to unregister from mbed Device Connector. You should see `Unregistered Object Successfully` printed to the serial port and the LED starts blinking. This will also stop your application. Press the **Reset** button to run the program again.
388388
389-
<span class="notes">**Note:** On non-K64F boards, there is no unregistration functionality and button presses are simulated through timer ticks incrementing every 15 seconds.</span>
389+
<span class="notes">**Note:** On boards without BUTTON2 there is no unregistration functionality. Boards without buttons the button presses are simulated through timer ticks incrementing every 15 seconds. Please note the actual printout on the board for the BUTTON1 and BUTTON2 changes a lot - you need map that out from the mbed OS board files.</span>
390390
391-
![SW2 pressed five times, as shown by the API Console](clicks.png)
391+
![BUTTON1 pressed five times, as shown by the API Console](clicks.png)
392392
393393
<span class="tips">**Tip:** If you get an error, for example `Server Response: 410 (Gone)`, clear your browser's cache, log out, and log back in.</span>
394394
@@ -398,14 +398,18 @@ handle_button_click, new value of counter is 1
398398
399399
The application exposes three [resources](https://docs.mbed.com/docs/mbed-device-connector-web-interfaces/en/latest/#the-mbed-device-connector-data-model):
400400
401-
1. `3200/0/5501`. Number of presses of **SW2** (GET).
401+
1. `3200/0/5501`. Number of presses of **BUTTON1** (GET).
402402
2. `3201/0/5850`. Blink function, blinks **LED1** when executed (POST).
403403
3. `3201/0/5853`. Blink pattern, used by the blink function to determine how to blink. In the format of `1000:500:1000:500:1000:500` (PUT).
404404
405405
To learn how to get notifications when resource 1 changes, or how to use resources 2 and 3, read the [mbed Device Connector Quick Start](https://github.com/ARMmbed/mbed-connector-api-node-quickstart).
406406
407407
## Known issues
408408
409+
### mbed OS 5.5
410+
411+
* [UBLOX_EVK_ODIN_W2]: BUTTON1 and BUTTON2 definitions are missing from board file.
412+
409413
### mbed OS 5.4
410414
411415
* [UBLOX_EVK_ODIN_W2]: This example is not compiling with IAR. See [#194](https://github.com/ARMmbed/mbed-os-example-client/issues/194)

main.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,22 @@ struct MbedClientDevice device = {
6666
MbedClient mbed_client(device);
6767

6868

69-
// In case of K64F board , there is button resource available
70-
// to change resource value and unregister
71-
#ifdef TARGET_K64F
69+
// If board has a button_1, use it to update the counter.
70+
#ifdef BUTTON1
7271
// Set up Hardware interrupt button.
73-
InterruptIn obs_button(SW2);
74-
InterruptIn unreg_button(SW3);
75-
#else
72+
InterruptIn obs_button(BUTTON1);
73+
#else // BUTTON1
7674
//In non K64F boards , set up a timer to simulate updating resource,
7775
// there is no functionality to unregister.
7876
Ticker timer;
7977
#endif
8078

79+
// If the board has a 2nd button - hook the unregistration to that.
80+
#ifdef BUTTON2
81+
InterruptIn unreg_button(BUTTON2);
82+
#endif // BUTTON2
83+
84+
8185
/*
8286
* Arguments for running "blink" in it's own thread.
8387
*/
@@ -242,11 +246,11 @@ class ButtonResource {
242246

243247
// up counter
244248
counter++;
245-
#ifdef TARGET_K64F
249+
#ifdef BUTTON1
246250
printf("handle_button_click, new value of counter is %d\n", counter);
247251
#else
248252
printf("simulate button_click, new value of counter is %d\n", counter);
249-
#endif
253+
#endif // BUTTON1
250254
// serialize the value of counter as a string, and tell connector
251255
char buffer[20];
252256
int size = sprintf(buffer,"%d",counter);
@@ -373,18 +377,20 @@ Add MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY in mbed_app
373377
LedResource led_resource;
374378
BigPayloadResource big_payload_resource;
375379
376-
#ifdef TARGET_K64F
377-
// On press of SW3 button on K64F board, example application
380+
#ifdef BUTTON2
381+
// On press 2nd button (boards that have it), example application
378382
// will call unregister API towards mbed Device Connector
379383
//unreg_button.fall(&mbed_client,&MbedClient::test_unregister);
380384
unreg_button.fall(&unregister);
385+
#endif // BUTTON2
381386
382-
// Observation Button (SW2) press will send update of endpoint resource values to connector
387+
#ifdef BUTTON1
388+
// 1st button - observation button press will send update of endpoint resource values to connector
383389
obs_button.fall(&button_clicked);
384390
#else
385391
// Send update of endpoint resource values to connector every 15 seconds periodically
386392
timer.attach(&button_clicked, 15.0);
387-
#endif
393+
#endif // BUTTON1
388394
389395
// Create endpoint interface to manage register and unregister
390396
mbed_client.create_interface(MBED_SERVER_ADDRESS, network);

0 commit comments

Comments
 (0)