This is my quadcopter prototype software, still in development!

Committer:
Anaesthetix
Date:
Tue Jul 23 14:01:42 2013 +0000
Revision:
1:ac68f0368a77
Parent:
0:978110f7f027
Other accelerometer added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anaesthetix 0:978110f7f027 1 /* Title: time
Anaesthetix 0:978110f7f027 2 * Implementation of the C time.h functions
Anaesthetix 0:978110f7f027 3 *
Anaesthetix 0:978110f7f027 4 * Provides mechanisms to set and read the current time, based
Anaesthetix 0:978110f7f027 5 * on the microcontroller Real-Time Clock (RTC), plus some
Anaesthetix 0:978110f7f027 6 * standard C manipulation and formating functions.
Anaesthetix 0:978110f7f027 7 *
Anaesthetix 0:978110f7f027 8 * Example:
Anaesthetix 0:978110f7f027 9 * > #include "mbed.h"
Anaesthetix 0:978110f7f027 10 * >
Anaesthetix 0:978110f7f027 11 * > int main() {
Anaesthetix 0:978110f7f027 12 * > set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37
Anaesthetix 0:978110f7f027 13 * >
Anaesthetix 0:978110f7f027 14 * > while(1) {
Anaesthetix 0:978110f7f027 15 * > time_t seconds = time(NULL);
Anaesthetix 0:978110f7f027 16 * >
Anaesthetix 0:978110f7f027 17 * > printf("Time as seconds since January 1, 1970 = %d\n", seconds);
Anaesthetix 0:978110f7f027 18 * >
Anaesthetix 0:978110f7f027 19 * > printf("Time as a basic string = %s", ctime(&seconds));
Anaesthetix 0:978110f7f027 20 * >
Anaesthetix 0:978110f7f027 21 * > char buffer[32];
Anaesthetix 0:978110f7f027 22 * > strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
Anaesthetix 0:978110f7f027 23 * > printf("Time as a custom formatted string = %s", buffer);
Anaesthetix 0:978110f7f027 24 * >
Anaesthetix 0:978110f7f027 25 * > wait(1);
Anaesthetix 0:978110f7f027 26 * > }
Anaesthetix 0:978110f7f027 27 * > }
Anaesthetix 0:978110f7f027 28 */
Anaesthetix 0:978110f7f027 29
Anaesthetix 0:978110f7f027 30 /* mbed Microcontroller Library - rtc_time
Anaesthetix 0:978110f7f027 31 * Copyright (c) 2009 ARM Limited. All rights reserved.
Anaesthetix 0:978110f7f027 32 */
Anaesthetix 0:978110f7f027 33
Anaesthetix 0:978110f7f027 34 #include <time.h>
Anaesthetix 0:978110f7f027 35
Anaesthetix 0:978110f7f027 36 #ifdef __cplusplus
Anaesthetix 0:978110f7f027 37 extern "C" {
Anaesthetix 0:978110f7f027 38 #endif
Anaesthetix 0:978110f7f027 39
Anaesthetix 0:978110f7f027 40 #if 0 // for documentation only
Anaesthetix 0:978110f7f027 41 /* Function: time
Anaesthetix 0:978110f7f027 42 * Get the current time
Anaesthetix 0:978110f7f027 43 *
Anaesthetix 0:978110f7f027 44 * Returns the current timestamp as the number of seconds since January 1, 1970
Anaesthetix 0:978110f7f027 45 * (the UNIX timestamp). The value is based on the current value of the
Anaesthetix 0:978110f7f027 46 * microcontroller Real-Time Clock (RTC), which can be set using <set_time>.
Anaesthetix 0:978110f7f027 47 *
Anaesthetix 0:978110f7f027 48 * Example:
Anaesthetix 0:978110f7f027 49 * > #include "mbed.h"
Anaesthetix 0:978110f7f027 50 * >
Anaesthetix 0:978110f7f027 51 * > int main() {
Anaesthetix 0:978110f7f027 52 * > time_t seconds = time(NULL);
Anaesthetix 0:978110f7f027 53 * > printf("It is %d seconds since January 1, 1970\n", seconds);
Anaesthetix 0:978110f7f027 54 * > }
Anaesthetix 0:978110f7f027 55 *
Anaesthetix 0:978110f7f027 56 * Variables:
Anaesthetix 0:978110f7f027 57 * t - Pointer to a time_t to be set, or NULL if not used
Anaesthetix 0:978110f7f027 58 * returns - Number of seconds since January 1, 1970 (the UNIX timestamp)
Anaesthetix 0:978110f7f027 59 */
Anaesthetix 0:978110f7f027 60 time_t time(time_t *t);
Anaesthetix 0:978110f7f027 61 #endif
Anaesthetix 0:978110f7f027 62
Anaesthetix 0:978110f7f027 63 /* Function: set_time
Anaesthetix 0:978110f7f027 64 * Set the current time
Anaesthetix 0:978110f7f027 65 *
Anaesthetix 0:978110f7f027 66 * Initialises and sets the time of the microcontroller Real-Time Clock (RTC)
Anaesthetix 0:978110f7f027 67 * to the time represented by the number of seconds since January 1, 1970
Anaesthetix 0:978110f7f027 68 * (the UNIX timestamp).
Anaesthetix 0:978110f7f027 69 *
Anaesthetix 0:978110f7f027 70 * Example:
Anaesthetix 0:978110f7f027 71 * > #include "mbed.h"
Anaesthetix 0:978110f7f027 72 * >
Anaesthetix 0:978110f7f027 73 * > int main() {
Anaesthetix 0:978110f7f027 74 * > set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37
Anaesthetix 0:978110f7f027 75 * > }
Anaesthetix 0:978110f7f027 76 *
Anaesthetix 0:978110f7f027 77 * Variables:
Anaesthetix 0:978110f7f027 78 * t - Number of seconds since January 1, 1970 (the UNIX timestamp)
Anaesthetix 0:978110f7f027 79 */
Anaesthetix 0:978110f7f027 80 void set_time(time_t t);
Anaesthetix 0:978110f7f027 81
Anaesthetix 0:978110f7f027 82 #if 0 // for documentation only
Anaesthetix 0:978110f7f027 83 /* Function: mktime
Anaesthetix 0:978110f7f027 84 * Converts a tm structure in to a timestamp
Anaesthetix 0:978110f7f027 85 *
Anaesthetix 0:978110f7f027 86 * Converts the tm structure in to a timestamp in seconds since January 1, 1970
Anaesthetix 0:978110f7f027 87 * (the UNIX timestamp). The values of tm_wday and tm_yday of the tm structure
Anaesthetix 0:978110f7f027 88 * are also updated to their appropriate values.
Anaesthetix 0:978110f7f027 89 *
Anaesthetix 0:978110f7f027 90 * Example:
Anaesthetix 0:978110f7f027 91 * > #include "mbed.h"
Anaesthetix 0:978110f7f027 92 * >
Anaesthetix 0:978110f7f027 93 * > int main() {
Anaesthetix 0:978110f7f027 94 * > // setup time structure for Wed, 28 Oct 2009 11:35:37
Anaesthetix 0:978110f7f027 95 * > struct tm t;
Anaesthetix 0:978110f7f027 96 * > t.tm_sec = 37; // 0-59
Anaesthetix 0:978110f7f027 97 * > t.tm_min = 35; // 0-59
Anaesthetix 0:978110f7f027 98 * > t.tm_hour = 11; // 0-23
Anaesthetix 0:978110f7f027 99 * > t.tm_mday = 28; // 1-31
Anaesthetix 0:978110f7f027 100 * > t.tm_mon = 9; // 0-11
Anaesthetix 0:978110f7f027 101 * > t.tm_year = 109; // year since 1900
Anaesthetix 0:978110f7f027 102 * >
Anaesthetix 0:978110f7f027 103 * > // convert to timestamp and display (1256729737)
Anaesthetix 0:978110f7f027 104 * > time_t seconds = mktime(&t);
Anaesthetix 0:978110f7f027 105 * > printf("Time as seconds since January 1, 1970 = %d\n", seconds);
Anaesthetix 0:978110f7f027 106 * > }
Anaesthetix 0:978110f7f027 107 *
Anaesthetix 0:978110f7f027 108 * Variables:
Anaesthetix 0:978110f7f027 109 * t - The tm structure to convert
Anaesthetix 0:978110f7f027 110 * returns - The converted timestamp
Anaesthetix 0:978110f7f027 111 */
Anaesthetix 0:978110f7f027 112 time_t mktime(struct tm *t);
Anaesthetix 0:978110f7f027 113 #endif
Anaesthetix 0:978110f7f027 114
Anaesthetix 0:978110f7f027 115 #if 0 // for documentation only
Anaesthetix 0:978110f7f027 116 /* Function: localtime
Anaesthetix 0:978110f7f027 117 * Converts a timestamp in to a tm structure
Anaesthetix 0:978110f7f027 118 *
Anaesthetix 0:978110f7f027 119 * Converts the timestamp pointed to by t to a (statically allocated)
Anaesthetix 0:978110f7f027 120 * tm structure.
Anaesthetix 0:978110f7f027 121 *
Anaesthetix 0:978110f7f027 122 * Example:
Anaesthetix 0:978110f7f027 123 * > #include "mbed.h"
Anaesthetix 0:978110f7f027 124 * >
Anaesthetix 0:978110f7f027 125 * > int main() {
Anaesthetix 0:978110f7f027 126 * > time_t seconds = 1256729737;
Anaesthetix 0:978110f7f027 127 * > struct tm *t = localtime(&seconds);
Anaesthetix 0:978110f7f027 128 * > }
Anaesthetix 0:978110f7f027 129 *
Anaesthetix 0:978110f7f027 130 * Variables:
Anaesthetix 0:978110f7f027 131 * t - Pointer to the timestamp
Anaesthetix 0:978110f7f027 132 * returns - Pointer to the (statically allocated) tm structure
Anaesthetix 0:978110f7f027 133 */
Anaesthetix 0:978110f7f027 134 struct tm *localtime(const time_t *t);
Anaesthetix 0:978110f7f027 135 #endif
Anaesthetix 0:978110f7f027 136
Anaesthetix 0:978110f7f027 137 #if 0 // for documentation only
Anaesthetix 0:978110f7f027 138 /* Function: ctime
Anaesthetix 0:978110f7f027 139 * Converts a timestamp to a human-readable string
Anaesthetix 0:978110f7f027 140 *
Anaesthetix 0:978110f7f027 141 * Converts a time_t timestamp in seconds since January 1, 1970 (the UNIX
Anaesthetix 0:978110f7f027 142 * timestamp) to a human readable string format. The result is of the
Anaesthetix 0:978110f7f027 143 * format: "Wed Oct 28 11:35:37 2009\n"
Anaesthetix 0:978110f7f027 144 *
Anaesthetix 0:978110f7f027 145 * Example:
Anaesthetix 0:978110f7f027 146 * > #include "mbed.h"
Anaesthetix 0:978110f7f027 147 * >
Anaesthetix 0:978110f7f027 148 * > int main() {
Anaesthetix 0:978110f7f027 149 * > time_t seconds = time(NULL);
Anaesthetix 0:978110f7f027 150 * > printf("Time as a string = %s", ctime(&seconds));
Anaesthetix 0:978110f7f027 151 * > }
Anaesthetix 0:978110f7f027 152 *
Anaesthetix 0:978110f7f027 153 * Variables:
Anaesthetix 0:978110f7f027 154 * t - The timestamp to convert
Anaesthetix 0:978110f7f027 155 * returns - Pointer to a (statically allocated) string containing the
Anaesthetix 0:978110f7f027 156 * human readable representation, including a '\n' character
Anaesthetix 0:978110f7f027 157 */
Anaesthetix 0:978110f7f027 158 char *ctime(const time_t *t);
Anaesthetix 0:978110f7f027 159 #endif
Anaesthetix 0:978110f7f027 160
Anaesthetix 0:978110f7f027 161 #if 0 // for documentation only
Anaesthetix 0:978110f7f027 162 /* Function: strftime
Anaesthetix 0:978110f7f027 163 * Converts a tm structure to a custom format human-readable string
Anaesthetix 0:978110f7f027 164 *
Anaesthetix 0:978110f7f027 165 * Creates a formated string from a tm structure, based on a string format
Anaesthetix 0:978110f7f027 166 * specifier provided.
Anaesthetix 0:978110f7f027 167 *
Anaesthetix 0:978110f7f027 168 * Format Specifiers:
Anaesthetix 0:978110f7f027 169 * %S - Second (00-59)
Anaesthetix 0:978110f7f027 170 * %M - Minute (00-59)
Anaesthetix 0:978110f7f027 171 * %H - Hour (00-23)
Anaesthetix 0:978110f7f027 172 * %d - Day (01-31)
Anaesthetix 0:978110f7f027 173 * %m - Month (01-12)
Anaesthetix 0:978110f7f027 174 * %Y/%y - Year (2009/09)
Anaesthetix 0:978110f7f027 175 *
Anaesthetix 0:978110f7f027 176 * %A/%a - Weekday Name (Monday/Mon)
Anaesthetix 0:978110f7f027 177 * %B/%b - Month Name (January/Jan)
Anaesthetix 0:978110f7f027 178 * %I - 12 Hour Format (01-12)
Anaesthetix 0:978110f7f027 179 * %p - "AM" or "PM"
Anaesthetix 0:978110f7f027 180 * %X - Time (14:55:02)
Anaesthetix 0:978110f7f027 181 * %x - Date (08/23/01)
Anaesthetix 0:978110f7f027 182 *
Anaesthetix 0:978110f7f027 183 * Example:
Anaesthetix 0:978110f7f027 184 * > #include "mbed.h"
Anaesthetix 0:978110f7f027 185 * >
Anaesthetix 0:978110f7f027 186 * > int main() {
Anaesthetix 0:978110f7f027 187 * > time_t seconds = time(NULL);
Anaesthetix 0:978110f7f027 188 * >
Anaesthetix 0:978110f7f027 189 * > char buffer[32];
Anaesthetix 0:978110f7f027 190 * > strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
Anaesthetix 0:978110f7f027 191 * > printf("Time as a formatted string = %s", buffer);
Anaesthetix 0:978110f7f027 192 * > }
Anaesthetix 0:978110f7f027 193 *
Anaesthetix 0:978110f7f027 194 * Variables:
Anaesthetix 0:978110f7f027 195 * buffer - String buffer to store the result
Anaesthetix 0:978110f7f027 196 * max - Maximum number of characters to store in the buffer
Anaesthetix 0:978110f7f027 197 * format - Format specifier string
Anaesthetix 0:978110f7f027 198 * t - Pointer to the tm structure to convert
Anaesthetix 0:978110f7f027 199 * returns - Number of characters copied
Anaesthetix 0:978110f7f027 200 */
Anaesthetix 0:978110f7f027 201 size_t strftime(char *buffer, size_t max, const char *format, const struct tm *t);
Anaesthetix 0:978110f7f027 202 #endif
Anaesthetix 0:978110f7f027 203
Anaesthetix 0:978110f7f027 204 #ifdef __cplusplus
Anaesthetix 0:978110f7f027 205 }
Anaesthetix 0:978110f7f027 206 #endif