Satellite Observers Workbench. NOT yet complete, just published for forum posters to \"cherry pick\" pieces of code as requiered as an example.

Dependencies:   mbed

Committer:
AjK
Date:
Mon Oct 11 10:34:55 2010 +0000
Revision:
0:0a841b89d614
Totally Alpha quality as this project isn\t completed. Just publishing it as it answers many questions asked in the forums

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:0a841b89d614 1 /****************************************************************************
AjK 0:0a841b89d614 2 * Copyright 2010 Andy Kirkham, Stellar Technologies Ltd
AjK 0:0a841b89d614 3 *
AjK 0:0a841b89d614 4 * This file is part of the Satellite Observers Workbench (SOWB).
AjK 0:0a841b89d614 5 *
AjK 0:0a841b89d614 6 * SOWB is free software: you can redistribute it and/or modify
AjK 0:0a841b89d614 7 * it under the terms of the GNU General Public License as published by
AjK 0:0a841b89d614 8 * the Free Software Foundation, either version 3 of the License, or
AjK 0:0a841b89d614 9 * (at your option) any later version.
AjK 0:0a841b89d614 10 *
AjK 0:0a841b89d614 11 * SOWB is distributed in the hope that it will be useful,
AjK 0:0a841b89d614 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
AjK 0:0a841b89d614 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
AjK 0:0a841b89d614 14 * GNU General Public License for more details.
AjK 0:0a841b89d614 15 *
AjK 0:0a841b89d614 16 * You should have received a copy of the GNU General Public License
AjK 0:0a841b89d614 17 * along with SOWB. If not, see <http://www.gnu.org/licenses/>.
AjK 0:0a841b89d614 18 *
AjK 0:0a841b89d614 19 * $Id: main.cpp 5 2010-07-12 20:51:11Z ajk $
AjK 0:0a841b89d614 20 *
AjK 0:0a841b89d614 21 ***************************************************************************/
AjK 0:0a841b89d614 22
AjK 0:0a841b89d614 23
AjK 0:0a841b89d614 24 #include "sowb.h"
AjK 0:0a841b89d614 25 #include "ctype.h"
AjK 0:0a841b89d614 26 #include "utils.h"
AjK 0:0a841b89d614 27 #include "gps.h"
AjK 0:0a841b89d614 28 #include "debug.h"
AjK 0:0a841b89d614 29
AjK 0:0a841b89d614 30 /** ascii2bin
AjK 0:0a841b89d614 31 *
AjK 0:0a841b89d614 32 * Converts an ascii char to binary nibble.
AjK 0:0a841b89d614 33 *
AjK 0:0a841b89d614 34 * @param char The character to convert [0-9][a-f][A-F]
AjK 0:0a841b89d614 35 * @return char the bin value or -1 on invalid hex char.
AjK 0:0a841b89d614 36 */
AjK 0:0a841b89d614 37 char ascii2bin(char c) {
AjK 0:0a841b89d614 38 if (c >= '0' && c <= '9') return (c - '0') & 0xF;
AjK 0:0a841b89d614 39 if (c >= 'A' && c <= 'F') return (c - 'A' + 10) & 0xF;
AjK 0:0a841b89d614 40 if (c >= 'a' && c <= 'f') return (c - 'a' + 10) & 0xF;
AjK 0:0a841b89d614 41 return (char)0xFF;
AjK 0:0a841b89d614 42 }
AjK 0:0a841b89d614 43
AjK 0:0a841b89d614 44 /** hex2bin
AjK 0:0a841b89d614 45 *
AjK 0:0a841b89d614 46 * Converts a hex ascii string to binary int.
AjK 0:0a841b89d614 47 *
AjK 0:0a841b89d614 48 * Note, no error checking, assume string is valid hex chars [0-9][a-f][A-F]
AjK 0:0a841b89d614 49 *
AjK 0:0a841b89d614 50 * @param char *s The string to convert.
AjK 0:0a841b89d614 51 * @param int len The length of the string to convert.
AjK 0:0a841b89d614 52 * @return uint32_t the converted value.
AjK 0:0a841b89d614 53 */
AjK 0:0a841b89d614 54 uint32_t hex2bin(char *s, int len) {
AjK 0:0a841b89d614 55 int i;
AjK 0:0a841b89d614 56 uint32_t rval;
AjK 0:0a841b89d614 57
AjK 0:0a841b89d614 58 for (rval = 0, i = 0; i < len; i++) rval = rval | (ascii2bin(*(s + i)) << ((len - i - 1) * 4));
AjK 0:0a841b89d614 59
AjK 0:0a841b89d614 60 return rval;
AjK 0:0a841b89d614 61 }
AjK 0:0a841b89d614 62
AjK 0:0a841b89d614 63 /** bin2ascii
AjK 0:0a841b89d614 64 *
AjK 0:0a841b89d614 65 * Convert a nibble to an ASCII character
AjK 0:0a841b89d614 66 *
AjK 0:0a841b89d614 67 * @param char c The nibble to convert
AjK 0:0a841b89d614 68 * @return char The character representation of the nibble.
AjK 0:0a841b89d614 69 */
AjK 0:0a841b89d614 70 char bin2ascii(char c) {
AjK 0:0a841b89d614 71 c &= 0xF;
AjK 0:0a841b89d614 72 if (c < 0xA) return c + '0';
AjK 0:0a841b89d614 73 return c + 'A' - 10;
AjK 0:0a841b89d614 74 }
AjK 0:0a841b89d614 75
AjK 0:0a841b89d614 76 /** bin2hex
AjK 0:0a841b89d614 77 *
AjK 0:0a841b89d614 78 * Convert a binary to a hex string representation.
AjK 0:0a841b89d614 79 * The caller should allocate a buffer for *s before
AjK 0:0a841b89d614 80 * calling this function. The allocation should be
AjK 0:0a841b89d614 81 * len + 1 in length to hold the string and the
AjK 0:0a841b89d614 82 * terminating null character.
AjK 0:0a841b89d614 83 *
AjK 0:0a841b89d614 84 * @param uint32_t d The value to convert.
AjK 0:0a841b89d614 85 * @param int len The string length.
AjK 0:0a841b89d614 86 * @param char *s Where to put the string.
AjK 0:0a841b89d614 87 * @return char * Returns *s passed in.
AjK 0:0a841b89d614 88 */
AjK 0:0a841b89d614 89 // 238E,238E
AjK 0:0a841b89d614 90 // O832,O832
AjK 0:0a841b89d614 91
AjK 0:0a841b89d614 92 char * bin2hex(uint32_t d, int len, char *s) {
AjK 0:0a841b89d614 93 char c, i = 0;
AjK 0:0a841b89d614 94 *(s + len) = '\0';
AjK 0:0a841b89d614 95 while (len) {
AjK 0:0a841b89d614 96 c = (d >> (4 * (len - 1))) & 0xF;
AjK 0:0a841b89d614 97 *(s + i) = bin2ascii(c);
AjK 0:0a841b89d614 98 len--; i++;
AjK 0:0a841b89d614 99 }
AjK 0:0a841b89d614 100 return s;
AjK 0:0a841b89d614 101 }
AjK 0:0a841b89d614 102
AjK 0:0a841b89d614 103 /** dec2bin
AjK 0:0a841b89d614 104 *
AjK 0:0a841b89d614 105 * Converts a decimal ascii string to binary int.
AjK 0:0a841b89d614 106 *
AjK 0:0a841b89d614 107 * Note, no error checking, assume string is valid hex chars [0-9]
AjK 0:0a841b89d614 108 *
AjK 0:0a841b89d614 109 * @param char *s The string to convert.
AjK 0:0a841b89d614 110 * @param int len The length of the string to convert.
AjK 0:0a841b89d614 111 * @return uint32_t the converted value.
AjK 0:0a841b89d614 112 */
AjK 0:0a841b89d614 113 uint32_t dec2bin(char *s, int len) {
AjK 0:0a841b89d614 114 int i, mul;
AjK 0:0a841b89d614 115 uint32_t rval = 0;
AjK 0:0a841b89d614 116
AjK 0:0a841b89d614 117 for (mul = 1, i = len; i; i--, mul *= 10) rval += (ascii2bin(*(s + i - 1)) * mul);
AjK 0:0a841b89d614 118
AjK 0:0a841b89d614 119 return rval;
AjK 0:0a841b89d614 120 }
AjK 0:0a841b89d614 121
AjK 0:0a841b89d614 122 /** strcsuml
AjK 0:0a841b89d614 123 *
AjK 0:0a841b89d614 124 * Return a two's compliment checksum char for th esupplied string.
AjK 0:0a841b89d614 125 *
AjK 0:0a841b89d614 126 * @param char * s The string to sum
AjK 0:0a841b89d614 127 * @param int len The length of the string.
AjK 0:0a841b89d614 128 * @return The two's compliment char.
AjK 0:0a841b89d614 129 */
AjK 0:0a841b89d614 130 char strcsuml(char *s, int len) {
AjK 0:0a841b89d614 131 char sum = 0;
AjK 0:0a841b89d614 132 while (len) {
AjK 0:0a841b89d614 133 sum += *(s +len - 1);
AjK 0:0a841b89d614 134 }
AjK 0:0a841b89d614 135 return (~sum) + 1;
AjK 0:0a841b89d614 136 }
AjK 0:0a841b89d614 137
AjK 0:0a841b89d614 138 /** strcsum
AjK 0:0a841b89d614 139 *
AjK 0:0a841b89d614 140 * Return a two's compliment checksum char for the null terminated supplied string.
AjK 0:0a841b89d614 141 *
AjK 0:0a841b89d614 142 * @param char * s The string to sum
AjK 0:0a841b89d614 143 * @return The two's compliment char.
AjK 0:0a841b89d614 144 */
AjK 0:0a841b89d614 145 char strcsum(char *s) {
AjK 0:0a841b89d614 146 return strcsuml(s, strlen(s));
AjK 0:0a841b89d614 147 }
AjK 0:0a841b89d614 148
AjK 0:0a841b89d614 149 /** strsuml
AjK 0:0a841b89d614 150 *
AjK 0:0a841b89d614 151 * Return the 8bit sum char for the supplied string.
AjK 0:0a841b89d614 152 *
AjK 0:0a841b89d614 153 * @param char * s The string to sum
AjK 0:0a841b89d614 154 * @param int len The length of the string.
AjK 0:0a841b89d614 155 * @return The sum
AjK 0:0a841b89d614 156 */
AjK 0:0a841b89d614 157 char strsuml(char *s, int len) {
AjK 0:0a841b89d614 158 char sum = 0;
AjK 0:0a841b89d614 159 while (len) {
AjK 0:0a841b89d614 160 sum += *(s +len - 1);
AjK 0:0a841b89d614 161 }
AjK 0:0a841b89d614 162 return sum;
AjK 0:0a841b89d614 163 }
AjK 0:0a841b89d614 164
AjK 0:0a841b89d614 165 /** strsum
AjK 0:0a841b89d614 166 *
AjK 0:0a841b89d614 167 * Return the 8bit sum of all the characters of the supplied string.
AjK 0:0a841b89d614 168 *
AjK 0:0a841b89d614 169 * @param char * s The string to sum
AjK 0:0a841b89d614 170 * @return The sum
AjK 0:0a841b89d614 171 */
AjK 0:0a841b89d614 172 char strsum(char *s) {
AjK 0:0a841b89d614 173 return strsuml(s, strlen(s));
AjK 0:0a841b89d614 174 }
AjK 0:0a841b89d614 175
AjK 0:0a841b89d614 176 /* Used for the date_AsString function. */
AjK 0:0a841b89d614 177 const char month_abv[][4] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Wtf" };
AjK 0:0a841b89d614 178
AjK 0:0a841b89d614 179 /** date_AsString
AjK 0:0a841b89d614 180 *
AjK 0:0a841b89d614 181 * Used to get the current date and return a formatted string.
AjK 0:0a841b89d614 182 * Note, the caller must have a 12byte buffer in place, pointed
AjK 0:0a841b89d614 183 * to by s to accept the string.
AjK 0:0a841b89d614 184 *
AjK 0:0a841b89d614 185 * @param GPS_TIME *t A pointer to the time data struct to "print as"
AjK 0:0a841b89d614 186 * @param char *s A pointer to a buffer to hold the formatted string.
AjK 0:0a841b89d614 187 */
AjK 0:0a841b89d614 188 void date_AsString(GPS_TIME *t, char *s) {
AjK 0:0a841b89d614 189 int month = t->month - 1; if (month > 11 || month < 0) month = 12; /* Ensure in range. */
AjK 0:0a841b89d614 190 sprintf(s, "%4.4d/%s/%2.2d", t->year, month_abv[month], t->day);
AjK 0:0a841b89d614 191 }
AjK 0:0a841b89d614 192
AjK 0:0a841b89d614 193 /** time_AsString
AjK 0:0a841b89d614 194 *
AjK 0:0a841b89d614 195 * Used to get the current time and return a formatted string.
AjK 0:0a841b89d614 196 * Note, the caller must have a 12byte buffer in place, pointed
AjK 0:0a841b89d614 197 * to by s to accept the string.
AjK 0:0a841b89d614 198 *
AjK 0:0a841b89d614 199 * @param GPS_TIME *t A pointer to the time data struct to "print as"
AjK 0:0a841b89d614 200 * @param char *s A pointer to a buffer to hold the formatted string.
AjK 0:0a841b89d614 201 */
AjK 0:0a841b89d614 202 void time_AsString(GPS_TIME *t, char *s) {
AjK 0:0a841b89d614 203 sprintf(s, "%2.2d:%2.2d:%2.2d.%1.1d%1.1d", t->hour, t->minute, t->second, t->tenth, t->hundreth);
AjK 0:0a841b89d614 204 }
AjK 0:0a841b89d614 205
AjK 0:0a841b89d614 206 /** double2dms
AjK 0:0a841b89d614 207 *
AjK 0:0a841b89d614 208 * Takes double and converts it to a printable display string of
AjK 0:0a841b89d614 209 * degrees, minutes and seconds.
AjK 0:0a841b89d614 210 * The caller is responsible for allocating the buffer *s before
AjK 0:0a841b89d614 211 * calling this function.
AjK 0:0a841b89d614 212 *
AjK 0:0a841b89d614 213 * @param char *s A pointer to the buffer to print to.
AjK 0:0a841b89d614 214 * @param double d The value to print.
AjK 0:0a841b89d614 215 */
AjK 0:0a841b89d614 216 void double2dms(char *s, double d) {
AjK 0:0a841b89d614 217 int degrees, minutes;
AjK 0:0a841b89d614 218 double seconds, t;
AjK 0:0a841b89d614 219
AjK 0:0a841b89d614 220 degrees = (int)d; t = (d - (double)degrees) * 60.;
AjK 0:0a841b89d614 221 minutes = (int)t;
AjK 0:0a841b89d614 222 seconds = (t - (double)minutes) * 60.;
AjK 0:0a841b89d614 223
AjK 0:0a841b89d614 224 sprintf(s, "%03d\xb0%02d\x27%02d\x22", degrees, minutes, (int)seconds);
AjK 0:0a841b89d614 225 }
AjK 0:0a841b89d614 226
AjK 0:0a841b89d614 227 /** printDouble
AjK 0:0a841b89d614 228 *
AjK 0:0a841b89d614 229 * Print a double to a string buffer with correct leading zero(s).
AjK 0:0a841b89d614 230 * The caller is responsible for allocating the buffer *s before
AjK 0:0a841b89d614 231 * calling this function.
AjK 0:0a841b89d614 232 *
AjK 0:0a841b89d614 233 * @param char *s A pointer to the buffer to print to.
AjK 0:0a841b89d614 234 * @param double d The value to print.
AjK 0:0a841b89d614 235 */
AjK 0:0a841b89d614 236 void printDouble(char *s, double d) {
AjK 0:0a841b89d614 237 if (isnan(d)) sprintf(s, "---.----");
AjK 0:0a841b89d614 238 else if (d > 100.) sprintf(s, "%.4f", d);
AjK 0:0a841b89d614 239 else if (d > 10.) sprintf(s, "0%.4f", d);
AjK 0:0a841b89d614 240 else sprintf(s, "00%.4f", d);
AjK 0:0a841b89d614 241 }
AjK 0:0a841b89d614 242
AjK 0:0a841b89d614 243 /** printDouble_3_1
AjK 0:0a841b89d614 244 *
AjK 0:0a841b89d614 245 * Print a double to a string buffer with correct leading zero(s).
AjK 0:0a841b89d614 246 * The caller is responsible for allocating the buffer *s before
AjK 0:0a841b89d614 247 * calling this function.
AjK 0:0a841b89d614 248 *
AjK 0:0a841b89d614 249 * @param char *s A pointer to the buffer to print to.
AjK 0:0a841b89d614 250 * @param double d The value to print.
AjK 0:0a841b89d614 251 */
AjK 0:0a841b89d614 252 char * printDouble_3_1(char *s, double d) {
AjK 0:0a841b89d614 253 char temp[16];
AjK 0:0a841b89d614 254 if (isnan(d)) sprintf(temp, "---.-");
AjK 0:0a841b89d614 255 else if (d > 100.) sprintf(temp, "%.6f", d);
AjK 0:0a841b89d614 256 else if (d > 10.) sprintf(temp, "0%.6f", d);
AjK 0:0a841b89d614 257 else sprintf(temp, "00%.6f", d);
AjK 0:0a841b89d614 258 memcpy(s, temp, 5);
AjK 0:0a841b89d614 259 *(s+5) = '\0';
AjK 0:0a841b89d614 260 return s;
AjK 0:0a841b89d614 261 }
AjK 0:0a841b89d614 262
AjK 0:0a841b89d614 263 /** printDouble_3_2
AjK 0:0a841b89d614 264 *
AjK 0:0a841b89d614 265 * Print a double to a string buffer with correct leading zero(s).
AjK 0:0a841b89d614 266 * The caller is responsible for allocating the buffer *s before
AjK 0:0a841b89d614 267 * calling this function.
AjK 0:0a841b89d614 268 *
AjK 0:0a841b89d614 269 * @param char *s A pointer to the buffer to print to.
AjK 0:0a841b89d614 270 * @param double d The value to print.
AjK 0:0a841b89d614 271 */
AjK 0:0a841b89d614 272 char * printDouble_3_2(char *s, double d) {
AjK 0:0a841b89d614 273 char temp[16];
AjK 0:0a841b89d614 274 if (isnan(d)) sprintf(temp, "---.--");
AjK 0:0a841b89d614 275 else if (d > 100.) sprintf(temp, "%.6f", d);
AjK 0:0a841b89d614 276 else if (d > 10.) sprintf(temp, "0%.6f", d);
AjK 0:0a841b89d614 277 else sprintf(temp, "00%.6f", d);
AjK 0:0a841b89d614 278 memcpy(s, temp, 6);
AjK 0:0a841b89d614 279 *(s+6) = '\0';
AjK 0:0a841b89d614 280 return s;
AjK 0:0a841b89d614 281 }
AjK 0:0a841b89d614 282
AjK 0:0a841b89d614 283 void printBuffer(char *s, int len) {
AjK 0:0a841b89d614 284 #ifdef DEBUG_ON
AjK 0:0a841b89d614 285 for (int i = 0; i < len / 0x10; i++) {
AjK 0:0a841b89d614 286 debug_printf("%02X: ", i);
AjK 0:0a841b89d614 287 for (int j = 0; j < 0x10; j++) {
AjK 0:0a841b89d614 288 debug_printf("%02X ", s[(i * 0x10) + j]);
AjK 0:0a841b89d614 289 if (j == 7) debug_printf(" ");
AjK 0:0a841b89d614 290 }
AjK 0:0a841b89d614 291 for (int j = 0; j < 0x10; j++) {
AjK 0:0a841b89d614 292 if (isprint(s[(i * 0x10) + j])) {
AjK 0:0a841b89d614 293 debug_printf("%c", s[(i * 0x10) + j]);
AjK 0:0a841b89d614 294 }
AjK 0:0a841b89d614 295 else {
AjK 0:0a841b89d614 296 debug_printf(".");
AjK 0:0a841b89d614 297 }
AjK 0:0a841b89d614 298 if (j == 7) debug_printf(" ");
AjK 0:0a841b89d614 299 }
AjK 0:0a841b89d614 300 debug_printf("\r\n");
AjK 0:0a841b89d614 301 }
AjK 0:0a841b89d614 302 #endif
AjK 0:0a841b89d614 303 }
AjK 0:0a841b89d614 304
AjK 0:0a841b89d614 305 inline void disable_irqs(void) {
AjK 0:0a841b89d614 306 NVIC_DisableIRQ(EINT3_IRQn);
AjK 0:0a841b89d614 307 NVIC_DisableIRQ(RIT_IRQn);
AjK 0:0a841b89d614 308 NVIC_DisableIRQ(UART0_IRQn);
AjK 0:0a841b89d614 309 NVIC_DisableIRQ(UART1_IRQn);
AjK 0:0a841b89d614 310 NVIC_DisableIRQ(UART2_IRQn);
AjK 0:0a841b89d614 311 NVIC_DisableIRQ(USB_IRQn);
AjK 0:0a841b89d614 312 }
AjK 0:0a841b89d614 313
AjK 0:0a841b89d614 314 inline void enable_irqs(void) {
AjK 0:0a841b89d614 315 NVIC_EnableIRQ(USB_IRQn);
AjK 0:0a841b89d614 316 NVIC_EnableIRQ(EINT3_IRQn);
AjK 0:0a841b89d614 317 NVIC_EnableIRQ(RIT_IRQn);
AjK 0:0a841b89d614 318 NVIC_EnableIRQ(UART0_IRQn);
AjK 0:0a841b89d614 319 NVIC_EnableIRQ(UART1_IRQn);
AjK 0:0a841b89d614 320 NVIC_EnableIRQ(UART2_IRQn);
AjK 0:0a841b89d614 321 }