teste de publish
Diff: devices.cpp
- Revision:
- 1:3f9e5b86d9e4
- Parent:
- 0:8c59a494a451
- Child:
- 2:ca3723da6668
--- a/devices.cpp Fri Mar 24 15:54:14 2017 +0000 +++ b/devices.cpp Thu Mar 30 19:53:22 2017 +0000 @@ -8,6 +8,8 @@ estadoConfiguracao_scheduleSet }; +/* +Antigo uint8_t resizeArray(uint32_t atual, uint32_t novo, char **array,uint16_t sizeUnit) { uint32_t i; @@ -31,6 +33,28 @@ *array = p; return 1; } +*/ + +uint8_t resizeArray(uint32_t atual, uint32_t novo, char **array,uint16_t sizeUnit) +{ + uint32_t i; + char *p = (char *) malloc(sizeUnit*novo); + + if(p==NULL) { + return 0; + } + + if(atual) { + //So sigo em frente se tinha algo dentro deste array + for(i=0; i<(novo*sizeUnit); i++) { + p[i]=*array[i]; + } + free(*array); + } + + *array = p; + return 1; +} void writeReadingsToSD(){ int i; @@ -119,6 +143,12 @@ } } + + + + + +//Funcao restaurada void device::verifyAlarms(){ //Inicio da verificação uint16_t alarmIndex; @@ -136,8 +166,7 @@ //Montar float! this->floatToBin(0,&value_float,&this->alarms[alarmIndex].value[0]); - if(this->alarms[alarmIndex].addrModbusRead != 254){ - //Bloco de leitura da variável modBus; + if(this->alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; modBusMaster1::readFloat( this->alarms[alarmIndex].addrModbusRead, this->alarms[alarmIndex].funcModbusRead, @@ -146,7 +175,7 @@ &leitura_float ); }else{ - leitura_float = aiFiltrada[this->alarms[alarmIndex].regModbusRead]; + leitura_float = temperaturas[this->alarms[alarmIndex].regModbusRead]; } if(this->alarms[alarmIndex].max!=0){ @@ -255,7 +284,7 @@ //Montar uint16_t! this->uint16_t_ToBin(0,&value_uint16_t,&this->alarms[alarmIndex].value[0]); - if(this->alarms[alarmIndex].addrModbusRead!=255){ + if(this->alarms[alarmIndex].addrModbusRead!=enderecoControladoraVirtual){ //Bloco de leitura da variável modBus; modBusMaster1::readRegister16BIT( this->alarms[alarmIndex].addrModbusRead, @@ -320,17 +349,13 @@ //O valor de alarm para bit é o value[0]!!! value_bit = (this->alarms[alarmIndex].value[0] > 0); //Qualquer valor maior que zero da saída 1 - if(this->alarms[alarmIndex].addrModbusRead != 254){ - //Bloco de leitura da variável modBus; - modBusMaster1::readCoils( - this->alarms[alarmIndex].addrModbusRead, - this->alarms[alarmIndex].regModbusRead, - 1, - &leitura_bit - ); - }else{ - leitura_bit = entradasDigitais[this->alarms[alarmIndex].regModbusRead]; - } + //Bloco de leitura da variável modBus; + modBusMaster1::readCoils( + this->alarms[alarmIndex].addrModbusRead, + this->alarms[alarmIndex].regModbusRead, + 1, + &leitura_bit + ); if(leitura_bit == value_bit){ this->alarms[alarmIndex].secAlarm++; @@ -403,16 +428,11 @@ } //atrasa o envio em 3 segundos para que possa ser gravado o registro do alarme. enviaDadosPorAlarme=10; - } - - /*printf("\n\nAlarme \"%s\".\n",this->alarms[alarmIndex].name); - printf("\nsecAlarme <%lu>.\n",this->alarms[alarmIndex].secAlarm); - printf("\nthis->alarms[alarmIndex].alarmFound <%lu>.\n",this->alarms[alarmIndex].alarmFound); - printf("\nalarmFound <%lu>.\n",alarmFound); - printf("\nmax <%lu>.\n",this->alarms[alarmIndex].max);*/ + } } } + void device::verifySchedules(){ char stringTime[15]; char *weekdayString; @@ -640,8 +660,7 @@ //printf("O tamanho da struct eh <%lu>.\n",sizeof(reading)); - if(resizeArray(this->numReadings, this->numReadings+1, (char **)&this->readings,sizeof(reading))) { - //printf("Resize em readings beleza!\n"); + if(resizeArray(this->numReadings, this->numReadings+1, (char **)&this->readings,sizeof(reading))) { this->numReadings++; } @@ -1013,7 +1032,8 @@ if(strstr(pTipoComando,"uint8_t")){tipoComando = modBusType_uint8_t;} if(strstr(pTipoComando,"uint16_t")){tipoComando = modBusType_uint16_t;} if(strstr(pTipoComando,"uint32_t")){tipoComando = modBusType_uint32_t;} - if(strstr(pTipoComando,"ir")){tipoComando = IrCommandType;} + if(strstr(pTipoComando,"IR")){tipoComando = IrCommandType;} + if(strstr(pTipoComando,"PWM")){tipoComando = PWMCommandType;} }else{ return false; } @@ -1028,40 +1048,14 @@ //floatValueToWrite = diversos::stringToFloat(strtok(NULL,","),100); floatValueToWrite = atof(strtok(NULL,",")); - if(param1 != 254){ - //Envio comando via Modbus - modBusMaster1::writeFloat( - param1, - param2, - param3, - &floatValueToWrite - ); - }else{ - //Executa comando interno; - auxMod = floatValueToWrite/10; - floatValueToWrite = floatValueToWrite-(auxMod*10); - SD1.period_us(auxMod); - switch(param3){ - case 0: - SD1.write(floatValueToWrite); - break; - case 1: - SD2.write(floatValueToWrite); - break; - case 2: - SD3.write(floatValueToWrite); - break; - case 3: - SD4.write(floatValueToWrite); - break; - case 4: - SD5.write(floatValueToWrite); - break; - case 5: - SD6.write(floatValueToWrite); - break; - } - } + + //Envio comando via Modbus + modBusMaster1::writeFloat( + param1, + param2, + param3, + &floatValueToWrite + ); break; case modBusType_uint32_t: param1 = atoi(strtok(NULL,",")); @@ -1143,8 +1137,45 @@ pTipoComando = strtok(NULL,"S");//Reaproveitando variável pTipoComando //printf("\r\nPacote IR <%s>.\r\n",pTipoComando); deserializaPacoteIR(pTipoComando); - enviaComandoIR(param1,param2); + enviaComandoIR(param1,param2); break; + + case PWMCommandType: + //float floatValuePWM; + //uint32_t auxPeriod; + param1 = atoi(strtok(NULL,",")); + param2 = atoi(strtok(NULL,",")); + param3 = atoi(strtok(NULL,",")); + //floatValueToWrite = diversos::stringToFloat(strtok(NULL,","),100); + floatValueToWrite = atof(strtok(NULL,",")); + + //Executa comando interno; + auxMod = floatValueToWrite/10; + floatValueToWrite = floatValueToWrite-(auxMod*10); + SD1.period_us(auxMod); + pwmPeriod = auxMod; + switch(param3){ + case 0: + SD1.write(floatValueToWrite); + break; + case 1: + SD2.write(floatValueToWrite); + break; + case 2: + SD3.write(floatValueToWrite); + break; + case 3: + SD4.write(floatValueToWrite); + break; + case 4: + SD5.write(floatValueToWrite); + break; + case 5: + SD6.write(floatValueToWrite); + break; + } + break; + default: //printf("comando nao encontrado\r\n"); break; @@ -1188,10 +1219,10 @@ for(readingIndex = 0;readingIndex < this->numReadings;readingIndex++){ switch(this->readings[readingIndex].type){ case modBusType_float: - if(this->readings[readingIndex].addr != 0){ + if(this->readings[readingIndex].addr != enderecoControladoraVirtual){ //Busca externa via Modbus floatBuffer = (float*) malloc(((this->readings[readingIndex].numRegs*sizeof(float))+1)); //Alocando mais um byte para status - ((char *)floatBuffer)[this->readings[readingIndex].numRegs*sizeof(float)]=modBusMaster1::readFloat( + modBusMaster1::readFloat( this->readings[readingIndex].addr, this->readings[readingIndex].func, this->readings[readingIndex].reg, @@ -1202,7 +1233,9 @@ //Busca Interna floatBuffer = (float*) malloc(sizeof(float)); //Populando floatBuffer com a entrada analógica - floatBuffer[0] = aiFiltrada[this->readings[readingIndex].reg]; + if(this->readings[readingIndex].reg < num_temperatureSensors){ + floatBuffer[0] = temperaturas[this->readings[readingIndex].reg]; + } } //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); @@ -1211,7 +1244,7 @@ break; case modBusType_uint32_t: uint32_tBuffer = (uint32_t*) malloc((this->readings[readingIndex].numRegs*sizeof(uint32_t))+1); //Alocando mais um byte para status - ((char *) uint32_tBuffer)[(this->readings[readingIndex].numRegs*sizeof(uint32_t))]=modBusMaster1::readRegister32BIT(//Ponho o status no ultimo byte + modBusMaster1::readRegister32BIT(//Ponho o status no ultimo byte this->readings[readingIndex].addr, this->readings[readingIndex].func, this->readings[readingIndex].reg, @@ -1224,9 +1257,9 @@ break; case modBusType_uint16_t: - if(this->readings[readingIndex].addr!=255){ + if(this->readings[readingIndex].addr!=enderecoControladoraVirtual){ uint16_tBuffer = (uint16_t*) malloc(((this->readings[readingIndex].numRegs*sizeof(uint16_t))+1));//Alocando mais um byte para status - ((char *) uint16_tBuffer)[this->readings[readingIndex].numRegs*sizeof(uint16_t)]=modBusMaster1::readRegister16BIT(// + modBusMaster1::readRegister16BIT(// this->readings[readingIndex].addr, this->readings[readingIndex].func, this->readings[readingIndex].reg, @@ -1244,9 +1277,9 @@ break; case modBusType_bit: - if(this->readings[readingIndex].addr != 0){ + if(this->readings[readingIndex].addr != enderecoControladoraVirtual){ boolBuffer = (bool*) malloc(((this->readings[readingIndex].numRegs*sizeof(bool))+1));//Alocando mais um byte para status - ((char *)boolBuffer)[this->readings[readingIndex].numRegs*sizeof(bool)]=modBusMaster1::readCoils( + modBusMaster1::readCoils( this->readings[readingIndex].addr, this->readings[readingIndex].reg, this->readings[readingIndex].numRegs,