From 1eaee14d69b95ed037298ae9fb8c2e482bdeb3b0 Mon Sep 17 00:00:00 2001
From: "Michael D. M. Dryden" <mdryden@chem.utoronto.ca>
Date: Thu, 19 Nov 2015 13:51:30 -0500
Subject: [PATCH] Finish TCS interlock. Completes #6

---
 DSTAT/src/experiment.c | 21 ++++++++++++++++-----
 DSTAT/src/main.c       | 12 +++++++++---
 DSTAT/src/settings.c   |  1 +
 3 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/DSTAT/src/experiment.c b/DSTAT/src/experiment.c
index efeaa07..e5ac5c9 100644
--- a/DSTAT/src/experiment.c
+++ b/DSTAT/src/experiment.c
@@ -60,6 +60,8 @@ void experiment_handler(char command){
     static uint8_t p5, o1, o2, o3;
     static int16_t pcv1, pcv2;
     static uint16_t pct1, pct2;
+    uint16_t tcs_data[] = {0,0,0,0};
+    uint16_t tcs_data1[] = {0,0,0,0};
 
     switch (command){
         case 'A': //ADS Buffer/rate/PGA values from ads1255.h
@@ -134,14 +136,23 @@ void experiment_handler(char command){
             scanf("%hhu", &o1);
             printf("#INFO: TCS_check: %hhu\n\r", o1);
             if (o1 > 0) {
-                if (settings.settings.tcs_enabled > 0) {
-                    uint16_t tcs_data[4] = {0,0,0,0};
+                if (settings.settings.tcs_enabled > 0){
                     tcs_readvalues(tcs_data);
-                    
-                    if (tcs_data[0] > settings.settings.tcs_clear_threshold) {
+                    delay_ms(25);
+                    tcs_readvalues(tcs_data1); // If sensor disconnected, second measurement should be exactly the same
+                    if (tcs_data[0] > settings.settings.tcs_clear_threshold){
                         printf("#ERR: Ambient light exceeds threshold %u\n\r", tcs_data[0]);
+                        printf("#INFO: TCS—%u %u %u %u\n\r", tcs_data[0], tcs_data[1], tcs_data[2], tcs_data[3]);
                         return;
                     }
+                    else if (tcs_data[3] == tcs_data1[3]){
+                        printf("#ERR: Ambient light sensor seems to be disconnected \n\r");
+                        return;
+                    }
+                }
+                else {
+                    printf("#ERR: Ambient light sensor disabled.\n\r");
+                    return;
                 }
             }
             
@@ -338,7 +349,7 @@ void pot_set_gain(void){
 			default:
 				printf("#WAR: Invalid pot gain.\n\r");
 				break;
-		
+            
 		return;
 	}
 }
diff --git a/DSTAT/src/main.c b/DSTAT/src/main.c
index 395c264..c7e5c68 100644
--- a/DSTAT/src/main.c
+++ b/DSTAT/src/main.c
@@ -34,9 +34,15 @@ int8_t command_handler(char command){
             break;
             
         case 'T': ;
-            uint16_t tcs_data[4] = {0,0,0,0};
-            tcs_readvalues(tcs_data);
-            printf("#INFO: TCS—%u %u %u %u\n\r", tcs_data[0], tcs_data[1], tcs_data[2], tcs_data[3]);
+            uint16_t tcs_data[] = {0,0,0,0};
+            if (settings.settings.tcs_enabled == 0){
+                printf("T-1.-1.-1.-1\n\r");
+            }
+            else{
+                tcs_readvalues(tcs_data);
+                printf("#INFO: TCS—%u %u %u %u\n\r", tcs_data[0], tcs_data[1], tcs_data[2], tcs_data[3]);
+                printf("T%u.%u.%u.%u\n\r", tcs_data[0], tcs_data[1], tcs_data[2], tcs_data[3]);
+            }
             break;
             
 		case 'V': //check version
diff --git a/DSTAT/src/settings.c b/DSTAT/src/settings.c
index 9cb5631..8b81a1d 100644
--- a/DSTAT/src/settings.c
+++ b/DSTAT/src/settings.c
@@ -43,6 +43,7 @@ void settings_read_eeprom(void){
     printf("#INFO: max5443_offset = %u\n\r", settings.settings.max5443_offset);
     printf("#INFO: tls_enabled = %u\n\r", settings.settings.tcs_enabled);
     printf("#INFO: tls_clear_threshold = %u\n\r", settings.settings.tcs_clear_threshold);
+    printf("Smax5443_offset.%u:tcs_enabled.%u:tcs_clear_threshold.%u\n\r", settings.settings.max5443_offset, settings.settings.tcs_enabled, settings.settings.tcs_clear_threshold);
 }
 
 void settings_write_eeprom(void){
-- 
GitLab