Index: rtos_arduino/trunk/arduino_lib/libraries/LuckyShield/src/lib/MAG3110.cpp
===================================================================
--- rtos_arduino/trunk/arduino_lib/libraries/LuckyShield/src/lib/MAG3110.cpp	(revision 175)
+++ rtos_arduino/trunk/arduino_lib/libraries/LuckyShield/src/lib/MAG3110.cpp	(revision 224)
@@ -16,40 +16,38 @@
 
 //Constructor
-MAG3110::MAG3110()
-{
-  
-}
-
-
+// MAG3110::MAG3110()
+// {
+// }
 
 // Configure magnetometer
 void MAG3110::begin(void) {
-  //CTRL_REG1
-  //DR2|DR1|DR0|OS1|OS0|FastRead|Trigger|ActiveMode|
-  // 0 | 1 | 1 | 1 | 1 |    0   |   0   |    1     |=dec121
+
   Wire.beginTransmission(MAG_ADDR);// transmit to device 0x0E
-  Wire.write(0x10);                // cntrl register1
-  Wire.write(0x79); // Active Mode, 1.25 Hz datarate, 8x oversampling
-  Wire.endTransmission();          // stop transmitting
+  Wire.write(0x11);              // cntrl register2
+  Wire.write(0x80);              // send 0x80, enable auto resets
+  Wire.endTransmission();       // stop transmitting
   
   delay(15);
   
-  //CTRL_REG2: 
-  //AutoMagRst|---|Raw|Mag_Rst|---|---|---|---|
-  //    1     | 0 | 0 |   0   | 0 | 0 | 0 | 0 |
   Wire.beginTransmission(MAG_ADDR);// transmit to device 0x0E
-  Wire.write(0x11);                // cntrl register2
-  Wire.write(0x80);        // Auto resets before each measurement
-  Wire.endTransmission();          // stop transmitting
+  Wire.write(0x10);              // cntrl register1
+  Wire.write(1);                 // send 0x01, active mode
+  Wire.endTransmission();       // stop transmitting
 }
 
+void MAG3110::read()
+{
+  mag_x =read16Data(0x01,0x02);
+  mag_y =read16Data(0x03,0x04);
+  mag_z =read16Data(0x05,0x06);
+}
+// read X value
 
-// read X value
-int MAG3110::readx(void)
+int MAG3110::read16Data(byte MSB, byte LSB)
 {
   int xl, xh;  //define the MSB and LSB
   
   Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
-  Wire.write(0x01);              // x MSB reg
+  Wire.write(MSB);              // x MSB reg
   Wire.endTransmission();       // stop transmitting
  
@@ -65,5 +63,5 @@
   
   Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
-  Wire.write(0x02);              // x LSB reg
+  Wire.write(LSB);              // x LSB reg
   Wire.endTransmission();       // stop transmitting
  
@@ -76,77 +74,12 @@
   }
   
-  int xout = (xl|(xh << 8)); //concatenate the MSB and LSB
-  return xout;
+  int out = (xl|(xh << 8)); //concatenate the MSB and LSB 
+  if (out & 0b1000000000000000){
+     //float yout1 = ((~yout & 0b0111111111111111)+ 1)*(-1) ; 
+    return float ((~out & 0b0111111111111111)+ 1)*(-1) ;
+  }
+  return float (out);
 }
 
-//read Y value
-int MAG3110::ready(void)
-{
-  int yl, yh;  //define the MSB and LSB
-  
-  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
-  Wire.write(0x03);              // y MSB reg
-  Wire.endTransmission();       // stop transmitting
- 
-  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
-  
-  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
-  while(Wire.available())    // slave may send less than requested
-  { 
-    yh = Wire.read(); // receive the byte
-  }
-  
-  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
-  
-  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
-  Wire.write(0x04);              // y LSB reg
-  Wire.endTransmission();       // stop transmitting
- 
-  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
-  
-  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
-  while(Wire.available())    // slave may send less than requested
-  { 
-    yl = Wire.read(); // receive the byte
-  }
-  
-  int yout = (yl|(yh << 8)); //concatenate the MSB and LSB
-  return yout;
-}
-
-// read Z value
-int MAG3110::readz(void)
-{
-  int zl, zh;  //define the MSB and LSB
-  
-  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
-  Wire.write(0x05);              // z MSB reg
-  Wire.endTransmission();       // stop transmitting
- 
-  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
-  
-  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
-  while(Wire.available())    // slave may send less than requested
-  { 
-    zh = Wire.read(); // receive the byte
-  }
-  
-  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
-  
-  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
-  Wire.write(0x06);              // z LSB reg
-  Wire.endTransmission();       // stop transmitting
- 
-  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
-  
-  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
-  while(Wire.available())    // slave may send less than requested
-  { 
-    zl = Wire.read(); // receive the byte
-  }
-  
-  int zout = (zl|(zh << 8)); //concatenate the MSB and LSB
-  return zout;
-}
 
 MAG3110 mag3110;
