teste de publish
Dependencies: DS1820 HighSpeedAnalogIn devices mbed
diversos.cpp@11:631bea162800, 2017-08-30 (annotated)
- Committer:
- brunofgc
- Date:
- Wed Aug 30 18:42:18 2017 +0000
- Revision:
- 11:631bea162800
- Parent:
- 10:263c093f8977
- Child:
- 13:b9183b4bc049
Ultima vers?o sem kick em watch dog timer dentro do processo de envio de dados.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brunofgc | 0:1c0a769988ee | 1 | #include "diversos.h" |
brunofgc | 0:1c0a769988ee | 2 | bool eventosRTC::segundos; |
brunofgc | 0:1c0a769988ee | 3 | bool eventosRTC::minutos; |
brunofgc | 0:1c0a769988ee | 4 | bool eventosRTC::rotina10Segundos; |
brunofgc | 0:1c0a769988ee | 5 | bool eventosRTC::rotina15Minutos; |
brunofgc | 0:1c0a769988ee | 6 | bool eventosRTC::rotina1hora; |
brunofgc | 0:1c0a769988ee | 7 | bool eventosRTC::rotina1segundo; |
brunofgc | 0:1c0a769988ee | 8 | WatchdogTimer diversos::wdt; |
brunofgc | 0:1c0a769988ee | 9 | char commands::buffer[tamBufferCommands]; |
brunofgc | 0:1c0a769988ee | 10 | |
brunofgc | 0:1c0a769988ee | 11 | |
brunofgc | 0:1c0a769988ee | 12 | uint32_t diversos::memAvailable(){ |
brunofgc | 0:1c0a769988ee | 13 | char stackVariable; |
brunofgc | 0:1c0a769988ee | 14 | char *heap; |
brunofgc | 0:1c0a769988ee | 15 | uint32_t result; |
brunofgc | 0:1c0a769988ee | 16 | heap = (char*)malloc(1); |
brunofgc | 0:1c0a769988ee | 17 | result = (uint32_t) ((&stackVariable) - heap); |
brunofgc | 0:1c0a769988ee | 18 | free(heap); |
brunofgc | 0:1c0a769988ee | 19 | return result; |
brunofgc | 0:1c0a769988ee | 20 | } |
brunofgc | 0:1c0a769988ee | 21 | |
brunofgc | 0:1c0a769988ee | 22 | void WatchdogTimer::kick(float s) { |
brunofgc | 0:1c0a769988ee | 23 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
brunofgc | 0:1c0a769988ee | 24 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
brunofgc | 0:1c0a769988ee | 25 | LPC_WDT->WDTC = s * (float)clk; |
brunofgc | 0:1c0a769988ee | 26 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
brunofgc | 0:1c0a769988ee | 27 | kick(); |
brunofgc | 0:1c0a769988ee | 28 | } |
brunofgc | 0:1c0a769988ee | 29 | // "kick" or "feed" the dog - reset the watchdog timer |
brunofgc | 0:1c0a769988ee | 30 | // by writing this required bit pattern |
brunofgc | 0:1c0a769988ee | 31 | void WatchdogTimer::kick() { |
brunofgc | 0:1c0a769988ee | 32 | LPC_WDT->WDFEED = 0xAA; |
brunofgc | 0:1c0a769988ee | 33 | LPC_WDT->WDFEED = 0x55; |
brunofgc | 0:1c0a769988ee | 34 | } |
brunofgc | 0:1c0a769988ee | 35 | |
brunofgc | 0:1c0a769988ee | 36 | /*double diversos::stringToFloat(char *p,uint16_t max){ |
brunofgc | 0:1c0a769988ee | 37 | uint16_t caractere=0; |
brunofgc | 0:1c0a769988ee | 38 | double iPart=0.0,dPart=0.0,f=0.0,mult=0.1; |
brunofgc | 0:1c0a769988ee | 39 | while(*p != '.') { |
brunofgc | 0:1c0a769988ee | 40 | caractere++; |
brunofgc | 0:1c0a769988ee | 41 | if(caractere>(max-1)){return 0.0;} |
brunofgc | 0:1c0a769988ee | 42 | iPart = (iPart*10) + (double)((*(p++)-'0')); |
brunofgc | 0:1c0a769988ee | 43 | } |
brunofgc | 0:1c0a769988ee | 44 | p++; |
brunofgc | 0:1c0a769988ee | 45 | while(*p!= '\0') { |
brunofgc | 0:1c0a769988ee | 46 | if(caractere>(max-1)){return 0.0;} |
brunofgc | 0:1c0a769988ee | 47 | dPart += (double)(((*(p++)-'0'))*mult); |
brunofgc | 0:1c0a769988ee | 48 | mult /= 10; |
brunofgc | 0:1c0a769988ee | 49 | } |
brunofgc | 0:1c0a769988ee | 50 | f = iPart + dPart; |
brunofgc | 0:1c0a769988ee | 51 | return f; |
brunofgc | 0:1c0a769988ee | 52 | }*/ |
brunofgc | 0:1c0a769988ee | 53 | |
brunofgc | 0:1c0a769988ee | 54 | /*double diversos::stringToFloat(char *p,uint16_t max){ |
brunofgc | 0:1c0a769988ee | 55 | uint16_t caractere=0; |
brunofgc | 0:1c0a769988ee | 56 | double iPart=0.0,dPart=0.0,f=0.0,mult=0.1; |
brunofgc | 0:1c0a769988ee | 57 | while(p[caractere] != '.'){ |
brunofgc | 0:1c0a769988ee | 58 | caractere++; |
brunofgc | 0:1c0a769988ee | 59 | if(caractere>(max-1)){return 0.0;} |
brunofgc | 0:1c0a769988ee | 60 | iPart = (iPart*10) + (((uint8_t)p[caractere])-30); |
brunofgc | 0:1c0a769988ee | 61 | } |
brunofgc | 0:1c0a769988ee | 62 | caractere++; |
brunofgc | 0:1c0a769988ee | 63 | while(p[caractere]!= '\0'){ |
brunofgc | 0:1c0a769988ee | 64 | if(caractere>(max-1)){return 0.0;} |
brunofgc | 0:1c0a769988ee | 65 | dPart += (((uint8_t)p[caractere])-30)*mult; |
brunofgc | 0:1c0a769988ee | 66 | mult /= 10; |
brunofgc | 0:1c0a769988ee | 67 | } |
brunofgc | 0:1c0a769988ee | 68 | f = iPart + dPart; |
brunofgc | 0:1c0a769988ee | 69 | return f; |
brunofgc | 0:1c0a769988ee | 70 | }*/ |
brunofgc | 0:1c0a769988ee | 71 | |
brunofgc | 3:9598af355293 | 72 | void diversos::strReplace(char *str, char *find, char *replace){ |
brunofgc | 3:9598af355293 | 73 | char aux[100]; |
brunofgc | 3:9598af355293 | 74 | char *ptr; |
brunofgc | 3:9598af355293 | 75 | char replaced = 0; |
brunofgc | 3:9598af355293 | 76 | char maxIterations = 0; |
brunofgc | 3:9598af355293 | 77 | replaced = 0; |
brunofgc | 3:9598af355293 | 78 | strcpy(aux,""); |
brunofgc | 3:9598af355293 | 79 | ptr = strtok(str,find); |
brunofgc | 3:9598af355293 | 80 | while((ptr!=NULL)&&(maxIterations<9)){ |
brunofgc | 3:9598af355293 | 81 | replaced = 1; |
brunofgc | 3:9598af355293 | 82 | maxIterations++; |
brunofgc | 3:9598af355293 | 83 | strcat(aux,ptr); |
brunofgc | 3:9598af355293 | 84 | strcat(aux,replace); |
brunofgc | 3:9598af355293 | 85 | ptr = strtok(NULL,find); |
brunofgc | 3:9598af355293 | 86 | pc.printf("Tirando <%s> em <%s>.\r\n",find,str); |
brunofgc | 3:9598af355293 | 87 | } |
brunofgc | 3:9598af355293 | 88 | if(replaced){ |
brunofgc | 3:9598af355293 | 89 | aux[strlen(aux)-1]=0; |
brunofgc | 3:9598af355293 | 90 | strcpy(str,aux); |
brunofgc | 3:9598af355293 | 91 | } |
brunofgc | 3:9598af355293 | 92 | //-------------------- |
brunofgc | 3:9598af355293 | 93 | } |
brunofgc | 3:9598af355293 | 94 | |
brunofgc | 4:13ff9c81dc10 | 95 | void diversos::processaPulsosEDs(){ |
brunofgc | 6:d4ebbaaba295 | 96 | if(ED1){pulsosEDs[0]++;} |
brunofgc | 6:d4ebbaaba295 | 97 | if(ED2){pulsosEDs[1]++;} |
brunofgc | 6:d4ebbaaba295 | 98 | if(ED3){pulsosEDs[2]++;} |
brunofgc | 4:13ff9c81dc10 | 99 | } |
brunofgc | 4:13ff9c81dc10 | 100 | |
brunofgc | 0:1c0a769988ee | 101 | void diversos::progressBar(uint32_t progresso,uint32_t total){ |
brunofgc | 0:1c0a769988ee | 102 | int i,j; |
brunofgc | 0:1c0a769988ee | 103 | char maxTam = 20; |
brunofgc | 0:1c0a769988ee | 104 | char engrenagem[4]={179,0x2F,0x2D,0x5C}; |
brunofgc | 0:1c0a769988ee | 105 | |
brunofgc | 0:1c0a769988ee | 106 | j=((double)progresso/(double)total)*maxTam; |
brunofgc | 0:1c0a769988ee | 107 | pc.printf("%c%c%c<",179,engrenagem[(unsigned int)progresso%4],179); |
brunofgc | 0:1c0a769988ee | 108 | for(i=0;i<maxTam;i++){ |
brunofgc | 0:1c0a769988ee | 109 | if(i<=j){ |
brunofgc | 0:1c0a769988ee | 110 | pc.printf("%c",178); |
brunofgc | 0:1c0a769988ee | 111 | }else{ |
brunofgc | 0:1c0a769988ee | 112 | pc.printf("%c",176); |
brunofgc | 0:1c0a769988ee | 113 | } |
brunofgc | 0:1c0a769988ee | 114 | } |
brunofgc | 0:1c0a769988ee | 115 | pc.printf("> %3.0f%% Progresso %5lu de %5lu\r ",((double)progresso/(double)total)*100,progresso,total); |
brunofgc | 0:1c0a769988ee | 116 | } |
brunofgc | 0:1c0a769988ee | 117 | |
brunofgc | 0:1c0a769988ee | 118 | void commands::exec(uint8_t idConnection){ |
brunofgc | 0:1c0a769988ee | 119 | char *ptr,*pCharFile,*ptrComando; |
brunofgc | 0:1c0a769988ee | 120 | char msg[100]; |
brunofgc | 0:1c0a769988ee | 121 | uint16_t currentCur,totalLengthCommands; |
brunofgc | 0:1c0a769988ee | 122 | static uint16_t fileCheckSum16BIT,checkSum16BIT_Lido; |
brunofgc | 7:ae9c47f62946 | 123 | |
brunofgc | 0:1c0a769988ee | 124 | totalLengthCommands = strlen(commands::buffer); |
brunofgc | 0:1c0a769988ee | 125 | ptrComando = strtok(commands::buffer,";"); |
brunofgc | 0:1c0a769988ee | 126 | |
brunofgc | 0:1c0a769988ee | 127 | do{ |
brunofgc | 2:55b7b466e742 | 128 | |
brunofgc | 2:55b7b466e742 | 129 | |
brunofgc | 2:55b7b466e742 | 130 | |
brunofgc | 0:1c0a769988ee | 131 | ptr=strstr(ptrComando,"DELETE_SENT_FILES"); |
brunofgc | 11:631bea162800 | 132 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 133 | sdCard::deleteSentFiles = true; |
brunofgc | 0:1c0a769988ee | 134 | } |
brunofgc | 0:1c0a769988ee | 135 | |
brunofgc | 0:1c0a769988ee | 136 | ptr=strstr(ptrComando,"SET_RTC:"); |
brunofgc | 0:1c0a769988ee | 137 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 138 | ptr = strtok(ptr,":"); |
brunofgc | 0:1c0a769988ee | 139 | ptr = strtok(NULL,";"); |
brunofgc | 0:1c0a769988ee | 140 | set_time(atoi(ptr)); |
brunofgc | 0:1c0a769988ee | 141 | time_t seconds = time(NULL); |
brunofgc | 0:1c0a769988ee | 142 | pc.printf("Horario atualizado pelo server %s",ctime(&seconds)); |
brunofgc | 0:1c0a769988ee | 143 | } |
brunofgc | 0:1c0a769988ee | 144 | |
brunofgc | 0:1c0a769988ee | 145 | ptr=strstr(ptrComando,"sendArmazenamento"); |
brunofgc | 0:1c0a769988ee | 146 | if(ptr) { |
brunofgc | 7:ae9c47f62946 | 147 | eventosRTC::rotina15Minutos=1; |
brunofgc | 0:1c0a769988ee | 148 | } |
brunofgc | 0:1c0a769988ee | 149 | |
brunofgc | 0:1c0a769988ee | 150 | ptr=strstr(ptrComando,"StartSendFile"); |
brunofgc | 0:1c0a769988ee | 151 | if(ptr){ |
brunofgc | 0:1c0a769988ee | 152 | modemCom::status.timeOut=100; |
brunofgc | 0:1c0a769988ee | 153 | strtok(ptr,",");//Descartando inicio |
brunofgc | 0:1c0a769988ee | 154 | pCharFile = strtok(NULL,",");//Selecionando nome do arquivo |
brunofgc | 0:1c0a769988ee | 155 | strcpy(sdCard::nomeArquivoEmRecebimento,pCharFile); |
brunofgc | 0:1c0a769988ee | 156 | pCharFile = strtok(NULL,";");//Selecionando string checkSum16BIT |
brunofgc | 0:1c0a769988ee | 157 | fileCheckSum16BIT = atoi(pCharFile); |
brunofgc | 0:1c0a769988ee | 158 | remove("/sd/RAD/tempFile.bin"); |
brunofgc | 0:1c0a769988ee | 159 | pc.printf("Iniciando recepcao do server arquivo deviceCfg.\n"); |
brunofgc | 0:1c0a769988ee | 160 | modemCom::status.recebendoArquivoDoServer = true; |
brunofgc | 0:1c0a769988ee | 161 | |
brunofgc | 0:1c0a769988ee | 162 | sprintf(modemCom::bufIn,"AT+CIPSEND=%u,%lu\r\n",idConnection,10); |
brunofgc | 0:1c0a769988ee | 163 | modem.puts(modemCom::bufIn); |
brunofgc | 0:1c0a769988ee | 164 | osDelay(100); |
brunofgc | 0:1c0a769988ee | 165 | modem.puts("sendData\r\n"); |
brunofgc | 0:1c0a769988ee | 166 | |
brunofgc | 0:1c0a769988ee | 167 | pc.printf("sendData\n"); |
brunofgc | 0:1c0a769988ee | 168 | modemCom::timeOutModem = 250; |
brunofgc | 0:1c0a769988ee | 169 | return; |
brunofgc | 0:1c0a769988ee | 170 | } |
brunofgc | 0:1c0a769988ee | 171 | |
brunofgc | 0:1c0a769988ee | 172 | ptr=strstr(ptrComando,"StopSendFile"); |
brunofgc | 0:1c0a769988ee | 173 | if(ptr){ |
brunofgc | 0:1c0a769988ee | 174 | pc.printf("Finalizando recepcao do server arquivo deviceCfg.\n"); |
brunofgc | 0:1c0a769988ee | 175 | modemCom::status.recebendoArquivoDoServer = false; |
brunofgc | 0:1c0a769988ee | 176 | checkSum16BIT_Lido = sdCard::calcCheckSum16BITFile("/sd/RAD/tempFile.bin"); |
brunofgc | 0:1c0a769988ee | 177 | if(fileCheckSum16BIT == checkSum16BIT_Lido){ |
brunofgc | 0:1c0a769988ee | 178 | if(sdCard::file_rename("/sd/RAD/tempFile.bin",sdCard::nomeArquivoEmRecebimento)){ |
brunofgc | 0:1c0a769988ee | 179 | /*sprintf(diversos::msg,"File Received checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 180 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 181 | sprintf(msg,"File Received checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 182 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 183 | pc.printf("File Received checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 184 | |
brunofgc | 0:1c0a769988ee | 185 | if(strstr(sdCard::nomeArquivoEmRecebimento,"devices.cfg")!=NULL){ |
brunofgc | 10:263c093f8977 | 186 | sdCard::deleteBanks(2); |
brunofgc | 0:1c0a769988ee | 187 | pc.printf("Resetando o drome.\r\n"); |
brunofgc | 0:1c0a769988ee | 188 | hardwareReset=true; |
brunofgc | 0:1c0a769988ee | 189 | } |
brunofgc | 0:1c0a769988ee | 190 | |
brunofgc | 0:1c0a769988ee | 191 | }else{ |
brunofgc | 0:1c0a769988ee | 192 | //modemCom::sendBufferCommandMode(2,"FileCorrupted.\r\n",strlen("FileCorrupted.\r\n")); |
brunofgc | 4:13ff9c81dc10 | 193 | sprintf(msg,"erro"); |
brunofgc | 0:1c0a769988ee | 194 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 195 | pc.printf("Recebido correto mas n foi possivel renomear arquivo.\r\n"); |
brunofgc | 0:1c0a769988ee | 196 | } |
brunofgc | 0:1c0a769988ee | 197 | |
brunofgc | 0:1c0a769988ee | 198 | }else{ |
brunofgc | 0:1c0a769988ee | 199 | /*sprintf(diversos::msg,"File Corrupted checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 200 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 6:d4ebbaaba295 | 201 | sprintf(msg,"erro",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 202 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 203 | pc.printf("File Corrupted checkSum16BIT_Lido <%lu>.\r\n",checkSum16BIT_Lido); |
brunofgc | 0:1c0a769988ee | 204 | } |
brunofgc | 0:1c0a769988ee | 205 | modemCom::status.timeOut=10; |
brunofgc | 9:cf406384efd9 | 206 | modemCom::timeOutModem = 50; |
brunofgc | 9:cf406384efd9 | 207 | modemCom::status.timeOut = 1; |
brunofgc | 0:1c0a769988ee | 208 | } |
brunofgc | 0:1c0a769988ee | 209 | |
brunofgc | 0:1c0a769988ee | 210 | /*ptr=strstr(ptrComando,"requestFile,"); |
brunofgc | 0:1c0a769988ee | 211 | if(ptr){ |
brunofgc | 0:1c0a769988ee | 212 | FILE *fp; |
brunofgc | 0:1c0a769988ee | 213 | char c; |
brunofgc | 0:1c0a769988ee | 214 | strtok(ptr,","); |
brunofgc | 0:1c0a769988ee | 215 | pCharFile = strtok(NULL,";"); |
brunofgc | 0:1c0a769988ee | 216 | |
brunofgc | 0:1c0a769988ee | 217 | fp = fopen(pCharFile,"r"); |
brunofgc | 0:1c0a769988ee | 218 | if(fp!=NULL){ |
brunofgc | 0:1c0a769988ee | 219 | do{ |
brunofgc | 0:1c0a769988ee | 220 | c = fgetc(fp); |
brunofgc | 0:1c0a769988ee | 221 | if(!feof(fp)){modem.printf("%c",c);} |
brunofgc | 0:1c0a769988ee | 222 | modemCom::status.timeOut=10; |
brunofgc | 0:1c0a769988ee | 223 | }while(!feof(fp)); |
brunofgc | 0:1c0a769988ee | 224 | fclose(fp); |
brunofgc | 0:1c0a769988ee | 225 | }else{ |
brunofgc | 0:1c0a769988ee | 226 | modem.printf("Nao foi possivel abrir o arquivo.\n"); |
brunofgc | 0:1c0a769988ee | 227 | } |
brunofgc | 0:1c0a769988ee | 228 | }*/ |
brunofgc | 0:1c0a769988ee | 229 | |
brunofgc | 1:0e0967c88590 | 230 | ptr=strstr(ptrComando,"execAct:"); |
brunofgc | 1:0e0967c88590 | 231 | if(ptr){ |
brunofgc | 1:0e0967c88590 | 232 | char *pChar; |
brunofgc | 1:0e0967c88590 | 233 | uint32_t actId; |
brunofgc | 1:0e0967c88590 | 234 | pChar = strtok(ptr,":"); |
brunofgc | 1:0e0967c88590 | 235 | pChar = strtok(NULL,""); |
brunofgc | 1:0e0967c88590 | 236 | actId = atoi(pChar); |
brunofgc | 1:0e0967c88590 | 237 | if(dispositivos[0]->execAct(actId)){ |
brunofgc | 1:0e0967c88590 | 238 | sprintf(msg,"execAct_ack"); |
brunofgc | 1:0e0967c88590 | 239 | }else{ |
brunofgc | 1:0e0967c88590 | 240 | sprintf(msg,"execAct_nack"); |
brunofgc | 1:0e0967c88590 | 241 | } |
brunofgc | 1:0e0967c88590 | 242 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 7:ae9c47f62946 | 243 | } |
brunofgc | 1:0e0967c88590 | 244 | |
brunofgc | 2:55b7b466e742 | 245 | if(strstr(ptr,"enviaDadosAoServer")){ |
brunofgc | 2:55b7b466e742 | 246 | eventosRTC::rotina15Minutos=true; |
brunofgc | 2:55b7b466e742 | 247 | sprintf(msg,"ack"); |
brunofgc | 2:55b7b466e742 | 248 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 2:55b7b466e742 | 249 | } |
brunofgc | 2:55b7b466e742 | 250 | |
brunofgc | 0:1c0a769988ee | 251 | ptr=strstr(ptrComando,"mbwrite:"); |
brunofgc | 0:1c0a769988ee | 252 | if(ptr){ |
brunofgc | 0:1c0a769988ee | 253 | uint8_t addr; |
brunofgc | 0:1c0a769988ee | 254 | uint16_t reg; |
brunofgc | 0:1c0a769988ee | 255 | uint32_t auxMod; |
brunofgc | 0:1c0a769988ee | 256 | char *pChar; |
brunofgc | 0:1c0a769988ee | 257 | |
brunofgc | 1:0e0967c88590 | 258 | //IR |
brunofgc | 1:0e0967c88590 | 259 | if(strstr(ptr,"IR,")){ |
brunofgc | 1:0e0967c88590 | 260 | uint8_t freq=38,porta=0; |
brunofgc | 1:0e0967c88590 | 261 | pChar = strtok(ptr,","); |
brunofgc | 1:0e0967c88590 | 262 | porta = atoi(strtok(NULL,",")); |
brunofgc | 1:0e0967c88590 | 263 | porta--; //Trazendo para índice 0; |
brunofgc | 1:0e0967c88590 | 264 | |
brunofgc | 1:0e0967c88590 | 265 | freq = atoi(strtok(NULL,",")); |
brunofgc | 1:0e0967c88590 | 266 | if(freq == 0){ |
brunofgc | 1:0e0967c88590 | 267 | //Se não tiver frequência, ponha 38. |
brunofgc | 1:0e0967c88590 | 268 | freq = 38; |
brunofgc | 1:0e0967c88590 | 269 | } |
brunofgc | 1:0e0967c88590 | 270 | |
brunofgc | 7:ae9c47f62946 | 271 | pChar = strtok(NULL,"S"); |
brunofgc | 5:7801f913384e | 272 | deserializaPacoteIR(pChar); |
brunofgc | 1:0e0967c88590 | 273 | enviaComandoIR(freq,porta); |
brunofgc | 1:0e0967c88590 | 274 | sprintf(msg,"WriteIR_ack"); |
brunofgc | 1:0e0967c88590 | 275 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 1:0e0967c88590 | 276 | } |
brunofgc | 1:0e0967c88590 | 277 | |
brunofgc | 0:1c0a769988ee | 278 | //Float |
brunofgc | 0:1c0a769988ee | 279 | if(strstr(ptr,"float,")){ |
brunofgc | 0:1c0a769988ee | 280 | float v_float; |
brunofgc | 0:1c0a769988ee | 281 | |
brunofgc | 0:1c0a769988ee | 282 | //Capturando parametros comuns |
brunofgc | 0:1c0a769988ee | 283 | pChar = strtok(ptr,","); |
brunofgc | 0:1c0a769988ee | 284 | |
brunofgc | 0:1c0a769988ee | 285 | //Parametro 1 |
brunofgc | 0:1c0a769988ee | 286 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 287 | addr = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 288 | |
brunofgc | 0:1c0a769988ee | 289 | //Parametro 2 |
brunofgc | 0:1c0a769988ee | 290 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 291 | reg = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 292 | |
brunofgc | 0:1c0a769988ee | 293 | //Parametro 3 |
brunofgc | 0:1c0a769988ee | 294 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 295 | //v_float = diversos::stringToFloat(pChar,50); |
brunofgc | 0:1c0a769988ee | 296 | v_float = atof(pChar); |
brunofgc | 1:0e0967c88590 | 297 | |
brunofgc | 1:0e0967c88590 | 298 | //Envio o dado via Modbus |
brunofgc | 1:0e0967c88590 | 299 | if(!modBusMaster1::writeFloat(addr,reg,1,&v_float)){ |
brunofgc | 1:0e0967c88590 | 300 | pc.printf("Valor <%f> escrito no registrador %lu do endereco modbus %u.\n",v_float,reg,addr); |
brunofgc | 0:1c0a769988ee | 301 | /*sprintf(diversos::msg,"WriteFloat_ack"); |
brunofgc | 0:1c0a769988ee | 302 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 303 | |
brunofgc | 1:0e0967c88590 | 304 | sprintf(msg,"WriteFloat_ack"); |
brunofgc | 1:0e0967c88590 | 305 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 1:0e0967c88590 | 306 | }else{ |
brunofgc | 1:0e0967c88590 | 307 | pc.printf("Erro ao escrever valor <%f> no registrador %lu do endereco modbus %u.\n",v_float,reg,addr); |
brunofgc | 1:0e0967c88590 | 308 | /*sprintf(diversos::msg,"WriteFloat_nack"); |
brunofgc | 1:0e0967c88590 | 309 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 1:0e0967c88590 | 310 | |
brunofgc | 1:0e0967c88590 | 311 | sprintf(msg,"WriteFloat_nack"); |
brunofgc | 0:1c0a769988ee | 312 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 1:0e0967c88590 | 313 | } |
brunofgc | 1:0e0967c88590 | 314 | } |
brunofgc | 1:0e0967c88590 | 315 | |
brunofgc | 1:0e0967c88590 | 316 | //PWM |
brunofgc | 1:0e0967c88590 | 317 | if(strstr(ptr,"PWM,")){ |
brunofgc | 1:0e0967c88590 | 318 | float v_float; |
brunofgc | 1:0e0967c88590 | 319 | |
brunofgc | 1:0e0967c88590 | 320 | //Capturando parametros comuns |
brunofgc | 1:0e0967c88590 | 321 | pChar = strtok(ptr,","); |
brunofgc | 1:0e0967c88590 | 322 | |
brunofgc | 1:0e0967c88590 | 323 | //Parametro 1 |
brunofgc | 1:0e0967c88590 | 324 | pChar = strtok(NULL,","); |
brunofgc | 1:0e0967c88590 | 325 | addr = atoi(pChar); |
brunofgc | 1:0e0967c88590 | 326 | |
brunofgc | 1:0e0967c88590 | 327 | //Parametro 2 |
brunofgc | 1:0e0967c88590 | 328 | pChar = strtok(NULL,","); |
brunofgc | 1:0e0967c88590 | 329 | reg = atoi(pChar); |
brunofgc | 1:0e0967c88590 | 330 | |
brunofgc | 1:0e0967c88590 | 331 | //Parametro 3 |
brunofgc | 1:0e0967c88590 | 332 | pChar = strtok(NULL,","); |
brunofgc | 1:0e0967c88590 | 333 | //v_float = diversos::stringToFloat(pChar,50); |
brunofgc | 1:0e0967c88590 | 334 | v_float = atof(pChar); |
brunofgc | 1:0e0967c88590 | 335 | |
brunofgc | 1:0e0967c88590 | 336 | pc.printf("\r\n\r\nVerificando conteudo de pChar antes de converter para float <%s>.\r\n",pChar); |
brunofgc | 1:0e0967c88590 | 337 | |
brunofgc | 1:0e0967c88590 | 338 | //Retirando a parte inteira referente ao periodo em us do pwm |
brunofgc | 1:0e0967c88590 | 339 | |
brunofgc | 1:0e0967c88590 | 340 | auxMod = v_float/10; |
brunofgc | 1:0e0967c88590 | 341 | v_float = v_float-(auxMod*10); |
brunofgc | 1:0e0967c88590 | 342 | SD1.period_us(auxMod); |
brunofgc | 1:0e0967c88590 | 343 | pwmPeriod = auxMod; |
brunofgc | 1:0e0967c88590 | 344 | switch(reg){ |
brunofgc | 1:0e0967c88590 | 345 | case 0: |
brunofgc | 1:0e0967c88590 | 346 | SD1.write(v_float); |
brunofgc | 1:0e0967c88590 | 347 | break; |
brunofgc | 1:0e0967c88590 | 348 | case 1: |
brunofgc | 1:0e0967c88590 | 349 | SD2.write(v_float); |
brunofgc | 1:0e0967c88590 | 350 | break; |
brunofgc | 1:0e0967c88590 | 351 | case 2: |
brunofgc | 1:0e0967c88590 | 352 | SD3.write(v_float); |
brunofgc | 1:0e0967c88590 | 353 | break; |
brunofgc | 1:0e0967c88590 | 354 | case 3: |
brunofgc | 1:0e0967c88590 | 355 | SD4.write(v_float); |
brunofgc | 1:0e0967c88590 | 356 | break; |
brunofgc | 1:0e0967c88590 | 357 | case 4: |
brunofgc | 1:0e0967c88590 | 358 | SD5.write(v_float); |
brunofgc | 1:0e0967c88590 | 359 | break; |
brunofgc | 1:0e0967c88590 | 360 | case 5: |
brunofgc | 1:0e0967c88590 | 361 | SD6.write(v_float); |
brunofgc | 1:0e0967c88590 | 362 | break; |
brunofgc | 0:1c0a769988ee | 363 | } |
brunofgc | 1:0e0967c88590 | 364 | pc.printf("Valor puro %f, %lu escrito como periodo e %f como duty no reg %u.\n",v_float,auxMod,v_float,reg); |
brunofgc | 1:0e0967c88590 | 365 | /*sprintf(diversos::msg,"WriteFloat_ack"); |
brunofgc | 1:0e0967c88590 | 366 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 1:0e0967c88590 | 367 | |
brunofgc | 1:0e0967c88590 | 368 | sprintf(msg,"WritePWM_ack"); |
brunofgc | 1:0e0967c88590 | 369 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 370 | } |
brunofgc | 0:1c0a769988ee | 371 | |
brunofgc | 0:1c0a769988ee | 372 | //uint32_t |
brunofgc | 0:1c0a769988ee | 373 | if(strstr(ptr,"uint32_t,")){ |
brunofgc | 0:1c0a769988ee | 374 | uint32_t v_uint32_t; |
brunofgc | 0:1c0a769988ee | 375 | |
brunofgc | 0:1c0a769988ee | 376 | //Capturando parametros comuns |
brunofgc | 0:1c0a769988ee | 377 | pChar = strtok(ptr,","); |
brunofgc | 0:1c0a769988ee | 378 | |
brunofgc | 0:1c0a769988ee | 379 | //Parametro 1 |
brunofgc | 0:1c0a769988ee | 380 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 381 | addr = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 382 | |
brunofgc | 0:1c0a769988ee | 383 | //Parametro 2 |
brunofgc | 0:1c0a769988ee | 384 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 385 | reg = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 386 | |
brunofgc | 0:1c0a769988ee | 387 | //Parametro 3 |
brunofgc | 0:1c0a769988ee | 388 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 389 | v_uint32_t = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 390 | |
brunofgc | 0:1c0a769988ee | 391 | if(!modBusMaster1::writeRegister32BIT(addr,reg,1,&v_uint32_t)){ |
brunofgc | 0:1c0a769988ee | 392 | pc.printf("Valor <%lu> escrito no registrador %lu do endereco modbus %u.\n",v_uint32_t,reg,addr); |
brunofgc | 0:1c0a769988ee | 393 | /*sprintf(diversos::msg,"Write_uint32_t_ack"); |
brunofgc | 0:1c0a769988ee | 394 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 395 | |
brunofgc | 1:0e0967c88590 | 396 | sprintf(msg,"Write_uint32_t_ack"); |
brunofgc | 0:1c0a769988ee | 397 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 398 | }else{ |
brunofgc | 0:1c0a769988ee | 399 | pc.printf("Erro ao escrever valor <%lu> no registrador %lu do endereco modbus %u.\n",v_uint32_t,reg,addr); |
brunofgc | 0:1c0a769988ee | 400 | /*sprintf(diversos::msg,"Write_uint32_t_nack"); |
brunofgc | 0:1c0a769988ee | 401 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 402 | |
brunofgc | 1:0e0967c88590 | 403 | sprintf(msg,"Write_uint32_t_nack"); |
brunofgc | 0:1c0a769988ee | 404 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 405 | } |
brunofgc | 0:1c0a769988ee | 406 | } |
brunofgc | 0:1c0a769988ee | 407 | |
brunofgc | 0:1c0a769988ee | 408 | //uint16_t |
brunofgc | 0:1c0a769988ee | 409 | if(strstr(ptr,"uint16_t,")){ |
brunofgc | 0:1c0a769988ee | 410 | uint16_t v_uint16_t; |
brunofgc | 0:1c0a769988ee | 411 | |
brunofgc | 0:1c0a769988ee | 412 | //Capturando parametros comuns |
brunofgc | 0:1c0a769988ee | 413 | pChar = strtok(ptr,","); |
brunofgc | 0:1c0a769988ee | 414 | |
brunofgc | 0:1c0a769988ee | 415 | //Parametro 1 |
brunofgc | 0:1c0a769988ee | 416 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 417 | addr = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 418 | |
brunofgc | 0:1c0a769988ee | 419 | //Parametro 2 |
brunofgc | 0:1c0a769988ee | 420 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 421 | reg = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 422 | |
brunofgc | 0:1c0a769988ee | 423 | //Parametro 3 |
brunofgc | 0:1c0a769988ee | 424 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 425 | v_uint16_t = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 426 | |
brunofgc | 0:1c0a769988ee | 427 | if(!modBusMaster1::writeRegister16BIT(addr,reg,1,&v_uint16_t)){ |
brunofgc | 0:1c0a769988ee | 428 | pc.printf("Valor <%lu> escrito no registrador %lu do endereco modbus %u.\n",v_uint16_t,reg,addr); |
brunofgc | 0:1c0a769988ee | 429 | /*sprintf(diversos::msg,"Write_uint16_t_ack"); |
brunofgc | 0:1c0a769988ee | 430 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 431 | |
brunofgc | 1:0e0967c88590 | 432 | sprintf(msg,"Write_uint16_t_ack"); |
brunofgc | 0:1c0a769988ee | 433 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 434 | }else{ |
brunofgc | 0:1c0a769988ee | 435 | pc.printf("Erro ao escrever valor <%lu> no registrador %lu do endereco modbus %u.\n",v_uint16_t,reg,addr); |
brunofgc | 0:1c0a769988ee | 436 | /*sprintf(diversos::msg,"Write_uint16_t_nack"); |
brunofgc | 0:1c0a769988ee | 437 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg)); */ |
brunofgc | 0:1c0a769988ee | 438 | |
brunofgc | 1:0e0967c88590 | 439 | sprintf(msg,"Write_uint16_t_nack"); |
brunofgc | 0:1c0a769988ee | 440 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 441 | } |
brunofgc | 0:1c0a769988ee | 442 | } |
brunofgc | 0:1c0a769988ee | 443 | |
brunofgc | 0:1c0a769988ee | 444 | //bit |
brunofgc | 0:1c0a769988ee | 445 | if(strstr(ptr,"bit,")){ |
brunofgc | 0:1c0a769988ee | 446 | bool v_bool; |
brunofgc | 0:1c0a769988ee | 447 | |
brunofgc | 0:1c0a769988ee | 448 | //Capturando parametros comuns |
brunofgc | 0:1c0a769988ee | 449 | pChar = strtok(ptr,","); |
brunofgc | 0:1c0a769988ee | 450 | |
brunofgc | 0:1c0a769988ee | 451 | //Parametro 1 |
brunofgc | 0:1c0a769988ee | 452 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 453 | addr = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 454 | |
brunofgc | 0:1c0a769988ee | 455 | //Parametro 2 |
brunofgc | 0:1c0a769988ee | 456 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 457 | reg = atoi(pChar); |
brunofgc | 0:1c0a769988ee | 458 | |
brunofgc | 0:1c0a769988ee | 459 | //Parametro 3 |
brunofgc | 0:1c0a769988ee | 460 | pChar = strtok(NULL,","); |
brunofgc | 0:1c0a769988ee | 461 | v_bool = (atoi(pChar) > 0); |
brunofgc | 0:1c0a769988ee | 462 | |
brunofgc | 5:7801f913384e | 463 | if(addr != enderecoControladoraVirtual){ |
brunofgc | 0:1c0a769988ee | 464 | //writeSingleCoil(uint8_t,uint16_t,bool); //Endereço slave, registrador, bool |
brunofgc | 0:1c0a769988ee | 465 | if(!modBusMaster1::writeSingleCoil(addr,reg,v_bool)){ |
brunofgc | 0:1c0a769988ee | 466 | pc.printf("Valor <%u> escrito no registrador %lu do endereco modbus %u.\n",v_bool,reg,addr); |
brunofgc | 0:1c0a769988ee | 467 | /*sprintf(diversos::msg,"Write_bool_ack"); |
brunofgc | 0:1c0a769988ee | 468 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 469 | |
brunofgc | 1:0e0967c88590 | 470 | sprintf(msg,"Write_bool_ack"); |
brunofgc | 0:1c0a769988ee | 471 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 472 | }else{ |
brunofgc | 0:1c0a769988ee | 473 | pc.printf("Erro ao escrever valor <%u> no registrador %lu do endereco modbus %u.\n",v_bool,reg,addr); |
brunofgc | 0:1c0a769988ee | 474 | /*sprintf(diversos::msg,"Write_bool_nack"); |
brunofgc | 0:1c0a769988ee | 475 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 476 | |
brunofgc | 1:0e0967c88590 | 477 | sprintf(msg,"Write_bool_nack"); |
brunofgc | 0:1c0a769988ee | 478 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 479 | } |
brunofgc | 0:1c0a769988ee | 480 | }else{ |
brunofgc | 0:1c0a769988ee | 481 | switch(reg){ |
brunofgc | 0:1c0a769988ee | 482 | case 0: |
brunofgc | 0:1c0a769988ee | 483 | SD1.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 484 | break; |
brunofgc | 0:1c0a769988ee | 485 | case 1: |
brunofgc | 0:1c0a769988ee | 486 | SD2.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 487 | break; |
brunofgc | 0:1c0a769988ee | 488 | case 2: |
brunofgc | 0:1c0a769988ee | 489 | SD3.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 490 | break; |
brunofgc | 0:1c0a769988ee | 491 | case 3: |
brunofgc | 0:1c0a769988ee | 492 | SD4.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 493 | break; |
brunofgc | 0:1c0a769988ee | 494 | case 4: |
brunofgc | 0:1c0a769988ee | 495 | SD5.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 496 | break; |
brunofgc | 0:1c0a769988ee | 497 | case 5: |
brunofgc | 0:1c0a769988ee | 498 | SD6.write(v_bool*1.0f); |
brunofgc | 0:1c0a769988ee | 499 | break; |
brunofgc | 0:1c0a769988ee | 500 | case 6: |
brunofgc | 0:1c0a769988ee | 501 | SD7 = v_bool; |
brunofgc | 0:1c0a769988ee | 502 | break; |
brunofgc | 0:1c0a769988ee | 503 | case 7: |
brunofgc | 0:1c0a769988ee | 504 | SD8 = v_bool; |
brunofgc | 0:1c0a769988ee | 505 | break; |
brunofgc | 0:1c0a769988ee | 506 | } |
brunofgc | 0:1c0a769988ee | 507 | pc.printf("Valor <%u> escrito no registrador %lu do endereco modbus %u.\n",v_bool,reg,addr); |
brunofgc | 0:1c0a769988ee | 508 | /*sprintf(diversos::msg,"Write_bool_ack"); |
brunofgc | 0:1c0a769988ee | 509 | modemCom::sendBufferCommandMode(2,diversos::msg,strlen(diversos::msg));*/ |
brunofgc | 0:1c0a769988ee | 510 | |
brunofgc | 1:0e0967c88590 | 511 | sprintf(msg,"Write_bool_ack"); |
brunofgc | 0:1c0a769988ee | 512 | modemCom::sendBufferCommandMode(idConnection,msg,strlen(msg)); |
brunofgc | 0:1c0a769988ee | 513 | } |
brunofgc | 0:1c0a769988ee | 514 | } |
brunofgc | 9:cf406384efd9 | 515 | modemCom::status.timeOut=5; |
brunofgc | 0:1c0a769988ee | 516 | } |
brunofgc | 0:1c0a769988ee | 517 | |
brunofgc | 0:1c0a769988ee | 518 | currentCur = strlen(ptrComando) + 1; //Aqui eu pego onde devo buscar novamente mais algum comando. |
brunofgc | 0:1c0a769988ee | 519 | if(currentCur>=totalLengthCommands){ |
brunofgc | 0:1c0a769988ee | 520 | //modemCom::status.timeOut=10; |
brunofgc | 0:1c0a769988ee | 521 | ptrComando = NULL; |
brunofgc | 0:1c0a769988ee | 522 | }else{ |
brunofgc | 0:1c0a769988ee | 523 | ptrComando = strtok(&ptrComando[currentCur],";"); |
brunofgc | 0:1c0a769988ee | 524 | } |
brunofgc | 0:1c0a769988ee | 525 | }while(ptrComando != NULL);//Este loop executa cada instrução. |
brunofgc | 0:1c0a769988ee | 526 | pc.printf("Atendidos todos os comandos.\n"); |
brunofgc | 0:1c0a769988ee | 527 | } |