yarp

Dependencies:   NeoStrip_mod VCNL40x0_1 mbed

Fork of XYZ_PROX_redux_01 by Tom Fleet

Files at this revision

API Documentation at this revision

Comitter:
ghostaudio
Date:
Fri May 30 16:36:49 2014 +0000
Parent:
3:91e3eb00df03
Commit message:
getting there but issue with sensors

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri May 30 12:52:11 2014 +0000
+++ b/main.cpp	Fri May 30 16:36:49 2014 +0000
@@ -3,8 +3,6 @@
 #include "NeoStrip.h"
 #define  NumPixels 4
 
-#define PIN_OUTPUT 6
-
 I2C i2c(p28, p27);
 DigitalOut mled0(LED1); 
 DigitalOut mled1(LED2); 
@@ -13,6 +11,7 @@
 Serial pc(USBTX, USBRX);
 DigitalOut reset(p29);
 NeoStrip strip(p26, NumPixels);
+DigitalIn interrupt (p25);
 
 const uint8_t MUX = 0xE0; // 11100000
 const uint8_t VCNL4020 = 0x26;
@@ -20,9 +19,14 @@
 char _send[3];
 char _receive[2];
 char port[2];
-const uint8_t chan[] = {0x01,0x02,0x04,0x08};
+const uint8_t chan0 = 0x01;
+const uint8_t chan1 = 0x02;
+const uint8_t chan2 = 0x04;
+const uint8_t chan3 = 0x08;
+const uint8_t chan[] = {chan0,chan1,chan2,chan3};
 int mleds[] = {0,0,0,0};
 
+void ReadOne(uint8_t channel);
 void Setup(void);
 void Setmleds(void);
 void SwitchChannel(uint8_t chan);
@@ -32,46 +36,70 @@
 void SetCommandRegister (unsigned char Command);
 void ReadCommandRegister (unsigned char *Command);
 void SetCurrent (unsigned char Current);
+void ReadCurrent (unsigned char *Current);
 void ReadProxiValue (unsigned int *ProxiValue);
 void SetProximityRate (unsigned char ProximityRate);
 void ReadProxiOnDemand (unsigned int *ProxiValue);
-void Setup(void);
-
+void SetupAll(void);
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 int main() {
     
-    Setup();
+    //SwitchChannel(chan1);            // set channel
+    //wait_ms(50);
+    //Setup();
+    SetupAll();
     
     while (1) {
         
         ReadAllSensors();
-        
+        //ReadOne(chan1);
     }
 }
 
-
+void ReadOne(uint8_t channel){
+    
+    unsigned int sensor = 0; 
+    SwitchChannel(channel);            // set channel
+        mleds[0] = 1;                      // LED on
+        Setmleds();
+        ReadProxiOnDemand(&sensor);    // read prox value on demand
+        mleds[0] = 0;                      // LED off
+        Setmleds();   
+        pc.printf("\r %i \n", sensor);
+}
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
+void SetupAll(void){
+    
+    SwitchChannel(chan0);            // set channel
+    wait_ms(20);
+    Setup();
+    SwitchChannel(chan1);            // set channel
+    wait_ms(20);
+    Setup();
+    SwitchChannel(chan2);            // set channel
+    wait_ms(20);
+    Setup();
+    SwitchChannel(chan3);            // set channel
+    wait_ms(20);
+    Setup();
+    pc.printf("\r setup ok \n ");
+    }
+    
+    
 void Setup(void) {
+    unsigned char Current=0;
     
     pc.baud(115200);    
-                                    // set BAUD
-    for (int i = 0; i < 4; i++){
-            
-            reset = 0;
-            wait(0.001);
-            reset = 1;
-            wait(0.001);
-            port[0] = chan[i];
-            i2c.write(MUX,port, 1);
-            SetCurrent(20);                              // Set current to 200mA
-            SetCommandRegister (COMMAND_ALL_DISABLE);    // ready for prox rate change
-            SetProximityRate (PROX_MEASUREMENT_RATE_31); // 31 measurements/s
-            wait(0.2);
-            
-        }
+    SetCurrent(20);                              // Set current to 200mA
+    ReadCurrent(&Current);
+    pc.printf("\n IR LED Current: %d\n", Current);
+    SetCommandRegister (COMMAND_ALL_DISABLE);    // ready for prox rate change
+    SetProximityRate (PROX_MEASUREMENT_RATE_31); // 31 measurements/s
+    SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_SELFTIMED_MODE_ENABLE);
+    pc.printf("\r done\n");
+    wait(0.2);
 }
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -87,9 +115,9 @@
 
 void SwitchChannel(uint8_t chan){
         reset = 0;
-        wait(0.001);
+        wait_ms(10);
         reset = 1;
-        wait(0.001);    
+        wait_ms(10);    
         port[0] = chan;
         i2c.write(MUX,port, 1);
     }
@@ -99,20 +127,23 @@
 void ReadAllSensors(void){
     
     for (int i = 0; i < 4; i++){
-            
+        
+        unsigned int sensor = 0; 
+        
         SwitchChannel(chan[i]);            // set channel
         mleds[i] = 1;                      // LED on
         Setmleds();
-        ReadProxiOnDemand(&sensors[i]);    // read prox value on demand
+        ReadProxiOnDemand(&sensor);    // read prox value on demand
+        wait_ms(10); 
         mleds[i] = 0;                      // LED off
-        Setmleds();    
+        Setmleds();   
+        sensors[i] = sensor;
+        pc.printf("\r %i ", sensors[i]);
         }
         
-        for (int i = 0; i < 4; i++){
-        pc.printf("\r %i ", sensors[i]);
-        }
-    pc.printf("\r \n ");};
-//}
+    pc.printf("\r \n ");
+    wait(0.001);
+}
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -151,6 +182,17 @@
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
+void ReadCurrent (unsigned char *Current) {
+
+    _send[0] = REGISTER_PROX_CURRENT;                       // VCNL40x0 IR LED current register
+    i2c.write(VCNL4020,_send, 1);                  // Write 1 byte on I2C
+    i2c.read(VCNL4020+1,_receive, 1);              // Read 1 byte on I2C
+
+    *Current = (unsigned char)(_receive[0]);
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
 void ReadProxiValue (unsigned int *ProxiValue) {
 
     _send[0] = REGISTER_PROX_VALUE;                // VCNL40x0 Proximity Value register