📄 otg_demo.c
字号:
*/
size = g_rfp_transferred_size > (MEMORY_FILLING_SIZE_FROM_FILE << 1) ? (MEMORY_FILLING_SIZE_FROM_FILE << 1) : g_rfp_transferred_size;
memcpy( (unsigned char *)data_buffer, g_rfp_tmp_buff, size );
/*
* Return read data size
*/
return ( size >> 1 ); // size convert from bytes to short
}
void remote_read_callback( endpoint_info_ptr dummy )
{
/*
* This is a call back function for bulk transfer
*/
transfer_instance *tip;
unsigned short size;
dummy = dummy;
size = g_rfp_tip->transferred_size;
g_rfp_bf_ptr += size;
g_rfp_transferred_size += size;
g_rfp_remaining_tr_size -= size;
if ( g_rfp_remaining_tr_size == 0 )
g_data_tr_done = 0xF;
else if ( (g_rfp_tip->completion_code) )
g_data_tr_done = g_rfp_tip->completion_code;
else if ( !g_rfp_transfer_stop ) // Retry for remaining data
atlmix_set_transfer( BULK_TRANSFER, 0, g_rfp_tr_index, g_rfp_bf_ptr, g_rfp_remaining_tr_size, IN, (g_far_end.target_device_instance_ptr)->epi_ptr[ EpIN ][ 4 ], remote_read_callback );
}
void play_remote_file( unsigned short (*buff_fill_func)( short *buffer_ptr ) )
{
device_instance *dvi_ptr;
if ( is_audio_active() )
return;
if ( NULL == (dvi_ptr = devep_find_class_interface( AUDIO_CLASS_INTERFACE, 0 )) )
return ;
g_rfp_tr_index = 0xFF;
if ( 0 != audio_initialize( dvi_ptr, "*", buff_fill_func, remote_stop ) )
return;
audio_start();
}
void remote_stop( void )
{
char bf[ 64 ];
unsigned short rx_size;
atlmix_free_transfer_index( BULK_TRANSFER, g_rfp_tr_index );
g_rfp_tr_index = 0xFF;
mprintf( WHITE, CONTINUE, " remote file play end. \r\n" );
if ( OTG_command_to_device( bf, OTG_CONTROL_TR_DIR_IN, OTG_DEMO_REMOTE_AUDIO, OTG_DEMO_SUB_COMMAND_REMOTE_F_CLOSE, 4, &rx_size, 1 ) )
{
OTG_release_far_end_device();
return;
}
mprintf( WHITE, CONTINUE, " remote file closed.\r\n" );
OTG_release_far_end_device();
}
// 222 222 222 222 222 222 222
// 2 2 2 2 2 2 2 2 2 2 2 2 2 2
// 2 2 2 2 2 2 2
// 2 2 2 2 2 2 2
// 2 2 2 2 2 2 2
// 22222 22222 22222 22222 22222 22222 22222
// PPPP EEEE RRRR I PPPP H H PPPP EEEE RRRR I PPPP H H PPPP EEEE RRRR I PPPP H H
// P P E R R I P P H H P P E R R I P P H H P P E R R I P P H H
// P P EEE R R I P P HHHHH P P EEE R R I P P HHHHH P P EEE R R I P P HHHHH
// PPP E RRRR I PPPP H H PPP E RRRR I PPPP H H PPP E RRRR I PPPP H H
// P EEEE R R I P H H P EEEE R R I P H H P EEEE R R I P H H
/*
** OTG demo applet "Music play with OTG remote source"
**
** Peripheral side service
*/
#define NUM_OF_BUFF 2
typedef struct _peri_bf {
unsigned char bp[ TRANSFER_SIZE ];
unsigned short length;
}
peri_bf;
peri_bf g_pbf[ 2 ];
unsigned char g_pbf_switch;
int g_rfp_file_handle = 0;
unsigned char *g_transmitting_buffer_pos_ptr;
unsigned short g_transmitting_length;
unsigned long g_file_size_bytes;
unsigned long g_total_transferred_bytes;
unsigned long g_last_access_time_stanp;
unsigned long g_remote_file_read_request;
void file_acccess_monitor( void );
char fmt[] = " %10lu bytes data transferred. (ramaining %03lu sec) \r";
unsigned long fmt_length = 60L;
unsigned long pos_calc_factor;
unsigned char g_Dc_display_rating;
unsigned char g_Dc_display_rating_mask = 0x0F;
#define SCALE_pos_calc_factor 26
void OTG_demo_peripheral_side_2( char *s, unsigned short *length, unsigned char index )
{
switch ( index & 0x70 )
{
case OTG_DEMO_SUB_COMMAND_REMOTE_F_OPEN :
if ( g_rfp_file_handle )
_dos_close( g_rfp_file_handle );
mprintf( WHITE, CONTINUE, " \r\n" );
mprintf( WHITE, CONTINUE, " \r\n" );
mprintf( WHITE, CONTINUE, " \r\n" );
mprintf( WHITE, CONTINUE, " \r\n" );
mprintf( LIGHTGREEN, CONTINUE, " audio file open \"%s\" \r\n", gp_sing_file_strings[ index & 0xF ].file_path_and_name );
Dc_printf( WHITE, CONTINUE, "remote command : File \"#%u\" open and start serving data.\r\n", index & 0xF );
if ( 0 != (_dos_open( gp_sing_file_strings[ index & 0xF ].file_path_and_name, O_RDONLY, &g_rfp_file_handle )) )
{
*((unsigned long *)s) = 0x0;
*length = 0x4;
mprintf( LIGHTGREEN, CONTINUE, " audio file open failed. \r\n" );
}
else
{
wave_header wave_head;
unsigned int readsz;
g_total_transferred_bytes = 0;
g_pbf_switch = 0;
g_file_size_bytes = file_size( g_rfp_file_handle );
*((unsigned long *)s) = g_file_size_bytes;
*length = 0x4;
pos_calc_factor = (fmt_length << SCALE_pos_calc_factor) / g_file_size_bytes;
g_Dc_display_rating = 0;
// Skip WAVE header part
_dos_read( g_rfp_file_handle, &wave_head, sizeof( wave_header ), &readsz );
if ( 0 != (_dos_read( g_rfp_file_handle, g_pbf[ g_pbf_switch ].bp, (MEMORY_FILLING_SIZE_FROM_FILE << 1), (unsigned *)(&(g_pbf[ g_pbf_switch ].length)) )) )
mprintf( LIGHTGREEN, CONTINUE, " audio file read failed. \r\n" );
mprintf( LIGHTGREEN, CONTINUE, " audio file opened to serve %s (%lu bytes)\r\n", gp_sing_file_strings[ index & 0xF ].file_path_and_name, g_file_size_bytes );
mprintf( WHITE, CONTINUE, "\r\n" );
mprintf( WHITE, CONTINUE, " " );
mprintf( (RED << 4) | WHITE, CONTINUE, " DC serving audio data to HOST peer. " );
mprintf( WHITE, CONTINUE, "\r\n" );
if ( *(gp_sing_file_strings[ index & 0xF ].file_comment) )
mprintf( WHITE, CONTINUE, " \"%s\"\r\n", gp_sing_file_strings[ index & 0xF ].file_comment );
else
mprintf( WHITE, CONTINUE, " \"%s\"\r\n", gp_sing_file_strings[ index & 0xF ].file_path_and_name );
mprintf( WHITE, CONTINUE, "\r\n\r\n\r\n" );
g_remote_file_read_request = 1;
g_last_access_time_stanp = gp_sof_counter;
gene_install_asynchronous_periodic_process( 7, file_acccess_monitor );
}
break;
// case OTG_DEMO_SUB_COMMAND_REMOTE_F_READ :
// break;
case OTG_DEMO_SUB_COMMAND_REMOTE_F_CLOSE :
gene_install_asynchronous_periodic_process( 7, NULL );
Dc_printf( WHITE, CONTINUE, "remote command : File close (%lu bytes data transferred)\r\n", g_total_transferred_bytes );
if ( g_rfp_file_handle )
{
_dos_close( g_rfp_file_handle );
g_rfp_file_handle = 0;
mprintf( WHITE, CONTINUE, " \r\n" );
mprintf( WHITE, CONTINUE, " \r\n" );
mprintf( WHITE, CONTINUE, " \r\n" );
mprintf( WHITE, CONTINUE, " \r\n" );
mprintf( LIGHTGREEN, CONTINUE, " audio file closed. Play finished.\r\n %10lu (%5.2lfM) bytes data transferred.\r\n", g_total_transferred_bytes, (double)g_total_transferred_bytes / (double)(1L << 20) );
mprintf( WHITE, CONTINUE, " \r\n" );
mprintf( WHITE, CONTINUE, " \r\n" );
mprintf( WHITE, CONTINUE, " \r\n" );
mprintf( WHITE, CONTINUE, " \r\n" );
*((unsigned long *)s) = 0xCCCCDDDD; // return dummy data
*length = 0x4;
}
break;
case OTG_DEMO_SUB_COMMAND_N_OF_FILES :
{
unsigned char i;
for ( i = 0; i < MAX_NUM_OF_AUDIO_FILES; i++ )
if ( !(*gp_sing_file_strings[ i ].file_path_and_name) )
break;
*((unsigned char *)s) = i;
*length = 0x1;
}
break;
default :
break;
}
}
void otg_demo_file_read_periph_side( void )
{
char s[ 70 ];
char pos_char;
unsigned char pos;
unsigned short i;
unsigned char progress_bar_character;
unsigned char progress_bar_background;
unsigned char progress_bar_done_character;
unsigned char progress_bar_done_background;
if ( !(g_Dc_display_rating++ & g_Dc_display_rating_mask) )
{
if ( gp_is_in_DOS_environment )
{
progress_bar_done_character = BLACK;
progress_bar_done_background = (LIGHTGRAY << 4);
progress_bar_character = LIGHTGREEN;
progress_bar_background = (CYAN << 4);
}
else
{
progress_bar_done_character = GREEN;
progress_bar_done_background = (WHITE << 4);
progress_bar_character = BLACK;
progress_bar_background = (LIGHTGREEN << 4);
}
sprintf( s, fmt, g_total_transferred_bytes, bytes_to_sec(g_file_size_bytes - g_total_transferred_bytes) );
pos = (pos_calc_factor * g_total_transferred_bytes) >> SCALE_pos_calc_factor;
pos_char = *(s + pos);
*(s + pos) = 0;
Dc_printf( progress_bar_done_background | progress_bar_done_character, CONTINUE, "%s", s );
*(s + pos) = pos_char;
Dc_printf( progress_bar_background | progress_bar_character, CONTINUE, "%s", s + pos );
}
if ( 0 != (_dos_read( g_rfp_file_handle, g_pbf[ g_pbf_switch ].bp, (MEMORY_FILLING_SIZE_FROM_FILE << 1), (unsigned *)(&(g_pbf[ g_pbf_switch ].length)) )) )
mprintf( LIGHTGREEN, CONTINUE, " audio file read failed. \r\n" );
g_total_transferred_bytes += g_pbf[ g_pbf_switch ].length;
g_transmitting_buffer_pos_ptr = g_pbf[ g_pbf_switch ].bp;
g_transmitting_length = (g_pbf[ g_pbf_switch ].length == (MEMORY_FILLING_SIZE_FROM_FILE << 1)) ? TRANSFER_SIZE : g_pbf[ g_pbf_switch ].length;
// g_pbf_switch = g_pbf_switch ? 0 : 1;
g_last_access_time_stanp = gp_sof_counter;
}
void file_acccess_monitor( void )
{
char dummy0[ 4 ];
unsigned short dummy1;
if ( g_remote_file_read_request )
{
g_remote_file_read_request = 0;
hwacces_isr_hw_disable();
start_pio_in();
hwacces_isr_hw_enable();
}
if ( gp_sof_counter < (g_last_access_time_stanp + 1000) )
return;
mprintf( LIGHTRED, CONTINUE, " WAV file device service detected no access for 1 second.\r\n" );
OTG_demo_peripheral_side_2( dummy0, &dummy1, OTG_DEMO_SUB_COMMAND_REMOTE_F_CLOSE ); // close file by device side self
}
// 333 333 333 333 333 333 333
// 3 3 3 3 3 3 3 3 3 3 3 3 3 3
// 3 3 3 3 3 3 3
// 3 3 3 3 3 3 3
// 3 3 3 3 3 3 3 3 3 3 3 3 3 3
// 333 333 333 333 333 333 333
// H H OOO SSSS TTTTT H H OOO SSSS TTTTT H H OOO SSSS TTTTT H H OOO SSSS TTTTT
// H H O O S T H H O O S T H H O O S T H H O O S T
// HHHHH O O SSS T HHHHH O O SSS T HHHHH O O SSS T HHHHH O O SSS T
// H H O O S T H H O O S T H H O O S T H H O O S T
// H H OOO SSSS T H H OOO SSSS T H H OOO SSSS T H H OOO SSSS T
/********* ********** ********** ********** ********** ********** ********** ********** ********** **********
OTG demo applet "Remote beep"
********* ********** ********** ********** ********** ********** ********** ********** ********** **********/
/*
** OTG demo applet "Remote beep"
**
** Host side service
*/
void OTG_demo_host_side_3( void )
{
char bf[ 64 ];
unsigned short rx_size;
unsigned short i;
if ( OTG_grab_far_end_device() )
return;
mprintf( LIGHTCYAN, CONTINUE, "\r\n\r\nOTG feature demo : " );
mprintf( WHITE, CONTINUE, "Beeping together\r\n\r\n" );
mprintf( WHITE, CONTINUE, "\r\n" );
OTG_command_to_device( bf, OTG_CONTROL_TR_DIR_IN, OTG_DEMO_REMOTE_BEEP, 0, 4, &rx_size, 1 );
beep( 440.00, 1.00, DECAY, POLY );
OTG_release_far_end_device();
}
// 333 333 333 333 333 333 333
// 3 3 3 3 3 3 3 3 3 3 3 3 3 3
// 3 3 3 3 3 3 3
// 3 3 3 3 3 3 3
// 3 3 3 3 3 3 3 3 3 3 3 3 3 3
// 333 333 333 333 333 333 333
// PPPP EEEE RRRR I PPPP H H PPPP EEEE RRRR I PPPP H H PPPP EEEE RRRR I PPPP H H
// P
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -