PLANET-Q INA226 Library

Dependents:   IZU2020_AVIONICS IZU2020_AVIONICS

Files at this revision

API Documentation at this revision

Comitter:
tanahashi
Date:
Tue Dec 17 14:06:39 2019 +0000
Parent:
4:9e7136efda5c
Commit message:
fix comment

Changed in this revision

PQINA226.cpp Show annotated file Show diff for this revision Revisions of this file
PQINA226.h Show annotated file Show diff for this revision Revisions of this file
diff -r 9e7136efda5c -r 7f4ebd11d751 PQINA226.cpp
--- a/PQINA226.cpp	Tue Dec 17 10:56:04 2019 +0000
+++ b/PQINA226.cpp	Tue Dec 17 14:06:39 2019 +0000
@@ -8,8 +8,9 @@
     _i2c->frequency(400000);
 }
 
-void INA226::begin(){
-    cmd[0] = CALIBRATION;
+void INA226::begin()
+{
+    cmd[0] = INA226_CALIBRATION;
     cmd[1] = 0x0A;
     cmd[2] = 0x00;
     _i2c->write(_addr, cmd, 3);
@@ -17,7 +18,7 @@
 
 bool INA226::test()
 {
-    cmd[0] = WHO_AM_I;
+    cmd[0] = INA226_WHO_AM_I;
     _i2c->write(_addr, cmd, 1);
     _i2c->read(_addr, buff, 1);
     if(buff[0] == 0x22) {
@@ -34,15 +35,17 @@
     read_power(power);
 }
 
-void INA226::read_voltage(float *voltage){
-    cmd[0] = BUS_VOLTAGE;
+void INA226::read_voltage(float *voltage)
+{
+    cmd[0] = INA226_BUS_VOLTAGE;
     _i2c->write(_addr, cmd, 1);
     _i2c->read(_addr, buff, 2);
-    *voltage = (short)(buff[0] << 8 | buff[1]) * VOLTAGE_LSB;
+    *voltage = (short)(buff[0] << 8 | buff[1]) * INA226_VOLTAGE_LSB;
 }
 
-void INA226::read_current(float *current){
-    cmd[0] = CURRENT;
+void INA226::read_current(float *current)
+{
+    cmd[0] = INA226_CURRENT;
     _i2c->write(_addr, cmd, 1);
     _i2c->read(_addr, buff, 2);
     *current = (short)(buff[0] << 8 | buff[1]);
@@ -50,8 +53,8 @@
 
 void INA226::read_power(float *power)
 {
-    cmd[0] = POWER;
+    cmd[0] = INA226_POWER;
     _i2c->write(_addr, cmd, 1);
     _i2c->read(_addr, buff, 2);
-    *power = (short)(buff[0] << 8 | buff[1]) * POWER_LSB;
+    *power = (short)(buff[0] << 8 | buff[1]) * INA226_POWER_LSB;
 }
diff -r 9e7136efda5c -r 7f4ebd11d751 PQINA226.h
--- a/PQINA226.h	Tue Dec 17 10:56:04 2019 +0000
+++ b/PQINA226.h	Tue Dec 17 14:06:39 2019 +0000
@@ -1,13 +1,13 @@
 #ifndef PQINA226_H
 #define PQINA226_H
 
-#define BUS_VOLTAGE 0x02
-#define POWER 0x03
-#define CURRENT 0x04
-#define CALIBRATION 0x05
-#define WHO_AM_I 0xFF
-#define VOLTAGE_LSB 1.25
-#define POWER_LSB 25
+#define INA226_BUS_VOLTAGE 0x02
+#define INA226_POWER 0x03
+#define INA226_CURRENT 0x04
+#define INA226_CALIBRATION 0x05
+#define INA226_WHO_AM_I 0xFF
+#define INA226_VOLTAGE_LSB 1.25
+#define INA226_POWER_LSB 25
 
 /** 電圧電流センサINA226のライブラリ
  * @code
@@ -26,8 +26,13 @@
 int main() {
     ina.begin();
     while(true){
-     ina.read(&voltage, &current, &power);
-     pc.printf("%f\t%f\t%f\r\n", voltage, current, power);
+        if(ina.test()){
+            ina.read(&voltage, &current, &power);
+            pc.printf("%f\t%f\t%f\r\n", voltage, current, power);
+        }
+        else {
+            pc.printf("[  FAIL  ] INA226 cannot be reached.\r\n");
+        }
     }
 }
  * @endcode
@@ -48,7 +53,7 @@
         A1_SDA = 0b10,
         A1_SCL = 0b11
     } A1_t;
-    
+
 private:
     I2C *_i2c;
     int _addr;