This is a mbed Client sample where ZXing is incorporated, and works on GR-PEACH and GR-LYCHEE.
Dependencies: DisplayApp AsciiFont
Overview
This sample program shows how to use mbed Client together with ZXing which is an open-source, multi-format 1D/2D barcode image processing library. For more info on ZXing, please refer to https://github.com/zxing/zxing.
Required hardware
- GR-PEACH ( https://developer.mbed.org/platforms/Renesas-GR-PEACH/ )
- Audio Camera Shield ( https://developer.mbed.org/teams/Renesas/wiki/Audio_Camera-shield )
- LCD Shield ( https://developer.mbed.org/teams/Renesas/wiki/LCD-shield )
- Ethernet cable and connection to the internet
- Wireless Camera shieled (This is GR-PEACH' Wifi(ESP32) optional.)
- GR-LYCHEE ( https://developer.mbed.org/platforms/Renesas-GR-LYCHEE/ )
Application setup
- Select the connection type. For details, please refer to the following wiki:
https://os.mbed.com/teams/Renesas/code/GR-PEACH_mbed-os-client-ZXingSample/wiki/Connection-type. - Set the client credentials. For details, please refer to the following wiki:
https://os.mbed.com/teams/Renesas/code/GR-PEACH_mbed-os-client-ZXingSample/wiki/Client-credentials. - Change Ethernet settings. For details, please refer to the following wiki:
https://developer.mbed.org/teams/Renesas/code/GR-PEACH_mbed-os-client-ZXingSample/wiki/Ethernet-settings. - Change Wifi settings. For details, please refer to the following wiki:
https://os.mbed.com/teams/Renesas/code/GR-PEACH_mbed-os-client-ZXingSample/wiki/Wifi-settings. - Set up an IP address. (This step is optional.) For details, please refer to the following wiki:
https://os.mbed.com/teams/Renesas/code/GR-PEACH_mbed-os-client-ZXingSample/wiki/IP-address-setup.
Building the example
To build this example:
- Import this example onto mbed Compiler.
- Configure the example in accordance with Application setup.
- Compile the example on mbed Compiler and download the resultant binary file.
- Plug the Ethernet cable into GR-PEACH or GR-LYCHEE if you are using Ethernet mode.
- Plug the micro-USB cable into the OpenSDA port which lies on the next to the RESET button.
- Copy the binary previously downloaded to your PC to GR-PEACH or GR-LYCHEE to flash this example. When the copy is successfully completed, the board is ready to work.
- Press the RESET button on the board to run the example.
- For verification, please refer to the following wiki:
https://developer.mbed.org/teams/Renesas/code/GR-PEACH_mbed-os-client-ZXingSample/wiki/Monitoring-the-application.
Application resources
This example exposes four resources listed below:
- 3202/0/5700. Decode result of barcode data input from camera (GET).
- 3201/0/5850. Blink function, blinks LED when executed (POST).
- 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).
- 3201/0/5855. Blink color, used by the blink function. Any of red, green, blue, cyan, yellow and magenta is acceptable if you are using GR-PEACH board (PUT).
- 3201/0/5855. Blink color, used by the blink function. Any of green, yellow, orange and red is acceptable if you are using GR-LYCHEE board (PUT).
For more info on how to get notifications when resource 1 changes, or how to use resource 2, 3 and 4, please look at
Import programGR-PEACH_mbed-connector-ZXingSample-node
Node.js based Web Application for mbed Device Connector specific to GR-PEACH_mbed-os-client-ZXingSample
main.cpp@0:eb73febb2bba, 2016-10-06 (annotated)
- Committer:
- <>
- Date:
- Thu Oct 06 18:00:30 2016 +0900
- Revision:
- 0:eb73febb2bba
- Child:
- 2:6ec5c1c1d41c
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 0:eb73febb2bba | 1 | /* |
<> | 0:eb73febb2bba | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
<> | 0:eb73febb2bba | 3 | * SPDX-License-Identifier: Apache-2.0 |
<> | 0:eb73febb2bba | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
<> | 0:eb73febb2bba | 5 | * not use this file except in compliance with the License. |
<> | 0:eb73febb2bba | 6 | * You may obtain a copy of the License at |
<> | 0:eb73febb2bba | 7 | * |
<> | 0:eb73febb2bba | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 0:eb73febb2bba | 9 | * |
<> | 0:eb73febb2bba | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 0:eb73febb2bba | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
<> | 0:eb73febb2bba | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 0:eb73febb2bba | 13 | * See the License for the specific language governing permissions and |
<> | 0:eb73febb2bba | 14 | * limitations under the License. |
<> | 0:eb73febb2bba | 15 | */ |
<> | 0:eb73febb2bba | 16 | #include "simpleclient.h" |
<> | 0:eb73febb2bba | 17 | #include <string> |
<> | 0:eb73febb2bba | 18 | #include <sstream> |
<> | 0:eb73febb2bba | 19 | #include <vector> |
<> | 0:eb73febb2bba | 20 | #include "mbed-trace/mbed_trace.h" |
<> | 0:eb73febb2bba | 21 | |
<> | 0:eb73febb2bba | 22 | #include "security.h" |
<> | 0:eb73febb2bba | 23 | |
<> | 0:eb73febb2bba | 24 | #include "mbed.h" |
<> | 0:eb73febb2bba | 25 | #include "rtos.h" |
<> | 0:eb73febb2bba | 26 | |
<> | 0:eb73febb2bba | 27 | #define ETHERNET 1 |
<> | 0:eb73febb2bba | 28 | #define WIFI 2 |
<> | 0:eb73febb2bba | 29 | #define MESH_LOWPAN_ND 3 |
<> | 0:eb73febb2bba | 30 | #define MESH_THREAD 4 |
<> | 0:eb73febb2bba | 31 | |
<> | 0:eb73febb2bba | 32 | #define STRINGIFY(s) #s |
<> | 0:eb73febb2bba | 33 | |
<> | 0:eb73febb2bba | 34 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
<> | 0:eb73febb2bba | 35 | #if(1) //bp3595 |
<> | 0:eb73febb2bba | 36 | #include "LWIPBP3595Interface.h" |
<> | 0:eb73febb2bba | 37 | LWIPBP3595Interface esp; |
<> | 0:eb73febb2bba | 38 | DigitalOut usb1en(P3_8); |
<> | 0:eb73febb2bba | 39 | #else |
<> | 0:eb73febb2bba | 40 | #include "ESP8266Interface.h" |
<> | 0:eb73febb2bba | 41 | ESP8266Interface esp(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
<> | 0:eb73febb2bba | 42 | #endif |
<> | 0:eb73febb2bba | 43 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
<> | 0:eb73febb2bba | 44 | #include "EthernetInterface.h" |
<> | 0:eb73febb2bba | 45 | EthernetInterface eth; |
<> | 0:eb73febb2bba | 46 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND |
<> | 0:eb73febb2bba | 47 | #define MESH |
<> | 0:eb73febb2bba | 48 | #include "NanostackInterface.h" |
<> | 0:eb73febb2bba | 49 | LoWPANNDInterface mesh; |
<> | 0:eb73febb2bba | 50 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
<> | 0:eb73febb2bba | 51 | #define MESH |
<> | 0:eb73febb2bba | 52 | #include "NanostackInterface.h" |
<> | 0:eb73febb2bba | 53 | ThreadInterface mesh; |
<> | 0:eb73febb2bba | 54 | #endif |
<> | 0:eb73febb2bba | 55 | |
<> | 0:eb73febb2bba | 56 | #ifndef MESH |
<> | 0:eb73febb2bba | 57 | // This is address to mbed Device Connector |
<> | 0:eb73febb2bba | 58 | #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684" |
<> | 0:eb73febb2bba | 59 | #else |
<> | 0:eb73febb2bba | 60 | // This is address to mbed Device Connector |
<> | 0:eb73febb2bba | 61 | #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684" |
<> | 0:eb73febb2bba | 62 | #endif |
<> | 0:eb73febb2bba | 63 | |
<> | 0:eb73febb2bba | 64 | Serial output(USBTX, USBRX); |
<> | 0:eb73febb2bba | 65 | |
<> | 0:eb73febb2bba | 66 | // These are example resource values for the Device Object |
<> | 0:eb73febb2bba | 67 | struct MbedClientDevice device = { |
<> | 0:eb73febb2bba | 68 | "Manufacturer_String", // Manufacturer |
<> | 0:eb73febb2bba | 69 | "Type_String", // Type |
<> | 0:eb73febb2bba | 70 | "ModelNumber_String", // ModelNumber |
<> | 0:eb73febb2bba | 71 | "SerialNumber_String" // SerialNumber |
<> | 0:eb73febb2bba | 72 | }; |
<> | 0:eb73febb2bba | 73 | |
<> | 0:eb73febb2bba | 74 | // Instantiate the class which implements LWM2M Client API (from simpleclient.h) |
<> | 0:eb73febb2bba | 75 | MbedClient mbed_client(device); |
<> | 0:eb73febb2bba | 76 | |
<> | 0:eb73febb2bba | 77 | |
<> | 0:eb73febb2bba | 78 | #if(1) //for zxing with camera |
<> | 0:eb73febb2bba | 79 | InterruptIn unreg_button(USER_BUTTON0); |
<> | 0:eb73febb2bba | 80 | #else |
<> | 0:eb73febb2bba | 81 | // In case of K64F board , there is button resource available |
<> | 0:eb73febb2bba | 82 | // to change resource value and unregister |
<> | 0:eb73febb2bba | 83 | #ifdef TARGET_K64F |
<> | 0:eb73febb2bba | 84 | // Set up Hardware interrupt button. |
<> | 0:eb73febb2bba | 85 | InterruptIn obs_button(SW2); |
<> | 0:eb73febb2bba | 86 | InterruptIn unreg_button(SW3); |
<> | 0:eb73febb2bba | 87 | #else |
<> | 0:eb73febb2bba | 88 | //In non K64F boards , set up a timer to simulate updating resource, |
<> | 0:eb73febb2bba | 89 | // there is no functionality to unregister. |
<> | 0:eb73febb2bba | 90 | Ticker timer; |
<> | 0:eb73febb2bba | 91 | #endif |
<> | 0:eb73febb2bba | 92 | #endif |
<> | 0:eb73febb2bba | 93 | |
<> | 0:eb73febb2bba | 94 | // LED Output |
<> | 0:eb73febb2bba | 95 | DigitalOut led1(LED1); |
<> | 0:eb73febb2bba | 96 | DigitalOut led2(LED2); |
<> | 0:eb73febb2bba | 97 | DigitalOut led3(LED3); |
<> | 0:eb73febb2bba | 98 | |
<> | 0:eb73febb2bba | 99 | /* |
<> | 0:eb73febb2bba | 100 | * The Led contains one property (pattern) and a function (blink). |
<> | 0:eb73febb2bba | 101 | * When the function blink is executed, the pattern is read, and the LED |
<> | 0:eb73febb2bba | 102 | * will blink based on the pattern. |
<> | 0:eb73febb2bba | 103 | */ |
<> | 0:eb73febb2bba | 104 | class LedResource { |
<> | 0:eb73febb2bba | 105 | public: |
<> | 0:eb73febb2bba | 106 | LedResource() { |
<> | 0:eb73febb2bba | 107 | // create ObjectID with metadata tag of '3201', which is 'digital output' |
<> | 0:eb73febb2bba | 108 | led_object = M2MInterfaceFactory::create_object("3201"); |
<> | 0:eb73febb2bba | 109 | M2MObjectInstance* led_inst = led_object->create_object_instance(); |
<> | 0:eb73febb2bba | 110 | |
<> | 0:eb73febb2bba | 111 | // 5855 = Multi-state output |
<> | 0:eb73febb2bba | 112 | M2MResource* color_res = led_inst->create_dynamic_resource("5855", "Color", |
<> | 0:eb73febb2bba | 113 | M2MResourceInstance::STRING, false); |
<> | 0:eb73febb2bba | 114 | // read and write |
<> | 0:eb73febb2bba | 115 | color_res->set_operation(M2MBase::GET_PUT_ALLOWED); |
<> | 0:eb73febb2bba | 116 | // set red as initial color |
<> | 0:eb73febb2bba | 117 | color_res->set_value((const uint8_t*)"red", 3); |
<> | 0:eb73febb2bba | 118 | |
<> | 0:eb73febb2bba | 119 | // 5853 = Multi-state output |
<> | 0:eb73febb2bba | 120 | M2MResource* pattern_res = led_inst->create_dynamic_resource("5853", "Pattern", |
<> | 0:eb73febb2bba | 121 | M2MResourceInstance::STRING, false); |
<> | 0:eb73febb2bba | 122 | // read and write |
<> | 0:eb73febb2bba | 123 | pattern_res->set_operation(M2MBase::GET_PUT_ALLOWED); |
<> | 0:eb73febb2bba | 124 | // set initial pattern (toggle every 200ms. 7 toggles in total) |
<> | 0:eb73febb2bba | 125 | pattern_res->set_value((const uint8_t*)"500:500:500:500:500:500:500", 27); |
<> | 0:eb73febb2bba | 126 | |
<> | 0:eb73febb2bba | 127 | // there's not really an execute LWM2M ID that matches... hmm... |
<> | 0:eb73febb2bba | 128 | M2MResource* led_res = led_inst->create_dynamic_resource("5850", "Blink", |
<> | 0:eb73febb2bba | 129 | M2MResourceInstance::OPAQUE, false); |
<> | 0:eb73febb2bba | 130 | // we allow executing a function here... |
<> | 0:eb73febb2bba | 131 | led_res->set_operation(M2MBase::POST_ALLOWED); |
<> | 0:eb73febb2bba | 132 | // when a POST comes in, we want to execute the led_execute_callback |
<> | 0:eb73febb2bba | 133 | led_res->set_execute_function(execute_callback(this, &LedResource::blink)); |
<> | 0:eb73febb2bba | 134 | } |
<> | 0:eb73febb2bba | 135 | |
<> | 0:eb73febb2bba | 136 | M2MObject* get_object() { |
<> | 0:eb73febb2bba | 137 | return led_object; |
<> | 0:eb73febb2bba | 138 | } |
<> | 0:eb73febb2bba | 139 | |
<> | 0:eb73febb2bba | 140 | void blink(void *) { |
<> | 0:eb73febb2bba | 141 | // read the value of 'Pattern' |
<> | 0:eb73febb2bba | 142 | M2MObjectInstance* inst = led_object->object_instance(); |
<> | 0:eb73febb2bba | 143 | M2MResource* res = inst->resource("5853"); |
<> | 0:eb73febb2bba | 144 | // read the value of 'Color' |
<> | 0:eb73febb2bba | 145 | M2MObjectInstance* instC = led_object->object_instance(); |
<> | 0:eb73febb2bba | 146 | M2MResource* resC = instC->resource("5855"); |
<> | 0:eb73febb2bba | 147 | |
<> | 0:eb73febb2bba | 148 | // values in mbed Client are all buffers, and we need a vector of int's |
<> | 0:eb73febb2bba | 149 | uint8_t* buffIn = NULL; |
<> | 0:eb73febb2bba | 150 | uint32_t sizeIn; |
<> | 0:eb73febb2bba | 151 | res->get_value(buffIn, sizeIn); |
<> | 0:eb73febb2bba | 152 | |
<> | 0:eb73febb2bba | 153 | uint8_t* cbuffIn = NULL; |
<> | 0:eb73febb2bba | 154 | uint32_t csizeIn; |
<> | 0:eb73febb2bba | 155 | resC->get_value(cbuffIn, csizeIn); |
<> | 0:eb73febb2bba | 156 | |
<> | 0:eb73febb2bba | 157 | // turn the buffer into a string, and initialize a vector<int> on the heap |
<> | 0:eb73febb2bba | 158 | std::string s((char*)buffIn, sizeIn); |
<> | 0:eb73febb2bba | 159 | std::vector<uint32_t>* v = new std::vector<uint32_t>; |
<> | 0:eb73febb2bba | 160 | |
<> | 0:eb73febb2bba | 161 | output.printf("led_execute_callback pattern=%s\r\n", s.c_str()); |
<> | 0:eb73febb2bba | 162 | |
<> | 0:eb73febb2bba | 163 | // our pattern is something like 500:200:500, so parse that |
<> | 0:eb73febb2bba | 164 | std::size_t found = s.find_first_of(":"); |
<> | 0:eb73febb2bba | 165 | while (found!=std::string::npos) { |
<> | 0:eb73febb2bba | 166 | |
<> | 0:eb73febb2bba | 167 | v->push_back(atoi((const char*)s.substr(0,found).c_str())); |
<> | 0:eb73febb2bba | 168 | s = s.substr(found+1); |
<> | 0:eb73febb2bba | 169 | found=s.find_first_of(":"); |
<> | 0:eb73febb2bba | 170 | if(found == std::string::npos) { |
<> | 0:eb73febb2bba | 171 | v->push_back(atoi((const char*)s.c_str())); |
<> | 0:eb73febb2bba | 172 | } |
<> | 0:eb73febb2bba | 173 | } |
<> | 0:eb73febb2bba | 174 | |
<> | 0:eb73febb2bba | 175 | |
<> | 0:eb73febb2bba | 176 | // do_blink is called with the vector, and starting at -1 |
<> | 0:eb73febb2bba | 177 | do_blink(v, cbuffIn, 0); |
<> | 0:eb73febb2bba | 178 | } |
<> | 0:eb73febb2bba | 179 | |
<> | 0:eb73febb2bba | 180 | private: |
<> | 0:eb73febb2bba | 181 | M2MObject* led_object; |
<> | 0:eb73febb2bba | 182 | |
<> | 0:eb73febb2bba | 183 | void do_blink(std::vector<uint32_t>* pattern, uint8_t* color, uint16_t position) { |
<> | 0:eb73febb2bba | 184 | |
<> | 0:eb73febb2bba | 185 | if (!strcmp((char *)color, "red")) { |
<> | 0:eb73febb2bba | 186 | // blink the LED in red color |
<> | 0:eb73febb2bba | 187 | led1 = !led1; |
<> | 0:eb73febb2bba | 188 | } |
<> | 0:eb73febb2bba | 189 | else if (!strcmp((char *)color, "green")) { |
<> | 0:eb73febb2bba | 190 | // blink in green color |
<> | 0:eb73febb2bba | 191 | led2 = !led2; |
<> | 0:eb73febb2bba | 192 | } |
<> | 0:eb73febb2bba | 193 | else if (!strcmp((char *)color, "blue")) { |
<> | 0:eb73febb2bba | 194 | // blink in blue color |
<> | 0:eb73febb2bba | 195 | led3 = !led3; |
<> | 0:eb73febb2bba | 196 | } |
<> | 0:eb73febb2bba | 197 | else if (!strcmp((char *)color, "cyan")) { |
<> | 0:eb73febb2bba | 198 | // blink in cyan color |
<> | 0:eb73febb2bba | 199 | led2 = !led2; |
<> | 0:eb73febb2bba | 200 | led3 = !led3; |
<> | 0:eb73febb2bba | 201 | } |
<> | 0:eb73febb2bba | 202 | else if (!strcmp((char *)color, "yellow")) { |
<> | 0:eb73febb2bba | 203 | // blink in yellow color |
<> | 0:eb73febb2bba | 204 | led1 = !led1; |
<> | 0:eb73febb2bba | 205 | led2 = !led2; |
<> | 0:eb73febb2bba | 206 | } |
<> | 0:eb73febb2bba | 207 | else if (!strcmp((char *)color, "magenta")) { |
<> | 0:eb73febb2bba | 208 | // blink in magenta color |
<> | 0:eb73febb2bba | 209 | led1 = !led1; |
<> | 0:eb73febb2bba | 210 | led3 = !led3; |
<> | 0:eb73febb2bba | 211 | } |
<> | 0:eb73febb2bba | 212 | else if (!strcmp((char *)color, "white")) { |
<> | 0:eb73febb2bba | 213 | // blink in white color |
<> | 0:eb73febb2bba | 214 | led1 = !led1; |
<> | 0:eb73febb2bba | 215 | led2 = !led2; |
<> | 0:eb73febb2bba | 216 | led3 = !led3; |
<> | 0:eb73febb2bba | 217 | } |
<> | 0:eb73febb2bba | 218 | else { |
<> | 0:eb73febb2bba | 219 | // no operation |
<> | 0:eb73febb2bba | 220 | } |
<> | 0:eb73febb2bba | 221 | |
<> | 0:eb73febb2bba | 222 | // up the position, if we reached the end of the vector |
<> | 0:eb73febb2bba | 223 | if (position >= pattern->size()) { |
<> | 0:eb73febb2bba | 224 | // free memory, and exit this function |
<> | 0:eb73febb2bba | 225 | delete pattern; |
<> | 0:eb73febb2bba | 226 | return; |
<> | 0:eb73febb2bba | 227 | } |
<> | 0:eb73febb2bba | 228 | |
<> | 0:eb73febb2bba | 229 | // how long do we need to wait before the next blink? |
<> | 0:eb73febb2bba | 230 | uint32_t delay_ms = pattern->at(position); |
<> | 0:eb73febb2bba | 231 | |
<> | 0:eb73febb2bba | 232 | // Invoke same function after `delay_ms` (upping position) |
<> | 0:eb73febb2bba | 233 | Thread::wait(delay_ms); |
<> | 0:eb73febb2bba | 234 | do_blink(pattern, color, ++position); |
<> | 0:eb73febb2bba | 235 | } |
<> | 0:eb73febb2bba | 236 | }; |
<> | 0:eb73febb2bba | 237 | |
<> | 0:eb73febb2bba | 238 | #if(1) //for zxing with camera |
<> | 0:eb73febb2bba | 239 | /* |
<> | 0:eb73febb2bba | 240 | * The Zxing contains a function (send string). |
<> | 0:eb73febb2bba | 241 | * When `handle_string_send` is executed, the string after decoding is sent. |
<> | 0:eb73febb2bba | 242 | */ |
<> | 0:eb73febb2bba | 243 | class ZxingResource { |
<> | 0:eb73febb2bba | 244 | public: |
<> | 0:eb73febb2bba | 245 | ZxingResource() { |
<> | 0:eb73febb2bba | 246 | // create ObjectID with metadata tag of '3202', which is 'send string' |
<> | 0:eb73febb2bba | 247 | zxing_object = M2MInterfaceFactory::create_object("3202"); |
<> | 0:eb73febb2bba | 248 | M2MObjectInstance* zxing_inst = zxing_object->create_object_instance(); |
<> | 0:eb73febb2bba | 249 | // create resource with ID '5700', which is 'send string' |
<> | 0:eb73febb2bba | 250 | M2MResource* zxing_res = zxing_inst->create_dynamic_resource("5700", "zxing", |
<> | 0:eb73febb2bba | 251 | M2MResourceInstance::STRING, true); |
<> | 0:eb73febb2bba | 252 | // we can read this value |
<> | 0:eb73febb2bba | 253 | zxing_res->set_operation(M2MBase::GET_ALLOWED); |
<> | 0:eb73febb2bba | 254 | // set initial value (all values in mbed Client are buffers) |
<> | 0:eb73febb2bba | 255 | // to be able to read this data easily in the Connector console, we'll use a string |
<> | 0:eb73febb2bba | 256 | zxing_res->set_value((uint8_t*)"0", 1); |
<> | 0:eb73febb2bba | 257 | } |
<> | 0:eb73febb2bba | 258 | |
<> | 0:eb73febb2bba | 259 | ~ZxingResource() { |
<> | 0:eb73febb2bba | 260 | } |
<> | 0:eb73febb2bba | 261 | |
<> | 0:eb73febb2bba | 262 | M2MObject* get_object() { |
<> | 0:eb73febb2bba | 263 | return zxing_object; |
<> | 0:eb73febb2bba | 264 | } |
<> | 0:eb73febb2bba | 265 | |
<> | 0:eb73febb2bba | 266 | /* |
<> | 0:eb73febb2bba | 267 | * When you success the decode process of barcode, we send the string after decoding to mbed Device Connector. |
<> | 0:eb73febb2bba | 268 | */ |
<> | 0:eb73febb2bba | 269 | void handle_string_send(char * addr, int size) { |
<> | 0:eb73febb2bba | 270 | M2MObjectInstance* inst = zxing_object->object_instance(); |
<> | 0:eb73febb2bba | 271 | M2MResource* res = inst->resource("5700"); |
<> | 0:eb73febb2bba | 272 | |
<> | 0:eb73febb2bba | 273 | printf("%s\r\n", addr); |
<> | 0:eb73febb2bba | 274 | |
<> | 0:eb73febb2bba | 275 | // tell the string to connector |
<> | 0:eb73febb2bba | 276 | res->set_value((uint8_t *)addr, size); |
<> | 0:eb73febb2bba | 277 | } |
<> | 0:eb73febb2bba | 278 | |
<> | 0:eb73febb2bba | 279 | private: |
<> | 0:eb73febb2bba | 280 | M2MObject* zxing_object; |
<> | 0:eb73febb2bba | 281 | }; |
<> | 0:eb73febb2bba | 282 | #else |
<> | 0:eb73febb2bba | 283 | /* |
<> | 0:eb73febb2bba | 284 | * The button contains one property (click count). |
<> | 0:eb73febb2bba | 285 | * When `handle_button_click` is executed, the counter updates. |
<> | 0:eb73febb2bba | 286 | */ |
<> | 0:eb73febb2bba | 287 | class ButtonResource { |
<> | 0:eb73febb2bba | 288 | public: |
<> | 0:eb73febb2bba | 289 | ButtonResource(): counter(0) { |
<> | 0:eb73febb2bba | 290 | // create ObjectID with metadata tag of '3200', which is 'digital input' |
<> | 0:eb73febb2bba | 291 | btn_object = M2MInterfaceFactory::create_object("3200"); |
<> | 0:eb73febb2bba | 292 | M2MObjectInstance* btn_inst = btn_object->create_object_instance(); |
<> | 0:eb73febb2bba | 293 | // create resource with ID '5501', which is digital input counter |
<> | 0:eb73febb2bba | 294 | M2MResource* btn_res = btn_inst->create_dynamic_resource("5501", "Button", |
<> | 0:eb73febb2bba | 295 | M2MResourceInstance::INTEGER, true /* observable */); |
<> | 0:eb73febb2bba | 296 | // we can read this value |
<> | 0:eb73febb2bba | 297 | btn_res->set_operation(M2MBase::GET_ALLOWED); |
<> | 0:eb73febb2bba | 298 | // set initial value (all values in mbed Client are buffers) |
<> | 0:eb73febb2bba | 299 | // to be able to read this data easily in the Connector console, we'll use a string |
<> | 0:eb73febb2bba | 300 | btn_res->set_value((uint8_t*)"0", 1); |
<> | 0:eb73febb2bba | 301 | } |
<> | 0:eb73febb2bba | 302 | |
<> | 0:eb73febb2bba | 303 | ~ButtonResource() { |
<> | 0:eb73febb2bba | 304 | } |
<> | 0:eb73febb2bba | 305 | |
<> | 0:eb73febb2bba | 306 | M2MObject* get_object() { |
<> | 0:eb73febb2bba | 307 | return btn_object; |
<> | 0:eb73febb2bba | 308 | } |
<> | 0:eb73febb2bba | 309 | |
<> | 0:eb73febb2bba | 310 | /* |
<> | 0:eb73febb2bba | 311 | * When you press the button, we read the current value of the click counter |
<> | 0:eb73febb2bba | 312 | * from mbed Device Connector, then up the value with one. |
<> | 0:eb73febb2bba | 313 | */ |
<> | 0:eb73febb2bba | 314 | void handle_button_click() { |
<> | 0:eb73febb2bba | 315 | M2MObjectInstance* inst = btn_object->object_instance(); |
<> | 0:eb73febb2bba | 316 | M2MResource* res = inst->resource("5501"); |
<> | 0:eb73febb2bba | 317 | |
<> | 0:eb73febb2bba | 318 | // up counter |
<> | 0:eb73febb2bba | 319 | counter++; |
<> | 0:eb73febb2bba | 320 | #ifdef TARGET_K64F |
<> | 0:eb73febb2bba | 321 | printf("handle_button_click, new value of counter is %d\r\n", counter); |
<> | 0:eb73febb2bba | 322 | #else |
<> | 0:eb73febb2bba | 323 | printf("simulate button_click, new value of counter is %d\r\n", counter); |
<> | 0:eb73febb2bba | 324 | #endif |
<> | 0:eb73febb2bba | 325 | // serialize the value of counter as a string, and tell connector |
<> | 0:eb73febb2bba | 326 | char buffer[20]; |
<> | 0:eb73febb2bba | 327 | int size = sprintf(buffer,"%d",counter); |
<> | 0:eb73febb2bba | 328 | res->set_value((uint8_t*)buffer, size); |
<> | 0:eb73febb2bba | 329 | } |
<> | 0:eb73febb2bba | 330 | |
<> | 0:eb73febb2bba | 331 | private: |
<> | 0:eb73febb2bba | 332 | M2MObject* btn_object; |
<> | 0:eb73febb2bba | 333 | uint16_t counter; |
<> | 0:eb73febb2bba | 334 | }; |
<> | 0:eb73febb2bba | 335 | #endif |
<> | 0:eb73febb2bba | 336 | |
<> | 0:eb73febb2bba | 337 | |
<> | 0:eb73febb2bba | 338 | // Network interaction must be performed outside of interrupt context |
<> | 0:eb73febb2bba | 339 | #if(1) //for zxing with camera |
<> | 0:eb73febb2bba | 340 | ZxingResource zxing_resource; |
<> | 0:eb73febb2bba | 341 | #else |
<> | 0:eb73febb2bba | 342 | Semaphore updates(0); |
<> | 0:eb73febb2bba | 343 | #endif |
<> | 0:eb73febb2bba | 344 | volatile bool registered = false; |
<> | 0:eb73febb2bba | 345 | volatile bool clicked = false; |
<> | 0:eb73febb2bba | 346 | osThreadId mainThread; |
<> | 0:eb73febb2bba | 347 | |
<> | 0:eb73febb2bba | 348 | void unregister() { |
<> | 0:eb73febb2bba | 349 | registered = false; |
<> | 0:eb73febb2bba | 350 | #if(1) //for zxing with camera |
<> | 0:eb73febb2bba | 351 | #else |
<> | 0:eb73febb2bba | 352 | updates.release(); |
<> | 0:eb73febb2bba | 353 | #endif |
<> | 0:eb73febb2bba | 354 | } |
<> | 0:eb73febb2bba | 355 | |
<> | 0:eb73febb2bba | 356 | #if(1) //for zxing with camera |
<> | 0:eb73febb2bba | 357 | #else |
<> | 0:eb73febb2bba | 358 | void button_clicked() { |
<> | 0:eb73febb2bba | 359 | clicked = true; |
<> | 0:eb73febb2bba | 360 | updates.release(); |
<> | 0:eb73febb2bba | 361 | } |
<> | 0:eb73febb2bba | 362 | #endif |
<> | 0:eb73febb2bba | 363 | |
<> | 0:eb73febb2bba | 364 | // debug printf function |
<> | 0:eb73febb2bba | 365 | void trace_printer(const char* str) { |
<> | 0:eb73febb2bba | 366 | printf("%s\r\n", str); |
<> | 0:eb73febb2bba | 367 | } |
<> | 0:eb73febb2bba | 368 | |
<> | 0:eb73febb2bba | 369 | // Status indication |
<> | 0:eb73febb2bba | 370 | Ticker status_ticker; |
<> | 0:eb73febb2bba | 371 | DigitalOut status_led(LED4); |
<> | 0:eb73febb2bba | 372 | void blinky() { status_led = !status_led; } |
<> | 0:eb73febb2bba | 373 | |
<> | 0:eb73febb2bba | 374 | #if(1) //for zxing with camera |
<> | 0:eb73febb2bba | 375 | extern void zxing_init(void (*pfunc)(char * addr, int size)); |
<> | 0:eb73febb2bba | 376 | extern int zxing_loop(); |
<> | 0:eb73febb2bba | 377 | |
<> | 0:eb73febb2bba | 378 | static void callback_zxing(char * addr, int size) { |
<> | 0:eb73febb2bba | 379 | zxing_resource.handle_string_send(addr, size); |
<> | 0:eb73febb2bba | 380 | } |
<> | 0:eb73febb2bba | 381 | #endif |
<> | 0:eb73febb2bba | 382 | |
<> | 0:eb73febb2bba | 383 | // Entry point to the program |
<> | 0:eb73febb2bba | 384 | int main() { |
<> | 0:eb73febb2bba | 385 | |
<> | 0:eb73febb2bba | 386 | #ifndef MBEDTLS_ENTROPY_HARDWARE_ALT |
<> | 0:eb73febb2bba | 387 | |
<> | 0:eb73febb2bba | 388 | #ifdef MBEDTLS_TEST_NULL_ENTROPY |
<> | 0:eb73febb2bba | 389 | #warning "mbedTLS security feature is disabled. Connection will not be secure !! Implement proper hardware entropy for your selected hardware." |
<> | 0:eb73febb2bba | 390 | |
<> | 0:eb73febb2bba | 391 | #else |
<> | 0:eb73febb2bba | 392 | |
<> | 0:eb73febb2bba | 393 | #error "This hardware does not have entropy, endpoint will not register to Connector.\ |
<> | 0:eb73febb2bba | 394 | You need to enable NULL ENTROPY for your application, but if this configuration change is made then no security is offered by mbed TLS.\ |
<> | 0:eb73febb2bba | 395 | Add MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY in mbed_app.json macros to register your endpoint." |
<> | 0:eb73febb2bba | 396 | #endif |
<> | 0:eb73febb2bba | 397 | |
<> | 0:eb73febb2bba | 398 | #endif |
<> | 0:eb73febb2bba | 399 | status_ticker.attach_us(blinky, 250000); |
<> | 0:eb73febb2bba | 400 | |
<> | 0:eb73febb2bba | 401 | // Keep track of the main thread |
<> | 0:eb73febb2bba | 402 | mainThread = osThreadGetId(); |
<> | 0:eb73febb2bba | 403 | |
<> | 0:eb73febb2bba | 404 | // Sets the console baud-rate |
<> | 0:eb73febb2bba | 405 | output.baud(115200); |
<> | 0:eb73febb2bba | 406 | |
<> | 0:eb73febb2bba | 407 | output.printf("Starting mbed Client example...\r\n"); |
<> | 0:eb73febb2bba | 408 | |
<> | 0:eb73febb2bba | 409 | mbed_trace_init(); |
<> | 0:eb73febb2bba | 410 | mbed_trace_print_function_set(trace_printer); |
<> | 0:eb73febb2bba | 411 | |
<> | 0:eb73febb2bba | 412 | NetworkInterface *network_interface = 0; |
<> | 0:eb73febb2bba | 413 | int connect_success = -1; |
<> | 0:eb73febb2bba | 414 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
<> | 0:eb73febb2bba | 415 | output.printf("\n\rUsing WiFi \r\n"); |
<> | 0:eb73febb2bba | 416 | output.printf("\n\rConnecting to WiFi..\r\n"); |
<> | 0:eb73febb2bba | 417 | #if(1) //bp3595 |
<> | 0:eb73febb2bba | 418 | usb1en = 1; |
<> | 0:eb73febb2bba | 419 | Thread::wait(5); |
<> | 0:eb73febb2bba | 420 | usb1en = 0; |
<> | 0:eb73febb2bba | 421 | Thread::wait(5); |
<> | 0:eb73febb2bba | 422 | connect_success = esp.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, MBED_CONF_APP_WIFI_SECURITY); |
<> | 0:eb73febb2bba | 423 | #else |
<> | 0:eb73febb2bba | 424 | connect_success = esp.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD); |
<> | 0:eb73febb2bba | 425 | #endif |
<> | 0:eb73febb2bba | 426 | network_interface = &esp; |
<> | 0:eb73febb2bba | 427 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
<> | 0:eb73febb2bba | 428 | output.printf("Using Ethernet\r\n"); |
<> | 0:eb73febb2bba | 429 | connect_success = eth.connect(); |
<> | 0:eb73febb2bba | 430 | network_interface = ð |
<> | 0:eb73febb2bba | 431 | #endif |
<> | 0:eb73febb2bba | 432 | #ifdef MESH |
<> | 0:eb73febb2bba | 433 | output.printf("Using Mesh\r\n"); |
<> | 0:eb73febb2bba | 434 | output.printf("\n\rConnecting to Mesh..\r\n"); |
<> | 0:eb73febb2bba | 435 | connect_success = mesh.connect(); |
<> | 0:eb73febb2bba | 436 | network_interface = &mesh; |
<> | 0:eb73febb2bba | 437 | #endif |
<> | 0:eb73febb2bba | 438 | if(connect_success == 0) { |
<> | 0:eb73febb2bba | 439 | output.printf("\n\rConnected to Network successfully\r\n"); |
<> | 0:eb73febb2bba | 440 | } else { |
<> | 0:eb73febb2bba | 441 | output.printf("\n\rConnection to Network Failed %d! Exiting application....\r\n", connect_success); |
<> | 0:eb73febb2bba | 442 | return 0; |
<> | 0:eb73febb2bba | 443 | } |
<> | 0:eb73febb2bba | 444 | const char *ip_addr = network_interface->get_ip_address(); |
<> | 0:eb73febb2bba | 445 | if (ip_addr) { |
<> | 0:eb73febb2bba | 446 | output.printf("IP address %s\r\n", ip_addr); |
<> | 0:eb73febb2bba | 447 | } else { |
<> | 0:eb73febb2bba | 448 | output.printf("No IP address\r\n"); |
<> | 0:eb73febb2bba | 449 | } |
<> | 0:eb73febb2bba | 450 | |
<> | 0:eb73febb2bba | 451 | #if(1) //for zxing with camera |
<> | 0:eb73febb2bba | 452 | LedResource led_resource; |
<> | 0:eb73febb2bba | 453 | |
<> | 0:eb73febb2bba | 454 | // On press of USER_BUTTON0 button on GR-PEACH board, example application |
<> | 0:eb73febb2bba | 455 | // will call unregister API towards mbed Device Connector |
<> | 0:eb73febb2bba | 456 | unreg_button.fall(&unregister); |
<> | 0:eb73febb2bba | 457 | #else |
<> | 0:eb73febb2bba | 458 | // we create our button and LED resources |
<> | 0:eb73febb2bba | 459 | ButtonResource button_resource; |
<> | 0:eb73febb2bba | 460 | LedResource led_resource; |
<> | 0:eb73febb2bba | 461 | |
<> | 0:eb73febb2bba | 462 | #ifdef TARGET_K64F |
<> | 0:eb73febb2bba | 463 | // On press of SW3 button on K64F board, example application |
<> | 0:eb73febb2bba | 464 | // will call unregister API towards mbed Device Connector |
<> | 0:eb73febb2bba | 465 | //unreg_button.fall(&mbed_client,&MbedClient::test_unregister); |
<> | 0:eb73febb2bba | 466 | unreg_button.fall(&unregister); |
<> | 0:eb73febb2bba | 467 | |
<> | 0:eb73febb2bba | 468 | // Observation Button (SW2) press will send update of endpoint resource values to connector |
<> | 0:eb73febb2bba | 469 | obs_button.fall(&button_clicked); |
<> | 0:eb73febb2bba | 470 | #else |
<> | 0:eb73febb2bba | 471 | // Send update of endpoint resource values to connector every 15 seconds periodically |
<> | 0:eb73febb2bba | 472 | timer.attach(&button_clicked, 15.0); |
<> | 0:eb73febb2bba | 473 | #endif |
<> | 0:eb73febb2bba | 474 | #endif |
<> | 0:eb73febb2bba | 475 | |
<> | 0:eb73febb2bba | 476 | // Create endpoint interface to manage register and unregister |
<> | 0:eb73febb2bba | 477 | mbed_client.create_interface(MBED_SERVER_ADDRESS, network_interface); |
<> | 0:eb73febb2bba | 478 | |
<> | 0:eb73febb2bba | 479 | // Create Objects of varying types, see simpleclient.h for more details on implementation. |
<> | 0:eb73febb2bba | 480 | M2MSecurity* register_object = mbed_client.create_register_object(); // server object specifying connector info |
<> | 0:eb73febb2bba | 481 | M2MDevice* device_object = mbed_client.create_device_object(); // device resources object |
<> | 0:eb73febb2bba | 482 | |
<> | 0:eb73febb2bba | 483 | // Create list of Objects to register |
<> | 0:eb73febb2bba | 484 | M2MObjectList object_list; |
<> | 0:eb73febb2bba | 485 | |
<> | 0:eb73febb2bba | 486 | // Add objects to list |
<> | 0:eb73febb2bba | 487 | object_list.push_back(device_object); |
<> | 0:eb73febb2bba | 488 | #if(1) //for zxing with camera |
<> | 0:eb73febb2bba | 489 | object_list.push_back(zxing_resource.get_object()); |
<> | 0:eb73febb2bba | 490 | #else |
<> | 0:eb73febb2bba | 491 | object_list.push_back(button_resource.get_object()); |
<> | 0:eb73febb2bba | 492 | #endif |
<> | 0:eb73febb2bba | 493 | object_list.push_back(led_resource.get_object()); |
<> | 0:eb73febb2bba | 494 | |
<> | 0:eb73febb2bba | 495 | // Set endpoint registration object |
<> | 0:eb73febb2bba | 496 | mbed_client.set_register_object(register_object); |
<> | 0:eb73febb2bba | 497 | |
<> | 0:eb73febb2bba | 498 | // Register with mbed Device Connector |
<> | 0:eb73febb2bba | 499 | mbed_client.test_register(register_object, object_list); |
<> | 0:eb73febb2bba | 500 | registered = true; |
<> | 0:eb73febb2bba | 501 | |
<> | 0:eb73febb2bba | 502 | #if(1) //for zxing with camera |
<> | 0:eb73febb2bba | 503 | zxing_init(&callback_zxing); |
<> | 0:eb73febb2bba | 504 | Timer update_timer; |
<> | 0:eb73febb2bba | 505 | update_timer.reset(); |
<> | 0:eb73febb2bba | 506 | update_timer.start(); |
<> | 0:eb73febb2bba | 507 | |
<> | 0:eb73febb2bba | 508 | while (registered) { |
<> | 0:eb73febb2bba | 509 | if (zxing_loop() == 0) { |
<> | 0:eb73febb2bba | 510 | update_timer.reset(); |
<> | 0:eb73febb2bba | 511 | } else if (update_timer.read() >= 25) { |
<> | 0:eb73febb2bba | 512 | mbed_client.test_update_register(); |
<> | 0:eb73febb2bba | 513 | update_timer.reset(); |
<> | 0:eb73febb2bba | 514 | } else { |
<> | 0:eb73febb2bba | 515 | // do nothing |
<> | 0:eb73febb2bba | 516 | } |
<> | 0:eb73febb2bba | 517 | Thread::wait(5); |
<> | 0:eb73febb2bba | 518 | } |
<> | 0:eb73febb2bba | 519 | #else |
<> | 0:eb73febb2bba | 520 | while (true) { |
<> | 0:eb73febb2bba | 521 | updates.wait(25000); |
<> | 0:eb73febb2bba | 522 | if(registered) { |
<> | 0:eb73febb2bba | 523 | if(!clicked) { |
<> | 0:eb73febb2bba | 524 | mbed_client.test_update_register(); |
<> | 0:eb73febb2bba | 525 | } |
<> | 0:eb73febb2bba | 526 | }else { |
<> | 0:eb73febb2bba | 527 | break; |
<> | 0:eb73febb2bba | 528 | } |
<> | 0:eb73febb2bba | 529 | if(clicked) { |
<> | 0:eb73febb2bba | 530 | clicked = false; |
<> | 0:eb73febb2bba | 531 | button_resource.handle_button_click(); |
<> | 0:eb73febb2bba | 532 | } |
<> | 0:eb73febb2bba | 533 | } |
<> | 0:eb73febb2bba | 534 | #endif |
<> | 0:eb73febb2bba | 535 | |
<> | 0:eb73febb2bba | 536 | mbed_client.test_unregister(); |
<> | 0:eb73febb2bba | 537 | status_ticker.detach(); |
<> | 0:eb73febb2bba | 538 | } |