Revision:
9:58491af42a2a
Parent:
8:e6403cb3582f
--- a/main.cpp	Thu Apr 02 09:19:44 2020 +0000
+++ b/main.cpp	Fri Apr 10 02:49:47 2020 +0000
@@ -5,14 +5,17 @@
 // ---------------------------------
 //    * Three threads co-operate to flash two LEDs
 //    * A simple way to inject a fault, by pressing a button 
-//    * The watchdog is configured with a 32ms timeout
+//    * The watchdog is configured with a 1sec timeout
 
 #define ON 1
 #define OFF 0
 DigitalOut led_red(LED_RED, ON);
+DigitalOut led_blue(LED_BLUE, ON);
 DigitalIn button(PTD0, PullUp);
 DigitalOut led1(PTC12, OFF);
 DigitalOut led2(PTC13, OFF);
+AnalogIn ain(A0) ;          // Analog input
+EventQueue queue;  // creates an event queue, to call read ADC
 
 Serial pc(USBTX, USBRX); // tx, rx, useful for debugging
 
@@ -39,18 +42,47 @@
     while (!button) ;  
 }
 
+
+    // Write voltage digits
+//   v  Voltage as scale int, e.g. 3.30 is 330
+void vToString(int v, char* s) {    
+    s[3] = '0' + (v % 10) ;
+    v = v / 10 ;
+    s[2] = '0' + (v % 10) ;
+    v = v / 10 ;
+    s[0] = '0' + (v % 10) ;
+}
+
 // ---Thread for controlling LED 1----------------
 //   Turn LED1 on/off in response to signals 
 // -----------------------------------------------
 void led1_thread() {  // method to run in thread
-
+    int volts = 0;
+    char vstring[] = "X.XX\r\n" ;
     int evt ;
     while (true) {
         evt = signals.wait_any(ON1 | OFF1); // wait for either signal
-        if (evt & ON1) led1 = ON ;
+        if (evt & ON1) {
+            led1 = ON ;
+            ThisThread::sleep_for(1) ;
+            // check if led is working
+            volts = ain.read_u16();
+            vToString(volts, vstring) ;
+            if (volts > 100 && volts < 250) {
+                pc.printf(vstring) ;
+                while(true) {
+                    led_blue = OFF;
+                }
+                wdt_kickA() ;
+            } else {
+                pc.printf(vstring) ;
+                led_red = OFF;
+            }
+        }
         if (evt & OFF1) led1 = OFF ;
-       // waitButton() ;  // POSSIBLE FAULT HERE
-        wdt_kickA() ;
+
+        // waitButton() ;  // POSSIBLE FAULT HERE
+        
 
     }
 }
@@ -78,10 +110,15 @@
         signals.set(ON1 | ON2) ;
         ThisThread::sleep_for(1000) ;
         signals.set(OFF1 | OFF2) ;
-         waitButton() ; // POSSIBLE FAULT HERE
+         //waitButton() ; // POSSIBLE FAULT HERE
     }
 }
 
+
+
+
+
+
 // -----------MAIN-------------------------------
 //    Configure watchdog. Start threads. 
 //    Show start up with RED for 1sec
@@ -92,21 +129,17 @@
 
 int main(void) {
     wdt_1sec() ; // initialise watchdog - 32ms timeout
-    //wdt_kick_all() ;
-    //tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms    
+    
     
     // start threads
     threadT.start(timer_thread) ; // start the timer thread 
     threadLED1.start(led1_thread) ; // start the LED1 control thread 
     threadLED2.start(led2_thread) ; // start the LED2 control thread 
     
-    // show start-up
-    led_red = OFF;
-    ThisThread::sleep_for(5000) ;
-    led_red = ON;
     
     // main thread does nothing more
     while (true) {
+
         ThisThread::sleep_for(10000) ;
     }
 }
\ No newline at end of file