this transfers data (which is stored in "bin" file in mbed storage) into LPC1114, LPC1115, LPC81x, LPC82x, LPC1768/LPC1769 and LPC11U68/LPC11E68 internal flash memory through ISP.

Dependencies:   mbed MODSERIAL DirectoryList

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dir_handling.cpp Source File

dir_handling.cpp

00001 #include    "mbed.h"
00002 #include    "dir_handling.h"
00003 #include    "_user_settings.h"
00004 #include    "DirectoryList.h"
00005 #include    "isp.h"
00006 
00007 #include    <iostream>
00008 #include    <string>
00009 #include    <cctype>
00010 #include    <algorithm>
00011 
00012 extern Serial      pc;
00013 extern BusOut      leds;
00014 
00015 void show_file_list( std::vector<std::string> list );
00016 void user_action_waiting_indicator();
00017 
00018 
00019 std::string get_file_name( std::string &default_path, const char *suffix )
00020 {
00021     Ticker  led_blink;
00022     int     last_slash;
00023 
00024     last_slash  = (int)default_path.rfind( "/" );
00025 
00026     if ( last_slash == std::string::npos ) {
00027         default_path.clear();
00028         return ( default_path );
00029     }
00030 
00031     string file_name( default_path.c_str() + last_slash + 1 );
00032     string path_name( default_path.c_str(), default_path.c_str() + last_slash );
00033 
00034     //printf( "path_name : %s\r\n", path_name.c_str() );
00035     //printf( "file_name : %s\r\n", file_name.c_str() );
00036 
00037     file_name.push_back( '.' );
00038     std::transform( file_name.begin(), file_name.end(), file_name.begin(), (int (*)(int))std::toupper );
00039         //  "toupper" function cast had been done for latest mbed-lib (checked with rev.100). 
00040         //  see --> http://stackoverflow.com/questions/7131858/stdtransform-and-toupper-no-matching-function
00041 
00042     DirectoryList               dir( path_name );
00043     std::vector<std::string>    list;
00044 
00045     for ( int i = 0; i < dir.size(); i++ ) {
00046         if ( dir[ i ] == file_name ) {
00047             return ( default_path );
00048         }
00049 
00050         if ( *suffix == '*' ) {
00051             list    = dir;
00052         } else {
00053             //printf( "-- [%s][%d]\r\n", dir[ i ].c_str(), dir[ i ].find( ".BIN" ) );
00054             //if ( (8 == dir[ i ].find( ".BIN" )) && (0 != dir[ i ].find( "IKA" )) ) {
00055             if ( (9 == dir[ i ].find( suffix )) ) {
00056                 list.push_back( dir[ i ] );
00057             }
00058         }
00059     }
00060 
00061     led_blink.attach( &user_action_waiting_indicator, 0.05 );
00062 
00063     printf( "\r\n  WARNING : No file which has name of \"%s\" was found.\r\n", default_path.c_str() );
00064     printf( "  now showing \"%s\" directory to select a source file.\r\n\r\n", path_name.c_str() );
00065 
00066     if ( !list.size() ) {
00067         printf( "  WARNING : No file which has suffix of \"%s\" was found.\r\n", suffix );
00068         default_path.clear();
00069         return ( default_path );
00070     }
00071 
00072 #define FORMAT_FOR_SELECTED_FILE_NAME   "    selected file : [%3d] : \"%-12s\"            \r"
00073 
00074     //printf( "  ==== directory by \"%s/*.%s\" (excluding \"%s/ika*.bin\") ====\r\n", path_name.c_str(), suffix, path_name.c_str() );
00075     printf( "  ==== directory by \"%s/*.%s\", %d files ====\r\n", path_name.c_str(), suffix, list.size() );
00076 
00077     show_file_list( list );
00078 
00079     int     done    = false;
00080     int     index   = 0;
00081     int     c;
00082 
00083     printf( FORMAT_FOR_SELECTED_FILE_NAME, index, list[ index ].c_str() );
00084     fflush( stdout );
00085 
00086     while ( !done ) {
00087         if ( pc.readable() ) {
00088             switch ( c  = pc.getc() ) {
00089                 case '\r' :
00090                 case '\n' :
00091                     done    = true;
00092                     break;
00093 
00094                 case 'e' :
00095                 case 'E' :
00096                     printf( "\r\n  Erasing the flash memory\r\n" );
00097                     default_path    = "\xFF";
00098                     return ( default_path );
00099                     //break;
00100 
00101                 case 'a' :
00102                 case 'A' :
00103                 case 'c' :
00104                 case 'C' :
00105                     printf( "\r\n  Aborted to cancel the ISP execution\r\n" );
00106                     default_path.clear();
00107                     return ( default_path );
00108                     //break;
00109 
00110                 case '0' :
00111                 case '1' :
00112                 case '2' :
00113                 case '3' :
00114                 case '4' :
00115                 case '5' :
00116                 case '6' :
00117                 case '7' :
00118                 case '8' :
00119                 case '9' :
00120                     index   = c - '0';
00121                     index   = ( (list.size() - 1) < index ) ? (list.size() - 1) : index;
00122                     break;
00123                 case '?' :
00124                     show_file_list( list );
00125                     break;
00126 
00127 
00128                 case 0x1B :
00129                     if ( 0x5B == pc.getc() )
00130                         //  arrow key pressed
00131                         switch ( c  = pc.getc() ) {
00132                             case 0x41 :
00133                             case 0x43 :
00134                                 index   += ( index == (list.size() - 1) ) ? 0 : 1;
00135                                 break;
00136                             case 0x42 :
00137                             case 0x44 :
00138                                 index   -= ( index == 0 ) ? 0 : 1;
00139                                 break;
00140                             default :
00141                                 break;
00142                         }
00143             }
00144 
00145             printf( FORMAT_FOR_SELECTED_FILE_NAME, index, list[ index ].c_str() );
00146             fflush( stdout );
00147         }
00148     }
00149 
00150     default_path.clear();
00151     default_path    = path_name + "/" + list[ index ];
00152 
00153     printf( "\r\n    flash writing source file : \"%s\"\r\n\r\n", default_path.c_str() );
00154 
00155     return ( default_path );
00156 }
00157 
00158 void show_file_list( std::vector<std::string> list )
00159 {
00160     int width;
00161     int height;
00162     int d_i;
00163 
00164     width   = (list.size() / 5) + 1;
00165     width   = (4 < width) ? 4 : width;
00166     height  = (list.size() + (width - 1)) / width;
00167 
00168     for ( int i = 0; i < height; i++ ) {
00169         for ( int j = 0; j < width; j++ ) {
00170 
00171             d_i     = i + j * height;
00172 
00173             if ( list.size() <= d_i )
00174                 break;
00175 
00176             printf( "  %3d : %-12s", d_i, list[ d_i ].c_str() );
00177         }
00178         printf( "\r\n" );
00179     }
00180     printf( "\r\n" );
00181     printf( "  ** Use arrow key (up/down or left/right) to select a file\r\n" );
00182     printf( "  ** or ..\r\n" );
00183     printf( "  **   press [a] key for abort and go to serial through mode\r\n" );
00184     printf( "  **   press [e] key for erasing flash\r\n" );
00185     printf( "  **   press [?] key to display this message again\r\n\r\n" );
00186 }
00187 
00188 
00189 void user_action_waiting_indicator()
00190 {
00191     static int  i   = 0;
00192 
00193     if ( (i == 0) || (i == 3) )
00194         leds    = 0xF;
00195     else
00196         leds    = 0x0;
00197 
00198     i++;
00199     i   &= 0xF;
00200 }
00201 
00202 
00203