3Axis gyro sensor sample. target sensor: L3GD20(ST)

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
satoshi1204
Date:
Tue Dec 18 09:08:07 2018 +0000
Parent:
0:a1d1374eea97
Commit message:
3Axis gyro sensor sample

Changed in this revision

L3GD20.cpp Show annotated file Show diff for this revision Revisions of this file
L3GD20.h Show annotated file Show diff for this revision Revisions of this file
diff -r a1d1374eea97 -r 191e963655c2 L3GD20.cpp
--- a/L3GD20.cpp	Mon Dec 17 12:47:04 2018 +0000
+++ b/L3GD20.cpp	Tue Dec 18 09:08:07 2018 +0000
@@ -2,13 +2,18 @@
 
 L3GD20::L3GD20(PinName sda, PinName scl) : i2c(sda, scl), address(0xD6)
 {
-    writeByte(0x20, 0x0F);
+    turnOn();
 }
 
 L3GD20::~L3GD20()
 {
 }
 
+void L3GD20::turnOn(void)
+{
+    writeByte(0x20, 0x0F); // turn on a sensor
+}
+
 void L3GD20::readData(double &x, double &y, double &z)
 {
     static const double rate = 0.00875;
@@ -26,16 +31,18 @@
 
 char L3GD20::readByte(char reg)
 {
-    i2c.write(address, &reg, 1);
+    static const int data_len = 1;
+    i2c.write(address, &reg, data_len);
     char data;
-    i2c.read(address, &data, 1);
+    i2c.read(address, &data, data_len);
     return data;
 }
 
 void L3GD20::writeByte(char reg, char data)
 {
-    char buf[2];
+    static const int data_len = 2;
+    char buf[data_len];
     buf[0] = reg;
     buf[1] = data;
-    i2c.write(address, buf, 2);
+    i2c.write(address, buf, data_len);
 }
diff -r a1d1374eea97 -r 191e963655c2 L3GD20.h
--- a/L3GD20.h	Mon Dec 17 12:47:04 2018 +0000
+++ b/L3GD20.h	Tue Dec 18 09:08:07 2018 +0000
@@ -9,6 +9,7 @@
 private:
     I2C i2c;
     unsigned char address;
+    void turnOn(void);
     char readByte(char reg);
     void writeByte(char reg, char data);
 };