OB1203 basic mbed driver

Dependents:   OB1203_IDT

Files at this revision

API Documentation at this revision

Comitter:
laserdad
Date:
Fri Oct 12 20:19:38 2018 +0000
Parent:
18:85dd16f99c3a
Child:
20:8d69cd11b8fa
Child:
25:ca9caacd0f9f
Commit message:
working--added AGC functions

Changed in this revision

OB1203.cpp Show annotated file Show diff for this revision Revisions of this file
OB1203.h Show annotated file Show diff for this revision Revisions of this file
--- a/OB1203.cpp	Fri Oct 05 16:55:14 2018 +0000
+++ b/OB1203.cpp	Fri Oct 12 20:19:38 2018 +0000
@@ -1,11 +1,10 @@
 #include "OB1203.h"
 #include "mbed.h"
-//#include "SoftI2C.h"
 
 extern Serial pc;
 
-OB1203::OB1203(I2C *i2c_obj)
-//OB1203::OB1203(SoftI2C *i2c_obj)
+//  //
+OB1203::OB1203(I2C *i2c_obj) 
 {
     i2c = i2c_obj;
 }
@@ -462,4 +461,44 @@
 {
     readBlock(OB1203_ADDR,REG_PART_ID,data,1);
     return data[0];
+}
+
+
+void OB1203::do_agc(uint32_t data, bool ch)
+{
+    const uint32_t tol1 = TOL1;
+    const uint32_t tol2 = TOL2;
+    const uint16_t in_range_persist = IN_RANGE_PERSIST;
+    static uint16_t in_range[2] = {0,0};
+    const uint16_t maxCurrent[2] = {IR_MAX_CURRENT , R_MAX_CURRENT};
+    const uint16_t step = STEP;
+    const uint32_t targetCounts[2] = {IR_TARGET_COUNTS, R_TARGET_COUNTS};
+     //ch = 0 for IR, 1 for R (channel)
+     if( data > targetCounts[ch] + (in_range[ch]>in_range_persist ? tol2: tol1) )
+    {       
+        if(data>targetCounts[ch] + tol2)
+            in_range[ch]=0;
+        
+       
+        if( (ch ? r_current : ir_current)>step)
+        {
+            (ch ? r_current : ir_current) -= step;
+            update = 1;
+        }
+    }
+    else if( data < targetCounts[ch] - (in_range[ch]>in_range_persist ? tol2 : tol1) )
+    {
+        if(data<targetCounts[ch] - tol2)
+            in_range[ch]=0;
+        if( (ch ? r_current : ir_current) +step<maxCurrent[ch]) //no need to go to full current
+        {
+            (ch ? r_current : ir_current) += step;
+            update = 1;
+        }
+    }
+    else
+    {
+        if( (data > (targetCounts[ch]-tol1) ) && (data < (targetCounts[ch]+tol1)) )
+            in_range[ch]++;
+    }
 }
\ No newline at end of file
--- a/OB1203.h	Fri Oct 05 16:55:14 2018 +0000
+++ b/OB1203.h	Fri Oct 12 20:19:38 2018 +0000
@@ -179,11 +179,19 @@
 #define POR_TIME_MS 5 //a guess
 
 
+#define IR_TARGET_COUNTS 196608
+#define R_TARGET_COUNTS 196608
+#define TOL1 6000
+#define TOL2 40000
+#define STEP 8
+#define IN_RANGE_PERSIST 4
+#define IR_MAX_CURRENT 0x02AF
+#define R_MAX_CURRENT 0x01FF
+
 
 class OB1203
 {
     I2C *i2c;
-//    SoftI2C *i2c;
 
 public:
     char osc_trim;
@@ -243,7 +251,6 @@
     char sig_out;
     
     OB1203 (I2C *);
-//        OB1203(SoftI2C *);
 
     // Low-level operations
     void reset();
@@ -289,12 +296,21 @@
     void parseFifoSamples(char, char *, uint32_t *);
     char get_part_ID(char *);
 
+
+    //agc functions
+    void do_agc(uint32_t,bool);
+    
     //variables
     uint16_t rate;
     char res;
     char gain;
     uint32_t data_max;
     uint32_t reg_max;
+    
+
+//    const uint32_t targetCounts[2];
+
+    bool update;
 
 };