Taking photo using GR-LYCHEE through Pelion Device Management.

This firmware project works for GR-LYCHEE and GR-PEACH.

Steps to build this firmware with Mbed CLI

Import project

$ mbed import http://os.mbed.com/users/coisme/code/Pelion-GR-LYCHEE-camera-firmware/

Setting

  • Ethernet (GR-PEACH only)
    • Change the file name mbed_app.RZ_A1H_Ethernet.json to mbed_app.json
  • Wi-Fi
    • Change the Wi-Fi setting in mbed_app.json, i.e. nsapi.default-wifi-ssid and nsapi.default-wifi-password

If you haven't set your Pelion API key, run the following command.

$ mbed config -G CLOUD_SDK_API_KEY <API_KEY>

Setting for Pelion Device Management

$ mbed dm init -d "example.com" --model-name "PELION_DEMO" -q --force

Compile

For GR-LYCHEE:

$ mbed compile -t GCC_ARM -m GR_LYCHEE

For GR-PEACH:

$ mbed compile -t GCC_ARM -m RZ_A1H

Write the created .bin file to your GR-LYCHEE/PEACH.

That's it! :-)

LED Status Indicator

LEDs next to UB0 show the status of your GR-LYCHEE/PEACH.

LED#GR-LYCHEEGR-PEACHStatusDescription
LED1greenredNormalTurned on after the device is registered to Pelion Device Management successfully.
LED2yellowgreenErrorTurned on when the network initialization failed. Check your Wi-Fi setting.
LED3orangeblueErrorTurned on when the Pelion Device Management Client initialization failed. Check your SD card.
LED4redredErrorTurned on when the network is disconnected. Check your Wi-Fi network status.

Clear device identity

If you want to clear the device's identity, connect to the device via serial terminal. Then input r command. The device flushes the identity storage, then reboot.

Known Issues

  • client_error(6) -> Client in reconnection mode NetworkError appears when connecting to network, but eventually connection will be established.
  • Warning message "MBEDTLS_TEST_NULL_ENTROPY has been enabled. This configuration is not secure and is not suitable for production use" appears when compiling the project for GR-PEACH. This can be ignored in development stage.

main.cpp

Committer:
Osamu Koizumi
Date:
2019-02-22
Revision:
11:414b4a4ebbc6
Parent:
10:29d334f90c99
Child:
14:0480150782c8

File content as of revision 11:414b4a4ebbc6:

// ----------------------------------------------------------------------------
// Copyright 2016-2018 ARM Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------
#ifndef MBED_TEST_MODE

#include "mbed.h"
#include "simple-mbed-cloud-client.h"
#include "FATFileSystem.h"
#include "EasyAttach_CameraAndLCD.h"
#include "dcache-control.h"
#include "JPEG_Converter.h"

// Initial photo data
#include "default_photo.h"

/**** User Selection *********/
#define VIDEO_PIXEL_HW         (320u)  /* QVGA */
#define VIDEO_PIXEL_VW         (240u)  /* QVGA */
#define JPEG_ENCODE_QUALITY    (75)    /* JPEG encode quality (min:1, max:75 (Considering the size of JpegBuffer, about 75 is the upper limit.)) */
/*****************************/

#define DATA_SIZE_PER_PIC      (2u)
#define FRAME_BUFFER_STRIDE    (((VIDEO_PIXEL_HW * DATA_SIZE_PER_PIC) + 31u) & ~31u)
#define FRAME_BUFFER_HEIGHT    (VIDEO_PIXEL_VW)

uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * FRAME_BUFFER_HEIGHT]__attribute((aligned(32)));
uint8_t JpegBuffer[1024 * 32]__attribute((aligned(32)));
DisplayBase Display;
JPEG_Converter Jcu;

// An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
// 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
EventQueue eventQueue;

// Default block device
BlockDevice *bd = BlockDevice::get_default_instance();
FATFileSystem fs("fs");

// Default network interface object
NetworkInterface *net = NetworkInterface::get_default_instance();

InterruptIn btn(USER_BUTTON0);
// Declaring pointers for access to Pelion Device Management Client resources outside of main()
MbedCloudClientResource *button_res;
MbedCloudClientResource *camera_trigger_res;
MbedCloudClientResource *camera_capture_res;

bool take_photo(uint8_t *photo_data, uint32_t *photo_data_len) {
    JPEG_Converter::bitmap_buff_info_t buff_info;
    JPEG_Converter::encode_options_t   encode_opt;

    if ((photo_data == NULL) || (photo_data_len == NULL)) {
        return false;
    }

    // Jpeg setting
    buff_info.width              = VIDEO_PIXEL_HW;
    buff_info.height             = VIDEO_PIXEL_VW;
    buff_info.format             = JPEG_Converter::WR_RD_YCbCr422;
    buff_info.buffer_address     = (void *)user_frame_buffer0;
    encode_opt.encode_buff_size  = *photo_data_len;
    encode_opt.input_swapsetting = JPEG_Converter::WR_RD_WRSWA_32_16_8BIT;

    dcache_invalid(photo_data, *photo_data_len);
    if (Jcu.encode(&buff_info, photo_data, (size_t *)photo_data_len, &encode_opt) != JPEG_Converter::JPEG_CONV_OK) {
        return false;
    }

    return true;
}

bool take_and_send_photo() {
    // Send photo data on button click
    uint32_t photo_data_len = sizeof(JpegBuffer);
    if (take_photo(JpegBuffer, &photo_data_len)) {
        M2MResource* m2m_res = camera_capture_res->get_m2m_resource();
        m2m_res->set_value((const uint8_t *)JpegBuffer, photo_data_len);
        // success
        return true;
    }
    // fail
    return false;
}

void button_press() {
    int v = button_res->get_value_int() + 1;
    button_res->set_value(v);
    printf("User button clicked %d times\n", v);

    if(!take_and_send_photo()) {
        printf("Failed to send photo.");
    }
}

void camera_capture_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status)
{
    printf("camera notification, status: %s (%d)\n", 
    	MbedCloudClientResource::delivery_status_to_string(status), status);
}

/**
 * PUT handler
 * @param resource The resource that triggered the callback
 * @param newValue Updated value for the resource
 */
void camera_trigger_callback(MbedCloudClientResource *resource, m2m::String newValue) {
    printf("Camera trigger received, new value: %s\n", newValue.c_str());

    if(!take_and_send_photo()) {
        printf("Failed to send photo.");
    }
}

/**
 * Notification callback handler
 * @param resource The resource that triggered the callback
 * @param status The delivery status of the notification
 */
void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
    printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
}

/**
 * Registration callback handler
 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
 */
void registered(const ConnectorClientEndpointInfo *endpoint) {
    printf("Connected to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
    // Set default photo
    M2MResource* m2m_res = camera_capture_res->get_m2m_resource();
    m2m_res->set_value(DEFAULT_PHOTO, DEFAULT_PHOTO_LEN);
}
 

int main(void) {
    printf("Starting Simple Pelion Device Management Client example\n");
    printf("Connecting to the network...\n");

    // Connect to the internet (DHCP is expected to be on)
    nsapi_error_t status = net->connect();

    if (status != NSAPI_ERROR_OK) {
        printf("Connecting to the network failed %d!\n", status);
        return -1;
    }

    printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());

    // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management
    SimpleMbedCloudClient client(net, bd, &fs);
    int client_status = client.init();
    if (client_status != 0) {
        printf("Pelion Client initialization failed (%d)\n", client_status);
        return -1;
    }

    // Creating resources, which can be written or read from the cloud
    button_res = client.create_resource("3200/0/5501", "button_shutter");
    button_res->set_value(0);
    button_res->methods(M2MMethod::GET);
    button_res->observable(true);
    button_res->attach_notification_callback(button_callback);

    camera_trigger_res = client.create_resource("3201/0/5853", "camera_trigger");
    camera_trigger_res->set_value(0);
    camera_trigger_res->methods(M2MMethod::GET | M2MMethod::PUT);
    camera_trigger_res->attach_put_callback(camera_trigger_callback);

	camera_capture_res = client.create_resource("3200/0/4014", "CameraCapture");
    camera_capture_res->set_value(0);
    camera_capture_res->methods(M2MMethod::GET);
    camera_capture_res->observable(true);
    camera_capture_res->attach_notification_callback(camera_capture_callback);

    printf("Initialized Pelion Client. Registering...\n");

    // Camera start
    EasyAttach_Init(Display);
    Display.Video_Write_Setting(
        DisplayBase::VIDEO_INPUT_CHANNEL_0,
        DisplayBase::COL_SYS_NTSC_358,
        (void *)user_frame_buffer0,
        FRAME_BUFFER_STRIDE,
        DisplayBase::VIDEO_FORMAT_YCBCR422,
        DisplayBase::WR_RD_WRSWA_32_16BIT,
        VIDEO_PIXEL_VW,
        VIDEO_PIXEL_HW
    );
    EasyAttach_CameraStart(Display, DisplayBase::VIDEO_INPUT_CHANNEL_0);

    // Jpeg setting
    Jcu.SetQuality(JPEG_ENCODE_QUALITY);

    // Callback that fires when registering is complete
    client.on_registered(&registered);

    // Register with Pelion Device Management
    client.register_and_connect();

    // Setup the button
    btn.mode(PullUp);

    // The button fall handler is placed in the event queue so it will run in
    // thread context instead of ISR context, which allows safely updating the cloud resource
    btn.fall(eventQueue.event(&button_press));

    // You can easily run the eventQueue in a separate thread if required
    eventQueue.dispatch_forever();
}
#endif