Just mouse test for x,y position

Dependencies:   mbed Mouse

main.cpp

Committer:
joshwilkins2013
Date:
2015-03-03
Revision:
0:48805553d80e
Child:
1:afd3fc7c27a2

File content as of revision 0:48805553d80e:

#include "mbed.h"

#define sensorConfig 0x00
#define sensorStatus 0x01
#define Delta_Y 0x02
#define Delta_X 0x03

Serial pc(USBTX, USBRX);

DigitalInOut pinC(PTD0);
DigitalInOut pinD(PTC4);

signed long mx, my;

void reSync(){
  pinC = 1; // ReSync (startup) mouse
  wait_us(5);
  pinC = 0;
  wait_us(1);
  pinC = 1; 
  wait_ms(1000);  // wait for OptiMouse serial transaction timer to time out:
}

uint8_t readRegister(uint8_t address){ // Bitbang SPI read operation
  int i = 7;
  uint8_t r = 0;
  pinD.output();   // Write the address of the register we want to read:
  for (; i>=0; i--){
    pinC = 0;
    pinD = (address & (1 << i));
    pinC = 1;
  }
  pinD.input();    // Switch data line from OUTPUT to INPUT
  wait_us(100);   // Wait according to datasheet
  for (i=7; i>=0; i--){     // Fetch the data!                          
    pinC = 0;
    pinC = 1;
    r |= ((pinD.read()) << i);
  }
  wait_us(100);
  return r;
}

void writeRegister(uint8_t address, uint8_t data){
  int i = 7;  
  address |= 0x80;// Set MSB high, to indicate write operation:
  pinD.output();  // Write the address:
  for (; i>=0; i--){
    pinC = 0;
    pinD = (address & (1 << i));
    pinC = 1;
  } 
  for (i=7; i>=0; i--){ // Write the data:
    pinC = 0;
    pinD = (data & (1 << i));
    pinC = 1;
  }
}

void forceAwake(char value){
  if (value>0) writeRegister(sensorConfig,0x01); 
  else writeRegister(sensorConfig,0x00);
}

signed char getDx(void){
  return (signed char) readRegister(Delta_X);
}

signed char getDy(void){
  return (signed char) readRegister(Delta_Y);
}

signed char status(void){
  return (signed char) readRegister(sensorStatus);
}

int main(){
    pc.baud(9600);
    pinC.output();
    pinD.input();
    reSync();
    forceAwake(1); // LED on
    wait_ms(100);
    while(1){
        mx = getDx();
        my = getDy();
                
        pc.printf("x = ");
        pc.printf("%x\t", mx);
        pc.printf("x = ");
        pc.printf("%x\n", my);
    }
}