Prof Greg Egan / Mbed 2 deprecated UAVXArm-GKE

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nonvolatile.c Source File

nonvolatile.c

00001 // ===============================================================================================
00002 // =                              UAVXArm Quadrocopter Controller                                =
00003 // =                           Copyright (c) 2008 by Prof. Greg Egan                             =
00004 // =                 Original V3.15 Copyright (c) 2007 Ing. Wolfgang Mahringer                   =
00005 // =                           http://code.google.com/p/uavp-mods/                               =
00006 // ===============================================================================================
00007 
00008 //    This is part of UAVXArm.
00009 
00010 //    UAVXArm is free software: you can redistribute it and/or modify it under the terms of the GNU
00011 //    General Public License as published by the Free Software Foundation, either version 3 of the
00012 //    License, or (at your option) any later version.
00013 
00014 //    UAVXArm is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without
00015 //    even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016 //    See the GNU General Public License for more details.
00017 
00018 //    You should have received a copy of the GNU General Public License along with this program.
00019 //    If not, see http://www.gnu.org/licenses/
00020 
00021 #include "UAVXArm.h"
00022 
00023 // transfers to Flash seem to be ~35KByte/Sec
00024 
00025 void CheckSDCardValid(void);
00026 
00027 void CreateLogfile(void);
00028 void CloseLogfile(void);
00029 void TxLogChar(uint8);
00030 
00031 void WritePXImagefile(void);
00032 boolean ReadPXImagefile(void);
00033 
00034 int8 ReadPX(uint16);
00035 int16 Read16PX(uint16);
00036 int32 Read32PX(uint16);
00037 void WritePX(uint16, int8);
00038 void Write16PX(uint16, int16);
00039 void Write32PX(uint16, int32);
00040 
00041 FILE *pxfile = NULL;
00042 FILE *newpxfile = NULL;
00043 
00044 const int PX_LENGTH = 2048;
00045 int8 PX[PX_LENGTH], PXNew[PX_LENGTH];
00046 
00047 void CheckSDCardValid(void) {
00048 
00049     F.SDCardValid =  SDCard.initialise_card() != 0;
00050 
00051 } // CheckSDCardValid
00052 
00053 void WritePXImagefile(void) {
00054     static uint16 a;
00055     static int32 CheckSum;
00056     static int8 v, i;
00057 
00058     UpdateRTC();
00059 
00060     if ( F.PXImageStale ) {
00061         if ( F.SDCardValid )
00062             newpxfile = fopen("/SDCard/Params.txt", "w");
00063         else
00064             newpxfile = fopen("/local/Params.txt", "w");
00065 
00066         if ( newpxfile != NULL ) {
00067             CheckSum = 0;
00068             for ( a = 0; a < PX_LENGTH; a++ ) {
00069                 v = PX[a];
00070                 CheckSum += (int32)v;
00071                 fprintf(newpxfile, "%i \r\n",  v);
00072             }
00073             fprintf(newpxfile, "%li \r\n ",  -CheckSum );
00074 
00075             i=0;
00076             while ( RTCString[i] != 0 )
00077                 fprintf(newpxfile, "%c", RTCString[i++]);
00078             fprintf(newpxfile, "\r\n");
00079 
00080             F.PXImageStale = false;
00081             fclose(newpxfile);
00082         }
00083     }
00084 
00085 } // WritePXIMagefile
00086 
00087 boolean ReadPXImagefile(void) {
00088     static uint16 a;
00089     static int32 v, CheckSum;
00090     static boolean OK;
00091 
00092     if ( F.SDCardValid )
00093         pxfile = fopen("/SDCard/Params.txt", "r");
00094     else
00095         pxfile = fopen("/local/Params.txt", "r");
00096 
00097     OK = false;
00098     if ( pxfile != NULL ) {
00099         CheckSum = 0;
00100         a = 0;
00101         for ( a = 0; a < PX_LENGTH ; a++ ) {
00102             r = fscanf(pxfile, "%i ", &v);
00103             CheckSum += v;
00104             PXNew[a] = (int8)v;
00105         }
00106         r = fscanf(pxfile, "%li ", &v);
00107         CheckSum += (int32)v;
00108         OK = CheckSum == 0;
00109 
00110         fclose(pxfile);
00111 
00112         if ( OK ) {
00113             for ( a = 0; a < PX_LENGTH; a++ )
00114                 PX[a] = PXNew[a];
00115             F.PXImageStale = false;
00116         }
00117     }
00118 
00119     return( OK );
00120 
00121 } // ReadPXImagefile
00122 
00123 void CreateLogfile(void) {
00124 
00125 #ifndef SUPPRESS_SDCARD
00126 
00127     static int16 i;
00128 
00129     UpdateRTC();
00130 
00131     if ( F.SDCardValid )
00132         strftime(RTCLogfile, 32, "/SDCard/L%H-%M.log", RTCTime );
00133     else
00134         strftime(RTCLogfile, 32, "/local/L%H-%M.log", RTCTime );
00135 
00136     logfile = fopen(RTCLogfile, "w");
00137 
00138     LogfileIsOpen = logfile != NULL;
00139     if ( LogfileIsOpen ) {
00140         i = 0;
00141         while ( RTCString[i] != 0 )
00142             TxLogChar(RTCString[i++]);
00143         TxLogChar(CR);
00144         TxLogChar(LF);
00145     }
00146     LogChars = 0;
00147 
00148 #endif // !SUPPRESS_SDCARD
00149 
00150 } // CreateLogfile
00151 
00152 void CloseLogfile(void) {
00153     fclose(logfile);
00154 } // CloseLog
00155 
00156 void TxLogChar(uint8 ch) {
00157 
00158 #ifndef SUPPRESS_SDCARD
00159     if ( LogfileIsOpen ) {
00160         LogfileIsOpen =  fprintf(logfile, "%c",  ch) > 0;
00161         if ( !LogfileIsOpen )
00162             CloseLogfile();
00163     }
00164 #endif // !SUPPRESS_SDCARD
00165 } // TxLogChar
00166 
00167 
00168 int8 ReadPX(uint16 a) {
00169     static int8 b;
00170     b = PX[a];
00171     return(b);
00172 } // ReadPX
00173 
00174 int16 Read16PX(uint16 a) {
00175     static i16u Temp16;
00176 
00177     Temp16.b0 = ReadPX(a);
00178     Temp16.b1 = ReadPX(a + 1);
00179 
00180     return ( Temp16.i16 );
00181 } // Read16P
00182 
00183 int32 Read32PX(uint16 a) {
00184     static i32u Temp32;
00185 
00186     Temp32.b0 = ReadPX(a);
00187     Temp32.b1 = ReadPX(a + 1);
00188     Temp32.b2 = ReadPX(a + 2);
00189     Temp32.b3 = ReadPX(a + 3);
00190 
00191     return ( Temp32.i32 );
00192 } // Read32P
00193 
00194 void WritePX(uint16 a, int8 d) {
00195     if ( PX[a] != d ) {
00196         PX[a] = d;
00197         F.PXImageStale = true;
00198     }
00199 } // WritePX
00200 
00201 void Write16PX(uint16 a, int16 d) {
00202     static i16u Temp16;
00203 
00204     Temp16.i16 = d;
00205     WritePX(a, Temp16.b0);
00206     WritePX(a + 1, Temp16.b1);
00207 
00208 } // Write16P
00209 
00210 void Write32PX(uint16 a, int32 d) {
00211     static i32u Temp32;
00212 
00213     Temp32.i32 = d;
00214     WritePX(a, Temp32.b0);
00215     WritePX(a + 1, Temp32.b1);
00216     WritePX(a + 2, Temp32.b2);
00217     WritePX(a + 3, Temp32.b3);
00218 
00219 } // Write16PX
00220