Satellite Observers Workbench. NOT yet complete, just published for forum posters to \"cherry pick\" pieces of code as requiered as an example.
satapi/mbedfilesys.c@0:0a841b89d614, 2010-10-11 (annotated)
- 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?
User | Revision | Line number | New 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 | /* Sadly stung by the Mbed libraries again. The sheer number |
AjK | 0:0a841b89d614 | 24 | of interrupts running on SOWB are incomp with the LocalFileSystem |
AjK | 0:0a841b89d614 | 25 | library which simply crashes the entire project. |
AjK | 0:0a841b89d614 | 26 | |
AjK | 0:0a841b89d614 | 27 | Need to get down to the electonics shop and buy myself some flash |
AjK | 0:0a841b89d614 | 28 | and DIY my own solution again. Sigh... */ |
AjK | 0:0a841b89d614 | 29 | |
AjK | 0:0a841b89d614 | 30 | #ifdef NEVERCOMPILETHIS |
AjK | 0:0a841b89d614 | 31 | |
AjK | 0:0a841b89d614 | 32 | #include "mbed.h" |
AjK | 0:0a841b89d614 | 33 | #include "mbedfilesys.h" |
AjK | 0:0a841b89d614 | 34 | #include "user.h" |
AjK | 0:0a841b89d614 | 35 | #include "utils.h" |
AjK | 0:0a841b89d614 | 36 | #include "gpio.h" |
AjK | 0:0a841b89d614 | 37 | #include "debug.h" |
AjK | 0:0a841b89d614 | 38 | |
AjK | 0:0a841b89d614 | 39 | LocalFileSystem local("local"); |
AjK | 0:0a841b89d614 | 40 | |
AjK | 0:0a841b89d614 | 41 | inline void disable_irqs(void) { |
AjK | 0:0a841b89d614 | 42 | NVIC_DisableIRQ(EINT3_IRQn); |
AjK | 0:0a841b89d614 | 43 | NVIC_DisableIRQ(RIT_IRQn); |
AjK | 0:0a841b89d614 | 44 | NVIC_DisableIRQ(UART0_IRQn); |
AjK | 0:0a841b89d614 | 45 | NVIC_DisableIRQ(UART1_IRQn); |
AjK | 0:0a841b89d614 | 46 | NVIC_DisableIRQ(UART2_IRQn); |
AjK | 0:0a841b89d614 | 47 | NVIC_DisableIRQ(USB_IRQn); |
AjK | 0:0a841b89d614 | 48 | } |
AjK | 0:0a841b89d614 | 49 | |
AjK | 0:0a841b89d614 | 50 | inline void enable_irqs(void) { |
AjK | 0:0a841b89d614 | 51 | NVIC_EnableIRQ(USB_IRQn); |
AjK | 0:0a841b89d614 | 52 | NVIC_EnableIRQ(EINT3_IRQn); |
AjK | 0:0a841b89d614 | 53 | NVIC_EnableIRQ(RIT_IRQn); |
AjK | 0:0a841b89d614 | 54 | NVIC_EnableIRQ(UART0_IRQn); |
AjK | 0:0a841b89d614 | 55 | NVIC_EnableIRQ(UART1_IRQn); |
AjK | 0:0a841b89d614 | 56 | NVIC_EnableIRQ(UART2_IRQn); |
AjK | 0:0a841b89d614 | 57 | } |
AjK | 0:0a841b89d614 | 58 | |
AjK | 0:0a841b89d614 | 59 | static int get_tle_from_file(const char *filename, int index, SAT_POS_DATA *sat) { |
AjK | 0:0a841b89d614 | 60 | FILE *fp; |
AjK | 0:0a841b89d614 | 61 | char *r, line[128]; |
AjK | 0:0a841b89d614 | 62 | int line_count = 0, sat_count = 0; |
AjK | 0:0a841b89d614 | 63 | |
AjK | 0:0a841b89d614 | 64 | fp = fopen(filename, "r"); |
AjK | 0:0a841b89d614 | 65 | if (!fp) { |
AjK | 0:0a841b89d614 | 66 | return SOWB_FILE_NOT_FOUND; |
AjK | 0:0a841b89d614 | 67 | } |
AjK | 0:0a841b89d614 | 68 | else { |
AjK | 0:0a841b89d614 | 69 | while (!feof(fp)) { |
AjK | 0:0a841b89d614 | 70 | LED1_ON; |
AjK | 0:0a841b89d614 | 71 | disable_irqs(); |
AjK | 0:0a841b89d614 | 72 | r = fgets(line, 127, fp); |
AjK | 0:0a841b89d614 | 73 | enable_irqs(); |
AjK | 0:0a841b89d614 | 74 | LED1_OFF; |
AjK | 0:0a841b89d614 | 75 | if(!fgets(line, 127, fp)) { |
AjK | 0:0a841b89d614 | 76 | fclose(fp); |
AjK | 0:0a841b89d614 | 77 | return SOWB_FILE_EOF_REACHED; |
AjK | 0:0a841b89d614 | 78 | } |
AjK | 0:0a841b89d614 | 79 | else { |
AjK | 0:0a841b89d614 | 80 | switch(line_count) { |
AjK | 0:0a841b89d614 | 81 | case 0: strncpy(sat->elements[0], line, 80); break; |
AjK | 0:0a841b89d614 | 82 | case 1: strncpy(sat->elements[1], line, 80); break; |
AjK | 0:0a841b89d614 | 83 | case 2: strncpy(sat->elements[2], line, 80); break; |
AjK | 0:0a841b89d614 | 84 | } |
AjK | 0:0a841b89d614 | 85 | line_count++; |
AjK | 0:0a841b89d614 | 86 | if (line_count == 3) { |
AjK | 0:0a841b89d614 | 87 | if (sat_count == index) { |
AjK | 0:0a841b89d614 | 88 | fclose(fp); |
AjK | 0:0a841b89d614 | 89 | return SOWB_FILE_OK; |
AjK | 0:0a841b89d614 | 90 | } |
AjK | 0:0a841b89d614 | 91 | line_count = 0; |
AjK | 0:0a841b89d614 | 92 | sat_count++; |
AjK | 0:0a841b89d614 | 93 | } |
AjK | 0:0a841b89d614 | 94 | } |
AjK | 0:0a841b89d614 | 95 | } |
AjK | 0:0a841b89d614 | 96 | } |
AjK | 0:0a841b89d614 | 97 | |
AjK | 0:0a841b89d614 | 98 | fclose(fp); |
AjK | 0:0a841b89d614 | 99 | return SOWB_FILE_INDEX_NOT_FOUND; |
AjK | 0:0a841b89d614 | 100 | } |
AjK | 0:0a841b89d614 | 101 | |
AjK | 0:0a841b89d614 | 102 | /* Used to hold the index across first/next calls. */ |
AjK | 0:0a841b89d614 | 103 | int satllite_index; |
AjK | 0:0a841b89d614 | 104 | |
AjK | 0:0a841b89d614 | 105 | int get_first_tle_from_file(const char *filename, SAT_POS_DATA *sat) { |
AjK | 0:0a841b89d614 | 106 | satllite_index = 0; |
AjK | 0:0a841b89d614 | 107 | return get_tle_from_file(filename, satllite_index, sat); |
AjK | 0:0a841b89d614 | 108 | } |
AjK | 0:0a841b89d614 | 109 | |
AjK | 0:0a841b89d614 | 110 | int get_next_tle_from_file(const char *filename, SAT_POS_DATA *sat) { |
AjK | 0:0a841b89d614 | 111 | satllite_index++; |
AjK | 0:0a841b89d614 | 112 | return get_tle_from_file(filename, satllite_index, sat); |
AjK | 0:0a841b89d614 | 113 | } |
AjK | 0:0a841b89d614 | 114 | |
AjK | 0:0a841b89d614 | 115 | void process_tle(SAT_POS_DATA *sat) { |
AjK | 0:0a841b89d614 | 116 | char line[128]; |
AjK | 0:0a841b89d614 | 117 | |
AjK | 0:0a841b89d614 | 118 | /* Wait until time and location are valid. */ |
AjK | 0:0a841b89d614 | 119 | do { |
AjK | 0:0a841b89d614 | 120 | observer_now(sat); |
AjK | 0:0a841b89d614 | 121 | user_call_process(); |
AjK | 0:0a841b89d614 | 122 | } |
AjK | 0:0a841b89d614 | 123 | while (!sat->time.is_valid || !sat->location.is_valid); |
AjK | 0:0a841b89d614 | 124 | |
AjK | 0:0a841b89d614 | 125 | debug_printf(" A %d\r\n", sizeof(SAT_POS_DATA)); |
AjK | 0:0a841b89d614 | 126 | debug_printf(" A %s", sat->elements[0]); |
AjK | 0:0a841b89d614 | 127 | debug_printf(" A %s", sat->elements[1]); |
AjK | 0:0a841b89d614 | 128 | debug_printf(" A %s", sat->elements[2]); |
AjK | 0:0a841b89d614 | 129 | time_AsString(&sat->time, line); |
AjK | 0:0a841b89d614 | 130 | debug_printf(" A AT %s\r\n", line); |
AjK | 0:0a841b89d614 | 131 | sprintf(line, " A LAT %.4f\r\n", sat->location.latitude); debug_printf("%s", line); |
AjK | 0:0a841b89d614 | 132 | sprintf(line, " A LON %.4f\r\n", sat->location.longitude); debug_printf("%s", line); |
AjK | 0:0a841b89d614 | 133 | sprintf(line, " A HEI %.4f\r\n", sat->location.height); debug_printf("%s", line); |
AjK | 0:0a841b89d614 | 134 | |
AjK | 0:0a841b89d614 | 135 | sat->tsince = 0; |
AjK | 0:0a841b89d614 | 136 | satallite_calculate(sat); |
AjK | 0:0a841b89d614 | 137 | debug_stringl(line, sprintf(line, " Azmith %.1f Elevation %.1f\r\n", sat->azimuth, sat->elevation)); |
AjK | 0:0a841b89d614 | 138 | } |
AjK | 0:0a841b89d614 | 139 | |
AjK | 0:0a841b89d614 | 140 | #endif |
AjK | 0:0a841b89d614 | 141 | |
AjK | 0:0a841b89d614 | 142 |