
TAIST class assignment
Fork of TAIST_modbus by
Diff: main.cpp
- Revision:
- 1:d2e8a3c822f1
- Parent:
- 0:f306cb0263a6
--- a/main.cpp Wed Mar 14 06:17:48 2018 +0000 +++ b/main.cpp Wed Mar 14 14:55:52 2018 +0000 @@ -1,29 +1,64 @@ #include "mbed.h" #include "modbus.h" +#include <math.h> +#include <stdio.h> +Ticker t; DigitalOut myled(LED1); +InterruptIn mbtn(USER_BUTTON); RawSerial pc(USBTX, USBRX); +int a; +int b,c,d; +int casep = 0; +int16_t nub = 0; +int16_t time1; +int16_t time2; +void count() +{ nub = nub +1; +} +void press() { + if((nub-c)>2){ + d = nub; + time2 = d-c; + a = nub; + } +} +void release() { + if((nub-b)>2){ + b = nub; + time1 = b-a; + c = nub ; + } +} void serial_callback() { uint8_t frame[6]; - char ch = pc.getc(); if (modbus_parser(ch, frame)) { - + switch(frame[3]){ + case 0: casep = 1; break; //Please enter :000300000003FA\r\n to show pushing time + case 1: casep = 2; break; //Please enter :000300010003F9\r\n to show releasing time + case 2: casep = 3; break; //Please enter :000300020003F8\r\n to show both pushing and releasing time + + } } } int main() { - // setup code - pc.attach(serial_callback); - // 1. button code - // 2. timer code + mbtn.fall(&press); + mbtn.rise(&release); + + t.attach(&count, 0.1); while(1) { - // loop code - myled = 1; // LED is ON - wait(0.2); // 200 ms - myled = 0; // LED is OFF - wait(1.0); // 1 sec + pc.attach(serial_callback); + switch(casep){ + case 0: break; + case 1: pc.printf("The releasing time taken was %.1f seconds\n\n",time2/10.0); break; + case 2: pc.printf("The pushing time taken was %.1f seconds\n\n",time1/10.0); break; + case 3: pc.printf("The pushing time taken was %.1f seconds\nThe release time taken was %.1f seconds\n\n",time1/10.0,time2/10.0); break; + default: break; + } + casep = 0; } -} +} \ No newline at end of file