Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: simple-mbed-client
Fork of simple-mbed-client-example by
main.cpp@14:ddc258abaaac, 2016-05-17 (annotated)
- Committer:
- janjongboom
- Date:
- Tue May 17 00:15:40 2016 +0000
- Revision:
- 14:ddc258abaaac
- Parent:
- 13:d4da5e6aa952
- Child:
- 16:71ec73ea9b6d
Use semaphore in interrupt context, and up value in main thread.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| geky | 4:dcd0494556be | 1 | /* |
| geky | 4:dcd0494556be | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
| geky | 4:dcd0494556be | 3 | * SPDX-License-Identifier: Apache-2.0 |
| geky | 4:dcd0494556be | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| geky | 4:dcd0494556be | 5 | * not use this file except in compliance with the License. |
| geky | 4:dcd0494556be | 6 | * You may obtain a copy of the License at |
| geky | 4:dcd0494556be | 7 | * |
| geky | 4:dcd0494556be | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| geky | 4:dcd0494556be | 9 | * |
| geky | 4:dcd0494556be | 10 | * Unless required by applicable law or agreed to in writing, software |
| geky | 4:dcd0494556be | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| geky | 4:dcd0494556be | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| geky | 4:dcd0494556be | 13 | * See the License for the specific language governing permissions and |
| geky | 4:dcd0494556be | 14 | * limitations under the License. |
| geky | 4:dcd0494556be | 15 | */ |
| geky | 4:dcd0494556be | 16 | #include "mbed.h" |
| janjongboom | 13:d4da5e6aa952 | 17 | #include "security.h" // security file from connector.mbed.com |
| janjongboom | 13:d4da5e6aa952 | 18 | #include "LWIPInterface.h" // using Ethernet for connectivity |
| janjongboom | 13:d4da5e6aa952 | 19 | #include "simple-mbed-client.h" // simple-mbed-client |
| geky | 4:dcd0494556be | 20 | |
| janjongboom | 13:d4da5e6aa952 | 21 | // So these lines are unique to connecting etc... but then it's just a normal program |
| geky | 4:dcd0494556be | 22 | |
| janjongboom | 13:d4da5e6aa952 | 23 | Serial pc(USBTX, USBRX); // talk back to the computer |
| janjongboom | 13:d4da5e6aa952 | 24 | LWIPInterface lwip; // define the Ethernet interface |
| janjongboom | 13:d4da5e6aa952 | 25 | SimpleMbedClient client; |
| geky | 4:dcd0494556be | 26 | |
| janjongboom | 13:d4da5e6aa952 | 27 | // Declare some peripherals here |
| janjongboom | 13:d4da5e6aa952 | 28 | DigitalOut led(LED_RED); |
| janjongboom | 13:d4da5e6aa952 | 29 | DigitalOut registeredLed(LED_BLUE, 1); |
| janjongboom | 13:d4da5e6aa952 | 30 | InterruptIn btn(SW3); |
| geky | 4:dcd0494556be | 31 | |
| janjongboom | 14:ddc258abaaac | 32 | // We need a semaphore because we cannot update value in interrupt context |
| janjongboom | 14:ddc258abaaac | 33 | Semaphore updates(0); |
| janjongboom | 14:ddc258abaaac | 34 | |
| janjongboom | 13:d4da5e6aa952 | 35 | // play function, invoked thru mbed Client |
| janjongboom | 13:d4da5e6aa952 | 36 | void play(void* args) { |
| janjongboom | 14:ddc258abaaac | 37 | printf("I'm gonna play a song!\n"); |
| janjongboom | 13:d4da5e6aa952 | 38 | } |
| geky | 0:9ad9d701b1c3 | 39 | |
| janjongboom | 13:d4da5e6aa952 | 40 | // patternUpdated is called when the value of led/0/pattern changes |
| janjongboom | 13:d4da5e6aa952 | 41 | void patternUpdated(string newPattern) { |
| janjongboom | 14:ddc258abaaac | 42 | printf("Got a new pattern... %s\n", newPattern.c_str()); |
| janjongboom | 13:d4da5e6aa952 | 43 | } |
| geky | 4:dcd0494556be | 44 | |
| janjongboom | 13:d4da5e6aa952 | 45 | // Here we define some mbed Client resources |
| janjongboom | 13:d4da5e6aa952 | 46 | SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED, true); |
| janjongboom | 13:d4da5e6aa952 | 47 | // need a callback when a PUT request comes in? Just pass in a callback |
| janjongboom | 13:d4da5e6aa952 | 48 | SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500", &patternUpdated); |
| geky | 4:dcd0494556be | 49 | |
| janjongboom | 13:d4da5e6aa952 | 50 | // Status callbacks if you're interested in that |
| janjongboom | 13:d4da5e6aa952 | 51 | void registered() { |
| janjongboom | 14:ddc258abaaac | 52 | printf("registered\n"); |
| janjongboom | 13:d4da5e6aa952 | 53 | registeredLed = 0; |
| janjongboom | 13:d4da5e6aa952 | 54 | } |
| janjongboom | 13:d4da5e6aa952 | 55 | void unregistered() { |
| janjongboom | 14:ddc258abaaac | 56 | printf("unregistered\n"); |
| janjongboom | 13:d4da5e6aa952 | 57 | registeredLed = 1; |
| geky | 4:dcd0494556be | 58 | } |
| geky | 4:dcd0494556be | 59 | |
| janjongboom | 14:ddc258abaaac | 60 | // we cannot do blocking stuff here, or device will crash |
| janjongboom | 14:ddc258abaaac | 61 | // release semaphore and update btn_count in the while() loop... |
| janjongboom | 13:d4da5e6aa952 | 62 | void fall() { |
| janjongboom | 14:ddc258abaaac | 63 | updates.release(); |
| janjongboom | 13:d4da5e6aa952 | 64 | } |
| janjongboom | 13:d4da5e6aa952 | 65 | |
| janjongboom | 13:d4da5e6aa952 | 66 | // normal function |
| janjongboom | 13:d4da5e6aa952 | 67 | void toggleLed() { |
| janjongboom | 13:d4da5e6aa952 | 68 | led = !led; |
| geky | 4:dcd0494556be | 69 | } |
| geky | 4:dcd0494556be | 70 | |
| geky | 4:dcd0494556be | 71 | int main() { |
| janjongboom | 13:d4da5e6aa952 | 72 | pc.baud(115200); |
| janjongboom | 13:d4da5e6aa952 | 73 | |
| janjongboom | 14:ddc258abaaac | 74 | printf("Hello world\n"); |
| janjongboom | 14:ddc258abaaac | 75 | |
| janjongboom | 13:d4da5e6aa952 | 76 | Ticker t; |
| janjongboom | 13:d4da5e6aa952 | 77 | t.attach(&toggleLed, 1.0f); |
| geky | 4:dcd0494556be | 78 | |
| janjongboom | 13:d4da5e6aa952 | 79 | btn.fall(&fall); |
| janjongboom | 13:d4da5e6aa952 | 80 | |
| janjongboom | 13:d4da5e6aa952 | 81 | // I want C++11 lambdas! |
| janjongboom | 13:d4da5e6aa952 | 82 | // here we define functions that should live in the cloud |
| janjongboom | 13:d4da5e6aa952 | 83 | client.define_function("music/0/play", &play); |
| geky | 4:dcd0494556be | 84 | |
| janjongboom | 13:d4da5e6aa952 | 85 | if (lwip.connect() != 0) { // connect to the internet |
| janjongboom | 14:ddc258abaaac | 86 | printf("Connect to eth failed...\n"); |
| janjongboom | 13:d4da5e6aa952 | 87 | return 1; |
| janjongboom | 13:d4da5e6aa952 | 88 | } |
| geky | 4:dcd0494556be | 89 | |
| janjongboom | 14:ddc258abaaac | 90 | printf("IP address %s\r\n", lwip.getIPAddress()); |
| janjongboom | 13:d4da5e6aa952 | 91 | |
| janjongboom | 13:d4da5e6aa952 | 92 | bool setup = client.setup(&lwip); // start mbed client! |
| janjongboom | 13:d4da5e6aa952 | 93 | if (!setup) { |
| janjongboom | 13:d4da5e6aa952 | 94 | printf("Setting up mbed_client failed...\n"); |
| janjongboom | 13:d4da5e6aa952 | 95 | return 1; |
| geky | 4:dcd0494556be | 96 | } |
| janjongboom | 14:ddc258abaaac | 97 | |
| janjongboom | 13:d4da5e6aa952 | 98 | client.on_registered(®istered); |
| janjongboom | 13:d4da5e6aa952 | 99 | client.on_unregistered(&unregistered); |
| janjongboom | 13:d4da5e6aa952 | 100 | |
| janjongboom | 14:ddc258abaaac | 101 | while (1) { |
| janjongboom | 14:ddc258abaaac | 102 | int v = updates.wait(25000); |
| janjongboom | 14:ddc258abaaac | 103 | |
| janjongboom | 14:ddc258abaaac | 104 | if (v == 1) { |
| janjongboom | 14:ddc258abaaac | 105 | btn_count = btn_count + 1; |
| janjongboom | 14:ddc258abaaac | 106 | // printf is a bit inflexible, static_cast to get the right value |
| janjongboom | 14:ddc258abaaac | 107 | printf("btn_count is now %d\n", static_cast<int>(btn_count)); |
| janjongboom | 14:ddc258abaaac | 108 | } |
| janjongboom | 14:ddc258abaaac | 109 | |
| janjongboom | 14:ddc258abaaac | 110 | // call keep_alive every now and then |
| janjongboom | 14:ddc258abaaac | 111 | client.keep_alive(); |
| janjongboom | 14:ddc258abaaac | 112 | } |
| geky | 4:dcd0494556be | 113 | } |
