Extension to serial communication. GetInput() method that takes string input from serial device. Work still in progress.

Dependents:   PWM_LED_Lights

Files at this revision

API Documentation at this revision

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

Changed in this revision

Bluetooth.cpp Show annotated file Show diff for this revision Revisions of this file
Bluetooth.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 31203426e0fb Bluetooth.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Bluetooth.cpp	Tue Sep 21 16:45:49 2021 +0000
@@ -0,0 +1,72 @@
+#include "Bluetooth.h"
+
+Bluetooth::Bluetooth(PinName tx, PinName rx) : Serial(tx, rx){
+}
+
+char Bluetooth::GetKey(void){
+    return Serial::getc();
+}
+
+void Bluetooth::PutKey(char key){
+    Serial::putc(key);
+}
+
+string Bluetooth::GetStringInput(void){
+char value[100];
+int index=0;
+char ch;
+    do{ 
+    if (Serial::readable()){
+        ch = Serial::getc();   
+        if (index<100)               
+            value[index++]=ch;  
+    }
+    } while (ch!='\r');    
+    value[index]='\x0';     
+    return (value);
+}
+
+void Bluetooth::Init(void){
+    state = false;
+    Bluetooth::printf("\nEnter ON to turn on the light!\n");
+}
+
+void Bluetooth::AskUser(string msgString){
+    if (msgString == "ON\r" || msgString == "on\r" || msgString == "On\r"){
+        state = true;
+    }
+    else if(msgString == "OFF\r" || msgString == "off\r" || msgString == "Off\r" || msgString == "0\r"){
+        state = false;
+    }
+}
+
+float Bluetooth::ReturnAnswer(float value){
+    if(state){
+        Bluetooth::printf("\nEnter brightness level [%%]: ");
+        value = atof((Bluetooth::GetStringInput()).c_str());
+        if(value < 0 || value > 100){
+            Bluetooth::printf("\nWARNING: Brightness can be set from 1 to 100 [%%]!");
+            Bluetooth::printf("\nEnter brightness level [%%]: ");
+        }
+        else if(value == 0){
+            Bluetooth::printf("\nLight is switched off\n");
+            Bluetooth::Init();
+            return 0;
+        }
+        else{
+            Bluetooth::printf("\nPress enter to change the brightness! or type Off or 0 to switch the light off\n");
+            return value/100;
+        };
+    }
+    else{
+        Bluetooth::printf("\nLight is switched off\n");
+        Bluetooth::Init();
+        return 0;
+    };
+}
+
+float Bluetooth::State(void){
+    float value;
+    Bluetooth::AskUser(Bluetooth::GetStringInput());    
+    return ReturnAnswer(value);
+}
\ No newline at end of file
diff -r 000000000000 -r 31203426e0fb Bluetooth.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Bluetooth.h	Tue Sep 21 16:45:49 2021 +0000
@@ -0,0 +1,22 @@
+#ifndef BLUETOOTH_H
+#define BLUETOOTH_H
+
+#include <string>
+#include <cstdlib>
+#include "mbed.h"
+
+class Bluetooth : public Serial{
+    public:   
+        Bluetooth(PinName tx, PinName rx);
+        float State();
+        void Init();
+    private:
+        bool state;
+        char GetKey(void);
+        void PutKey(char key);
+        void AskUser(string msgString);
+        float ReturnAnswer(float value);
+        string GetStringInput(void);
+};
+
+#endif
\ No newline at end of file