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