The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:20e0c61e0684 1 /* mbed Microcontroller Library
ganlikun 0:20e0c61e0684 2 * Copyright (c) 2017-2017 ARM Limited
ganlikun 0:20e0c61e0684 3 *
ganlikun 0:20e0c61e0684 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:20e0c61e0684 5 * you may not use this file except in compliance with the License.
ganlikun 0:20e0c61e0684 6 * You may obtain a copy of the License at
ganlikun 0:20e0c61e0684 7 *
ganlikun 0:20e0c61e0684 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:20e0c61e0684 9 *
ganlikun 0:20e0c61e0684 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:20e0c61e0684 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:20e0c61e0684 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:20e0c61e0684 13 * See the License for the specific language governing permissions and
ganlikun 0:20e0c61e0684 14 * limitations under the License.
ganlikun 0:20e0c61e0684 15 */
ganlikun 0:20e0c61e0684 16
ganlikun 0:20e0c61e0684 17 #include "mbed_mktime.h"
ganlikun 0:20e0c61e0684 18
ganlikun 0:20e0c61e0684 19 /*
ganlikun 0:20e0c61e0684 20 * time constants
ganlikun 0:20e0c61e0684 21 */
ganlikun 0:20e0c61e0684 22 #define SECONDS_BY_MINUTES 60
ganlikun 0:20e0c61e0684 23 #define MINUTES_BY_HOUR 60
ganlikun 0:20e0c61e0684 24 #define SECONDS_BY_HOUR (SECONDS_BY_MINUTES * MINUTES_BY_HOUR)
ganlikun 0:20e0c61e0684 25 #define HOURS_BY_DAY 24
ganlikun 0:20e0c61e0684 26 #define SECONDS_BY_DAY (SECONDS_BY_HOUR * HOURS_BY_DAY)
ganlikun 0:20e0c61e0684 27
ganlikun 0:20e0c61e0684 28 /*
ganlikun 0:20e0c61e0684 29 * 2 dimensional array containing the number of seconds elapsed before a given
ganlikun 0:20e0c61e0684 30 * month.
ganlikun 0:20e0c61e0684 31 * The second index map to the month while the first map to the type of year:
ganlikun 0:20e0c61e0684 32 * - 0: non leap year
ganlikun 0:20e0c61e0684 33 * - 1: leap year
ganlikun 0:20e0c61e0684 34 */
ganlikun 0:20e0c61e0684 35 static const uint32_t seconds_before_month[2][12] = {
ganlikun 0:20e0c61e0684 36 {
ganlikun 0:20e0c61e0684 37 0,
ganlikun 0:20e0c61e0684 38 31 * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 39 (31 + 28) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 40 (31 + 28 + 31) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 41 (31 + 28 + 31 + 30) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 42 (31 + 28 + 31 + 30 + 31) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 43 (31 + 28 + 31 + 30 + 31 + 30) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 44 (31 + 28 + 31 + 30 + 31 + 30 + 31) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 45 (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 46 (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 47 (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 48 (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 49 },
ganlikun 0:20e0c61e0684 50 {
ganlikun 0:20e0c61e0684 51 0,
ganlikun 0:20e0c61e0684 52 31 * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 53 (31 + 29) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 54 (31 + 29 + 31) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 55 (31 + 29 + 31 + 30) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 56 (31 + 29 + 31 + 30 + 31) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 57 (31 + 29 + 31 + 30 + 31 + 30) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 58 (31 + 29 + 31 + 30 + 31 + 30 + 31) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 59 (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 60 (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 61 (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 62 (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) * SECONDS_BY_DAY,
ganlikun 0:20e0c61e0684 63 }
ganlikun 0:20e0c61e0684 64 };
ganlikun 0:20e0c61e0684 65
ganlikun 0:20e0c61e0684 66 bool _rtc_is_leap_year(int year) {
ganlikun 0:20e0c61e0684 67 /*
ganlikun 0:20e0c61e0684 68 * since in practice, the value manipulated by this algorithm lie in the
ganlikun 0:20e0c61e0684 69 * range [70 : 138], the algorith can be reduced to: year % 4.
ganlikun 0:20e0c61e0684 70 * The algorithm valid over the full range of value is:
ganlikun 0:20e0c61e0684 71
ganlikun 0:20e0c61e0684 72 year = 1900 + year;
ganlikun 0:20e0c61e0684 73 if (year % 4) {
ganlikun 0:20e0c61e0684 74 return false;
ganlikun 0:20e0c61e0684 75 } else if (year % 100) {
ganlikun 0:20e0c61e0684 76 return true;
ganlikun 0:20e0c61e0684 77 } else if (year % 400) {
ganlikun 0:20e0c61e0684 78 return false;
ganlikun 0:20e0c61e0684 79 }
ganlikun 0:20e0c61e0684 80 return true;
ganlikun 0:20e0c61e0684 81
ganlikun 0:20e0c61e0684 82 */
ganlikun 0:20e0c61e0684 83 return (year) % 4 ? false : true;
ganlikun 0:20e0c61e0684 84 }
ganlikun 0:20e0c61e0684 85
ganlikun 0:20e0c61e0684 86 time_t _rtc_mktime(const struct tm* time) {
ganlikun 0:20e0c61e0684 87 // partial check for the upper bound of the range
ganlikun 0:20e0c61e0684 88 // normalization might happen at the end of the function
ganlikun 0:20e0c61e0684 89 // this solution is faster than checking if the input is after the 19th of
ganlikun 0:20e0c61e0684 90 // january 2038 at 03:14:07.
ganlikun 0:20e0c61e0684 91 if ((time->tm_year < 70) || (time->tm_year > 138)) {
ganlikun 0:20e0c61e0684 92 return ((time_t) -1);
ganlikun 0:20e0c61e0684 93 }
ganlikun 0:20e0c61e0684 94
ganlikun 0:20e0c61e0684 95 uint32_t result = time->tm_sec;
ganlikun 0:20e0c61e0684 96 result += time->tm_min * SECONDS_BY_MINUTES;
ganlikun 0:20e0c61e0684 97 result += time->tm_hour * SECONDS_BY_HOUR;
ganlikun 0:20e0c61e0684 98 result += (time->tm_mday - 1) * SECONDS_BY_DAY;
ganlikun 0:20e0c61e0684 99 result += seconds_before_month[_rtc_is_leap_year(time->tm_year)][time->tm_mon];
ganlikun 0:20e0c61e0684 100
ganlikun 0:20e0c61e0684 101 if (time->tm_year > 70) {
ganlikun 0:20e0c61e0684 102 // valid in the range [70:138]
ganlikun 0:20e0c61e0684 103 uint32_t count_of_leap_days = ((time->tm_year - 1) / 4) - (70 / 4);
ganlikun 0:20e0c61e0684 104 result += (((time->tm_year - 70) * 365) + count_of_leap_days) * SECONDS_BY_DAY;
ganlikun 0:20e0c61e0684 105 }
ganlikun 0:20e0c61e0684 106
ganlikun 0:20e0c61e0684 107 if (result > INT32_MAX) {
ganlikun 0:20e0c61e0684 108 return (time_t) -1;
ganlikun 0:20e0c61e0684 109 }
ganlikun 0:20e0c61e0684 110
ganlikun 0:20e0c61e0684 111 return result;
ganlikun 0:20e0c61e0684 112 }
ganlikun 0:20e0c61e0684 113
ganlikun 0:20e0c61e0684 114 bool _rtc_localtime(time_t timestamp, struct tm* time_info) {
ganlikun 0:20e0c61e0684 115 if (((int32_t) timestamp) < 0) {
ganlikun 0:20e0c61e0684 116 return false;
ganlikun 0:20e0c61e0684 117 }
ganlikun 0:20e0c61e0684 118
ganlikun 0:20e0c61e0684 119 time_info->tm_sec = timestamp % 60;
ganlikun 0:20e0c61e0684 120 timestamp = timestamp / 60; // timestamp in minutes
ganlikun 0:20e0c61e0684 121 time_info->tm_min = timestamp % 60;
ganlikun 0:20e0c61e0684 122 timestamp = timestamp / 60; // timestamp in hours
ganlikun 0:20e0c61e0684 123 time_info->tm_hour = timestamp % 24;
ganlikun 0:20e0c61e0684 124 timestamp = timestamp / 24; // timestamp in days;
ganlikun 0:20e0c61e0684 125
ganlikun 0:20e0c61e0684 126 // compute the weekday
ganlikun 0:20e0c61e0684 127 // The 1st of January 1970 was a Thursday which is equal to 4 in the weekday
ganlikun 0:20e0c61e0684 128 // representation ranging from [0:6]
ganlikun 0:20e0c61e0684 129 time_info->tm_wday = (timestamp + 4) % 7;
ganlikun 0:20e0c61e0684 130
ganlikun 0:20e0c61e0684 131 // years start at 70
ganlikun 0:20e0c61e0684 132 time_info->tm_year = 70;
ganlikun 0:20e0c61e0684 133 while (true) {
ganlikun 0:20e0c61e0684 134 if (_rtc_is_leap_year(time_info->tm_year) && timestamp >= 366) {
ganlikun 0:20e0c61e0684 135 ++time_info->tm_year;
ganlikun 0:20e0c61e0684 136 timestamp -= 366;
ganlikun 0:20e0c61e0684 137 } else if (!_rtc_is_leap_year(time_info->tm_year) && timestamp >= 365) {
ganlikun 0:20e0c61e0684 138 ++time_info->tm_year;
ganlikun 0:20e0c61e0684 139 timestamp -= 365;
ganlikun 0:20e0c61e0684 140 } else {
ganlikun 0:20e0c61e0684 141 // the remaining days are less than a years
ganlikun 0:20e0c61e0684 142 break;
ganlikun 0:20e0c61e0684 143 }
ganlikun 0:20e0c61e0684 144 }
ganlikun 0:20e0c61e0684 145
ganlikun 0:20e0c61e0684 146 time_info->tm_yday = timestamp;
ganlikun 0:20e0c61e0684 147
ganlikun 0:20e0c61e0684 148 // convert days into seconds and find the current month
ganlikun 0:20e0c61e0684 149 timestamp *= SECONDS_BY_DAY;
ganlikun 0:20e0c61e0684 150 time_info->tm_mon = 11;
ganlikun 0:20e0c61e0684 151 bool leap = _rtc_is_leap_year(time_info->tm_year);
ganlikun 0:20e0c61e0684 152 for (uint32_t i = 0; i < 12; ++i) {
ganlikun 0:20e0c61e0684 153 if ((uint32_t) timestamp < seconds_before_month[leap][i]) {
ganlikun 0:20e0c61e0684 154 time_info->tm_mon = i - 1;
ganlikun 0:20e0c61e0684 155 break;
ganlikun 0:20e0c61e0684 156 }
ganlikun 0:20e0c61e0684 157 }
ganlikun 0:20e0c61e0684 158
ganlikun 0:20e0c61e0684 159 // remove month from timestamp and compute the number of days.
ganlikun 0:20e0c61e0684 160 // note: unlike other fields, days are not 0 indexed.
ganlikun 0:20e0c61e0684 161 timestamp -= seconds_before_month[leap][time_info->tm_mon];
ganlikun 0:20e0c61e0684 162 time_info->tm_mday = (timestamp / SECONDS_BY_DAY) + 1;
ganlikun 0:20e0c61e0684 163
ganlikun 0:20e0c61e0684 164 return true;
ganlikun 0:20e0c61e0684 165 }
ganlikun 0:20e0c61e0684 166