the complete model of the heart model

Dependencies:   TextLCD mbed-rtos mbed

Fork of heart by William Archer

Revision:
2:b4551e56cd1c
Parent:
1:6ad7b5bf9c27
Child:
3:5941bb3c4fc2
--- a/main.cpp	Wed Nov 30 02:32:59 2016 +0000
+++ b/main.cpp	Wed Nov 30 03:45:47 2016 +0000
@@ -4,6 +4,8 @@
 #include <time.h>
 #include <stdlib.h>
 
+//use screen /dev/tty.. on mac in terminal to receive input
+
 TextLCD lcd(p15,p16,p17,p18,p19,p20,TextLCD::LCD16x2);
 Serial pc(USBTX, USBRX); //set up serial
 DigitalOut natAPace(LED1); //leds for pacing
@@ -19,16 +21,20 @@
 unsigned int v_time = 0;
 
 int mode = 0; //0 = random, 1 = manual, 2 = test
-
+int modeInManul = -1; //0 = manualmode apace, 1 = manualmode vpace
+bool modeSwitch = false;
 //constants
 
-const int minwait_A = 10;
-const int minwait_V = 20;
+const int minwait_A = 100;
+const int minwait_V = 200;
+
+/*
 const int LRI = 1000;
 const int VRP = 400;
 const int PVARP = 500;
 const int URI = 1000;
 const int AVI = 100;
+*/
 
 void switch_modes() {
     switch(mode) {
@@ -55,6 +61,8 @@
         
 }
 
+
+
 void send_signal(int type) { //type=0 a_signal, type=1 v_signal
         
         switch(type) {
@@ -69,6 +77,12 @@
         //v_sense = 0;
 }
 
+void modeSwitchDelay(){
+    //srand(time(NULL));
+    //int time = rand()%9700 + 300;
+    Thread::wait(1000);   
+}
+
 void heart_keyboard() {
     while(true) { //thread is continuously running
             if(pc.readable()) {
@@ -77,21 +91,24 @@
                 switch(c) {
                     case 'r': //set to random mode
                         mode = 0;
+                        modeSwitch = true;
                         break;
-                    case 'm': //set to manual mode
+                    case 'm': //mset to manual mode
                         mode = 1;
+                        modeSwitch = true;
                         break;
                     case 't': //set to test mode
                         mode = 2;
+                        modeSwitch = true;
                         break;
                     case 'a': //asignal
-                        if(mode) { //only if in manual (heart_mode == 1)
-                            send_signal(0);
+                        if(mode == 1) { //only if in manual (heart_mode == 1)
+                            modeInManul = 0;
                         }
                         break;
                     case 'v': //vsignal
-                        if(mode) { //only if in manual (heart_mode == 1)
-                            send_signal(1);
+                        if(mode == 1) { //only if in manual (heart_mode == 1)
+                            modeInManul = 1;
                         }
                         break; 
                     default: //erroneous key get rid of it
@@ -134,42 +151,59 @@
     Thread::wait(500);
 }
 
+void performModeDelay(){
+    if(modeSwitch == true){
+        modeSwitchDelay();
+        modeSwitch = false;
+    }
+}
+
 void Random(){
     //TODO: heart behaviour in random mode
-    while(mode == 0){
-        natApaceBlink();
-        
+    while(1){
+        if(mode == 0){
+            performModeDelay();
+            //natApaceBlink();
+        }
     }
 }
 
 void Manual(){
     //TODO: heart behaviour in manual mode
-    while(mode == 1){
-        natVpaceBlink();
+    while(1){
+        if(mode == 1){
+            performModeDelay();
+            //natVpaceBlink();
+            if(modeInManul == 0){
+                ApaceBlink();
+                modeInManul = -1;
+            }
+            
+            if(modeInManul == 1){
+                VpaceBlink();
+                modeInManul = -1;
+            }
+        }
     }   
 }
 
 void Test(){
     //TODO: heart behaviour in test mode
-    while(mode == 2){
-        ApaceBlink();
+    while(1){
+        if(mode == 2){
+            performModeDelay();
+            //ApaceBlink();
+        }
     }
 }
 
-void modeSwitchDelay(){
-    srand(time(NULL));
-    int time = rand()%9700 + 300;
-    Thread::wait(time);   
-}
-
 int main() {
     //TODO: Set up threads
     Thread keyboard(heart_keyboard);
     //Note: only one of the following threads will be on duty when heart runs
     //and it is done through checking the mode the heart is in.
-    Thread random(Random);
-    Thread manual(Manual);
-    Thread test(Test);
-    while(1){   
-    }
+    Thread random_(Random);
+    Thread manual_(Manual);
+    Thread test_(Test);
+    while(1);
 }