Mbed Cloud example program for workshop in W27 2018.

Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:119624335925 1 // ----------------------------------------------------------------------------
MACRUM 0:119624335925 2 // Copyright 2016-2017 ARM Ltd.
MACRUM 0:119624335925 3 //
MACRUM 0:119624335925 4 // SPDX-License-Identifier: Apache-2.0
MACRUM 0:119624335925 5 //
MACRUM 0:119624335925 6 // Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 0:119624335925 7 // you may not use this file except in compliance with the License.
MACRUM 0:119624335925 8 // You may obtain a copy of the License at
MACRUM 0:119624335925 9 //
MACRUM 0:119624335925 10 // http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:119624335925 11 //
MACRUM 0:119624335925 12 // Unless required by applicable law or agreed to in writing, software
MACRUM 0:119624335925 13 // distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 0:119624335925 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:119624335925 15 // See the License for the specific language governing permissions and
MACRUM 0:119624335925 16 // limitations under the License.
MACRUM 0:119624335925 17 // ----------------------------------------------------------------------------
MACRUM 0:119624335925 18
MACRUM 0:119624335925 19 #include "update_ui_example.h"
MACRUM 0:119624335925 20
MACRUM 0:119624335925 21 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
MACRUM 0:119624335925 22
MACRUM 0:119624335925 23 #include <stdio.h>
MACRUM 0:119624335925 24 #include <stdint.h>
MACRUM 0:119624335925 25
MACRUM 0:119624335925 26 static MbedCloudClient* _client;
MACRUM 0:119624335925 27
MACRUM 0:119624335925 28 #ifdef ARM_UPDATE_CLIENT_VERSION_VALUE
MACRUM 0:119624335925 29 #if ARM_UPDATE_CLIENT_VERSION_VALUE > 101000
MACRUM 0:119624335925 30 void update_ui_set_cloud_client(MbedCloudClient* client)
MACRUM 0:119624335925 31 {
MACRUM 0:119624335925 32 _client = client;
MACRUM 0:119624335925 33 }
MACRUM 0:119624335925 34
MACRUM 0:119624335925 35 void update_authorize(int32_t request)
MACRUM 0:119624335925 36 {
MACRUM 0:119624335925 37 switch (request)
MACRUM 0:119624335925 38 {
MACRUM 0:119624335925 39 /* Cloud Client wishes to download new firmware. This can have a negative
MACRUM 0:119624335925 40 impact on the performance of the rest of the system.
MACRUM 0:119624335925 41
MACRUM 0:119624335925 42 The user application is supposed to pause performance sensitive tasks
MACRUM 0:119624335925 43 before authorizing the download.
MACRUM 0:119624335925 44
MACRUM 0:119624335925 45 Note: the authorization call can be postponed and called later.
MACRUM 0:119624335925 46 This doesn't affect the performance of the Cloud Client.
MACRUM 0:119624335925 47 */
MACRUM 0:119624335925 48 case MbedCloudClient::UpdateRequestDownload:
MACRUM 0:119624335925 49 printf("Firmware download requested\r\n");
MACRUM 0:119624335925 50 printf("Authorization granted\r\n");
MACRUM 0:119624335925 51 _client->update_authorize(MbedCloudClient::UpdateRequestDownload);
MACRUM 0:119624335925 52
MACRUM 0:119624335925 53 break;
MACRUM 0:119624335925 54
MACRUM 0:119624335925 55 /* Cloud Client wishes to reboot and apply the new firmware.
MACRUM 0:119624335925 56
MACRUM 0:119624335925 57 The user application is supposed to save all current work before rebooting.
MACRUM 0:119624335925 58
MACRUM 0:119624335925 59 Note: the authorization call can be postponed and called later.
MACRUM 0:119624335925 60 This doesn't affect the performance of the Cloud Client.
MACRUM 0:119624335925 61 */
MACRUM 0:119624335925 62 case MbedCloudClient::UpdateRequestInstall:
MACRUM 0:119624335925 63 printf("Firmware install requested\r\n");
MACRUM 0:119624335925 64 printf("Authorization granted\r\n");
MACRUM 0:119624335925 65 _client->update_authorize(MbedCloudClient::UpdateRequestInstall);
MACRUM 0:119624335925 66 break;
MACRUM 0:119624335925 67
MACRUM 0:119624335925 68 default:
MACRUM 0:119624335925 69 printf("Error - unknown request\r\n");
MACRUM 0:119624335925 70 break;
MACRUM 0:119624335925 71 }
MACRUM 0:119624335925 72 }
MACRUM 0:119624335925 73 #endif
MACRUM 0:119624335925 74 #endif
MACRUM 0:119624335925 75
MACRUM 0:119624335925 76 void update_progress(uint32_t progress, uint32_t total)
MACRUM 0:119624335925 77 {
MACRUM 0:119624335925 78 uint8_t percent = (uint8_t)((uint64_t)progress * 100 / total);
MACRUM 0:119624335925 79
MACRUM 0:119624335925 80 /* only show progress bar if debug trace is disabled */
MACRUM 0:119624335925 81 #if !defined(MBED_CONF_MBED_TRACE_ENABLE) \
MACRUM 0:119624335925 82 && !ARM_UC_ALL_TRACE_ENABLE \
MACRUM 0:119624335925 83 && !ARM_UC_HUB_TRACE_ENABLE
MACRUM 0:119624335925 84
MACRUM 0:119624335925 85 printf("\rDownloading: [");
MACRUM 0:119624335925 86 for (uint8_t index = 0; index < 50; index++)
MACRUM 0:119624335925 87 {
MACRUM 0:119624335925 88 if (index < percent / 2)
MACRUM 0:119624335925 89 {
MACRUM 0:119624335925 90 printf("+");
MACRUM 0:119624335925 91 }
MACRUM 0:119624335925 92 else if (index == percent / 2)
MACRUM 0:119624335925 93 {
MACRUM 0:119624335925 94 static uint8_t old_max = 0;
MACRUM 0:119624335925 95 static uint8_t counter = 0;
MACRUM 0:119624335925 96
MACRUM 0:119624335925 97 if (index == old_max)
MACRUM 0:119624335925 98 {
MACRUM 0:119624335925 99 counter++;
MACRUM 0:119624335925 100 }
MACRUM 0:119624335925 101 else
MACRUM 0:119624335925 102 {
MACRUM 0:119624335925 103 old_max = index;
MACRUM 0:119624335925 104 counter = 0;
MACRUM 0:119624335925 105 }
MACRUM 0:119624335925 106
MACRUM 0:119624335925 107 switch (counter % 4)
MACRUM 0:119624335925 108 {
MACRUM 0:119624335925 109 case 0:
MACRUM 0:119624335925 110 printf("/");
MACRUM 0:119624335925 111 break;
MACRUM 0:119624335925 112 case 1:
MACRUM 0:119624335925 113 printf("-");
MACRUM 0:119624335925 114 break;
MACRUM 0:119624335925 115 case 2:
MACRUM 0:119624335925 116 printf("\\");
MACRUM 0:119624335925 117 break;
MACRUM 0:119624335925 118 case 3:
MACRUM 0:119624335925 119 default:
MACRUM 0:119624335925 120 printf("|");
MACRUM 0:119624335925 121 break;
MACRUM 0:119624335925 122 }
MACRUM 0:119624335925 123 }
MACRUM 0:119624335925 124 else
MACRUM 0:119624335925 125 {
MACRUM 0:119624335925 126 printf(" ");
MACRUM 0:119624335925 127 }
MACRUM 0:119624335925 128 }
MACRUM 0:119624335925 129 printf("] %d %%", percent);
MACRUM 0:119624335925 130 fflush(stdout);
MACRUM 0:119624335925 131 #else
MACRUM 0:119624335925 132 printf("Downloading: %d %%\r\n", percent);
MACRUM 0:119624335925 133 #endif
MACRUM 0:119624335925 134
MACRUM 0:119624335925 135 if (progress == total)
MACRUM 0:119624335925 136 {
MACRUM 0:119624335925 137 printf("\r\nDownload completed\r\n");
MACRUM 0:119624335925 138 }
MACRUM 0:119624335925 139 }
MACRUM 0:119624335925 140
MACRUM 0:119624335925 141 #endif // MBED_CLOUD_CLIENT_SUPPORT_UPDATE