use mbed-src latest version and everything works well. RTC is also fine.

Dependencies:   L3GD20 LIS3DH TextLCD mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mon.cpp Source File

mon.cpp

00001 /*
00002  * mbed Application program
00003  *
00004  *  Copyright (c) 2010-2014 Kenji Arai / JH1PJL
00005  *  http://www.page.sannet.ne.jp/kenjia/index.html
00006  *  http://mbed.org/users/kenjiArai/
00007  *      Created:  May       15th, 2010
00008  *      Spareted: June      25th, 2014      mon() & mon_hw()
00009  *      Ported:   July      12th, 2014      from L152RE
00010  *      Revised:  Feburary   8th, 2015
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
00013  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
00014  * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018  
00019 //  Include ---------------------------------------------------------------------------------------
00020 #include "mbed.h"
00021 #include "rtos.h"
00022 
00023 //  Object ----------------------------------------------------------------------------------------
00024 Serial pc(USBTX, USBRX);
00025 
00026 //  Definition ------------------------------------------------------------------------------------
00027 #define BAUD(x)                 pc.baud(x)
00028 #define GETC(x)                 pc.getc(x)
00029 #define PUTC(x)                 pc.putc(x);
00030 #define PRINTF(...)             pc.printf(__VA_ARGS__)
00031 #define READABLE(x)             pc.readable(x)
00032 
00033 //  RAM -------------------------------------------------------------------------------------------
00034 static char linebuf[64];
00035 
00036 extern float fa[3];    // Acc  0:X, 1:Y, 2:Z
00037 extern float fg[3];    // Gyro 0:X, 1:Y, 2:Z
00038 extern uint8_t show_flag0;
00039 extern uint8_t show_flag1;
00040 
00041 //  ROM / Constant data ---------------------------------------------------------------------------
00042 static char *const mon_msg = "Monitor for mbed system, created on "__DATE__"";
00043 
00044 //  Function prototypes ---------------------------------------------------------------------------
00045 extern void debug_interface(void);
00046 extern int read_sw(uint8_t n);
00047 
00048 //-------------------------------------------------------------------------------------------------
00049 //  Control Program
00050 //-------------------------------------------------------------------------------------------------
00051 //  Put \r\n
00052 static void put_rn ( void ){
00053     Thread::yield(); // change thread
00054     PUTC('\r');
00055     Thread::yield(); // change thread
00056     PUTC('\n');
00057 }
00058 
00059 //  Put \r
00060 static void put_r ( void ){
00061     Thread::yield(); // change thread
00062     PUTC('\r');
00063 }
00064 
00065 //  Change string -> integer
00066 //int xatoi (char **str, unsigned long *res){
00067 static int xatoi (char **str, int32_t *res){
00068 unsigned long val;
00069 unsigned char c, radix, s = 0;
00070 
00071     while ((c = **str) == ' ') (*str)++;
00072     if (c == '-') {
00073         s = 1;
00074         c = *(++(*str));
00075     }
00076     if (c == '0') {
00077         c = *(++(*str));
00078         if (c <= ' ') { *res = 0;   return 1; }
00079         if (c == 'x') {
00080             radix = 16;
00081             c = *(++(*str));
00082         } else {
00083             if (c == 'b') {
00084                 radix = 2;
00085                 c = *(++(*str));
00086             } else {
00087                 if ((c >= '0')&&(c <= '9')){    radix = 8;
00088                 }   else {   return 0;}
00089             }
00090         }
00091     } else {
00092         if ((c < '1')||(c > '9')){  return 0;}
00093         radix = 10;
00094     }
00095     val = 0;
00096     while (c > ' ') {
00097         if (c >= 'a') c -= 0x20;
00098         c -= '0';
00099         if (c >= 17) {
00100             c -= 7;
00101             if (c <= 9) return 0;
00102         }
00103         if (c >= radix) return 0;
00104         val = val * radix + c;
00105         c = *(++(*str));
00106     }
00107     if (s) val = -val;
00108     *res = val;
00109     return 1;
00110 }
00111 
00112 //-------------------------------------------------------------------------------------------------
00113 //  Monitor
00114 //-------------------------------------------------------------------------------------------------
00115 //  Help Massage
00116 static void msg_hlp (void){
00117     PRINTF(mon_msg);                        put_rn();
00118     PRINTF("d - Show control data");        put_rn();
00119     PRINTF("m - Show mail data");           put_rn();
00120     PRINTF("s - Show USER Button");         put_rn();
00121     PRINTF("t - Check and set RTC");        put_rn();
00122     PRINTF("r - Show RTC date & time");     put_rn();
00123     PRINTF("x - Goto HW monitor");          put_rn();
00124     PRINTF("q - Return to main");           put_rn();
00125 }
00126 
00127 //  Get key input data
00128 static void get_line (char *buff, int len){
00129 char c;
00130 int idx = 0;
00131 
00132     for (;;) {
00133         c = GETC();
00134         //    Added by Kenji Arai / JH1PJL   May 9th, 2010
00135         if (c == '\r') {
00136             buff[idx++] = c;
00137             break;
00138         }
00139         if ((c == '\b') && idx) {
00140             idx--;
00141             PUTC(c);
00142             PUTC(' ');
00143             PUTC(c);
00144         }
00145         if (((uint8_t)c >= ' ') && (idx < len - 1)) {
00146             buff[idx++] = c;
00147             PUTC(c);
00148         }
00149         Thread::yield(); // change thread
00150         Thread::wait(10); // Wait 10mS
00151     }
00152     buff[idx] = 0;
00153     PUTC('\n');
00154 }
00155 
00156 // RTC related subroutines
00157 static void chk_and_set_time(char *ptr){
00158 //unsigned long p1;
00159 int32_t p1;
00160 struct tm t;
00161 time_t seconds;
00162 char buf[40];
00163 
00164     if (xatoi(&ptr, &p1)){
00165         t.tm_year       = (uint8_t)p1 + 100;
00166         PRINTF("Year:%d ",p1);
00167         xatoi( &ptr, &p1 );
00168         t.tm_mon        = (uint8_t)p1 - 1;
00169         PRINTF("Month:%d ",p1);
00170         xatoi( &ptr, &p1 );
00171         t.tm_mday       = (uint8_t)p1;
00172         PRINTF("Day:%d ",p1);
00173         xatoi( &ptr, &p1 );
00174         t.tm_hour       = (uint8_t)p1;
00175         PRINTF("Hour:%d ",p1);
00176         xatoi( &ptr, &p1 );
00177         t.tm_min        = (uint8_t)p1;
00178         PRINTF("Min:%d ",p1);
00179         xatoi( &ptr, &p1 );
00180         t.tm_sec        = (uint8_t)p1;
00181         PRINTF("Sec: %d \r\n",p1);
00182         seconds = mktime(&t);
00183         set_time(seconds);
00184     }
00185     seconds = time(NULL);
00186     strftime(buf, 40, "%B %d,'%y, %H:%M:%S", localtime(&seconds));
00187     PRINTF("Date: %s\r\n", buf);
00188 }
00189 
00190 // ---------- Program starts here! ---------------------------------------------------------------
00191 int mon(void) {
00192 char *ptr;
00193 
00194     BAUD(9600);
00195     put_rn();
00196     put_rn();
00197     PRINTF("%s [Help:'?' key]", mon_msg);
00198     put_rn();
00199     for (;;) {
00200         put_r();
00201         PUTC('>');
00202         ptr = linebuf;
00203         get_line(ptr, sizeof(linebuf));
00204         switch (*ptr++) {
00205     //---------------------------------------------------------------------------------------------
00206     //  Check control data
00207     //---------------------------------------------------------------------------------------------
00208         case 'd' :
00209             while(true) {
00210                 put_r();
00211                 PRINTF("G:%+6.1f,%+6.1f,%+6.1f, ", fg[0], fg[1], fg[2]);
00212                 Thread::yield(); // change thread               
00213                 PRINTF("A:%+6.1f,%+6.1f,%+6.1f ", fa[0], fa[1], fa[2]);
00214                 put_rn();
00215                 if ( READABLE() ) {
00216                     break;
00217                 }
00218                 Thread::wait(200); // Wait 200mS
00219             }
00220             break;
00221     //---------------------------------------------------------------------------------------------
00222     //  Show Mail data
00223     //---------------------------------------------------------------------------------------------
00224         case 'm' :
00225             put_rn();
00226             while(true) {
00227                 show_flag0 = 1;
00228                 if ( READABLE() ) {
00229                     break;
00230                 }
00231                 Thread::wait(100); // Wait 100mS
00232             }
00233             show_flag0 = 0;
00234             break;
00235     //---------------------------------------------------------------------------------------------
00236     //  Show switch status
00237     //---------------------------------------------------------------------------------------------
00238         case 's' :
00239             put_r();
00240             PRINTF("Show USER_Button status");
00241             put_rn();
00242             PRINTF("every 500mS  - hit any key for stop");
00243             put_rn();
00244             while (true){
00245                 PRINTF("SW0 = ");
00246                 if (read_sw(0) == 1) {  PRINTF("ON ");
00247                 } else {                PRINTF("OFF");
00248                 }
00249                 PRINTF(", SW1 = ");
00250                 if (read_sw(1) == 1) {  PRINTF("ON");
00251                 } else {                PRINTF("OFF");
00252                 }
00253                 put_rn();
00254                 if (READABLE()){ GETC(); break;}
00255                 wait(0.5);
00256             }
00257             break;
00258     //---------------------------------------------------------------------------------------------
00259     //  check and set RTC
00260     //---------------------------------------------------------------------------------------------
00261         case 't' :
00262             put_r(); 
00263             chk_and_set_time(ptr);               
00264             break;
00265     //---------------------------------------------------------------------------------------------
00266     //  check and set RTC
00267     //---------------------------------------------------------------------------------------------
00268         case 'r' :
00269             put_r();
00270             while(true) {
00271                 show_flag1 = 1;
00272                 if ( READABLE() ) {
00273                     break;
00274                 }
00275                 Thread::wait(50); // Wait 100mS
00276             }
00277             show_flag1 = 0;             
00278             break;
00279     //---------------------------------------------------------------------------------------------
00280     //  help
00281     //---------------------------------------------------------------------------------------------
00282         case '?' :
00283             put_r(); 
00284             msg_hlp();                 
00285             break;
00286     //---------------------------------------------------------------------------------------------
00287     //  Go back to main()
00288     //---------------------------------------------------------------------------------------------
00289         case 'q' :        // Quit
00290             PRINTF("\rReturn to main\r\n");
00291             PRINTF("cannot control anymore from here\r\n");
00292             return 0;
00293         }
00294     }
00295 }