Ross O'Halloran / Mbed OS Babyseat_NewFirmware_copy_sean

Fork of AdiSense1000 by PoC_Team

Branch:
gd_dbg2142
Revision:
32:119f86d3d9ed
Parent:
31:de49744b57a6
--- a/main.cpp	Fri Jun 22 09:29:24 2018 +0100
+++ b/main.cpp	Wed Jul 18 19:01:56 2018 +0000
@@ -86,37 +86,95 @@
 extern ADI_SENSE_CONFIG multichannel_continuous_config;
 extern ADI_SENSE_CONFIG multichannel_multicycle_config;
 extern ADI_SENSE_CONFIG multichannel_singlecycle_config;
+extern ADI_SENSE_CONFIG multichannel_singlecycle_config_dam;
 
 /* Change the following pointer to select any of the configurations above */
-static ADI_SENSE_CONFIG *pSelectedConfig = &sensor0_rtd_2w_pt100_config;
-
+//static ADI_SENSE_CONFIG *pSelectedConfig = &sensor0_rtd_2w_pt100_config;
+static ADI_SENSE_CONFIG *pSelectedConfig = &multichannel_singlecycle_config_dam;
 static ADI_SENSE_CONNECTION connectionInfo = PLATFORM_CONNECTION_INFO;
 
-int main()
+#define RES_SIZE 500
+
+typedef struct __attribute__((packed, aligned(1))) {
+    uint16_t type : 2;
+    uint16_t fs : 10;
+    uint16_t padding : 4;
+} FILTER_CH;
+
+typedef struct __attribute__((packed, aligned(1))) {
+    FILTER_CH data[4];
+    uint8_t fails;
+}  FILTER_DATA;
+FILTER_DATA result[RES_SIZE] = {0};
+
+
+FILTER_DATA getRandomFilterData()
+{
+    static int counter = 89;
+    const uint8_t fails[] = {88, 130, 253, 91, 94, 97, 98,100,101, 31,34,37,40};
+    const int n = sizeof(fails)/sizeof(uint8_t);
+    FILTER_DATA fd = {0};
+//    int all1 = 0;//rand() % 2;
+    int all1 = rand() % 2;
+    all1 = 0;
+    for (int ch=2; ch<6; ++ch) {
+        fd.data[ch-2].type = all1 * rand()%3+1;
+        if (fd.data[ch-2].type == 1) {
+            fd.data[ch-2].fs = ch == 2? fails[counter % n] : 1;//*/(rand() & 0xFF) +1;
+        } else {
+            fd.data[ch-2].fs = 1;
+        }
+    }
+    counter++;
+    return fd;
+}
+
+
+void setFilterData(FILTER_DATA fd)
+{
+    for (int ch=2; ch<6; ++ch) {
+        ADI_SENSE_1000_ADC_FILTER_CONFIG *filter;
+        filter = &pSelectedConfig->adisense1000.channels[ch].adcChannelConfig.filter;
+        filter->type = (ADI_SENSE_1000_ADC_FILTER_TYPE)(fd.data[ch-2].type);
+        filter->fs = fd.data[ch-2].fs;
+    }
+    pSelectedConfig->adisense1000.power.powerMode = ADI_SENSE_1000_POWER_MODE_LOW;
+}
+ 
+
+void printFilterData(FILTER_DATA fd, int colourLimit, int idx)
+{
+    char colour[16] = "";
+    if (colourLimit) {
+        strncpy(colour, fd.fails == colourLimit? HRED_TEXT"" :
+                 (fd.fails == 0? HGREEN_TEXT"" : YELLOW_TEXT""), 15);
+        adi_sense_Print(colour);
+    }
+    if (idx >= 0) {
+        adi_sense_Print("%04d: ", idx);
+    }
+    if (colourLimit)
+        adi_sense_Print("fails:%d/%d ", fd.fails, colourLimit);
+    for (int ch=2; ch<6; ch++) {
+        adi_sense_Print("%4d ", fd.data[ch-2].type == 1? fd.data[ch-2].fs : -fd.data[ch-2].type);
+    }
+    adi_sense_Print("\r\n"NORMAL_TEXT);
+}
+
+
+ADI_SENSE_RESULT initDevice(ADI_SENSE_DEVICE_HANDLE hDevice,
+                            const FILTER_DATA *fd)
 {
     ADI_SENSE_RESULT res;
     ADI_SENSE_STATUS status;
-    ADI_SENSE_DEVICE_HANDLE hDevice;
-    ADI_SENSE_MEASUREMENT_MODE eMeasurementMode = ADI_SENSE_MEASUREMENT_MODE_NORMAL;
     bool bDeviceReady;
 
     /*
-     * Open an ADI Sense device instance.
-     */
-    res = adi_sense_Open(0, &connectionInfo, &hDevice);
-    if (res != ADI_SENSE_SUCCESS)
-    {
-        ADI_SENSE_LOG_ERROR("Failed to open device instance");
-        return res;
-    }
-
-    /*
      * Reset the given ADI Sense device....
      */
     ADI_SENSE_LOG_INFO("Resetting ADI Sense device, please wait...");
     res = adi_sense_Reset(hDevice);
-    if (res != ADI_SENSE_SUCCESS)
-    {
+    if (res != ADI_SENSE_SUCCESS) {
         ADI_SENSE_LOG_ERROR("Failed to reset device");
         return res;
     }
@@ -126,8 +184,7 @@
     do {
         wait_ms(100);
         res = adi_sense_GetDeviceReadyState(hDevice, &bDeviceReady);
-        if (res != ADI_SENSE_SUCCESS)
-        {
+        if (res != ADI_SENSE_SUCCESS) {
             ADI_SENSE_LOG_ERROR("Failed to get device ready-state");
             return res;
         }
@@ -138,16 +195,18 @@
      * Write configuration settings to the device registers.
      * Settings are not applied until adi_sense_ApplyConfigUpdates() is called.
      */
-    ADI_SENSE_LOG_INFO("Setting device configuration");
+
+    ADI_SENSE_LOG_INFO(HBLUE_TEXT"Setting device configuration");
+    setFilterData(*fd);
+    printFilterData(*fd, 0, -1);
+
     res = adi_sense_SetConfig(hDevice, pSelectedConfig);
-    if (res != ADI_SENSE_SUCCESS)
-    {
+    if (res != ADI_SENSE_SUCCESS) {
         ADI_SENSE_LOG_ERROR("Failed to set device configuration");
         return res;
     }
     res = adi_sense_ApplyConfigUpdates(hDevice);
-    if (res != ADI_SENSE_SUCCESS)
-    {
+    if (res != ADI_SENSE_SUCCESS) {
         ADI_SENSE_LOG_ERROR("Failed to apply device configuration");
         return res;
     }
@@ -155,29 +214,102 @@
      * Check device status after updating the configuration
      */
     res = adi_sense_GetStatus(hDevice, &status);
-    if (res != ADI_SENSE_SUCCESS)
-    {
+    if (res != ADI_SENSE_SUCCESS) {
         ADI_SENSE_LOG_ERROR("Failed to retrieve device status");
         return res;
     }
     if (status.deviceStatus &
-        (ADI_SENSE_DEVICE_STATUS_ERROR | ADI_SENSE_DEVICE_STATUS_ALERT))
-    {
+            (ADI_SENSE_DEVICE_STATUS_ERROR | ADI_SENSE_DEVICE_STATUS_ALERT)) {
         utils_printStatus(&status);
     }
+    return ADI_SENSE_SUCCESS;
+}
+
+
+int main()
+{
+    ADI_SENSE_RESULT res;
+    ADI_SENSE_DEVICE_HANDLE hDevice;
+    ADI_SENSE_MEASUREMENT_MODE eMeasurementMode = ADI_SENSE_MEASUREMENT_MODE_NORMAL;
+    FILTER_DATA fd = {
+        .fails = 0,
+        .data = {
+            {1, 120, 0},
+            {2, 120, 0},
+            {3, 120, 0},
+            {1,   4, 0},
+        }
+    };
+
+    fd = getRandomFilterData();
+    /*
+     * Open an ADI Sense device instance.
+     */
+    res = adi_sense_Open(0, &connectionInfo, &hDevice);
+    if (res != ADI_SENSE_SUCCESS) {
+        ADI_SENSE_LOG_ERROR("Failed to open device instance");
+        return res;
+    }
 
-    /*
-     * Kick off the measurement cycle here
-     */
-    ADI_SENSE_LOG_INFO("Configuration completed, starting measurement cycles");
-    utils_runMeasurement(hDevice, eMeasurementMode);
+    ADI_SENSE_LOG_INFO(" ");
+    ADI_SENSE_LOG_INFO(" ");
+    ADI_SENSE_LOG_INFO(" ");
+    ADI_SENSE_LOG_INFO(" ");
+    ADI_SENSE_LOG_INFO(HGREEN_TEXT"------------------------- ----------");
+    ADI_SENSE_LOG_INFO(HGREEN_TEXT"OPENING ADISENSE DEVICE 0 ----------");
+    ADI_SENSE_LOG_INFO(HGREEN_TEXT"------------------------- ----------");
+    ADI_SENSE_LOG_INFO(" ");
+    ADI_SENSE_LOG_INFO(" ");
+
+    int cfgCount = 0;
+    int repetitionCount = 0;
+    const int maxRepetitions = 1;
+
+    ADI_SENSE_LOG_INFO(HBLUE_TEXT"GD bugfix#2142-24 4xRTD4 SensorN LOW power seqOfFails burst=1 300 filter Result table");//gd
+
+    do {
+        res = initDevice(hDevice, &fd);
+        if (res)
+            return res;
+        /*
+         * Kick off the measurement cycle here
+         */
+        ADI_SENSE_LOG_INFO(HBLUE_TEXT"GD-24 Run %d:%d",cfgCount+1, repetitionCount+1);
+        res = utils_runMeasurement(hDevice, eMeasurementMode);
+        ADI_SENSE_LOG_INFO("back from %d",repetitionCount+1);
+        if (res) {
+            fd.fails++;
+            ADI_SENSE_LOG_ERROR("Failed at %d:%d with error %d. Fail %d/%d",
+                                cfgCount+1, repetitionCount+1, res, fd.fails, maxRepetitions);
+        }
+
+        if (++repetitionCount >= maxRepetitions) {
+            int fails = 0;
+            int oks = 0;
+            result[cfgCount] = fd;
+            for (int idx=0; idx<=cfgCount; idx++) {
+                printFilterData(result[idx], maxRepetitions, idx);
+                fails += result[idx].fails >= maxRepetitions? 1 : 0;
+                oks += result[idx].fails == 0? 1 : 0;
+            }
+            ADI_SENSE_LOG_INFO("Total=%d, fails=%.2f%%, semiFails=%.2f%%, oks=%.2f%%",
+                               cfgCount+1, 100.0*fails/(cfgCount+1),
+                               100.0*(cfgCount+1-fails-oks)/(cfgCount+1),
+                               100.0*oks/(cfgCount+1));
+            repetitionCount = 0;
+            fd = getRandomFilterData();
+            cfgCount++;
+        }
+
+    } while (cfgCount < RES_SIZE);
+    
+    ADI_SENSE_LOG_INFO("End of loop %d", cfgCount);
 
     /*
      * Clean up and exit
      */
     res = adi_sense_Close(hDevice);
-    if (res != ADI_SENSE_SUCCESS)
-    {
+    if (res != ADI_SENSE_SUCCESS) {
         ADI_SENSE_LOG_ERROR("Failed to close device instance");
         return res;
     }