-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathHW_Test.c
290 lines (249 loc) · 6.62 KB
/
HW_Test.c
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "Mouse.h"
#include "Touch.h"
#include <string.h>
#include <stdlib.h>
#include <wiringPi.h>
#include "mcp23017.c"
const char *i2cNode1 = "/dev/i2c-1"; //linux i2c node for the Hyperpixel display on Pi 4
const char *i2cNode2 = "/dev/i2c-11"; //linux i2c node for the Hyperpixel display on Pi 4
int mcp23017_addr = 0x20; //MCP23017 I2C Address
int p1;
int p2;
int k1;
int k2;
int lastp1;
int lastk1;
int lastp2;
int lastk2;
#define pttPin 0 // Wiring Pi pin number. Physical pin is 11
#define keyPin 1 //Wiring Pi pin number. Physical pin is 12
#define i2cPttPin 0 //MCP23017 PTT Input if fitted Port A bit 0
#define i2cKeyPin 1 //MCP23017 Key Input if fitted Port A bit 1
int mousePresent;
int touchPresent;
int hyperPixelPresent;
int MCP23017Present;
char mousePath[20];
char touchPath[20];
char i2cPath[20];
void detectHw();
void processTouch();
void processMouse(int mbut);
void initMCP23017(int add);
void initGPIO(void);
void processGPIO(void);
int main()
{
printf("\n\nStarting Langstone Hardware Test\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
detectHw();
if(mousePresent)
{
printf("Mouse detected on %s\n",mousePath);
initMouse(mousePath);
}
else
{
printf("ERROR:- No Mouse Detected\n");
}
if(touchPresent)
{
printf("Touch Screen detected on %s\n",touchPath);
initTouch(touchPath);
}
else
{
printf("ERROR:- No Touch Screen Detected\n");
}
if(hyperPixelPresent)
{
printf("HyperPixel Screen Detected\n");
initTouch(touchPath);
}
if(MCP23017Present)
{
printf("MCP23017 Detected on i2c Bus %s\n",i2cPath);
}
initGPIO();
printf("\n");
if(mousePresent) printf("Looking for Mouse Scroll Wheel and Buttons\n");
if(touchPresent) printf("Looking for Touches \n");
if(MCP23017Present) printf("Looking for inputs from MCP23017 pins PA0 and PA1\n");
if(hyperPixelPresent==0) printf("Looking for inputs from GPIO pins 11 (PTT) and 12 (KEY)\n");
printf("\nTesting...... Enter <Control C> to exit\n\n");
while(1)
{
if(touchPresent)
{
if(getTouch()==1)
{
processTouch();
}
}
if(mousePresent)
{
int but=getMouse();
if(but>0)
{
processMouse(but);
}
}
processGPIO();
}
}
void detectHw()
{
FILE * fp;
char * ln=NULL;
size_t len=0;
ssize_t rd;
int p;
char handler[2][10];
char * found;
p=0;
mousePresent=0;
touchPresent=0;
fp=fopen("/proc/bus/input/devices","r");
while ((rd=getline(&ln,&len,fp)!=-1))
{
if(ln[0]=='N') //name of device
{
p=0;
if((strstr(ln,"FT5406")!=NULL) || (strstr(ln,"pi-ts")!=NULL)) //Found Raspberry Pi TouchScreen entry
{
p=1;
hyperPixelPresent=0;
}
if(strstr(ln,"Goodix")!=NULL) //Found Hyperpixel TouchScreen entry
{
p=1;
hyperPixelPresent=1;
}
}
if(ln[0]=='H') //handlers
{
if(strstr(ln,"mouse")!=NULL)
{
found=strstr(ln,"event");
strcpy(handler[p],found);
handler[p][6]=0;
if(p==0)
{
sprintf(mousePath,"/dev/input/%s",handler[0]);
mousePresent=1;
}
if(p==1)
{
sprintf(touchPath,"/dev/input/%s",handler[1]);
touchPresent=1;
}
}
}
}
fclose(fp);
if(ln) free(ln);
// try to initialise MCP23017 i2c chip for additonal I/O
// this will set or reset MCP23017Present flag
initMCP23017(mcp23017_addr);
}
void processTouch()
{
if(hyperPixelPresent==1)
{
float tempX=touchX;
float tempY=touchY;
tempX=tempX*0.6; //convert 0-800 to 0-480
tempY=800-(tempY*1.6666); //convert 480-0 to 0-800
touchX=tempY; //swap X and Y
touchY=tempX;
}
printf("Touch detected at X=%d Y=%d\n",touchX,touchY);
}
void processMouse(int mbut)
{
if(mbut==128) //scroll whell turned
{
if(mouseScroll>0) printf("Scroll Wheel Clicked Clockwise\n");
if(mouseScroll<0) printf("Scroll Wheel Clicked Anticlockwise\n");
mouseScroll=0;
}
if(mbut==1+128) //Left Mouse Button down
{
printf("Left Mouse Button Pressed\n");
}
if(mbut==2+128) //Right Mouse Button down
{
printf("Right Mouse Button Pressed\n");
}
}
// Configure the MCP23017 GPIO Extender if it is fitted :
void initMCP23017(int add)
{
int resp;
resp=i2c_init(i2cNode1); //Initialize i2c node for a pi with normal display
sprintf(i2cPath,"%s",i2cNode1);
if(resp<0) //not found, try for pi with Hypepixel4 display
{
resp=i2c_init(i2cNode2); //Initialize i2c node fo4r a pi with normal display
sprintf(i2cPath,"%s",i2cNode2);
if(resp<0)
{
MCP23017Present=0; //neither found so disable it
return;
}
}
resp= mcp23017_readport(mcp23017_addr,GPIOA); //dummy read to check if device is present
if(resp<0)
{
MCP23017Present=0;
return;
}
mcp23017_writereg(add,IODIR,GPIOA,0x03); //Port A bits 0 and 1 are inputs (PTT and KEY)
mcp23017_writereg(add,GPPU,GPIOA,0x03); //Port A pullups enabled
mcp23017_writereg(add,IODIR,GPIOB,0x00); //Port B all outputs for Band Bits
mcp23017_writeport(add,GPIOA,0);
mcp23017_writeport(add,GPIOB,0); //Zero all outputs
MCP23017Present=1;
}
void initGPIO(void)
{
if(hyperPixelPresent==0)
{
wiringPiSetup();
pinMode(pttPin,INPUT);
pinMode(keyPin,INPUT);
}
}
void processGPIO(void)
{
if(hyperPixelPresent==0) //can only use GPIO if Hyperpixel Display is not fitted.
{
p1=digitalRead(pttPin);
k1=digitalRead(keyPin);
if(p1!=lastp1)
{
lastp1=p1;
printf("GPIO Pin 11 (PTT) =%d\n",p1);
}
if(k1!=lastk1)
{
lastk1=k1;
printf("GPIO Pin 12 (KEY) =%d\n",k1);
}
}
if(MCP23017Present==1) //MCP23017 extender chip can be used with any display.
{
p2=mcp23017_readbit(mcp23017_addr,GPIOA,0);
k2=mcp23017_readbit(mcp23017_addr,GPIOA,1);
if(p2!=lastp2)
{
lastp2=p2;
printf("MCP23017 Pin PA0 (PTT) =%d\n",p2);
}
if(k2!=lastk2)
{
lastk2=k2;
printf("MCP23017 Pin PA1 (KEY) =%d\n",k2);
}
}
}