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