Library to calculate movement and to draw the objects in the pong game
Dependencies: RTC-DS1307 SPI_TFT_ILI9341 TFT_fonts mbed
Fork of MainSketch by
Poll.h@11:d812de0e5136, 2017-05-24 (annotated)
- Committer:
- tunagonen
- Date:
- Wed May 24 15:18:13 2017 +0000
- Revision:
- 11:d812de0e5136
- Parent:
- 9:eee503060d69
l
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mlin | 9:eee503060d69 | 1 | |
mlin | 9:eee503060d69 | 2 | int readX() |
mlin | 9:eee503060d69 | 3 | { |
mlin | 9:eee503060d69 | 4 | int delta=0,xv1=0,xv2=0,k=0; |
mlin | 9:eee503060d69 | 5 | int temp1=0,temp2=0; |
mlin | 9:eee503060d69 | 6 | |
mlin | 9:eee503060d69 | 7 | AnalogIn yp(PTB3); |
mlin | 9:eee503060d69 | 8 | AnalogIn ym(PTB2); |
mlin | 9:eee503060d69 | 9 | DigitalOut xp(PTB0); |
mlin | 9:eee503060d69 | 10 | DigitalOut xm(PTB1); |
mlin | 9:eee503060d69 | 11 | |
mlin | 9:eee503060d69 | 12 | xp=1; // set positive sdie of x high |
mlin | 9:eee503060d69 | 13 | xm=0; |
mlin | 9:eee503060d69 | 14 | // dont need to do anyhting to set low side as it should be fine. |
mlin | 9:eee503060d69 | 15 | // but do need to disconnect yp |
mlin | 9:eee503060d69 | 16 | //yp.PinMode(PullNone) |
mlin | 9:eee503060d69 | 17 | delta = 0; |
mlin | 9:eee503060d69 | 18 | for(k=0; k<10; k++) { // make 10 readings to average |
mlin | 9:eee503060d69 | 19 | temp1= (int)ym.read_u16(); |
mlin | 9:eee503060d69 | 20 | temp2 = (int)yp.read_u16(); |
mlin | 9:eee503060d69 | 21 | xv1+=temp1; // get value |
mlin | 9:eee503060d69 | 22 | xv2+=temp2; // get other value |
mlin | 9:eee503060d69 | 23 | delta+= abs(temp1-temp2)/10; //gets individual differences |
mlin | 9:eee503060d69 | 24 | // pc.printf("val1 = %d - val2 = %d-diff = %d\n\r",temp1,temp2,temp1-temp2); |
mlin | 9:eee503060d69 | 25 | //observing behaviour when touching / nt touching |
mlin | 9:eee503060d69 | 26 | |
mlin | 9:eee503060d69 | 27 | } |
mlin | 9:eee503060d69 | 28 | //delta=abs(xv2-xv1)/10; |
mlin | 9:eee503060d69 | 29 | if(delta<300) touching=1; |
mlin | 9:eee503060d69 | 30 | else touching=0; |
mlin | 9:eee503060d69 | 31 | pc.printf("delta=%d \t %d\n\r",delta,touching); |
mlin | 9:eee503060d69 | 32 | xp=0; |
mlin | 9:eee503060d69 | 33 | xm=0; |
mlin | 9:eee503060d69 | 34 | return (240 - (240 * ((xv1 + xv2) / 2 / 10 - 5800)) / 51200); //returns the average of both |
mlin | 9:eee503060d69 | 35 | //return(xv2/10); //maybe better to return the average of both.... |
mlin | 9:eee503060d69 | 36 | } |
mlin | 9:eee503060d69 | 37 | // subroutine to read y values - has different pin functions .. |
mlin | 9:eee503060d69 | 38 | int readY() |
mlin | 9:eee503060d69 | 39 | { |
mlin | 9:eee503060d69 | 40 | DigitalOut yp(PTB3); |
mlin | 9:eee503060d69 | 41 | DigitalOut ym(PTB2); |
mlin | 9:eee503060d69 | 42 | AnalogIn xp(PTB0); |
mlin | 9:eee503060d69 | 43 | AnalogIn xm(PTB1); |
mlin | 9:eee503060d69 | 44 | int delta=0,yv1=0,yv2=0,k=0; |
mlin | 9:eee503060d69 | 45 | int temp1=0,temp2=0; |
mlin | 9:eee503060d69 | 46 | yp=1; // set positive sdie of x high |
mlin | 9:eee503060d69 | 47 | ym=0; |
mlin | 9:eee503060d69 | 48 | // dont need to do anyhting to set low side as it should be fine. |
mlin | 9:eee503060d69 | 49 | // but do need to disconnect yp |
mlin | 9:eee503060d69 | 50 | //yp.PinMode(PullNone) |
mlin | 9:eee503060d69 | 51 | delta = 0; |
mlin | 9:eee503060d69 | 52 | for(k=0; k<10; k++) { // make 10 readings to average |
mlin | 9:eee503060d69 | 53 | temp1= (int)xm.read_u16(); |
mlin | 9:eee503060d69 | 54 | temp2 = (int)xp.read_u16(); |
mlin | 9:eee503060d69 | 55 | yv1+=temp1; // get value |
mlin | 9:eee503060d69 | 56 | yv2+=temp2; // get other value |
mlin | 9:eee503060d69 | 57 | delta+= abs(temp1-temp2)/10; |
mlin | 9:eee503060d69 | 58 | //pc.printf("val1 = %d - val2 = %d-diff = %d\n\r",temp1,temp2,temp1-temp2); |
mlin | 9:eee503060d69 | 59 | } |
mlin | 9:eee503060d69 | 60 | //int yval=(int)xm.read_u16(); // get value |
mlin | 9:eee503060d69 | 61 | //pc.printf("yval=%d",yval); |
mlin | 9:eee503060d69 | 62 | yp=0; |
mlin | 9:eee503060d69 | 63 | ym=0; |
mlin | 9:eee503060d69 | 64 | return (320 - (320 * ((yv1 + yv2)/ 20 - 3000)) / 58300); // returns Y |
mlin | 9:eee503060d69 | 65 | // return(yval); |
mlin | 9:eee503060d69 | 66 | |
mlin | 9:eee503060d69 | 67 | } |
mlin | 9:eee503060d69 | 68 | |
mlin | 9:eee503060d69 | 69 | |
mlin | 9:eee503060d69 | 70 | |
mlin | 9:eee503060d69 | 71 | void poll(){ |
mlin | 9:eee503060d69 | 72 | int xp,yp = 0; |
mlin | 9:eee503060d69 | 73 | xp=readX(); |
mlin | 9:eee503060d69 | 74 | yp=readY(); |
mlin | 9:eee503060d69 | 75 | } |