Extension to serial communication. GetInput() method that takes string input from serial device

Dependents:   PWM_LED_Lights

Files at this revision

API Documentation at this revision

Comitter:
nzupcic
Date:
Tue Sep 21 16:45:38 2021 +0000
Commit message:
1st upload

Changed in this revision

Host.cpp Show annotated file Show diff for this revision Revisions of this file
Host.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Host.cpp	Tue Sep 21 16:45:38 2021 +0000
@@ -0,0 +1,34 @@
+#include "Host.h"
+
+Host::Host(PinName tx, PinName rx) : Serial(tx, rx){
+}
+
+string Host::GetInput(){
+    char s_value[5];
+    int i = 0;
+    char ch;
+    do{ 
+        if (Serial::readable()){
+            ch = Serial::getc();   
+            if (i < 5){          
+                s_value[i++]=ch;  
+            };
+            if (Host::timer.read_ms() > 5000){
+                s_value[i] = '\r';
+            };
+        }
+    }
+    while (ch!='\r');  
+    s_value[i] = '\x0';
+    Host::timer.reset();      
+    return (s_value);
+}
+
+void Host::Init(float startValue){
+    Host::timer.start();
+    Host::Enable    = false;
+    Host::Value     = startValue;
+    Host::LastState = 0.0;
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Host.h	Tue Sep 21 16:45:38 2021 +0000
@@ -0,0 +1,19 @@
+#ifndef HOST_H
+#define HOST_H
+
+#include <string>
+#include <cstdlib>
+#include "mbed.h"
+
+class Host : public Serial{
+    public:   
+        Host(PinName tx, PinName rx);
+        void    Init(float startValue);
+        string  GetInput();
+        float   Value;
+        bool    Enable;
+        float   LastState;
+        Timer   timer;
+};
+
+#endif
\ No newline at end of file