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

Commit 4e35701

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. Add instructions how to figure out which button is which one.
1 parent 4655548 commit 4e35701

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

README.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,28 +367,40 @@ Registered object succesfully!
367367
368368
<span class="notes">**Note:** Device name is the endpoint name you will need later on when [testing the application](https://github.com/ARMmbed/mbed-os-example-client#testing-the-application).</span>
369369
370-
When you press the **SW2** button on your board you should see messages about the value changes:
370+
When you press the **BUTTON1** button on your board you should see messages about the value changes:
371371
372372
```
373373
handle_button_click, new value of counter is 1
374374
```
375+
## Which button is BUTTON1 or BUTTON2?
375376
377+
The abstract button definitions are not printed on the boards. The easiest way to find out which button is which is to grep the code. Example below finds the `BUTTON1` for K64F.
378+
379+
```bash
380+
<shell: mbed-os-example-client/> cd mbed-os
381+
<shell: mbed-os-example-client/mbed-os>$ git grep BUTTON1 |grep -i K64F
382+
targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/TARGET_FRDM/PinNames.h: BUTTON1 = SW2,
383+
```
384+
385+
I.e. we can deduce that BUTTON1 is the button labeled SW2 in the actual physical board.
386+
387+
376388
## Testing the application
377389

378390
1. Flash the application.
379391
2. Verify that the registration succeeded. You should see `Registered object successfully!` printed to the serial port.
380392
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).
393+
4. Press the **BUTTON1** button on the device a number of times (make a note of how many times you did that).
382394
5. Go to [Device Connector > API Console](https://connector.mbed.com/#console).
383395
6. Click the **Endpoint directory lookups** drop down menu.
384396
![](/docs/img/ep_lookup.PNG)
385397
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.
398+
8. The number of times you pressed **BUTTON1** is shown.
399+
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.
388400

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>
401+
<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>
390402

391-
![SW2 pressed five times, as shown by the API Console](clicks.png)
403+
![BUTTON1 pressed five times, as shown by the API Console](clicks.png)
392404

393405
<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>
394406

@@ -398,14 +410,18 @@ handle_button_click, new value of counter is 1
398410

399411
The application exposes three [resources](https://docs.mbed.com/docs/mbed-device-connector-web-interfaces/en/latest/#the-mbed-device-connector-data-model):
400412

401-
1. `3200/0/5501`. Number of presses of **SW2** (GET).
413+
1. `3200/0/5501`. Number of presses of **BUTTON1** (GET).
402414
2. `3201/0/5850`. Blink function, blinks **LED1** when executed (POST).
403415
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).
404416

405417
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).
406418

407419
## Known issues
408420

421+
### mbed OS 5.5
422+
423+
* [UBLOX_EVK_ODIN_W2]: BUTTON1 and BUTTON2 definitions are missing from board file.
424+
409425
### mbed OS 5.4
410426

411427
* [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)