From a9050e6dc5c04efe2b2731ac5370dcf3554b839c Mon Sep 17 00:00:00 2001
From: Stephen Birarda <commit@birarda.com>
Date: Wed, 27 Feb 2013 14:45:14 -0800
Subject: [PATCH] read from magnetometer on SDA, SLC

---
 hardware/head_hand/head_hand.pde | 50 ++++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 6 deletions(-)

diff --git a/hardware/head_hand/head_hand.pde b/hardware/head_hand/head_hand.pde
index 844f352e56..920f1428ef 100644
--- a/hardware/head_hand/head_hand.pde
+++ b/hardware/head_hand/head_hand.pde
@@ -5,6 +5,8 @@
 //  
 //  PIN WIRING:  Connect input sensors to the channels in following manner
 //
+//  SDA:     Magnetomer SDA
+//  SDL:     Magnetomer SDL
 //  AIN 10:  Yaw Gyro (shaking your head 'no')
 //  AIN 16:  Pitch Gyro (nodding your head 'yes') 
 //  AIN 17:  Roll Gyro (looking quizzical, tilting your head)
@@ -12,11 +14,16 @@
 //  AIN 19:  Up/Down acceleration (sitting up/ducking in front of your monitor)
 //  AIN 20:  Forward/Back acceleration (Toward or away from your monitor)
 
+// include HardWire for I2C communication to Magnetomer
+#include <HardWire.h>
+
 #define NUM_CHANNELS 6
 #define MSECS_PER_SAMPLE 10
 
 #define LED_PIN 12
 
+const int COMPASS_ADDRESS = 0x42 >> 1;
+
 int inputPins[NUM_CHANNELS] = {10,16,17,18,19,20};
 
 int LED = 0;
@@ -26,8 +33,12 @@ unsigned int time;
 int measured[NUM_CHANNELS];
 float accumulate[NUM_CHANNELS];
 
+bool readFromMag = false;
+
 int sampleCount = 0;
 
+HardWire Wire(1, I2C_FAST_MODE);
+
 void setup()
 {
   int i;
@@ -36,6 +47,7 @@ void setup()
     measured[i] = analogRead(inputPins[i]);
     accumulate[i] = measured[i];
   }
+  Wire.begin();
   pinMode(BOARD_LED_PIN, OUTPUT);
   pinMode(LED_PIN,OUTPUT);
   time = millis();
@@ -44,11 +56,22 @@ void setup()
 void loop()
 {
   int i;
+  int compassReading;
+  bool magTransmit = false;
   sampleCount++;
   
   for (i = 0; i < NUM_CHANNELS; i++) {
     accumulate[i] += analogRead(inputPins[i]);
   }
+  
+  if (readFromMag && magTransmit == false) {
+     // get current absolute magnetometer value
+     Wire.beginTransmission(COMPASS_ADDRESS);
+     Wire.send('A');
+     Wire.endTransmission();
+     magTransmit = true;
+  }
+  
   if ((millis() - time) >= MSECS_PER_SAMPLE) {
       samplesSent++;
       time = millis();
@@ -58,7 +81,7 @@ void loop()
         SerialUSB.print(" ");
         accumulate[i] = 0;
       }
-      
+            
       if ((samplesSent % 100 == 0) && (samplesSent % 150 == 0))  {
         LED = !LED;
         digitalWrite(LED_PIN, LED);
@@ -72,10 +95,25 @@ void loop()
       else 
         SerialUSB.print("0");
         
-      SerialUSB.println("");
+      SerialUSB.print(" ");
       sampleCount = 0;
+      
+      if (readFromMag) {
+        // send the absolute value from the magnetomer
+        Wire.requestFrom(COMPASS_ADDRESS, 2);
+      
+        if (2 <= Wire.available()) {
+  		    compassReading = Wire.receive();
+  		    compassReading = compassReading << 8;
+  		    compassReading += Wire.receive();
+  	      compassReading /= 10;
+  	      SerialUSB.print(compassReading);   
+        }
+      }
+      
+      SerialUSB.println("");
+      
+      // reset the magnetomerTransmit to false to prep it in next loop
+      magTransmit = false;
   }  
-}
-
-
-  
+}
\ No newline at end of file