Modbus serial assignment

Dependencies:   mbed

Fork of TAIST_modbus by Supachai Vorapojpisut

Revision:
1:b50d9b988d1e
Parent:
0:f306cb0263a6
--- a/main.cpp	Wed Mar 14 06:17:48 2018 +0000
+++ b/main.cpp	Wed Mar 14 14:55:36 2018 +0000
@@ -1,29 +1,77 @@
 #include "mbed.h"
 #include "modbus.h"
-
+//DigitalIn button1(USER_BUTTON);
+InterruptIn mybtn(USER_BUTTON);
 DigitalOut myled(LED1);
 RawSerial pc(USBTX, USBRX);
+Timer t1,t2,Timer1;
+uint8_t id= 40001;
+uint16_t offset=1;
+uint16_t press=3,release=4,temp=0;
+bool pushed = false;
 
 void serial_callback()
-{
+{printf("serial call back\n");
     uint8_t frame[6];
+    char ch = pc.getc();
+    printf("ch = %c \n ",ch);
     
-    char ch = pc.getc();
-    if (modbus_parser(ch, frame)) {
-        
+    modbus_init(id);
+    modbus_read(offset);
+    
+    temp=modbus_update( offset, press);
+    
+     printf("Temp= %f\n",temp);
+    if (modbus_parser(ch, frame)) {  
+         printf("Modbus Parser\n");
     }
 }
 
+
+void ledON()
+{
+    myled = 1; //LED ON
+    wait(0.2); // 200 ms
+     t1.start();
+    t2.stop();
+    if(t2.read()<0.1)
+    { printf("Bouncing\n");}
+    else
+        {printf("Button released for %.1f seconds.\r\n", t2.read());
+        release=t2.read();
+    t2.reset();
+    }
+}
+
+void ledOFF()
+{
+    myled = 0; //LED OFF
+    wait(0.2); // 200 ms
+    t1.stop();
+    t2.start();   
+    if(t1.read()<0.1)
+    { printf("Bouncing\n");}
+    else
+    {
+        printf("Button push for %.1f seconds.\r\n", t1.read());
+        press=t1.read();
+        t1.reset();
+    }
+}
+
+
+
+
 int main() {
     // setup code
     pc.attach(serial_callback);
+    
     // 1. button code
+      mybtn.fall(&ledON); // Interrupt occur
+    mybtn.rise(&ledOFF);
     // 2. timer code
+      
     while(1) {
-        // loop code
-        myled = 1; // LED is ON
-        wait(0.2); // 200 ms
-        myled = 0; // LED is OFF
-        wait(1.0); // 1 sec
+        
     }
-}
+}
\ No newline at end of file