#1

Dependencies:   mbed SB1602E

Fork of mbed-cloud-connect-sensor-laser-distance by Andrea Corrado

Files at this revision

API Documentation at this revision

Comitter:
KStefan
Date:
Mon Dec 10 07:22:31 2018 +0000
Parent:
8:0f74264cc38a
Commit message:
OK

Changed in this revision

DebounceIn.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
vl53l0x_api.lib Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebounceIn.h	Mon Dec 10 07:22:31 2018 +0000
@@ -0,0 +1,57 @@
+#include "mbed.h"
+
+class DebounceIn : public DigitalIn
+{
+    public:
+    Ticker ticker;
+    int    shadow;
+    int    counter;
+    int    samples;
+    
+public:
+
+    /* Setzt die Entprellzeit (in µs), default sind 1000µs (1ms) */
+    void set_debounce_us(int i) {
+     ticker.attach_us(callback(this,&DebounceIn::cb), i);
+    //ticker.attach_us(this, &DebounceIn::cb, i);
+    //ticker.attach_us(&DebounceIn::cb, i);
+    }
+
+    /* Definiert die Anzahl der Samples um den 'Schatten'-Zustand des DigitalIn-Pins zu schalten */
+    void set_samples(int i) {
+        samples = i;
+    }
+
+    /* Liest den Wert des entprellten Pins */
+    int Read(void) {
+        return shadow;
+    }
+
+//#ifdef MBED_OPERATORS
+       operator int() { return Read(); }
+//#endif
+
+    /* Konstruktor */
+    DebounceIn(PinName pin) : DigitalIn(pin) {
+        counter = 0;
+        samples = 10;
+        // shadow = 0;
+        set_debounce_us(1000);
+    }
+
+    
+
+protected:
+
+    void cb() {
+        if (!DigitalIn::read()) {
+            if (counter < samples) counter++;
+            if (counter == samples) shadow = 1;
+        } else {
+            if (counter > 0) counter--;
+            if (counter == 0) shadow = 0;
+        }
+    }
+
+
+};
\ No newline at end of file
--- a/main.cpp	Tue May 01 19:16:01 2018 +0000
+++ b/main.cpp	Mon Dec 10 07:22:31 2018 +0000
@@ -1,128 +1,35 @@
 #include "mbed.h"
 #include "SB1602E.h"
-#include "vl53l0x_api.h"
-#include "vl53l0x_platform.h"
-#include "vl53l0x_i2c_platform.h"
-
 #define USE_I2C_2V8
-
-DigitalOut myled(P0_7); // 7=rot, 8=gruen
-
-I2C i2c_disp(P0_5, P0_4); //  SDA, SCL auf NXP U24
-SB1602E lcd( i2c_disp );
+#include "DebounceIn.h"
 
-VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) {
-    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
-    uint8_t NewDatReady=0;
-    uint32_t LoopNb;
-    
-    if (Status == VL53L0X_ERROR_NONE) {
-        LoopNb = 0;
-        do {
-            Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady);
-            if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) {
-                break;
-            }
-            LoopNb = LoopNb + 1;
-            VL53L0X_PollingDelay(Dev);
-        } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
+DigitalOut myled(P0_8); // 7=rot, 8=grün, 9=blau
+DigitalIn sw_o(P0_12);
+InterruptIn sw_b(P0_13);
+InterruptIn sw_g(P0_14);
+Ticker ticker1;
+
 
-        if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
-            Status = VL53L0X_ERROR_TIME_OUT;
-        }
-    }
+I2C i2c_disp(P0_5, P0_4); //  SDA, SCL auf LPC11U24
+SB1602E lcd(i2c_disp);
 
-    return Status;
-}
-
-VL53L0X_Error WaitStopCompleted(VL53L0X_DEV Dev) {
-    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
-    uint32_t StopCompleted=0;
-    uint32_t LoopNb;
+int count_o, count_b, count_g = 0;
 
-    if (Status == VL53L0X_ERROR_NONE) {
-        LoopNb = 0;
-        do {
-            Status = VL53L0X_GetStopCompletedStatus(Dev, &StopCompleted);
-            if ((StopCompleted == 0x00) || Status != VL53L0X_ERROR_NONE) {
-                break;
-            }
-            LoopNb = LoopNb + 1;
-            VL53L0X_PollingDelay(Dev);
-        } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
-
-        if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
-            Status = VL53L0X_ERROR_TIME_OUT;
-        }
-
+void blue(){
+    count_b++;
     }
-
-    return Status;
-}
+void green() {
+    count_g++;
+    }
 
 int main()
-{
-    
-    //Setup laser
-    int var=1, measure=0;
-    int ave=0, sum=0;
-    VL53L0X_Dev_t MyDevice;
-    VL53L0X_Dev_t *pMyDevice = &MyDevice;
-    VL53L0X_RangingMeasurementData_t    RangingMeasurementData;
-    VL53L0X_RangingMeasurementData_t   *pRangingMeasurementData    = &RangingMeasurementData;
-    
-    // Initialize Comms laster
-    pMyDevice->I2cDevAddr      = 0x52;
-    pMyDevice->comms_type      =  1;
-    pMyDevice->comms_speed_khz =  400;
-    
-    
-    VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0);
-    VL53L0X_DataInit(&MyDevice); 
-    uint32_t refSpadCount;
-    uint8_t isApertureSpads;
-    uint8_t VhvSettings;
-    uint8_t PhaseCal;
-    
-    VL53L0X_StaticInit(pMyDevice); 
-    VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization
-    VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization
-    VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode
-    //VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.1*65536));  //Long Range modus
-    //VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(60*65536)); // Long Range
-    //VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 33000);  // Long Range
-    //VL53L0X_SetVcselPulsePeriod(pMyDevice, VL53L0X_VCSEL_PERIOD_PRE_RANGE, 18);  // Long Range
-    //VL53L0X_SetVcselPulsePeriod(pMyDevice, VL53L0X_VCSEL_PERIOD_FINAL_RANGE, 14);  // Long Range
+{  
+    lcd.printf(0, "Red Blue Green\r");    //  Parameter von printf auf LCD: Zeilennummer (0 or 1), string
+    while(1) {
 
-    VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.25*65536)); //High Accuracy mode, see API PDF
-    VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(18*65536)); //High Accuracy mode, see API PDF
-    VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 200000); //High Accuracy mode, see API PDF
-    VL53L0X_StartMeasurement(pMyDevice);
-    
-    while(1) {
-        //lcd.cls();
-        //lcd.locate(0,3);
-        //lcd.printf("[DISTANCE]");
-        
-        lcd.printf( 0, "Laser-Messung:\r" );    //  line# (0 or 1), string
+        lcd.printf(1,1, "%d   %d   %d", count_o, count_b, count_g);
 
         myled = !myled;
-            while(var<=5){
-                WaitMeasurementDataReady(pMyDevice);
-                VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData);
-                measure=pRangingMeasurementData->RangeMilliMeter;
-                sum=sum+measure;
-                VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
-                VL53L0X_PollingDelay(pMyDevice);
-                var++;
-                }
-        ave=sum/var;
-        var=1;
-        sum=0; 
-        //lcd.locate(0,15);
-        //lcd.printf("%dmm", ave); // Print to LCD values
-        lcd.printf( 1,1, "         ");
-        lcd.printf( 1,1, "%dmm", ave);    //  line# (0 or 1), string
-        wait(0.2);
+
     }
 }
\ No newline at end of file
--- a/vl53l0x_api.lib	Tue May 01 19:16:01 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/users/KStefan/code/vl53l0x_api/#e6a9bf80cbc2