Pelion test

Committer:
dkato
Date:
Wed Feb 20 03:15:11 2019 +0000
Revision:
9:eac9e0c9effd
Parent:
8:5f1c0849001c
Add camera function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:6d2053b84a92 1 // ----------------------------------------------------------------------------
MACRUM 0:6d2053b84a92 2 // Copyright 2016-2018 ARM Ltd.
MACRUM 0:6d2053b84a92 3 //
MACRUM 0:6d2053b84a92 4 // SPDX-License-Identifier: Apache-2.0
MACRUM 0:6d2053b84a92 5 //
MACRUM 0:6d2053b84a92 6 // Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 0:6d2053b84a92 7 // you may not use this file except in compliance with the License.
MACRUM 0:6d2053b84a92 8 // You may obtain a copy of the License at
MACRUM 0:6d2053b84a92 9 //
MACRUM 0:6d2053b84a92 10 // http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:6d2053b84a92 11 //
MACRUM 0:6d2053b84a92 12 // Unless required by applicable law or agreed to in writing, software
MACRUM 0:6d2053b84a92 13 // distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 0:6d2053b84a92 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:6d2053b84a92 15 // See the License for the specific language governing permissions and
MACRUM 0:6d2053b84a92 16 // limitations under the License.
MACRUM 0:6d2053b84a92 17 // ----------------------------------------------------------------------------
MACRUM 0:6d2053b84a92 18 #ifndef MBED_TEST_MODE
MACRUM 0:6d2053b84a92 19
MACRUM 0:6d2053b84a92 20 #include "mbed.h"
MACRUM 0:6d2053b84a92 21 #include "simple-mbed-cloud-client.h"
MACRUM 0:6d2053b84a92 22 #include "FATFileSystem.h"
dkato 9:eac9e0c9effd 23 #include "EasyAttach_CameraAndLCD.h"
dkato 9:eac9e0c9effd 24 #include "dcache-control.h"
dkato 9:eac9e0c9effd 25 #include "JPEG_Converter.h"
MACRUM 0:6d2053b84a92 26
dkato 9:eac9e0c9effd 27 /**** User Selection *********/
dkato 9:eac9e0c9effd 28 #define VIDEO_PIXEL_HW (320u) /* QVGA */
dkato 9:eac9e0c9effd 29 #define VIDEO_PIXEL_VW (240u) /* QVGA */
dkato 9:eac9e0c9effd 30 #define JPEG_ENCODE_QUALITY (75) /* JPEG encode quality (min:1, max:75 (Considering the size of JpegBuffer, about 75 is the upper limit.)) */
dkato 9:eac9e0c9effd 31 /*****************************/
dkato 9:eac9e0c9effd 32
dkato 9:eac9e0c9effd 33 #define DATA_SIZE_PER_PIC (2u)
dkato 9:eac9e0c9effd 34 #define FRAME_BUFFER_STRIDE (((VIDEO_PIXEL_HW * DATA_SIZE_PER_PIC) + 31u) & ~31u)
dkato 9:eac9e0c9effd 35 #define FRAME_BUFFER_HEIGHT (VIDEO_PIXEL_VW)
dkato 9:eac9e0c9effd 36
dkato 9:eac9e0c9effd 37 uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * FRAME_BUFFER_HEIGHT]__attribute((aligned(32)));
dkato 9:eac9e0c9effd 38 uint8_t JpegBuffer[1024 * 32]__attribute((aligned(32)));
dkato 9:eac9e0c9effd 39 DisplayBase Display;
dkato 9:eac9e0c9effd 40 JPEG_Converter Jcu;
Osamu Koizumi 4:7c58c47eca55 41
MACRUM 0:6d2053b84a92 42 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
MACRUM 0:6d2053b84a92 43 // This is great because things such as network operations are illegal in ISR, so updating a resource in a button's fall() function is not allowed
MACRUM 0:6d2053b84a92 44 EventQueue eventQueue;
MACRUM 0:6d2053b84a92 45
MACRUM 0:6d2053b84a92 46 // Default block device
MACRUM 0:6d2053b84a92 47 BlockDevice *bd = BlockDevice::get_default_instance();
MACRUM 0:6d2053b84a92 48 FATFileSystem fs("fs");
MACRUM 0:6d2053b84a92 49
MACRUM 0:6d2053b84a92 50 // Default network interface object
MACRUM 0:6d2053b84a92 51 NetworkInterface *net = NetworkInterface::get_default_instance();
MACRUM 0:6d2053b84a92 52
MACRUM 0:6d2053b84a92 53 InterruptIn btn(USER_BUTTON0);
MACRUM 0:6d2053b84a92 54 // Declaring pointers for access to Pelion Device Management Client resources outside of main()
MACRUM 0:6d2053b84a92 55 MbedCloudClientResource *button_res;
MACRUM 0:6d2053b84a92 56 MbedCloudClientResource *pattern_res;
Osamu Koizumi 4:7c58c47eca55 57 MbedCloudClientResource *res_camera_capture;
MACRUM 0:6d2053b84a92 58
dkato 9:eac9e0c9effd 59 bool take_photo(uint8_t *photo_data, uint32_t *photo_data_len) {
dkato 9:eac9e0c9effd 60 JPEG_Converter::bitmap_buff_info_t buff_info;
dkato 9:eac9e0c9effd 61 JPEG_Converter::encode_options_t encode_opt;
dkato 9:eac9e0c9effd 62
dkato 9:eac9e0c9effd 63 if ((photo_data == NULL) || (photo_data_len == NULL)) {
dkato 9:eac9e0c9effd 64 return false;
dkato 9:eac9e0c9effd 65 }
dkato 9:eac9e0c9effd 66
dkato 9:eac9e0c9effd 67 // Jpeg setting
dkato 9:eac9e0c9effd 68 buff_info.width = VIDEO_PIXEL_HW;
dkato 9:eac9e0c9effd 69 buff_info.height = VIDEO_PIXEL_VW;
dkato 9:eac9e0c9effd 70 buff_info.format = JPEG_Converter::WR_RD_YCbCr422;
dkato 9:eac9e0c9effd 71 buff_info.buffer_address = (void *)user_frame_buffer0;
dkato 9:eac9e0c9effd 72 encode_opt.encode_buff_size = *photo_data_len;
dkato 9:eac9e0c9effd 73 encode_opt.input_swapsetting = JPEG_Converter::WR_RD_WRSWA_32_16_8BIT;
dkato 9:eac9e0c9effd 74
dkato 9:eac9e0c9effd 75 dcache_invalid(photo_data, *photo_data_len);
dkato 9:eac9e0c9effd 76 if (Jcu.encode(&buff_info, photo_data, photo_data_len, &encode_opt) != JPEG_Converter::JPEG_CONV_OK) {
dkato 9:eac9e0c9effd 77 return false;
dkato 9:eac9e0c9effd 78 }
dkato 9:eac9e0c9effd 79
dkato 9:eac9e0c9effd 80 return true;
dkato 9:eac9e0c9effd 81 }
dkato 9:eac9e0c9effd 82
MACRUM 0:6d2053b84a92 83 void button_press() {
MACRUM 0:6d2053b84a92 84 int v = button_res->get_value_int() + 1;
MACRUM 0:6d2053b84a92 85
MACRUM 0:6d2053b84a92 86 button_res->set_value(v);
MACRUM 0:6d2053b84a92 87
Osamu Koizumi 7:826cdcc3c8ec 88 printf("User button clicked %d times\n", v);
Osamu Koizumi 7:826cdcc3c8ec 89
Osamu Koizumi 7:826cdcc3c8ec 90 // Send photo data on button click
Osamu Koizumi 4:7c58c47eca55 91 M2MResource* m2m_res = res_camera_capture->get_m2m_resource();
dkato 9:eac9e0c9effd 92 uint32_t photo_data_len = sizeof(JpegBuffer);
dkato 9:eac9e0c9effd 93 if (take_photo(JpegBuffer, &photo_data_len) != false) {
dkato 9:eac9e0c9effd 94 m2m_res->set_value((const uint8_t *)JpegBuffer, photo_data_len);
dkato 9:eac9e0c9effd 95 }
Osamu Koizumi 4:7c58c47eca55 96 }
Osamu Koizumi 4:7c58c47eca55 97
Osamu Koizumi 4:7c58c47eca55 98 void res_camera_capture_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status)
Osamu Koizumi 4:7c58c47eca55 99 {
Osamu Koizumi 4:7c58c47eca55 100 printf("camera notification, status: %s (%d)\n",
Osamu Koizumi 4:7c58c47eca55 101 MbedCloudClientResource::delivery_status_to_string(status), status);
MACRUM 0:6d2053b84a92 102 }
MACRUM 0:6d2053b84a92 103
MACRUM 0:6d2053b84a92 104 /**
MACRUM 0:6d2053b84a92 105 * PUT handler
MACRUM 0:6d2053b84a92 106 * @param resource The resource that triggered the callback
MACRUM 0:6d2053b84a92 107 * @param newValue Updated value for the resource
MACRUM 0:6d2053b84a92 108 */
MACRUM 0:6d2053b84a92 109 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
MACRUM 0:6d2053b84a92 110 printf("PUT received, new value: %s\n", newValue.c_str());
MACRUM 0:6d2053b84a92 111 }
MACRUM 0:6d2053b84a92 112
MACRUM 0:6d2053b84a92 113 /**
MACRUM 0:6d2053b84a92 114 * POST handler
MACRUM 0:6d2053b84a92 115 * @param resource The resource that triggered the callback
MACRUM 0:6d2053b84a92 116 * @param buffer If a body was passed to the POST function, this contains the data.
MACRUM 0:6d2053b84a92 117 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
MACRUM 0:6d2053b84a92 118 * @param size Size of the body
MACRUM 0:6d2053b84a92 119 */
Osamu Koizumi 8:5f1c0849001c 120 void camera_trigger_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
Osamu Koizumi 8:5f1c0849001c 121 printf("POST received. Camera takes a picture.", pattern_res->get_value().c_str());
Osamu Koizumi 8:5f1c0849001c 122 M2MResource* m2m_res = res_camera_capture->get_m2m_resource();
dkato 9:eac9e0c9effd 123 uint32_t photo_data_len = sizeof(JpegBuffer);
dkato 9:eac9e0c9effd 124 if (take_photo(JpegBuffer, &photo_data_len) != false) {
dkato 9:eac9e0c9effd 125 m2m_res->set_value((const uint8_t *)JpegBuffer, photo_data_len);
dkato 9:eac9e0c9effd 126 }
MACRUM 0:6d2053b84a92 127 }
MACRUM 0:6d2053b84a92 128
MACRUM 0:6d2053b84a92 129 /**
MACRUM 0:6d2053b84a92 130 * Notification callback handler
MACRUM 0:6d2053b84a92 131 * @param resource The resource that triggered the callback
MACRUM 0:6d2053b84a92 132 * @param status The delivery status of the notification
MACRUM 0:6d2053b84a92 133 */
MACRUM 0:6d2053b84a92 134 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
MACRUM 0:6d2053b84a92 135 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
MACRUM 0:6d2053b84a92 136 }
MACRUM 0:6d2053b84a92 137
MACRUM 0:6d2053b84a92 138 /**
MACRUM 0:6d2053b84a92 139 * Registration callback handler
MACRUM 0:6d2053b84a92 140 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
MACRUM 0:6d2053b84a92 141 */
MACRUM 0:6d2053b84a92 142 void registered(const ConnectorClientEndpointInfo *endpoint) {
MACRUM 0:6d2053b84a92 143 printf("Connected to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
Osamu Koizumi 7:826cdcc3c8ec 144 // Once registered, send default photo.
Osamu Koizumi 7:826cdcc3c8ec 145 M2MResource* m2m_res = res_camera_capture->get_m2m_resource();
dkato 9:eac9e0c9effd 146 uint32_t photo_data_len = sizeof(JpegBuffer);
dkato 9:eac9e0c9effd 147 if (take_photo(JpegBuffer, &photo_data_len) != false) {
dkato 9:eac9e0c9effd 148 m2m_res->set_value((const uint8_t *)JpegBuffer, photo_data_len);
dkato 9:eac9e0c9effd 149 }
MACRUM 0:6d2053b84a92 150 }
MACRUM 0:6d2053b84a92 151
MACRUM 0:6d2053b84a92 152 int main(void) {
MACRUM 0:6d2053b84a92 153 printf("Starting Simple Pelion Device Management Client example\n");
MACRUM 0:6d2053b84a92 154 printf("Connecting to the network...\n");
MACRUM 0:6d2053b84a92 155
MACRUM 0:6d2053b84a92 156 // Connect to the internet (DHCP is expected to be on)
MACRUM 0:6d2053b84a92 157 nsapi_error_t status = net->connect();
MACRUM 0:6d2053b84a92 158
MACRUM 0:6d2053b84a92 159 if (status != NSAPI_ERROR_OK) {
MACRUM 0:6d2053b84a92 160 printf("Connecting to the network failed %d!\n", status);
MACRUM 0:6d2053b84a92 161 return -1;
MACRUM 0:6d2053b84a92 162 }
MACRUM 0:6d2053b84a92 163
MACRUM 0:6d2053b84a92 164 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
MACRUM 0:6d2053b84a92 165
MACRUM 0:6d2053b84a92 166 // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management
MACRUM 0:6d2053b84a92 167 SimpleMbedCloudClient client(net, bd, &fs);
MACRUM 0:6d2053b84a92 168 int client_status = client.init();
MACRUM 0:6d2053b84a92 169 if (client_status != 0) {
MACRUM 0:6d2053b84a92 170 printf("Pelion Client initialization failed (%d)\n", client_status);
MACRUM 0:6d2053b84a92 171 return -1;
MACRUM 0:6d2053b84a92 172 }
MACRUM 0:6d2053b84a92 173
MACRUM 0:6d2053b84a92 174 // Creating resources, which can be written or read from the cloud
MACRUM 0:6d2053b84a92 175 button_res = client.create_resource("3200/0/5501", "button_count");
MACRUM 0:6d2053b84a92 176 button_res->set_value(0);
MACRUM 0:6d2053b84a92 177 button_res->methods(M2MMethod::GET);
MACRUM 0:6d2053b84a92 178 button_res->observable(true);
MACRUM 0:6d2053b84a92 179 button_res->attach_notification_callback(button_callback);
MACRUM 0:6d2053b84a92 180
MACRUM 0:6d2053b84a92 181 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
MACRUM 0:6d2053b84a92 182 pattern_res->set_value("500:500:500:500:500:500:500:500");
MACRUM 0:6d2053b84a92 183 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
MACRUM 0:6d2053b84a92 184 pattern_res->attach_put_callback(pattern_updated);
MACRUM 0:6d2053b84a92 185
Osamu Koizumi 4:7c58c47eca55 186 res_camera_capture = client.create_resource("3200/0/4014", "CameraCapture");
Osamu Koizumi 4:7c58c47eca55 187 res_camera_capture->set_value(0);
Osamu Koizumi 4:7c58c47eca55 188 res_camera_capture->methods(M2MMethod::GET);
Osamu Koizumi 4:7c58c47eca55 189 res_camera_capture->observable(true);
Osamu Koizumi 4:7c58c47eca55 190 res_camera_capture->attach_notification_callback(res_camera_capture_callback);
Osamu Koizumi 4:7c58c47eca55 191
MACRUM 0:6d2053b84a92 192 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
MACRUM 0:6d2053b84a92 193 blink_res->methods(M2MMethod::POST);
Osamu Koizumi 8:5f1c0849001c 194 blink_res->attach_post_callback(camera_trigger_callback);
MACRUM 0:6d2053b84a92 195
MACRUM 0:6d2053b84a92 196 printf("Initialized Pelion Client. Registering...\n");
MACRUM 0:6d2053b84a92 197
dkato 9:eac9e0c9effd 198 // Camera start
dkato 9:eac9e0c9effd 199 EasyAttach_Init(Display);
dkato 9:eac9e0c9effd 200 Display.Video_Write_Setting(
dkato 9:eac9e0c9effd 201 DisplayBase::VIDEO_INPUT_CHANNEL_0,
dkato 9:eac9e0c9effd 202 DisplayBase::COL_SYS_NTSC_358,
dkato 9:eac9e0c9effd 203 (void *)user_frame_buffer0,
dkato 9:eac9e0c9effd 204 FRAME_BUFFER_STRIDE,
dkato 9:eac9e0c9effd 205 DisplayBase::VIDEO_FORMAT_YCBCR422,
dkato 9:eac9e0c9effd 206 DisplayBase::WR_RD_WRSWA_32_16BIT,
dkato 9:eac9e0c9effd 207 VIDEO_PIXEL_VW,
dkato 9:eac9e0c9effd 208 VIDEO_PIXEL_HW
dkato 9:eac9e0c9effd 209 );
dkato 9:eac9e0c9effd 210 EasyAttach_CameraStart(Display, DisplayBase::VIDEO_INPUT_CHANNEL_0);
dkato 9:eac9e0c9effd 211
dkato 9:eac9e0c9effd 212 // Jpeg setting
dkato 9:eac9e0c9effd 213 Jcu.SetQuality(JPEG_ENCODE_QUALITY);
dkato 9:eac9e0c9effd 214
MACRUM 0:6d2053b84a92 215 // Callback that fires when registering is complete
MACRUM 0:6d2053b84a92 216 client.on_registered(&registered);
MACRUM 0:6d2053b84a92 217
MACRUM 0:6d2053b84a92 218 // Register with Pelion Device Management
MACRUM 0:6d2053b84a92 219 client.register_and_connect();
MACRUM 0:6d2053b84a92 220
MACRUM 0:6d2053b84a92 221 // Setup the button
MACRUM 0:6d2053b84a92 222 btn.mode(PullUp);
MACRUM 0:6d2053b84a92 223
MACRUM 0:6d2053b84a92 224 // The button fall handler is placed in the event queue so it will run in
MACRUM 0:6d2053b84a92 225 // thread context instead of ISR context, which allows safely updating the cloud resource
MACRUM 0:6d2053b84a92 226 btn.fall(eventQueue.event(&button_press));
MACRUM 0:6d2053b84a92 227
MACRUM 0:6d2053b84a92 228 // You can easily run the eventQueue in a separate thread if required
MACRUM 0:6d2053b84a92 229 eventQueue.dispatch_forever();
MACRUM 0:6d2053b84a92 230 }
MACRUM 0:6d2053b84a92 231 #endif