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

Improved button support #289

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,28 +367,40 @@ Registered object succesfully!

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

When you press the **SW2** button on your board you should see messages about the value changes:
When you press the **BUTTON1** button on your board you should see messages about the value changes:

```
handle_button_click, new value of counter is 1
```
## Which button is BUTTON1 or BUTTON2?

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.

```bash
<shell: mbed-os-example-client/> cd mbed-os
<shell: mbed-os-example-client/mbed-os>$ git grep BUTTON1 |grep -i K64F
targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/TARGET_FRDM/PinNames.h: BUTTON1 = SW2,
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the idea of having to dive into the hidden places of the source code to identify a button, that's not a great UX! However, I I know it's a pain to identify buttons, as boards vendors use different names.
@senthilr have you come across this situation with other examples apps?

I'd suggest pointing at the platform pages to find out where buttons are located:
https://developer.mbed.org/platforms

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one will not work straight out of the box, because the buttons were ENUMs... We need to figure out an alternative solution.


I.e. we can deduce that BUTTON1 is the button labeled SW2 in the actual physical board.


## Testing the application

1. Flash the application.
2. Verify that the registration succeeded. You should see `Registered object successfully!` printed to the serial port.
3. On mbed Device Connector, go to [My devices > Connected devices](https://connector.mbed.com/#endpoints). Your device should be listed here.
4. Press the **SW2** button on the device a number of times (make a note of how many times you did that).
4. Press the **BUTTON1** button on the device a number of times (make a note of how many times you did that).
5. Go to [Device Connector > API Console](https://connector.mbed.com/#console).
6. Click the **Endpoint directory lookups** drop down menu.
![](/docs/img/ep_lookup.PNG)
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**.
8. The number of times you pressed **SW2** is shown.
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.
8. The number of times you pressed **BUTTON1** is shown.
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.

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

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

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

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

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

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

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

## Known issues

### mbed OS 5.5

* [UBLOX_EVK_ODIN_W2]: BUTTON1 and BUTTON2 definitions are missing from board file.

### mbed OS 5.4

* [UBLOX_EVK_ODIN_W2]: This example is not compiling with IAR. See [#194](https://github.com/ARMmbed/mbed-os-example-client/issues/194)
Expand Down
30 changes: 18 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,22 @@ struct MbedClientDevice device = {
MbedClient mbed_client(device);


// In case of K64F board , there is button resource available
// to change resource value and unregister
#ifdef TARGET_K64F
// If board has a button_1, use it to update the counter.
#ifdef BUTTON1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the BUTTON1 and BUTTON2 identifiers are actually enum values (see PinNames.h:208 for TARGET_K64F) , so the preprocessor has no knowledge of them. Maybe they should be behind a macro where we could collect other standard definitions like the buttons?

Copy link
Contributor Author

@JanneKiiskila JanneKiiskila Aug 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One solution - perhaps we can add the button resource run-time, IF the button is available AND always add a timer resource?

Or alternatively - do the buttons.h -header that defines the #ifdefds per board.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some boards seem to define buttons that do not exist, see: https://github.com/ARMmbed/mbed-os/blob/master/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/TARGET_NUCLEO_F303K8/PinNames.h#L106

The same enum contains a "not connected" value too. If we could rely on some enum value indicating that a button is not available (like the example), we could check that at runtime. But then there's still the issue of missing enum names that cause compile errors.

// Set up Hardware interrupt button.
InterruptIn obs_button(SW2);
InterruptIn unreg_button(SW3);
#else
InterruptIn obs_button(BUTTON1);
#else // BUTTON1
//In non K64F boards , set up a timer to simulate updating resource,
// there is no functionality to unregister.
Ticker timer;
#endif

// If the board has a 2nd button - hook the unregistration to that.
#ifdef BUTTON2
InterruptIn unreg_button(BUTTON2);
#endif // BUTTON2


/*
* Arguments for running "blink" in it's own thread.
*/
Expand Down Expand Up @@ -242,11 +246,11 @@ class ButtonResource {

// up counter
counter++;
#ifdef TARGET_K64F
#ifdef BUTTON1
printf("handle_button_click, new value of counter is %d\n", counter);
#else
printf("simulate button_click, new value of counter is %d\n", counter);
#endif
#endif // BUTTON1
// serialize the value of counter as a string, and tell connector
char buffer[20];
int size = sprintf(buffer,"%d",counter);
Expand Down Expand Up @@ -373,18 +377,20 @@ Add MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY in mbed_app
LedResource led_resource;
BigPayloadResource big_payload_resource;

#ifdef TARGET_K64F
// On press of SW3 button on K64F board, example application
#ifdef BUTTON2
// On press 2nd button (boards that have it), example application
// will call unregister API towards mbed Device Connector
//unreg_button.fall(&mbed_client,&MbedClient::test_unregister);
unreg_button.fall(&unregister);
#endif // BUTTON2

// Observation Button (SW2) press will send update of endpoint resource values to connector
#ifdef BUTTON1
// 1st button - observation button press will send update of endpoint resource values to connector
obs_button.fall(&button_clicked);
#else
// Send update of endpoint resource values to connector every 15 seconds periodically
timer.attach(&button_clicked, 15.0);
#endif
#endif // BUTTON1

// Create endpoint interface to manage register and unregister
mbed_client.create_interface(MBED_SERVER_ADDRESS, network);
Expand Down