Serial communication between a F303RE and a mobile phone using a HC-05 BT.

Dependencies:   mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
jcabello7
Date:
Wed Dec 09 12:58:00 2015 +0000
Commit message:
Initial version (not working)

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 09 12:58:00 2015 +0000
@@ -0,0 +1,165 @@
+#include "mbed.h"
+#include "rtos.h"
+
+class Hc05 : public Serial
+{
+
+public:
+    Timer timer;
+    Hc05(PinName tx, PinName rx) : Serial(tx, rx) {
+        baud(115200);
+        timer.start();
+        //printf("Constructora");
+    }
+    ~Hc05() {
+        timer.stop();
+        //printf("Destructora");
+    }
+    bool enviaString(char* str) {
+        if (str[0] != '\0')
+            printf("%s", str);
+        return true;
+    }
+
+    void enviaStringAT(char* str) {
+        //baud(38400);
+        if (str[0] != '\0')
+            printf("%s\r\n", str);
+    }
+    bool llegirStringAT(char* str) {
+        if(readable()) {
+            int i=0;
+            str[i] = getc();
+            //Quan detecta un 10 o un 13 surt del bucle (en el mode AT, envia 13+10)
+            while(str[i] != 10 && (i < 128)) {
+                i++;
+                str[i] = getc();
+            }
+            str[i]='\0';
+            return true;
+        } else {
+            str[0] = '\0';
+            return false;
+        }
+    }
+
+    bool llegirString(char* str) {
+        if(readable()) {
+            timer.reset();
+            int i=0;
+            str[i] = getc();
+            if (str[i] == '@')
+                return false;
+            //Quan detecta un 10 o un 13 surt del bucle (en el mode AT, envia 13+10)
+            while(str[i] != 13 && (i < 128)) {
+                i++;
+                str[i] = getc();
+            }
+            str[i]='\0';
+            return true;
+        } else {
+            str[0] = '\0';
+            return false;
+        }
+    }
+
+    void modeAT(bool _bool) {
+        if (_bool) {
+            baud(38400);
+        } else {
+            baud(115200);
+        }
+    }
+
+    bool connexioOK() {
+        if(timer.read_ms() > 500)
+            return false;
+        else
+            return true;
+    }
+
+    void checkConn() {
+        if(readable())
+            timer.reset();
+    }
+};
+
+class Pc : public Serial
+{
+public:
+    Pc(PinName tx, PinName rx) : Serial(tx, rx) {
+        baud(115200);
+        printf("Constructora\n");
+    }
+    ~Pc() {
+        printf("Destructora\n");
+    }
+    bool enviaString(char* str) {
+        if (str[0] != '\0')
+            printf("%s\n", str);
+        return true;
+    }
+
+    bool llegirString(char* str) {
+        if(readable()) {
+            int i=0;
+            str[i] = getc();
+            while(str[i] != 13 && (i < 128)) {
+                i++;
+                str[i] = getc();
+            }
+            str[i]='\0';
+            return true;
+        } else {
+            str[0] = '\0';
+            return false;
+        }
+    }
+};
+
+
+
+
+
+void llegir_thread(void const *args);
+
+Mutex mutex;
+
+Pc pc(USBTX, USBRX);
+Hc05 bt(D8, D2);
+
+
+
+int main()
+{
+    
+
+    //Thread thread(llegir_thread);
+    char str[128];
+    
+    while(1) {
+        //Thread::signal_wait(0x1);
+        if(pc.llegirString(str))
+            bt.enviaString(str);
+        if(bt.llegirString(str))
+            pc.enviaString(str);
+
+        if(!(bt.connexioOK()))
+            pc.printf("ERROR DE CONNEXIO\n");
+        Thread::wait(500);
+        //Thread::signal_set(0x2);
+    }
+
+}
+
+void llegir_thread(void const *args) {
+    while(1){
+        //Thread::signal_wait(0x2);
+        mutex.lock();
+        bt.checkConn();
+        mutex.unlock();
+        Thread::wait(300);
+        
+        //Thread::signal_set(0x1);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Dec 09 12:58:00 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/mbed-rtos/#c825593ece39
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Dec 09 12:58:00 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/165afa46840b
\ No newline at end of file