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

📄 printer.c

📁 使用ISP1362芯片的USB_OTG参考设计源代码比较新的版本
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
**  WASABI-Hot! version 1.2c   
**
**
**      -- copyright (c) 2001-2004 by Philips Japan, Ltd. -- All rights reserved --
**
**
**      ** This code has been made to check/learn                          ** 
**      **                             the ISP1362/ISP1363 functionalities **
**      ** Release 06-Aug-2004                                             **
**
**      OKANO, Akifumi
**      
**		Application Laboratory, Mobile and Connectivity
**      Semiconductors Div, Philips Japan Ltd.
**      akifumi.okano@philips.com
**      +81-3-3740-4668
*/

//	12-Nov.-2001 fseek() is removed to reduce standard library call.
//	12-Nov.-2001 File access changed from fread() to read()
//	13-Nov.-2001 Bug fixed
//	28-Jan.-2002 Simultaneous printing support
//	30-Jan.-2001  Source code directory structure changed


/****************************************************************************/
/*	includes																*/
/****************************************************************************/

#include		<stdio.h>
#include		<stdlib.h>
#include		<alloc.h>
#include		<string.h>
#include		<ctype.h>
#include		<fcntl.h>
#include		<dos.h>

#include		"_hc_core/atl_mix.h"
#include		"_hc_core/dev_ep.h"
#include		"_hc_core/transfer.h"

#include		"_hc_cls/cls_hndl.h"

#include		"class_dr/audio/sing.h"
#include		"class_dr/printer/printer.h"

#include		"ui.h"
#include		"general.h"


/****************************************************************************/
/*	constants																*/
/****************************************************************************/

#define			PRINT_BUFFER_SIZE			(10 * (1 << 10))	//	Bytes
#define			PRINT_BUFFER_FILL_REQUEST	PRINT_BUFFER_SIZE

#define			DEFAULT_PRINT_FILE			"default.pcl"

#define			NUM_OF_VENDER				3
#define			PAGE_END_CHARS				3

#define			NAK_RETRY_WAIT				20		//	ms


/****************************************************************************/
/*	global vars																*/
/****************************************************************************/

char					gp_printer_default_file_path[ FILE_NAME_STR_LENGTH ]		= "";

unsigned char			g_init_command_array[ 2 ][ 8 ]							=  {
																						{ 0x01, 0x0B, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 },
																						{ 0xA1, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA1, 0x00 }
																					};


ptr_vid_suffix			printer_vid_suffix[ NUM_OF_VENDER ]						= {
																						{	VENDER_ID_HP,    DEFAULT_FILE_SUFFIX_HP    },
																						{	VENDER_ID_EPSON, DEFAULT_FILE_SUFFIX_EPSON },
																						{	VENDER_ID_CANON, DEFAULT_FILE_SUFFIX_CANON }
																					};
char					text_page_end[ PAGE_END_CHARS ]							= { 0x0D, 0x0A, 0x0C };

unsigned short			num_of_text_page_end_chars								= PAGE_END_CHARS;			



unsigned char			g_num_of_printer_devices								= 0;
unsigned char			g_printer_device_address_list[ MAX_PRINTER_DEVICES ]	= { 0, 0 };
																		

/****************************************************************************/
/*	function prototypes														*/
/****************************************************************************/

void				printer_main( void );
printer_instance	*printer_job_initialization( printer_instance *pi_ptr );
void				start_printer( printer_instance *pi_ptr );
void				stop_printer( printer_instance *pi_ptr );
void				read_file_and_bulk_out( endpoint_info_ptr epi_ptr );
void				async_read_file_and_bulk_out( void );
void				printer_maintenance( printer_instance	*pi_ptr );

void				printer_progress_indicator( void );
unsigned long		bulk_transfer_service_from_file( unsigned char *buffer, int g_printer_image_file_handle, unsigned long file_size, endpoint_info *epi_ptr );
int					print_file_name_input_dialog( char *file_name, printer_instance *pi_ptr );
char				*vender_independent_suffix( unsigned char address );
char				isThisTextFile( char *file_name );
unsigned long 		printer_file_size( int g_printer_image_file_handle );
void				printer_status_monitor( unsigned char monitor_index );
unsigned char		is_NAK_wait( printer_instance *pi_ptr );
unsigned char		number_of_current_printing_job( void );

unsigned long		file_size( int fh );

printer_instance	*select_a_target_printer( void );

/****************************************************************************/
/*	function definitions													*/
/****************************************************************************/


/*********															*********/
/*																			*/
/*	Generic functions														*/
/*																			*/
/*		These functions are called from application							*/
/*																			*/
/*********															*********/


void printer_main( void )
{
	printer_instance *pi_ptr;
	
	mprintf( LIGHTGREEN, CONTINUE, "\r\n\r\n  PrinterTest started\r\n" );

	if ( NULL == (pi_ptr	= select_a_target_printer()) )				//	select_a_target_printer() is defined in ui.c
	{
		mprintf( YELLOW, CONTINUE, "Print cancelled.\r\n" );
		return;
	}
	
	if ( NULL != pi_ptr->image_buffer_ptr )
	{
		stop_printer( pi_ptr );
		mprintf( YELLOW, CONTINUE, "    Printing terminated by user.\r\n" );
		
		return;
	}
		
	if ( NULL == (pi_ptr	= printer_job_initialization( pi_ptr )) )
		return;
		
	start_printer( pi_ptr );
}


printer_instance *printer_job_initialization( printer_instance *pi_ptr )
{
	device_instance		*dvi_ptr;
	char				file_name[ FILE_NAME_STR_LENGTH ];
	double				size;
	unsigned char		i;
#ifdef TEXT_FILE_PRINTING
	char				text_mode;
#endif


	/*								*/
	/*  Selection for printer file	*/
	/*								*/
	
	print_file_name_input_dialog( file_name, pi_ptr );

	if ( -1 == *file_name )
	{
		mprintf( LIGHTGREEN, CONTINUE, "    Pirnt canceled by user by pressing [Esc] key.\r\n" );
		return ( NULL );
	}
	
	if ( !(*file_name) )
		strcpy( file_name, DEFAULT_PRINT_FILE );
	
	/*									*/
	/*  Check TEXT file					*/
	/*									*/

#ifdef TEXT_FILE_PRINTING
	if ( -1 == (text_mode	= isThisTextFile( file_name )) )
	{
		mprintf( LIGHTGREEN, CONTINUE, "    error @ opening print source file \"%s\"\r\n", file_name );
		return ( NULL );
	}
#endif
	
	/*									*/
	/*  File open 						*/
	/*									*/
	
	if ( 0 != (_dos_open( file_name, O_RDONLY, &( pi_ptr->image_file_handle) )) )
	{
		mprintf( LIGHTGREEN, CONTINUE, "    error @ opening print source file \"%s\"\r\n", file_name );
		return ( NULL );
	}
	
	pi_ptr->file_size	= printer_file_size(  pi_ptr->image_file_handle );
	
	mprintf( LIGHTGREEN, CONTINUE, "\r\n" );
	mprintf( LIGHTGREEN, CONTINUE, "    printing file \"%s\"\r\n", file_name );
	mprintf( LIGHTGREEN, CONTINUE, "    printing data size %lu ",  pi_ptr->file_size );
	
	if (  pi_ptr->file_size >> 20 )
		mprintf( LIGHTGREEN,  CONTINUE, "(%.2fM)", (double) pi_ptr->file_size / (double)(1L << 20) );
	else
		mprintf( LIGHTGREEN,  CONTINUE, "(%.2fK)", (double) pi_ptr->file_size / (double)(1 << 10) );
		
	mprintf( LIGHTGREEN,  CONTINUE, " bytes.\r\n" );
		
	
	mprintf(      GREEN, CONTINUE, "    printing can be aborted by key [p].\r\n" );
	mprintf(      GREEN, CONTINUE, "    other key press will be ignored.\r\n" );
	
		
	/*									*/
	/*  Allocating application buffer	*/
	/*									*/
	
	if ( NULL == ( pi_ptr->image_buffer_ptr	= (unsigned char *)malloc( (PRINT_BUFFER_SIZE & ~0x3F) * sizeof( unsigned char ) )) )	//	buffer size must be "N * 64 bytes"
		mprintf( LIGHTRED, ABORT, "malloc error @ printer_main\r\n" );
	
	/*									*/
	/*  Set target endpoint				*/
	/*									*/
	
	dvi_ptr		= devep_find_device( pi_ptr->target_device_address );

	for ( i = 1; i <= 2; i++ )
		if ( NULL != (pi_ptr->endpoint_ptr	= (dvi_ptr->epi_ptr)[ EpOUT ][ i ]) )
			break;

	return ( pi_ptr );
}


void test_func( void )
{
	device_instance		*dvi_ptr;
	unsigned char		i;

	for ( i = 0; i < MAX_PRINTER_DEVICES; i++ )
		mprintf( WHITE, CONTINUE, "    addr:%u = 0x%08lX", g_printer_device_address_list[ i ], ((printer_instance *)(devep_find_device( g_printer_device_address_list[ i ] )->class_instance_ptr))->time_for_action );

	mprintf( WHITE, CONTINUE, "\r" );
}




void start_printer( printer_instance *pi_ptr )
{
	unsigned short		size;

	if ( (NO_OPEN_ATL_TRANSFER_SLOT == (pi_ptr->tr_index	= atlmix_get_open_transfer_index( BULK_TRANSFER ))) )
		mprintf( LIGHTRED, ABORT, "error @ transfer_control_transaction, transfer list full.\r\n" );

	pi_ptr->transferred_size		= 0L;
	pi_ptr->time_for_action			= ACTION_START;
	
	//	re-install monitor line to clear the display
	ui_install_status_monitor_custom_routine( PRINTER_STATUS_MONITOR_LINE + pi_ptr->pi_index, printer_status_monitor );

	if ( 1 == number_of_current_printing_job() )
		gene_install_asynchronous_periodic_process( 1, async_read_file_and_bulk_out );

//gene_install_asynchronous_periodic_process( 9, test_func );

	//	Start print by setting buffer fill request flag;
	pi_ptr->buffer_status	= PRINT_BUFFER_SIZE;
}


void stop_printer( printer_instance *pi_ptr )
{
	pi_ptr->time_for_action				= ACTION_IDLE;

	atlmix_free_transfer_index( BULK_TRANSFER, pi_ptr->tr_index );
	
	free( pi_ptr->image_buffer_ptr );
	pi_ptr->image_buffer_ptr			= NULL;
	pi_ptr->buffer_status				= 0;
	
	_dos_close( pi_ptr->image_file_handle );
	
	if ( 0 == number_of_current_printing_job() )
		gene_install_asynchronous_periodic_process( 1, NULL );

	mprintf( LIGHTGREEN, CONTINUE, "\r\n    printer operation finished.\r\n" );
	mprintf(      GREEN, CONTINUE,     "    %lu", pi_ptr->transferred_size );
	
	if ( pi_ptr->transferred_size >> 20 )
		mprintf( GREEN,  CONTINUE, " (%.2fM)", (double)pi_ptr->transferred_size / (double)(1L << 20) );
	else
		mprintf( GREEN,  CONTINUE, " (%.2fK)", (double)pi_ptr->transferred_size / (double)(1 << 10) );

	mprintf( GREEN, CONTINUE, " bytes transferred.\r\n\r\n" );

	//	re-install monitor line to clear the display
	ui_install_status_monitor_custom_routine( PRINTER_STATUS_MONITOR_LINE + pi_ptr->pi_index, printer_status_monitor );
}




/*********															*********/
/*																			*/
/*	Printer job maintenance function										*/
/*																			*/
/*		This functions is called from ATL scheduller as callback function	*/
/*																			*/
/*********															*********/

void async_read_file_and_bulk_out( void )
{
	unsigned short		size;
	unsigned char		read_flag;
	printer_instance	*pi_ptr;
	unsigned char		i;

	for ( i = 0; i < MAX_PRINTER_DEVICES; i++ )
	{
		if ( g_printer_device_address_list[ i ] )
		{
			pi_ptr	= (printer_instance *)(devep_find_device( g_printer_device_address_list[ i ] )->class_instance_ptr);

			if ( pi_ptr->time_for_action < gp_sof_counter )
				printer_maintenance( pi_ptr );
		}
	}
}


void printer_maintenance( printer_instance	*pi_ptr )
{
	unsigned short		transferred_size;
	unsigned short		size;

	if ( pi_ptr->time_for_action == ACTION_START )
	{
		transferred_size					= 0;
		pi_ptr->requested_transfer_size		= 0;
	}
	else
	{
		transferred_size	= (pi_ptr->tr_instance_ptr)->transferred_size;

		if ( (transferred_size == 0) && (pi_ptr->time_for_action == ACTION_IMMIDIATE) )
		{
			pi_ptr->time_for_action		= gp_sof_counter + NAK_RETRY_WAIT;
			return;
		}
	}
	
	pi_ptr->time_for_action		 = ACTION_IDLE;

	pi_ptr->transferred_size	+= transferred_size;	//	Update total transferred size
	pi_ptr->buffer_status		+= transferred_size;	//	Transferred size from previous buffer filling
	
	size						 = pi_ptr->requested_transfer_size - transferred_size;

	if ( size == 0 )	//	Needs file read to fill buffer
	{
		pi_ptr->buffer_status				= 0;

		_dos_read( pi_ptr->image_file_handle, pi_ptr->image_buffer_ptr, PRINT_BUFFER_SIZE, (unsigned *)(&size) );

		if ( size )
			pi_ptr->tr_instance_ptr		= atlmix_set_transfer( BULK_TRANSFER, 0, pi_ptr->tr_index, pi_ptr->image_buffer_ptr, size, OUT, pi_ptr->endpoint_ptr, read_file_and_bulk_out );
		else
			stop_printer( pi_ptr );
			
		pi_ptr->requested_transfer_size		= size;
	}
	else
	{

⌨️ 快捷键说明

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