diff options
| author | hathach <[email protected]> | 2013-09-25 19:52:05 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2013-09-25 19:52:05 +0700 |
| commit | 89898acd5d7e0551a89e90affde5502504af720c (patch) | |
| tree | 2d6712bad02d5acc2eed1512e877025a91b88829 /demos/host/src | |
| parent | 1fee50b9fe64c32f62baa8f96e1fa3687fa7da5d (diff) | |
almost complete cli for msc
Diffstat (limited to 'demos/host/src')
| -rw-r--r-- | demos/host/src/cli.c | 132 | ||||
| -rw-r--r-- | demos/host/src/main.c | 7 | ||||
| -rw-r--r-- | demos/host/src/msc_app.c | 30 |
3 files changed, 128 insertions, 41 deletions
diff --git a/demos/host/src/cli.c b/demos/host/src/cli.c index c8e84c960..bd45da9ed 100644 --- a/demos/host/src/cli.c +++ b/demos/host/src/cli.c @@ -39,10 +39,15 @@ #if TUSB_CFG_HOST_MSC +#include "ff.h" +#include "diskio.h" + // command, function, description #define CLI_COMMAND_TABLE(ENTRY) \ - ENTRY(unknow , cli_cmd_unknow, NULL) \ - ENTRY(help , cli_cmd_help, NULL) \ + ENTRY(unknow, cli_cmd_unknow , NULL) \ + ENTRY(help , cli_cmd_help , NULL) \ + ENTRY(ls , cli_cmd_list , "list items in current directory") \ + ENTRY(cd , cli_cmd_changedir, "change current directory") \ //--------------------------------------------------------------------+ // Expands the function to have the standard function signature @@ -95,26 +100,19 @@ static cli_cmdfunc_t cli_command_tbl[] = }; //--------------------------------------------------------------------+ -// IMPLEMENTATION +// INTERNAL OBJECT & FUNCTION DECLARATION //--------------------------------------------------------------------+ -tusb_error_t cli_cmd_unknow(char const * para) -{ - puts("unknown command, please type \"help\""); - return TUSB_ERROR_NONE; -} - -tusb_error_t cli_cmd_help(char const * para) -{ - puts("current supported commands are:"); - puts("cd\tchange directory"); - puts("ls\tlist directory"); +enum { + ASCII_BACKSPACE = 8, +}; - return TUSB_ERROR_NONE; -} +#define CLI_MAX_BUFFER 256 +static char cli_buffer[CLI_MAX_BUFFER]; -#define CLI_MAX_BUFFER 50 -static char cli_buffer[CLI_MAX_BUFFER]; +//--------------------------------------------------------------------+ +// IMPLEMENTATION +//--------------------------------------------------------------------+ void cli_init(void) { @@ -124,7 +122,7 @@ void cli_init(void) void cli_poll(char ch) { if ( isprint(ch) ) - { + { // accumulate & echo if (strlen(cli_buffer) < CLI_MAX_BUFFER) { cli_buffer[ strlen(cli_buffer) ] = ch; @@ -135,20 +133,34 @@ void cli_poll(char ch) memclr_(cli_buffer, CLI_MAX_BUFFER); } } - else if ( ch == '\r') + else if ( ch == ASCII_BACKSPACE && strlen(cli_buffer)) { + printf("\33[1D"); // move curback + printf("\33[0K"); // clear to the end of line + cli_buffer[ strlen(cli_buffer)-1 ] = 0; + } + else if ( ch == '\r') + { // execute command putchar('\n'); - for(cli_cmdtype_t cmd_id = CLI_CMDTYPE_help; cmd_id < CLI_CMDTYPE_COUNT; cmd_id++) + char* p_space = strchr(cli_buffer, ' '); + uint32_t command_len = (p_space == NULL) ? strlen(cli_buffer) : (p_space - cli_buffer); + char* p_para = (p_space == NULL) ? NULL : (p_space+1); + + cli_cmdtype_t cmd_id; + for(cmd_id = CLI_CMDTYPE_COUNT - 1; cmd_id > 0; cmd_id--) { - if( 0 == strncmp(cli_buffer, cli_string_tbl[cmd_id], CLI_MAX_BUFFER) ) + if( 0 == strncmp(cli_buffer, cli_string_tbl[cmd_id], command_len) ) { - cli_command_tbl[cmd_id](NULL); - memclr_(cli_buffer, CLI_MAX_BUFFER); - return; + break; } } - cli_cmd_unknow(NULL); + cli_command_tbl[cmd_id]( p_para ); + + f_getcwd(cli_buffer, CLI_MAX_BUFFER); + printf("\nMSC %c%s\n$ ", + 'E'+cli_buffer[0]-'0', + cli_buffer+1); memclr_(cli_buffer, CLI_MAX_BUFFER); } else if (ch=='\t') // \t may be used for auto-complete later @@ -157,4 +169,72 @@ void cli_poll(char ch) } } +//--------------------------------------------------------------------+ +// UNKNOWN Command +//--------------------------------------------------------------------+ +tusb_error_t cli_cmd_unknow(char const * para) +{ + puts("unknown command, please type \"help\""); + return TUSB_ERROR_NONE; +} + +//--------------------------------------------------------------------+ +// HELP command +//--------------------------------------------------------------------+ +tusb_error_t cli_cmd_help(char const * para) +{ + puts("current supported commands are:"); + for(cli_cmdtype_t cmd_id = CLI_CMDTYPE_help+1; cmd_id < CLI_CMDTYPE_COUNT; cmd_id++) + { + printf("%s\t%s\n", cli_string_tbl[cmd_id], cli_description_tbl[cmd_id]); + } + + return TUSB_ERROR_NONE; +} + +//--------------------------------------------------------------------+ +// LS Command +//--------------------------------------------------------------------+ +tusb_error_t cli_cmd_list(const char * p_para) +{ + DIR target_dir; + + if ( (p_para == NULL) || (strlen(p_para) == 0) ) // list current directory + { + ASSERT_INT( FR_OK, f_opendir(&target_dir, "."), TUSB_ERROR_FAILED) ; + + FILINFO dir_entry; + while( (f_readdir(&target_dir, &dir_entry) == FR_OK) && dir_entry.fname[0] != 0) + { + if ( dir_entry.fname[0] != '.' ) // ignore . and .. entry + { + printf("%s%c\n", dir_entry.fname, + dir_entry.fattrib & AM_DIR ? '/' : ' '); + } + } + } + else + { + puts("ls only supports list current directory only, try to cd to that folder first"); + } + + return TUSB_ERROR_NONE; +} + +//--------------------------------------------------------------------+ +// CD Command +//--------------------------------------------------------------------+ +tusb_error_t cli_cmd_changedir(const char * p_para) +{ + if ( (p_para == NULL) || (strlen(p_para) == 0) ) return TUSB_ERROR_INVALID_PARA; + + if ( FR_OK != f_chdir(p_para) ) + { + printf("%s : No such file or directory\n", p_para); + return TUSB_ERROR_INVALID_PARA; + } + + return TUSB_ERROR_NONE; +} + #endif diff --git a/demos/host/src/main.c b/demos/host/src/main.c index d38dba3f4..d6c7e43e4 100644 --- a/demos/host/src/main.c +++ b/demos/host/src/main.c @@ -50,8 +50,6 @@ #include "app_os_prio.h" #endif -#include "cli.h" - #include "mouse_app.h" #include "keyboard_app.h" #include "msc_app.h" @@ -115,11 +113,6 @@ void os_none_start_scheduler(void) cdc_serial_app_task(NULL); rndis_app_task(NULL); - int ch = getchar(); - if ( ch > 0 ) - { - cli_poll( (char) ch); - } } } #endif diff --git a/demos/host/src/msc_app.c b/demos/host/src/msc_app.c index a6a3583d7..3971d7557 100644 --- a/demos/host/src/msc_app.c +++ b/demos/host/src/msc_app.c @@ -47,6 +47,7 @@ #if TUSB_CFG_HOST_MSC +#include "cli.h" #include "ff.h" #include "diskio.h" @@ -71,7 +72,6 @@ void tusbh_msc_mounted_cb(uint8_t dev_addr) uint8_t const* p_vendor = tusbh_msc_get_vendor_name(dev_addr); uint8_t const* p_product = tusbh_msc_get_product_name(dev_addr); - printf("Name: "); for(uint8_t i=0; i<8; i++) putchar(p_vendor[i]); printf(" "); @@ -95,12 +95,13 @@ void tusbh_msc_mounted_cb(uint8_t dev_addr) return; } - DIR root_dir; - if ( f_opendir(&root_dir, "/") != FR_OK ) - { - puts("open root dir failed"); - return; - } + char volume_label[20] = {0}; + f_getlabel(NULL, volume_label, NULL); + printf("Label: %s\n\n", volume_label); + + f_chdrive(dev_addr-1); // change to newly mounted drive + f_chdir("/"); // root as current dir + printf("MSC %c:/\n$ ", 'E'+dev_addr-1); } } @@ -112,7 +113,7 @@ void tusbh_msc_unmounted_isr(uint8_t dev_addr) void tusbh_msc_isr(uint8_t dev_addr, tusb_event_t event, uint32_t xferred_bytes) { - putchar('x'); + } //--------------------------------------------------------------------+ @@ -126,7 +127,20 @@ void msc_app_init(void) //------------- main task -------------// OSAL_TASK_FUNCTION( msc_app_task ) (void* p_task_para) { + OSAL_TASK_LOOP_BEGIN + + osal_task_delay(10); + if ( disk_is_ready(0) ) + { + int ch = getchar(); + if ( ch > 0 ) + { + cli_poll( (char) ch); + } + } + + OSAL_TASK_LOOP_END } #else |
