Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Terminal.cpp
00001 #include "System.h" 00002 00003 00004 //***************************************************************** 00005 //Terminal Related stuff 00006 //***************************************************************** 00007 00008 #define MAX_TERMINAL_LINE_CHARS 128 00009 #define MAX_TERMINAL_CMD_CHARS 32 00010 CHAR TerminalLineBuf[MAX_TERMINAL_LINE_CHARS]; 00011 BYTE TerminalPos; 00012 CHAR TerminalCmdBuf[MAX_TERMINAL_CMD_CHARS+1]; 00013 CHAR TerminalArgs[MAX_TERMINAL_LINE_CHARS-MAX_TERMINAL_CMD_CHARS]; 00014 BYTE NextCharIn; 00015 BOOL CmdFound; 00016 00017 00018 00019 #define NUM_TERMINAL_CMDS 7 00020 00021 char *TerminalCommands[NUM_TERMINAL_CMDS] = {"help","reboot","v","i","Time","Date","RTC_Init"}; 00022 00023 typedef void (*TerminalCallback)(char *); 00024 00025 00026 void TerminalCmd_Help(char *arg); 00027 void TerminalCmd_Reboot(char *arg); 00028 void TerminalCmd_v(char *arg); 00029 void TerminalCmd_i(char *arg); 00030 void TerminalCmd_Time(char *arg); 00031 void TerminalCmd_Date(char *arg); 00032 void TerminalCmd_RTC_Init(char *arg); 00033 00034 TerminalCallback TerminalCallbacks[NUM_TERMINAL_CMDS] ={TerminalCmd_Help}; 00035 00036 void InitTerminal() 00037 { 00038 //Initialize the terminal command callbacks 00039 TerminalCallbacks[0] = TerminalCmd_Help; 00040 TerminalCallbacks[1] = TerminalCmd_Reboot; 00041 TerminalCallbacks[2] = TerminalCmd_v; 00042 TerminalCallbacks[3] = TerminalCmd_i; 00043 TerminalCallbacks[4] = TerminalCmd_Time; 00044 TerminalCallbacks[5] = TerminalCmd_Date; 00045 TerminalCallbacks[6] = TerminalCmd_RTC_Init; 00046 } 00047 00048 00049 extern "C" void mbed_reset(); 00050 00051 void TerminalCmd_RTC_Init(char *arg); 00052 { 00053 set_time(1256729737); 00054 PrintfEnqueue(&PCBackDoorTx,"\r\nTime Reset\r\n"); 00055 } 00056 00057 00058 void TerminalCmd_Time(char *arg) 00059 { 00060 int Hour,Minute,Second=0; 00061 int Items; 00062 BOOL Error = FALSE; 00063 time_t TimeStampTemp; 00064 00065 00066 Items = sscanf(LineIn, "time=%d : %d : %d", &Hour, &Minute, &Second); 00067 if(Items == 3) 00068 { 00069 PrintfEnqueue(&PCBackDoorTx,"\r\n"); 00070 if(Hour>23) 00071 { 00072 PrintfEnqueue(&PCBackDoorTx,"Hour entry must be between 0 and 24\r\n"); 00073 Error = TRUE; 00074 } 00075 if(Minute>60) 00076 { 00077 PrintfEnqueue(&PCBackDoorTx,"Minute entry must be between 0 and 60\r\n"); 00078 Error = TRUE; 00079 } 00080 if(Second>60) 00081 { 00082 PrintfEnqueue(&PCBackDoorTx,"Second entry must be between 0 and 60\r\n"); 00083 Error = TRUE; 00084 } 00085 00086 if(Error == TRUE) 00087 { 00088 PrintfEnqueue(&PCBackDoorTx,"Error in time format. Time not changed.\r\n"); 00089 } 00090 else 00091 { 00092 TimeStampTemp = time(NULL); 00093 //convert to tm struct 00094 CurrentTime = localtime(&TimeStampTemp); 00095 //dump in our new valus 00096 CurrentTime->tm_sec = Second; 00097 CurrentTime->tm_min = Minute; 00098 CurrentTime->tm_hour = Hour; 00099 //set the new time 00100 set_time(mktime(CurrentTime)); 00101 PrintfEnqueue(&PCBackDoorTx,"Time set to %d:%d:%d\r\n",Hour,Minute,Second); 00102 } 00103 00104 } 00105 } 00106 00107 void TerminalCmd_Date(char *arg) 00108 { 00109 int Day,Month,Year=0; 00110 int Items; 00111 BOOL Error = FALSE; 00112 time_t TimeStampTemp; 00113 00114 Items = sscanf(LineIn, "date=%d / %d / %d", &Month, &Day, &Year); 00115 if(Items == 3) 00116 { 00117 PrintfEnqueue(&PCBackDoorTx,"\r\n"); 00118 if(Month>12 || Month < 1) 00119 { 00120 PrintfEnqueue(&PCBackDoorTx,"Month entry must be between 1 and 12\r\n"); 00121 Error = TRUE; 00122 } 00123 if(Day>31 || Day<1) 00124 { 00125 PrintfEnqueue(&PCBackDoorTx,"Day entry must be between 1 and 31\r\n"); 00126 Error = TRUE; 00127 } 00128 if(Year<1900) 00129 { 00130 PrintfEnqueue(&PCBackDoorTx,"Year entry must be greater than 1900\r\n"); 00131 Error = TRUE; 00132 } 00133 00134 if(Error == TRUE) 00135 { 00136 PrintfEnqueue(&PCBackDoorTx,"Error in time format. Date not changed.\r\n"); 00137 } 00138 else 00139 { 00140 //Get the current time in seconds since unix epoch 00141 TimeStampTemp = time(NULL); 00142 //convert to tm struct fom 00143 CurrentTime = localtime(&TimeStampTemp); 00144 //dump in our new valus 00145 CurrentTime->tm_mon = Month-1; 00146 CurrentTime->tm_mday = Day; 00147 CurrentTime->tm_year = Year - 1900; 00148 //set the new time 00149 set_time(mktime(CurrentTime)); 00150 PrintfEnqueue(&PCBackDoorTx,"Date set to %d/%d/%d\r\n",Month,Day,Year); 00151 } 00152 00153 } 00154 } 00155 00156 void TerminalCmd_v(char *arg) 00157 { 00158 // PrintfEnqueue(&PCBackDoorTx,"\r\nVbus: %.1f\r\n",Vout); 00159 } 00160 00161 void TerminalCmd_i(char *arg) 00162 { 00163 // PrintfEnqueue(&PCBackDoorTx,"\r\nIbus: %.1f\r\n",Iout); 00164 } 00165 00166 void TerminalCmd_Help(char *arg) 00167 { 00168 BYTE i; 00169 00170 PrintfEnqueue(&PCBackDoorTx,"\r\n\r\bCommandList:\r\n"); 00171 PrintfEnqueue(&PCBackDoorTx,"----------------------\r\n"); 00172 00173 for(i=0;i<NUM_TERMINAL_CMDS;i++) 00174 { 00175 PrintfEnqueue(&PCBackDoorTx,"%s\r\n",TerminalCommands[i]); 00176 } 00177 00178 } 00179 00180 void TerminalCmd_Reboot(char *arg) 00181 { 00182 mbed_reset(); 00183 } 00184 00185 void TerminalCmd_Strip(char *arg) 00186 { 00187 PrintfEnqueue(&PCBackDoorTx,"\r\n%s\r\n", arg); 00188 } 00189 00190 void ProcessTerminal() 00191 { 00192 00193 BYTE i,j; 00194 00195 if(BytesInQueue(&PCBackDoorRx)>0) 00196 { 00197 ByteDequeue(&PCBackDoorRx,&NextCharIn); 00198 00199 switch(NextCharIn) 00200 { 00201 case '\r': 00202 00203 TerminalLineBuf[TerminalPos++] = 0x0; 00204 ByteEnqueue(&PCBackDoorTx,NextCharIn); 00205 00206 if(TerminalPos > 1) 00207 { 00208 //find the command 00209 i=0; 00210 while(TerminalLineBuf[i]>0x20 && TerminalLineBuf[i]<0x7f) 00211 { 00212 TerminalCmdBuf[i] = TerminalLineBuf[i]; 00213 i++; 00214 00215 if(i==MAX_TERMINAL_CMD_CHARS) 00216 { 00217 break; 00218 } 00219 } 00220 00221 TerminalCmdBuf[i] = 0; 00222 TerminalCmdBuf[i+1] = 0; 00223 00224 strcpy(TerminalArgs,&TerminalLineBuf[i]); 00225 00226 CmdFound = FALSE; 00227 for(j=0;j<NUM_TERMINAL_CMDS;j++) 00228 { 00229 if(strcmp(TerminalCmdBuf,TerminalCommands[j]) == 0) 00230 { 00231 if(TerminalCallbacks[j] != NULL) 00232 TerminalCallbacks[j](TerminalArgs); 00233 00234 CmdFound = TRUE; 00235 break; 00236 } 00237 } 00238 if(CmdFound == FALSE) 00239 { 00240 PrintfEnqueue(&PCBackDoorTx,"%s command not recognized.\r\n", TerminalCmdBuf); 00241 } 00242 } 00243 ByteEnqueue(&PCBackDoorTx,'\n'); 00244 ByteEnqueue(&PCBackDoorTx,'>'); 00245 TerminalPos = 0; 00246 00247 break; 00248 00249 case '\b': 00250 if(TerminalPos > 0) 00251 { 00252 TerminalPos--; 00253 ByteEnqueue(&PCBackDoorTx,NextCharIn); 00254 } 00255 break; 00256 00257 default: 00258 00259 if(TerminalPos == 0 && NextCharIn == 0x020) 00260 { 00261 //Do nothing if space bar is pressed at begining of line 00262 } 00263 else if(NextCharIn >= 0x20 && NextCharIn<0x7F) 00264 { 00265 00266 if(TerminalPos < MAX_TERMINAL_LINE_CHARS-1) 00267 { 00268 TerminalLineBuf[TerminalPos++] = NextCharIn; 00269 ByteEnqueue(&PCBackDoorTx,NextCharIn); 00270 } 00271 } 00272 00273 break; 00274 00275 } 00276 } 00277 00278 } 00279
Generated on Thu Jul 14 2022 09:05:34 by
 1.7.2
 1.7.2