Jose Gutierrez Cabello / Mbed 2 deprecated projecte_v2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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