the emg filtering part of the program

Dependencies:   HIDScope biquadFilter mbed MODSERIAL

Fork of EMG by Tom Tom

Revision:
25:1a71424b05ff
Parent:
24:01b4b51b5dc6
Child:
26:91d48c0b722d
--- a/main.cpp	Thu Oct 20 08:26:41 2016 +0000
+++ b/main.cpp	Thu Oct 20 10:31:45 2016 +0000
@@ -18,8 +18,13 @@
 
 volatile bool sampletimer = false;
 volatile bool buttonflag = false;
+volatile bool newcase = false;
+
 double threshold = 0.04;
 double samplefreq=0.002;
+double emg02;
+double emg12;
+double emg22;
 
 typedef enum { STATE_CALIBRATION, STATE_PAUZE, STATE_X, STATE_X_NEG, STATE_Y, STATE_Y_NEG, STATE_XY, STATE_XY_NEG } states;
 
@@ -72,6 +77,92 @@
     buttonflag=true;
 }
 
+void sample_button(states &mystate)
+{
+    states myoldstate=mystate;
+    char key=pc.getc();
+
+   // pc.printf("%c/n",key);
+
+    switch (key) {
+        case 'p' : // run
+            emg02=0.0;
+            emg12=0.0;
+            emg22=0.0;
+            break;
+        case 'q' : // run
+            emg02=0.0;
+            emg12=1.0;
+            emg22=0.0;
+            break;
+        case 'w' : // run
+            emg02=0.0;
+            emg12=0.0;
+            emg22=1.0;
+            break;
+        case 'e' : // run
+            emg02=0.0;
+            emg12=1.0;
+            emg22=1.0;
+            break;
+        case 'a' : // run
+            emg02=1.0;
+            emg12=1.0;
+            emg22=0.0;
+            break;
+        case 's' : // run
+            emg02=1.0;
+            emg12=0.0;
+            emg22=1.0;
+            break;
+        case 'd' : // run
+            emg02=1.0;
+            emg12=1.0;
+            emg22=1.0;
+            break;
+    }
+
+
+
+
+
+    led = !led;
+    if (emg02>threshold) {
+        if (emg12>threshold&&emg22>threshold) {
+            mystate = STATE_XY_NEG;
+        } else if (emg12>threshold) {
+            mystate = STATE_X_NEG;
+
+        } else if (emg22>threshold) {
+            mystate = STATE_Y_NEG;
+        } else {
+            mystate = STATE_PAUZE;
+        }
+    } else {
+        if (emg12>threshold&&emg22>threshold) {
+            mystate = STATE_XY;
+        } else if (emg12>threshold) {
+            mystate = STATE_X;
+
+        } else if (emg22>threshold) {
+            mystate = STATE_Y;
+        } else {
+            mystate = STATE_PAUZE;
+        }
+    }
+
+    if (buttonflag==true) {
+        mystate = STATE_CALIBRATION;
+    }
+
+    if (myoldstate==mystate) {
+        newcase=false;
+    } else {
+        newcase=true;
+    }
+    sampletimer = false;
+}
+
 void sample(states &mystate)
 {
 
@@ -93,6 +184,7 @@
      *   Finally, send all channels to the PC at once */
     scope.send();
     /*   To indicate that the function is working, the LED is toggled */
+
     led = !led;
     if (emg02>threshold) {
         if (emg12>threshold&&emg22>threshold) {
@@ -148,37 +240,61 @@
 
     while(1) {
         if (sampletimer==true) {
-            sample(mystate);
+            //sample(mystate);
+            sample_button(mystate);
+        }
+        // switch, case
 
-            // switch, case
-            switch (mystate) {
-                case STATE_CALIBRATION : // calibration
-                    pc.printf("calibration");
-                    while (button_calibrate==0) {}
-                    buttonflag=false;
-                    break;
-                case STATE_X : // run
-                    pc.printf("X");
-                    break;
-                case STATE_X_NEG : // run
-                    pc.printf("Xneg");
-                    break;
-                case STATE_Y : // execute mode 1
-                    pc.printf("Y");
-                    break;
-                case STATE_Y_NEG : // execute mode 1
-                    pc.printf("Yneg");
-                    break;
-                case STATE_XY : // execute mode 2
-                    pc.printf("XY");
-                    break;
-                case STATE_XY_NEG : // execute mode 2
-                    pc.printf("XYneg");
-                    break;
-                case STATE_PAUZE : // default
-                    pc.printf("pauze");
-                    break;
-            }
+        switch (mystate) {
+            case STATE_CALIBRATION : // calibration
+                pc.printf("calibration");
+                while (button_calibrate==0) {}
+                buttonflag=false;
+                break;
+            case STATE_X : // run
+                for (int n=0; n<1; n++) {
+                    if (newcase==true) {
+                        pc.printf("X\n");
+                        newcase=false;
+                    }
+                }
+                break;
+            case STATE_X_NEG : // run
+                if (newcase==true) {
+                    pc.printf("Xneg\n");
+                    newcase=false;
+                }
+                break;
+            case STATE_Y : // execute mode 1
+                if (newcase==true) {
+                    pc.printf("Y\n");
+                    newcase=false;
+                }
+                break;
+            case STATE_Y_NEG : // execute mode 1
+                if (newcase==true) {
+                    pc.printf("Yneg\n");
+                    newcase=false;
+                }
+                break;
+            case STATE_XY : // execute mode 2
+                if (newcase==true) {
+                    pc.printf("XY\n");
+                    newcase=false;
+                }
+                break;
+            case STATE_XY_NEG : // execute mode 2
+                if (newcase==true) {
+                    pc.printf("XYneg\n");
+                    newcase=false;
+                }
+                break;
+            case STATE_PAUZE : // default
+                if (newcase==true) {
+                    pc.printf("PAUZE\n");
+                    newcase=false;
+                }                break;
         }
+
     }
 }
\ No newline at end of file