พัทธนันทน์ ศรีสุขนันทน์ / Mbed 2 deprecated TAIST_modbus

Dependencies:   mbed

Fork of TAIST_modbus by Supachai Vorapojpisut

Revision:
1:c5e6595a03db
Parent:
0:f306cb0263a6
Child:
2:6d1f053a6033
--- a/main.cpp	Wed Mar 14 06:17:48 2018 +0000
+++ b/main.cpp	Wed Mar 14 14:34:01 2018 +0000
@@ -3,27 +3,61 @@
 
 DigitalOut myled(LED1);
 RawSerial pc(USBTX, USBRX);
+InterruptIn button1(USER_BUTTON);
+Ticker callfn;
+
+bool oldButton;
+int16_t timeInt1;
+int16_t timeInt2;
+int countclk = 0;
+
 
 void serial_callback()
 {
     uint8_t frame[6];
+    char ch = pc.getc();
     
-    char ch = pc.getc();
+    //pc.printf("charac: %c \n", ch);
+    //printf("%d",modbus_parser(ch, frame));
     if (modbus_parser(ch, frame)) {
-        
+        //printf("F %d\n", frame[2]);
+        if (frame[2] == 1) {
+            printf("Timer 1: %.1f second\n", timeInt1/10.0);
+            //pc.printf(":AA0301%s\n,
+            
+        }
+        else if (frame[2] == 0) {
+            printf("Timer 2: %.1f second\n", timeInt2/10.0);
+        }
     }
 }
 
+void timerFcn(){
+    if ((oldButton == 1) && (button1 == 0)) {
+           timeInt2 = countclk;
+           countclk = 0;
+           printf("timeInt2 = %d\n", timeInt2);
+    }
+    else if ((oldButton == 0) && (button1 == 1)) {
+           timeInt1 = countclk;
+           countclk = 0;
+           printf("timeInt1 = %d\n", timeInt1);
+    }
+    countclk++;
+    oldButton = button1;
+}
+
 int main() {
     // setup code
     pc.attach(serial_callback);
     // 1. button code
+    //button1.mode("PullNone");
     // 2. timer code
+    callfn.attach(timerFcn, 0.1);
+    //pc.printf("INITILIZED\n");
     while(1) {
         // loop code
-        myled = 1; // LED is ON
-        wait(0.2); // 200 ms
-        myled = 0; // LED is OFF
-        wait(1.0); // 1 sec
+        myled = !myled;
+        wait(0.1);
     }
 }