Detect a HMC5883 magnetometer by reading the id registers for NUCLEO-L432KC

Files at this revision

API Documentation at this revision

Comitter:
skyscraper
Date:
Thu Aug 30 20:07:17 2018 +0000
Parent:
0:10e9548cdb3d
Commit message:
change from string to detect hmc5883 message

Changed in this revision

compass_3d.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 10e9548cdb3d -r 8cd11758eff3 compass_3d.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compass_3d.h	Thu Aug 30 20:07:17 2018 +0000
@@ -0,0 +1,6 @@
+/*
+3d compass
+need a vector3d
+*/
+
+struct 
\ No newline at end of file
diff -r 10e9548cdb3d -r 8cd11758eff3 main.cpp
--- a/main.cpp	Thu Aug 30 13:36:05 2018 +0000
+++ b/main.cpp	Thu Aug 30 20:07:17 2018 +0000
@@ -1,5 +1,6 @@
 
 #include "mbed.h"
+#include <cstring>
 #include <I2C.h>
 #include <Serial.h>
 
@@ -21,12 +22,20 @@
 {
     //wait for mag to init
     Thread::wait(500);
+    
     // move pointer to loc
-    i2c.write(i2c_addr,&id_regA,1);
-    char id_input[4];
-    i2c.read(i2c_addr,id_input,3);
-    id_input[3] = '\0';
-    pc.printf("config got %s\n",id_input);
+    if ( i2c.write(i2c_addr,&id_regA,1) == 0){
+        char id_input[4];
+        i2c.read(i2c_addr,id_input,3);
+        id_input[3] = '\0';
+        if (strcmp(id_input,"H43") == 0){
+          pc.printf("Detected a HMC5883",id_input);
+        }else{
+         pc.printf("config Not got a HMC5883",id_input);
+        }
+    }else{
+        pc.printf("HMC5883 write failed\n");
+    }
     //check we are still running
     for (;;){
        led1 = !led1;