Jose Gutierrez Cabello / Mbed 2 deprecated projecte_v3_rtos

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 
00004 class Hc05 : public Serial
00005 {
00006 
00007 public:
00008     Timer timer;
00009     Hc05(PinName tx, PinName rx) : Serial(tx, rx) {
00010         baud(115200);
00011         timer.start();
00012         //printf("Constructora");
00013     }
00014     ~Hc05() {
00015         timer.stop();
00016         //printf("Destructora");
00017     }
00018     bool enviaString(char* str) {
00019         if (str[0] != '\0')
00020             printf("%s", str);
00021         return true;
00022     }
00023 
00024     void enviaStringAT(char* str) {
00025         //baud(38400);
00026         if (str[0] != '\0')
00027             printf("%s\r\n", str);
00028     }
00029     bool llegirStringAT(char* str) {
00030         if(readable()) {
00031             int i=0;
00032             str[i] = getc();
00033             //Quan detecta un 10 o un 13 surt del bucle (en el mode AT, envia 13+10)
00034             while(str[i] != 10 && (i < 128)) {
00035                 i++;
00036                 str[i] = getc();
00037             }
00038             str[i]='\0';
00039             return true;
00040         } else {
00041             str[0] = '\0';
00042             return false;
00043         }
00044     }
00045 
00046     bool llegirString(char* str) {
00047         if(readable()) {
00048             timer.reset();
00049             int i=0;
00050             str[i] = getc();
00051             if (str[i] == '@')
00052                 return false;
00053             //Quan detecta un 10 o un 13 surt del bucle (en el mode AT, envia 13+10)
00054             while(str[i] != 13 && (i < 128)) {
00055                 i++;
00056                 str[i] = getc();
00057             }
00058             str[i]='\0';
00059             return true;
00060         } else {
00061             str[0] = '\0';
00062             return false;
00063         }
00064     }
00065 
00066     void modeAT(bool _bool) {
00067         if (_bool) {
00068             baud(38400);
00069         } else {
00070             baud(115200);
00071         }
00072     }
00073 
00074     bool connexioOK() {
00075         if(timer.read_ms() > 500)
00076             return false;
00077         else
00078             return true;
00079     }
00080 
00081     void checkConn() {
00082         if(readable())
00083             timer.reset();
00084     }
00085 };
00086 
00087 class Pc : public Serial
00088 {
00089 public:
00090     Pc(PinName tx, PinName rx) : Serial(tx, rx) {
00091         baud(115200);
00092         printf("Constructora\n");
00093     }
00094     ~Pc() {
00095         printf("Destructora\n");
00096     }
00097     bool enviaString(char* str) {
00098         if (str[0] != '\0')
00099             printf("%s\n", str);
00100         return true;
00101     }
00102 
00103     bool llegirString(char* str) {
00104         if(readable()) {
00105             int i=0;
00106             str[i] = getc();
00107             while(str[i] != 13 && (i < 128)) {
00108                 i++;
00109                 str[i] = getc();
00110             }
00111             str[i]='\0';
00112             return true;
00113         } else {
00114             str[0] = '\0';
00115             return false;
00116         }
00117     }
00118 };
00119 
00120 
00121 
00122 
00123 
00124 void llegir_thread(void const *args);
00125 
00126 Mutex mutex;
00127 
00128 Pc pc(USBTX, USBRX);
00129 Hc05 bt(D8, D2);
00130 
00131 
00132 
00133 int main()
00134 {
00135     
00136 
00137     //Thread thread(llegir_thread);
00138     char str[128];
00139     
00140     while(1) {
00141         //Thread::signal_wait(0x1);
00142         if(pc.llegirString(str))
00143             bt.enviaString(str);
00144         if(bt.llegirString(str))
00145             pc.enviaString(str);
00146 
00147         if(!(bt.connexioOK()))
00148             pc.printf("ERROR DE CONNEXIO\n");
00149         Thread::wait(500);
00150         //Thread::signal_set(0x2);
00151     }
00152 
00153 }
00154 
00155 void llegir_thread(void const *args) {
00156     while(1){
00157         //Thread::signal_wait(0x2);
00158         mutex.lock();
00159         bt.checkConn();
00160         mutex.unlock();
00161         Thread::wait(300);
00162         
00163         //Thread::signal_set(0x1);
00164     }
00165 }