The Professional Perfectionists
/
Test_Mouse
Just mouse test for x,y position
main.cpp@1:afd3fc7c27a2, 2015-03-06 (annotated)
- Committer:
- thiennguyen1310
- Date:
- Fri Mar 06 20:45:05 2015 +0000
- Revision:
- 1:afd3fc7c27a2
- Parent:
- 0:48805553d80e
- Child:
- 2:ed0782fc9b00
fixed print statement %x to %f;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joshwilkins2013 | 0:48805553d80e | 1 | #include "mbed.h" |
joshwilkins2013 | 0:48805553d80e | 2 | |
joshwilkins2013 | 0:48805553d80e | 3 | #define sensorConfig 0x00 |
joshwilkins2013 | 0:48805553d80e | 4 | #define sensorStatus 0x01 |
joshwilkins2013 | 0:48805553d80e | 5 | #define Delta_Y 0x02 |
joshwilkins2013 | 0:48805553d80e | 6 | #define Delta_X 0x03 |
joshwilkins2013 | 0:48805553d80e | 7 | |
joshwilkins2013 | 0:48805553d80e | 8 | Serial pc(USBTX, USBRX); |
joshwilkins2013 | 0:48805553d80e | 9 | |
thiennguyen1310 | 1:afd3fc7c27a2 | 10 | DigitalInOut pinC(PTD0); // clock |
thiennguyen1310 | 1:afd3fc7c27a2 | 11 | DigitalInOut pinD(PTC4); // data |
joshwilkins2013 | 0:48805553d80e | 12 | |
thiennguyen1310 | 1:afd3fc7c27a2 | 13 | signed long mx, my; float dx, dy; |
joshwilkins2013 | 0:48805553d80e | 14 | |
joshwilkins2013 | 0:48805553d80e | 15 | void reSync(){ |
joshwilkins2013 | 0:48805553d80e | 16 | pinC = 1; // ReSync (startup) mouse |
joshwilkins2013 | 0:48805553d80e | 17 | wait_us(5); |
joshwilkins2013 | 0:48805553d80e | 18 | pinC = 0; |
joshwilkins2013 | 0:48805553d80e | 19 | wait_us(1); |
joshwilkins2013 | 0:48805553d80e | 20 | pinC = 1; |
thiennguyen1310 | 1:afd3fc7c27a2 | 21 | wait_us(1000); // wait for OptiMouse serial transaction timer to time out: |
joshwilkins2013 | 0:48805553d80e | 22 | } |
joshwilkins2013 | 0:48805553d80e | 23 | |
joshwilkins2013 | 0:48805553d80e | 24 | uint8_t readRegister(uint8_t address){ // Bitbang SPI read operation |
joshwilkins2013 | 0:48805553d80e | 25 | int i = 7; |
joshwilkins2013 | 0:48805553d80e | 26 | uint8_t r = 0; |
joshwilkins2013 | 0:48805553d80e | 27 | pinD.output(); // Write the address of the register we want to read: |
joshwilkins2013 | 0:48805553d80e | 28 | for (; i>=0; i--){ |
joshwilkins2013 | 0:48805553d80e | 29 | pinC = 0; |
joshwilkins2013 | 0:48805553d80e | 30 | pinD = (address & (1 << i)); |
joshwilkins2013 | 0:48805553d80e | 31 | pinC = 1; |
joshwilkins2013 | 0:48805553d80e | 32 | } |
joshwilkins2013 | 0:48805553d80e | 33 | pinD.input(); // Switch data line from OUTPUT to INPUT |
joshwilkins2013 | 0:48805553d80e | 34 | wait_us(100); // Wait according to datasheet |
joshwilkins2013 | 0:48805553d80e | 35 | for (i=7; i>=0; i--){ // Fetch the data! |
joshwilkins2013 | 0:48805553d80e | 36 | pinC = 0; |
joshwilkins2013 | 0:48805553d80e | 37 | pinC = 1; |
joshwilkins2013 | 0:48805553d80e | 38 | r |= ((pinD.read()) << i); |
joshwilkins2013 | 0:48805553d80e | 39 | } |
joshwilkins2013 | 0:48805553d80e | 40 | wait_us(100); |
joshwilkins2013 | 0:48805553d80e | 41 | return r; |
joshwilkins2013 | 0:48805553d80e | 42 | } |
joshwilkins2013 | 0:48805553d80e | 43 | |
joshwilkins2013 | 0:48805553d80e | 44 | void writeRegister(uint8_t address, uint8_t data){ |
joshwilkins2013 | 0:48805553d80e | 45 | int i = 7; |
joshwilkins2013 | 0:48805553d80e | 46 | address |= 0x80;// Set MSB high, to indicate write operation: |
joshwilkins2013 | 0:48805553d80e | 47 | pinD.output(); // Write the address: |
joshwilkins2013 | 0:48805553d80e | 48 | for (; i>=0; i--){ |
joshwilkins2013 | 0:48805553d80e | 49 | pinC = 0; |
joshwilkins2013 | 0:48805553d80e | 50 | pinD = (address & (1 << i)); |
joshwilkins2013 | 0:48805553d80e | 51 | pinC = 1; |
joshwilkins2013 | 0:48805553d80e | 52 | } |
joshwilkins2013 | 0:48805553d80e | 53 | for (i=7; i>=0; i--){ // Write the data: |
joshwilkins2013 | 0:48805553d80e | 54 | pinC = 0; |
joshwilkins2013 | 0:48805553d80e | 55 | pinD = (data & (1 << i)); |
joshwilkins2013 | 0:48805553d80e | 56 | pinC = 1; |
joshwilkins2013 | 0:48805553d80e | 57 | } |
joshwilkins2013 | 0:48805553d80e | 58 | } |
joshwilkins2013 | 0:48805553d80e | 59 | |
joshwilkins2013 | 0:48805553d80e | 60 | void forceAwake(char value){ |
joshwilkins2013 | 0:48805553d80e | 61 | if (value>0) writeRegister(sensorConfig,0x01); |
joshwilkins2013 | 0:48805553d80e | 62 | else writeRegister(sensorConfig,0x00); |
joshwilkins2013 | 0:48805553d80e | 63 | } |
joshwilkins2013 | 0:48805553d80e | 64 | |
joshwilkins2013 | 0:48805553d80e | 65 | signed char getDx(void){ |
joshwilkins2013 | 0:48805553d80e | 66 | return (signed char) readRegister(Delta_X); |
joshwilkins2013 | 0:48805553d80e | 67 | } |
joshwilkins2013 | 0:48805553d80e | 68 | |
joshwilkins2013 | 0:48805553d80e | 69 | signed char getDy(void){ |
joshwilkins2013 | 0:48805553d80e | 70 | return (signed char) readRegister(Delta_Y); |
joshwilkins2013 | 0:48805553d80e | 71 | } |
joshwilkins2013 | 0:48805553d80e | 72 | |
joshwilkins2013 | 0:48805553d80e | 73 | signed char status(void){ |
joshwilkins2013 | 0:48805553d80e | 74 | return (signed char) readRegister(sensorStatus); |
joshwilkins2013 | 0:48805553d80e | 75 | } |
joshwilkins2013 | 0:48805553d80e | 76 | |
joshwilkins2013 | 0:48805553d80e | 77 | int main(){ |
joshwilkins2013 | 0:48805553d80e | 78 | pc.baud(9600); |
joshwilkins2013 | 0:48805553d80e | 79 | pinC.output(); |
joshwilkins2013 | 0:48805553d80e | 80 | pinD.input(); |
joshwilkins2013 | 0:48805553d80e | 81 | reSync(); |
joshwilkins2013 | 0:48805553d80e | 82 | forceAwake(1); // LED on |
joshwilkins2013 | 0:48805553d80e | 83 | wait_ms(100); |
joshwilkins2013 | 0:48805553d80e | 84 | while(1){ |
joshwilkins2013 | 0:48805553d80e | 85 | mx = getDx(); |
joshwilkins2013 | 0:48805553d80e | 86 | my = getDy(); |
thiennguyen1310 | 1:afd3fc7c27a2 | 87 | dx = mx; |
thiennguyen1310 | 1:afd3fc7c27a2 | 88 | dy = my; |
joshwilkins2013 | 0:48805553d80e | 89 | |
joshwilkins2013 | 0:48805553d80e | 90 | pc.printf("x = "); |
thiennguyen1310 | 1:afd3fc7c27a2 | 91 | pc.printf("%f\t", dx); |
thiennguyen1310 | 1:afd3fc7c27a2 | 92 | pc.printf("y = "); |
thiennguyen1310 | 1:afd3fc7c27a2 | 93 | pc.printf("%f\n", dy); |
joshwilkins2013 | 0:48805553d80e | 94 | } |
joshwilkins2013 | 0:48805553d80e | 95 | } |
joshwilkins2013 | 0:48805553d80e | 96 |