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

Dependents:   PWM_LED_Lights

Committer:
nzupcic
Date:
Tue Sep 21 16:45:38 2021 +0000
Revision:
0:134dece3e39b
1st upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nzupcic 0:134dece3e39b 1 #include "Host.h"
nzupcic 0:134dece3e39b 2
nzupcic 0:134dece3e39b 3 Host::Host(PinName tx, PinName rx) : Serial(tx, rx){
nzupcic 0:134dece3e39b 4 }
nzupcic 0:134dece3e39b 5
nzupcic 0:134dece3e39b 6 string Host::GetInput(){
nzupcic 0:134dece3e39b 7 char s_value[5];
nzupcic 0:134dece3e39b 8 int i = 0;
nzupcic 0:134dece3e39b 9 char ch;
nzupcic 0:134dece3e39b 10 do{
nzupcic 0:134dece3e39b 11 if (Serial::readable()){
nzupcic 0:134dece3e39b 12 ch = Serial::getc();
nzupcic 0:134dece3e39b 13 if (i < 5){
nzupcic 0:134dece3e39b 14 s_value[i++]=ch;
nzupcic 0:134dece3e39b 15 };
nzupcic 0:134dece3e39b 16 if (Host::timer.read_ms() > 5000){
nzupcic 0:134dece3e39b 17 s_value[i] = '\r';
nzupcic 0:134dece3e39b 18 };
nzupcic 0:134dece3e39b 19 }
nzupcic 0:134dece3e39b 20 }
nzupcic 0:134dece3e39b 21 while (ch!='\r');
nzupcic 0:134dece3e39b 22 s_value[i] = '\x0';
nzupcic 0:134dece3e39b 23 Host::timer.reset();
nzupcic 0:134dece3e39b 24 return (s_value);
nzupcic 0:134dece3e39b 25 }
nzupcic 0:134dece3e39b 26
nzupcic 0:134dece3e39b 27 void Host::Init(float startValue){
nzupcic 0:134dece3e39b 28 Host::timer.start();
nzupcic 0:134dece3e39b 29 Host::Enable = false;
nzupcic 0:134dece3e39b 30 Host::Value = startValue;
nzupcic 0:134dece3e39b 31 Host::LastState = 0.0;
nzupcic 0:134dece3e39b 32 }
nzupcic 0:134dece3e39b 33
nzupcic 0:134dece3e39b 34