⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printer.c

📁 使用ISP1362芯片的USB_OTG参考设计源代码比较新的版本
💻 C
📖 第 1 页 / 共 2 页
字号:
		pi_ptr->requested_transfer_size		= size;

		atlmix_set_transfer( BULK_TRANSFER, 0, pi_ptr->tr_index, (pi_ptr->image_buffer_ptr) + pi_ptr->buffer_status, size, OUT, pi_ptr->endpoint_ptr, read_file_and_bulk_out );
	}
}


void read_file_and_bulk_out( endpoint_info_ptr epi_ptr )
{
	printer_instance	*pi_ptr;

	pi_ptr						= (printer_instance *)(devep_find_device( epi_ptr->dev_addr )->class_instance_ptr);
	pi_ptr->time_for_action		= ACTION_IMMIDIATE;
}


/*********															*********/
/*																			*/
/*	Device class handling functions											*/
/*																			*/
/*		This functions is called from functions in cls_hndl.c as callback	*/
/*		functions. 															*/
/*		All these functions are needed to be installed by 					*/
/*		"clshndl_initialization_method_install()"	in "cls_hndl.c".			*/
/*																			*/
/*		These functions will be called at printer device attach/detach		*/
/*		events. 															*/
/*																			*/
/*********															*********/


unsigned short printer_init_commands( device_instance *dvi_ptr )
{
	unsigned short			err;
	unsigned char			*buff_ptr;
	printer_instance		*pi_ptr;
	int						c_pos;
	unsigned char			list_index;

	if ( g_num_of_printer_devices == MAX_PRINTER_DEVICES )
	{
		mprintf( LIGHTRED, CONTINUE, "Too many printers. This code supprt only %d printer devices.", MAX_PRINTER_DEVICES );
		return ( 1 );
	}
	
	if ( devep_regist_config_descriptors( dvi_ptr, 1, 0 ) )
		return ( error );	

	g_num_of_printer_devices++;
	
	for ( list_index = 0; list_index < MAX_PRINTER_DEVICES; list_index++ )
	{
		if ( g_printer_device_address_list[ list_index ] == 0 )
		{
			g_printer_device_address_list[ list_index ]	= dvi_ptr->address;
			break;
		}
	}

	/*
	**	Make a printer instance for printer device
	*/

	if ( NULL == (pi_ptr		= (printer_instance *)malloc( sizeof( printer_instance ) )) )
		mprintf( LIGHTRED, CONTINUE, "error @ printer_init_commands : making printer class instance.\r\n" );
		
	dvi_ptr->class_instance_ptr		= pi_ptr;
	
	pi_ptr->time_for_action			= ACTION_IDLE;
	pi_ptr->image_buffer_ptr		= NULL;
	pi_ptr->target_device_address	= dvi_ptr->address;
	pi_ptr->buffer_status			= 0;
	
	/*
	**	Setting for monitor status display (variable setting for callback function "printer_status_monitor()")
	*/

	c_pos	= ui_unicode_to_C_string( pi_ptr->monitor_string, (dvi_ptr->string_manufacturer) + 1, (((std_string_descriptor *)(dvi_ptr->string_manufacturer))->bLength) >> 1 );
	ui_unicode_to_C_string( (pi_ptr->monitor_string) + c_pos + 1, (dvi_ptr->string_product) + 1, (((std_string_descriptor *)(dvi_ptr->string_product))->bLength) >> 1 );
	
	*((pi_ptr->monitor_string) + c_pos)		= ' ';
	
	pi_ptr->pi_index			= list_index;
	pi_ptr->operation_flag		= 1;
	
	ui_install_status_monitor_custom_routine( PRINTER_STATUS_MONITOR_LINE + list_index, printer_status_monitor );

	return ( 0 );
}


unsigned short printer_dispose_process( device_instance *dvi_ptr )
{
	printer_instance	*pi_ptr;
	unsigned char		pi_index;

	pi_ptr		= (printer_instance *)(dvi_ptr->class_instance_ptr);

	if ( pi_ptr->image_buffer_ptr != NULL )  //	Check printing process is in process ot not.
		stop_printer( pi_ptr );
				
	pi_ptr->target_device_address	= 0;
	pi_index						= pi_ptr->pi_index;
	
	ui_install_status_monitor_custom_routine( PRINTER_STATUS_MONITOR_LINE + pi_index, NULL );
	
	free( dvi_ptr->class_instance_ptr );
	dvi_ptr->class_instance_ptr		= NULL;
	
	g_printer_device_address_list[ pi_index ]	= 0;
	g_num_of_printer_devices--;

	return ( 0 );
}


/*********															*********/
/*																			*/
/*	Utility functions														*/
/*																			*/
/*********															*********/


int print_file_name_input_dialog( char *file_name, printer_instance *pi_ptr )
{
	mprintf( YELLOW, CONTINUE, "\r\n" );
	mprintf( YELLOW, CONTINUE, "    type in the file name (and path) to print.\r\n" );
	mprintf( YELLOW, CONTINUE, "    just [Return] makes to use default file.\r\n\r\n" );
	
	ui_get_string_from_console( file_name, FILE_NAME_STR_LENGTH, WHITE, "    file name >> ", YELLOW, vender_independent_suffix(  pi_ptr->target_device_address ), gp_printer_default_file_path, NULL );
	mprintf( YELLOW, CONTINUE, "\r\n" );
	
	return ( !(*file_name) );
}


char *vender_independent_suffix( unsigned char address )
{
	int		i;
	
	for ( i = 0; i < NUM_OF_VENDER; i++ )
		if ( venderID( devep_find_device( address ) ) == printer_vid_suffix[ i ].vid )
			return ( printer_vid_suffix[ i ].suffix );
	
	return ( 0 );
}


char isThisTextFile( char *file_name )
{
	//	This function returns 0xFF, if file can not be opened.
	
	FILE	*fp;
	char	data;
	
	if ( NULL == (fp	= fopen( file_name, "rb" )) )
		return ( 0xFF );	

	fread( &data, sizeof( unsigned char ), 1, fp );
	
	fclose( fp );
	
	if ( isprint( data ) )
		return ( 1 );
	else
		return ( 0 );
}


#include	<sys\stat.h>

unsigned long file_size( int fh )
{
	return ( printer_file_size( fh ) );
}


unsigned long printer_file_size( int fh )
{
	struct stat		stat_buffer;
	
	fstat( fh, &stat_buffer );

	return ( (unsigned long)(stat_buffer.st_size) );
}


unsigned char number_of_current_printing_job( void )
{
	unsigned char	i;
	unsigned char	count	= 0;

	for ( i = 0; i < MAX_PRINTER_DEVICES; i++ )
		if ( g_printer_device_address_list[ i ] )
			count	+= (((printer_instance *)(devep_find_device( g_printer_device_address_list[ i ] )->class_instance_ptr))->image_buffer_ptr) ? 1 : 0;

	return ( count );
}



/*********															*********/
/*																			*/
/*	User interface (to show the printer job status) function				*/
/*																			*/
/*		This functions is called from "ui_status_monitor()" in "ui.c" as 		*/
/*		callback function. 													*/
/*		This routine will be installed by "printer_init_commands()"	at		*/
/*		printer device enumeration.											*/
/*																			*/
/*********															*********/

//#define		DEBUG_MONITOR

void printer_status_monitor( unsigned char monitor_index )
{
	device_instance		*dvi_ptr;
	printer_instance	*pi_ptr;
	unsigned char		current_state;
	unsigned char		prev_state;

	monitor_index	-= PRINTER_STATUS_MONITOR_LINE;
	
	if ( !g_printer_device_address_list[ monitor_index ] )
		return;
	
	dvi_ptr			= devep_find_device( g_printer_device_address_list[ monitor_index ] );
	pi_ptr			= (printer_instance *)(dvi_ptr->class_instance_ptr);
	prev_state		= pi_ptr->operation_flag;
	
	current_state	= (pi_ptr->image_buffer_ptr) ? 1 : 0;
	
#ifdef DEBUG_MONITOR

	mprintf( BG_BLUE | WHITE    , CONTINUE, "    Printer" );

	if ( pi_ptr->image_buffer_ptr )
		mprintf( BG_BLUE | LIGHTGRAY, CONTINUE, "  : %p, %lu, %lu, %d", pi_ptr, pi_ptr->file_size, pi_ptr->transferred_size, pi_ptr->operation_flag );
	else
		mprintf( BG_BLUE | LIGHTGRAY, CONTINUE, "  : %p, %p", pi_ptr, pi_ptr->image_buffer_ptr );
		
#else

	if ( current_state && !prev_state )
	{
		mprintf( BG_BLUE | WHITE    , CONTINUE, "    Printer" );
		mprintf( BG_BLUE | LIGHTGRAY, CONTINUE, "(addr %2d) : ",pi_ptr->target_device_address );
		mprintf( BG_BLUE | WHITE    , CONTINUE, "printing " );
		mprintf( BG_BLUE | LIGHTGRAY, CONTINUE, "%s %5.1f%% (%8lu / %8lu bytes)", is_NAK_wait( pi_ptr ) ? "NAKing": "      ", ((double)(pi_ptr->transferred_size) / (double)(pi_ptr->file_size)) * 100.00, pi_ptr->transferred_size, pi_ptr->file_size );
	}
	
	if ( current_state )
	{
		gotoxy( 33, wherey() );
		mprintf( BG_BLUE | LIGHTGRAY, CONTINUE, "%s %5.1f%% (%8lu", is_NAK_wait( pi_ptr ) ? "NAKing": "      ", ((double)(pi_ptr->transferred_size) / (double)(pi_ptr->file_size)) * 100.00, pi_ptr->transferred_size );
	}
	
	if ( prev_state && !current_state )
	{
		mprintf( BG_BLUE | WHITE    , CONTINUE, "    Printer" );
		mprintf( BG_BLUE | LIGHTGRAY, CONTINUE, "(addr %2d) : ready. (%s)", pi_ptr->target_device_address, pi_ptr->monitor_string );
	}
	
	pi_ptr->operation_flag	= current_state;
	
#endif
}


unsigned char is_NAK_wait( printer_instance *pi_ptr )
{
	if ( 
			(pi_ptr->time_for_action == ACTION_IMMIDIATE)
				||
			(pi_ptr->time_for_action == ACTION_START    )
				||
			(pi_ptr->time_for_action == ACTION_IDLE     )
		)
		return ( False );
	else
		return ( True );
}


printer_instance *select_a_target_printer( void )
{
	printer_instance	*pi_list[ MAX_PRINTER_DEVICES ];
	unsigned char		num_of_prn;
	unsigned char		next_address;
	device_instance		*dvi_ptr;
	printer_instance	*pip;
	int					key;
	int					i;

	for ( num_of_prn = 0, next_address = 0; num_of_prn < MAX_PRINTER_DEVICES; num_of_prn++ )
	{
		if ( NULL == (dvi_ptr	= devep_find_class_interface( PRINTER_CLASS_INTERFACE, next_address )) )
			break;
		
		pi_list[ num_of_prn ]	= (printer_instance *)(dvi_ptr->class_instance_ptr);
		next_address			= dvi_ptr->address + 1;
	}

	if ( num_of_prn == 0 )
	{
		mprintf( YELLOW, CONTINUE, "No printer calss device has been found.\r\n" );
		return ( NULL );
	}
	else if ( num_of_prn == 1 )
	{
		return ( pi_list[ 0 ] );
	}	

	mprintf( YELLOW, CONTINUE, "    %d printers are on the USB.\r\n\r\n", num_of_prn );
	mprintf( YELLOW, CONTINUE, "    Choose a printer\r\n" );
	mprintf( YELLOW, CONTINUE, "    ^^^^^^^^^^^^^^^^\r\n" );
	

	for ( i = 0; i < num_of_prn; i++ )
	{
		pip		= pi_list[ i ];

		mprintf( YELLOW, CONTINUE, "      [%d] - Addr %2d ", i, pip->target_device_address );
		mprintf( YELLOW, CONTINUE, "(%s) : ", (pip->image_buffer_ptr) ? "busy" : "idle" );
		
		dvi_ptr		= devep_find_device( pip->target_device_address );
	
		if ( dvi_ptr->string_manufacturer != NULL )
			ui_print_unicode( YELLOW, dvi_ptr->string_manufacturer );
		else
			mprintf( YELLOW, CONTINUE, "Unknown printer manufacturer" );

		mprintf( YELLOW, CONTINUE, " - " );
			
		if ( dvi_ptr->string_product != NULL )
			ui_print_unicode( YELLOW, dvi_ptr->string_product );
		else
			mprintf( YELLOW, CONTINUE, "Unknown printer product" );
		
		mprintf( YELLOW, CONTINUE, "\r\n" );
	}
	
	mprintf( YELLOW, CONTINUE, "                                  choose a printer number : [ ]\r" );
	mprintf( YELLOW, CONTINUE, "                                  choose a printer number : [" );

	key		= (ui_key_wait() & 0xFF) - '0';
	
	mprintf( YELLOW, CONTINUE, "  \r                                  choose a printer number : [%d]\r\n\r\n", key );

	if ( !((unsigned int)key < num_of_prn) )
		return ( NULL );
	
	if ( pi_list[ key ]->image_buffer_ptr )
	{
		mprintf( YELLOW, CONTINUE, "Cancel printing?  \r\n" );

		if ( (devep_find_device( (pi_list[ key ])->target_device_address ))->string_product != NULL )
		{
			mprintf( YELLOW, CONTINUE, "    print process on \"" );

			ui_print_unicode( WHITE, (devep_find_device( (pi_list[ key ])->target_device_address ))->string_product );
		mprintf( YELLOW, CONTINUE, "\"\r\n" );
		}
		mprintf( YELLOW, CONTINUE, "  To continue printing, press [n] key.  \r\n" );
		mprintf( YELLOW, CONTINUE, "  To abort printing,    press other key. \r\n  " );
		mprintf( YELLOW, CONTINUE, "  >> " );
		
		if ( (ui_key_wait() & 0xFF) == 'n' )
		{	
			mprintf( YELLOW, CONTINUE, "\r\n" );
			return ( NULL );				//	if key is 'n', do nothing.
		}
	}

	mprintf( YELLOW, CONTINUE, "\r\n" );
	return ( pi_list[ key ] );
}









⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -