📄 printer.c
字号:
/*
** Kenobi2 version 1.3
**
** ** This code has been made to check/learn the 1161 functionalities **
** ** Release 25-Feb-2002 **
**
** OKANO, Akifumi
**
** Computing Segment, Semisonductors 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
/****************************************************************************/
/* includes */
/****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <dos.h>
#include "data_tr.h"
#include "dev_ep.h"
#include "tr_gene.h"
#include "ui.h"
#include "general.h"
#include "sing.h"
#include "printer.h"
/****************************************************************************/
/* constants */
/****************************************************************************/
#define COMMAND_IN 0x03F1 // Bytes
//#define PRINT_BUFFER_SIZE 8192 // Bytes
//#define PRINT_BUFFER_SIZE (20 * (1 << 10)) // Bytes
#define PRINT_BUFFER_SIZE 4096 // Bytes
#define BULK_TR_MAX_SIZE 960 // Bytes
#define TARGET_ENDPOINT 2
#define DEFAULT_PRINT_FILE "default.pcl"
#define DISPLAY_SLOWDOWN_THRESHOLD 0
#define NUM_OF_VENDER 3
#define PAGE_END_CHARS 3
/****************************************************************************/
/* global vars */
/****************************************************************************/
char g_printer_file_str[ MAX_NUM_OF_PRINTER_FILES ][ FILE_NAME_STR_LENGTH ]; // Printer file list
char g_printer_file_str_curs = 0; // Printer file list cursor
char g_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
}
};
unsigned char g_debug_flg = 0;
unsigned char g_printer_current_target_device_address; // To point the current target printer device address
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;
endpoint_info_ptr g_printer_endpoint_ptr;
/****************************************************************************/
/* function prototypes */
/****************************************************************************/
void printer_main( void );
unsigned short printer_init( device_instance *dvi_ptr );
unsigned long bulk_transfer_service_from_file( unsigned char *buffer, int file_handle, unsigned long file_size, endpoint_info *epi_ptr );
int print_file_name_input_dialog( char *file_name );
char *vender_independent_suffix( unsigned char address );
char isThisTextFile( char *file_name );
unsigned long printer_file_size( int file_handle );
/****************************************************************************/
/* function definitions */
/****************************************************************************/
void printer_main( void )
{
int file_handle;
unsigned char *print_image_buffer;
char file_name[ FILE_NAME_STR_LENGTH ];
device_instance *dvi_ptr;
char text_mode;
unsigned long file_size;
unsigned long total_size = 0;
int transfered_size;
int target_ep_index;
double size;
/* */
/* Printer start message */
/* */
mprintf( LIGHTGREEN, CONTINUE, "\r\n\r\n PrinterTest started\r\n" );
/* */
/* Finding USB printer device */
/* */
if ( NULL == (dvi_ptr = find_class_interface( PRINTER_CLASS_INTERFACE )) )
{
mprintf( LIGHTGREEN, CONTINUE, "\r\n Printer device can not be found.\r\n" );
return;
}
g_printer_current_target_device_address = dvi_ptr->address;
if ( 0 != printer_init( dvi_ptr ) )
{
mprintf( LIGHTGREEN, CONTINUE, " error @ print initialization\r\n" );
return;
}
/* */
/* Selection for printer file */
/* */
print_file_name_input_dialog( file_name );
if ( -1 == *file_name )
{
mprintf( LIGHTGREEN, CONTINUE, " Pirnt canceled by user by pressing [Esc] key.\r\n" );
return;
}
if ( !(*file_name) )
strcpy( file_name, DEFAULT_PRINT_FILE );
/* */
/* Check TEXT file */
/* */
if ( -1 == (text_mode = isThisTextFile( file_name )) )
{
mprintf( LIGHTGREEN, CONTINUE, " error @ opening print source file \"%s\"\r\n", file_name );
return;
}
if ( 0 != (_dos_open( file_name, O_RDONLY, &file_handle )) )
{
mprintf( LIGHTGREEN, CONTINUE, " error @ opening print source file \"%s\"\r\n", file_name );
return;
}
file_size = printer_file_size( file_handle );
mprintf( LIGHTGREEN, CONTINUE, "\r\n" );
mprintf( LIGHTGREEN, CONTINUE, " printing file \"%s\"\r\n", file_name );
mprintf( LIGHTGREEN, CONTINUE, " printing data size %lu ", file_size );
if ( file_size >> 20 )
mprintf( LIGHTGREEN, CONTINUE, "(%.2fM)", (double)file_size / (double)(1L << 20) );
else
mprintf( LIGHTGREEN, CONTINUE, "(%.2fK)", (double)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 == (print_image_buffer = (unsigned char *)malloc( (PRINT_BUFFER_SIZE) * sizeof( unsigned char ) )) )
mprintf( RED, ABORT, "malloc error @ printer_main\r\n" );
/* */
/* Read file, send on bulk */
/* */
if ( ((dvi_ptr->epi_ptr)[ 1 ])->direction == 0 ) // "0" means drection=OUT
target_ep_index = 1;
else
target_ep_index = 2;
g_printer_endpoint_ptr = (dvi_ptr->epi_ptr)[ target_ep_index ];
total_size = bulk_transfer_service_from_file( print_image_buffer, file_handle, file_size, g_printer_endpoint_ptr );
/* */
/* Bulk tr finished */
/* */
if ( text_mode ) // If it was TEXT file, add pagebreak.
{
transfered_size = num_of_text_page_end_chars;
do
{
transaction( OUT, (unsigned char *)(&text_page_end), (unsigned short *)(&transfered_size), g_printer_endpoint_ptr );
}
while ( !transfered_size );
}
mprintf( LIGHTGREEN, CONTINUE, " %8lu ", total_size );
if ( total_size >> 20 )
mprintf( LIGHTGREEN, CONTINUE, "(%.2fM) bytes transferred.\r\n", (double)total_size / (double)(1L << 20) );
else
mprintf( LIGHTGREEN, CONTINUE, "(%.2fK) bytes transferred.\r\n", (double)total_size / (double)(1 << 10) );
mprintf( LIGHTGREEN, CONTINUE, "\r\n print finished\r\n\r\n" );
g_printer_current_target_device_address = 0;
free( print_image_buffer );
_dos_close( file_handle );
}
unsigned short printer_init( device_instance *dvi_ptr )
{
unsigned short err;
unsigned char *buff_ptr;
if ( NULL == (buff_ptr = (unsigned char *)malloc( 161 * sizeof( unsigned char ) )) )
mprintf( RED, ABORT, "malloc error @ print_init\r\n" );
#if 0
if ( venderID( dvi_ptr ) == VENDER_ID_HP )
{
if ( 0x0FFF & control_transfer( (USB_Device_Request *)g_init_command_array[ 0 ], dvi_ptr, buff_ptr ) )
{
free( buff_ptr );
return ( err );
}
}
#endif
if ( 0x0FFF & control_transfer( (USB_Device_Request *)g_init_command_array[ 1 ], dvi_ptr, buff_ptr ) )
{
mprintf( LIGHTGREEN, CONTINUE, " error @ print initialization 1 0x%04X\r\n" );
free( buff_ptr );
return ( err );
}
free( buff_ptr );
return ( 0 );
}
unsigned long bulk_transfer_service_from_file( unsigned char *base_buffer_ptr, int file_handle, unsigned long file_size, endpoint_info *epi_ptr )
{
unsigned char *current_buffer_ptr;
device_instance *dvi_ptr;
unsigned int read_size;
unsigned int transfered_size;
unsigned char completion_code;
unsigned long total_size = 0;
unsigned long display_slow = 0;
mprintf( LIGHTGREEN, CONTINUE, " waiting for the printer ready.\r" );
while ( 1 )
{
if ( FILE_ACCESS_READ != dos_read_exclusive( file_handle, base_buffer_ptr, PRINT_BUFFER_SIZE, &read_size, FILE_ACCESS_FREE, FILE_ACCESS_REQUEST_FROM_PRTR ) )
continue;
if ( read_size == 0 )
break;
current_buffer_ptr = base_buffer_ptr;
while ( read_size )
{
// transfered_size = read_size;
transfered_size = read_size > BULK_TR_MAX_SIZE ? BULK_TR_MAX_SIZE : read_size;
completion_code = transaction( OUT, current_buffer_ptr, (unsigned short *)(&transfered_size), epi_ptr );
if ( isCCFatalError( completion_code ) )
{
mprintf( RED, CONTINUE, "\r\n error @ communication, completion_code = 0x%01X.\r\n\r\n", completion_code );
return ( total_size );
}
if ( !transfered_size )
{
if ( DISPLAY_SLOWDOWN_THRESHOLD == display_slow )
{
mprintf( YELLOW, CONTINUE, " SlowedDown by Printer \r" );
wait_ms( 50 );
}
display_slow = 1;
}
else
{
read_size -= transfered_size;
current_buffer_ptr += transfered_size;
total_size += transfered_size;
if ( key_polling_timing() )
mprintf( LIGHTGREEN, CONTINUE, " %8lu bytes (%5.1f%) transferred.\r", total_size, ((double)total_size / (double)file_size) * 100.00 );
display_slow = 0;
}
/* */
/* Check abort command */
/* */
if ( key_polling_timing() )
if ( bioskey( 1 ) )
if ( 'p' == (bioskey( 0 ) & 0xFF) )
{
mprintf( RED, CONTINUE, "\r\n printing terminated by user.\r\n\r\n" );
return ( total_size );
}
/* */
/* Check device still exists */
/* */
if ( NULL == find_device( g_printer_current_target_device_address ) )
{
mprintf( LIGHTGREEN, CONTINUE, " error @ printer device disappeared.\r\n" );
return ( total_size );
}
}
}
return ( total_size );
}
int print_file_name_input_dialog( char *file_name )
{
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" );
get_string_from_console( file_name, FILE_NAME_STR_LENGTH, WHITE, " file name >> ", YELLOW, vender_independent_suffix( g_printer_current_target_device_address ) );
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( 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 printer_file_size( int file_handle )
{
struct stat stat_buffer;
fstat( file_handle, &stat_buffer );
return ( (unsigned long)(stat_buffer.st_size) );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -