Blame view

bit_image.c 3.72 KB
996f12b0   aarnaude   controle en mode ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  /* code contrôle de l'imprimante 
   *
   * EPSON LQ 570 + 
   * P406
   * Buffer size = 8kB
   *
   */
  
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <termios.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <strings.h>
  
  /**************
   * Constantes *
   * ************/
  
  #define SPEED B9600
  #define PATH "/dev/usb/lp0"
  #define N_TEST 3
  
  /**********************
   * Commandes usuelles *
   * ********************/
  
  unsigned int N_COLUMN=12;
  unsigned char ESC=0x1B;
  unsigned char AT = 0x40;//@
  unsigned char CR = 0x0D;
  unsigned char LF = 0x0A;
  unsigned char FF = 0x0C;
  unsigned char OPAR = 0x28;//(
  unsigned char NUL = 0;
  unsigned char UN = 1;
  
  /*********************************
   * Fonctions communication série *
   *********************************/
  
  struct termios saveterm;
  
  int init_serial(char *device,int speed) {
  	struct termios new;
  	int fd=open(device,O_RDWR|O_NOCTTY); 
  	if(fd<0){perror(device); exit(-1);}
  	tcgetattr(fd,&saveterm); 
  	bzero(&new,sizeof(new));
  	new.c_cflag=CLOCAL|CREAD|speed|CS8;
  	new.c_iflag=0;
  	new.c_oflag=0;
  	new.c_lflag=0;  // set input mode (non-canonical, no echo,...) 
  	new.c_cc[VTIME]=0; // inter-character timer unused 
  	new.c_cc[VMIN]=1; // blocking read until 1 char received 
  	tcflush(fd, TCIFLUSH);
  	tcsetattr(fd,TCSANOW,&new);
  	return fd;
  }
  
  void close_serial(int fd) {
  	tcsetattr(fd,TCSANOW,&saveterm);
  	close(fd);
  }
  
  /*********************************************
   * Fonction d'initialisation de l'imprimante *
   *********************************************/
  
  void init_impr(int fd)
  {
  	write(fd, &ESC, 1); //ESC
  	write(fd, &AT, 1); //@ : ESC @ permet d'initialiser l'imprimante
  }
  
  /*****************************************
   * Fonctions de controle de l'imprimante *
   *****************************************/
  
  void bit_image(int fd){
  	//P29
  	unsigned char STAR = 0x2A;
  	unsigned char PLUS = 0x2B;
  	unsigned int QH = 48;
  	unsigned int data1 = 0xA0;
  	unsigned int data2 = 0x3A;
  	unsigned int data3 = 0x9F;
  	unsigned int data4 = 0xCD;
  
  	unsigned char G = 0x47;
  	unsigned int j = 0;
  	unsigned int test = 0;	
  
  	//P176
  	//Entering Graphic mode	
  	write(fd,&ESC,1);
  	write(fd,&OPAR,1);
  	write(fd,&G,1);
  	write(fd,&UN,1);
  	write(fd,&NUL,1);
  	write(fd,&UN,1);
  
  	// Set line spacing with ESC + 48
  	write(fd,&ESC,1);
  	write(fd,&PLUS,1);
  	write(fd,&QH,1);
  
  	//Go to starting line
  	write(fd,&CR,1);
  	write(fd,&LF,1);	
  
  	// ESC * for each line of dots P184
  	for(test=0;test<N_TEST;test++){
  		write(fd,&ESC,1);
  		write(fd,&STAR,1);
  		write(fd,&NUL,1); // m = 0
  		write(fd,&N_COLUMN,1); // nl
  		write(fd,&NUL,1); // nh
  		for(j=0;j<(N_COLUMN);j++){
  			//8 dot columns, 1 byte per column
  			write(fd,&data1,1);
  			write(fd,&data2,1);
  			write(fd,&data3,1);
  			write(fd,&data4,1);
  		}
  		// CR LF
  		write(fd,&CR,1);
  		write(fd,&LF,1);
  	}	
  	
  	data1=0xF0;
  	data2=0xF1;
  	data3=0xF2;
  	data4=0xF4;
  	for(test=0;test<N_TEST;test++){
  		write(fd,&ESC,1);
  		write(fd,&STAR,1);
  		write(fd,&NUL,1); // m = 0
  		write(fd,&N_COLUMN,1); // nl
  		write(fd,&NUL,1); // nh
  		for(j=0;j<N_COLUMN;j++){
  			// 8 dot columns, 1 byte per column 
  			write(fd,&data1,1);
  			write(fd,&data2,1);
  			write(fd,&data3,1);
  			write(fd,&data4,1);
  		}
  		// CR LF
  		write(fd,&CR,1);
  		write(fd,&LF,1);
  	}	
  	
  	data1=0xA0;
  	data2=0xA1;
  	data3=0xA2;
  	data4=0xA4;
  	for(test=0;test<N_TEST;test++){
  		write(fd,&ESC,1);
  		write(fd,&STAR,1);
  		write(fd,&NUL,1); // m = 0
  		write(fd,&N_COLUMN,1); // nl
  		write(fd,&NUL,1); // nh
  		for(j=0;j<N_COLUMN;j++){
  			// 8 dot columns, 1 byte per column 
  			write(fd,&data1,1);
  			write(fd,&data2,1);
  			write(fd,&data3,1);
  			write(fd,&data4,1);
  		}
  		// CR LF
  		write(fd,&CR,1);
  		write(fd,&LF,1);
  	}	
  }
  
  /********
   * Main *
   ********/
  
  int main(void) {
  	int fd = init_serial(PATH, SPEED);
  	init_impr(fd);
  	bit_image(fd);
  	close_serial(fd);
  	return 0;
  }