I need to write a program that uses a raspberry pi with a touch screen. I am sup
ID: 3687217 • Letter: I
Question
I need to write a program that uses a raspberry pi with a touch screen. I am supose to writ my own operatins system, I need the touch screen to show the date and time. I need the user to be able to program in days and times that will start a program also the user should be able to set a temperature that is desiered. I have a fan that adjusts by a dial, I need the program to come on and have a servo turn the fan on 180 degrees I have a temp sensor that will moniter the temp, the program needs to keep checking the temp once it reaches the desiered temperature it will turn the fan down but will keep checking and use the servo to keep adjusting the fan speed for a user set time frame. If there is any help at all I would appreciate it.
Explanation / Answer
Programming for a Touchscreen on the Raspberry Pi:
To accept input from a touchscreen we have to use the event interface of the Linux input system. We use the ioctl capabilities of the event interface, in addition to the normal read and write calls to get information from the touchscreen. This blog post explains how to use the touchscreen within your own programs using C as well as writing directly to the framebuffer.
The drivers I use for my touchscreen uses these options and codes;
From the above output;
The main program
There are two things I haven’t covered yet and that is calibrating of the touchscreen and jitter.
This blog entry wont cover calibration.
Jitter is when some of your sampling results for the X or Y values are out. Nearly all touchscreen do this. A quick fix is to collect a number of samples and then average the value. The are also other complex libraries available that do a lot better job at reducing jitter. Eg. tslib
We do need to scale the values for the X and Y coordinates read from the touchscreen as they are at a higher sensitivity than what the resolution of the TFT is.
My touchscreen returns a value between 190/280 and 3830 for the X or Y readings. So we need to scale this to match the resolution of the TFT, which is 320×240.
I use the values returned from getTouchScreenDetails() and framebufferInitialize() to work out what the scale value is.
1
2
3
4
scaleXvalue = ((float)screenXmax-screenXmin) / xres;
printf ("X Scale Factor = %f ", scaleXvalue);
scaleYvalue = ((float)screenYmax-screenYmin) / yres;
printf ("Y Scale Factor = %f ", scaleYvalue);
The main loop is below. To try and reduce jitter, i take a number of samples and then average them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
while(1){
for (sample = 0; sample < SAMPLE_AMOUNT; sample++){
getTouchSample(&rawX, &rawY, &rawPressure);
Xsamples[sample] = rawX;
Ysamples[sample] = rawY;
}
Xaverage = 0;
Yaverage = 0;
for ( x = 0; x < SAMPLE_AMOUNT; x++ ){
Xaverage += Xsamples[x];
Yaverage += Ysamples[x];
}
Xaverage = Xaverage/SAMPLE_AMOUNT;
Yaverage = Yaverage/SAMPLE_AMOUNT;
scaledX = Xaverage / scaleXvalue;
scaledY = Yaverage / scaleYvalue;
drawSquare(scaledX, scaledY);
}
1
2
3
4
scaleXvalue = ((float)screenXmax-screenXmin) / xres;
printf ("X Scale Factor = %f ", scaleXvalue);
scaleYvalue = ((float)screenYmax-screenYmin) / yres;
printf ("Y Scale Factor = %f ", scaleYvalue);