teste de publish
devices.cpp@24:4d44626e5b78, 2018-06-08 (annotated)
- Committer:
- brunofgc
- Date:
- Fri Jun 08 22:12:18 2018 +0000
- Revision:
- 24:4d44626e5b78
- Parent:
- 23:d3ef6bb8412a
teste de publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brunofgc | 0:8c59a494a451 | 1 | #include "devices.h" |
brunofgc | 0:8c59a494a451 | 2 | enum { |
brunofgc | 0:8c59a494a451 | 3 | estadoConfiguracao_idle = 0, |
brunofgc | 0:8c59a494a451 | 4 | estadoConfiguracao_creatingDevice, |
brunofgc | 0:8c59a494a451 | 5 | estadoConfiguracao_alarmSet, |
brunofgc | 0:8c59a494a451 | 6 | estadoConfiguracao_readingsSet, |
brunofgc | 4:3f92979d1ffb | 7 | estadoConfiguracao_scheduleSet, |
brunofgc | 4:3f92979d1ffb | 8 | estadoConfiguracao_scheduleExceptionSet |
brunofgc | 0:8c59a494a451 | 9 | }; |
brunofgc | 0:8c59a494a451 | 10 | |
brunofgc | 19:d52176ff683d | 11 | uint8_t numAlarms; |
brunofgc | 19:d52176ff683d | 12 | uint8_t numSchedules; |
brunofgc | 19:d52176ff683d | 13 | uint8_t numScheduleExceptions; |
brunofgc | 19:d52176ff683d | 14 | uint16_t numReadings; |
brunofgc | 19:d52176ff683d | 15 | alarm alarms[maxAlarms] __attribute__ ((section("AHBSRAM0"))); |
brunofgc | 19:d52176ff683d | 16 | reading readings[maxReadings] __attribute__ ((section("AHBSRAM0"))); |
brunofgc | 19:d52176ff683d | 17 | schedule schedules[maxSchedules] __attribute__ ((section("AHBSRAM0"))); |
brunofgc | 19:d52176ff683d | 18 | scheduleException scheduleExceptions[maxSchedulesExceptions] __attribute__ ((section("AHBSRAM0"))); |
brunofgc | 1:3f9e5b86d9e4 | 19 | |
brunofgc | 19:d52176ff683d | 20 | void floatToBin(uint8_t dir, float *v,uint8_t *p) |
brunofgc | 0:8c59a494a451 | 21 | { |
brunofgc | 0:8c59a494a451 | 22 | //Variáveis locais |
brunofgc | 0:8c59a494a451 | 23 | union { |
brunofgc | 0:8c59a494a451 | 24 | char c[4]; |
brunofgc | 0:8c59a494a451 | 25 | float v; |
brunofgc | 0:8c59a494a451 | 26 | }u; |
brunofgc | 0:8c59a494a451 | 27 | int i; |
brunofgc | 0:8c59a494a451 | 28 | |
brunofgc | 0:8c59a494a451 | 29 | if(dir) { |
brunofgc | 0:8c59a494a451 | 30 | u.v = *v; |
brunofgc | 0:8c59a494a451 | 31 | for(i=0; i<4; i++) { |
brunofgc | 0:8c59a494a451 | 32 | p[i]=u.c[3-i]; |
brunofgc | 0:8c59a494a451 | 33 | } |
brunofgc | 0:8c59a494a451 | 34 | } else { |
brunofgc | 0:8c59a494a451 | 35 | for(i=0; i<4; i++) { |
brunofgc | 0:8c59a494a451 | 36 | u.c[3-i]=p[i]; |
brunofgc | 0:8c59a494a451 | 37 | } |
brunofgc | 0:8c59a494a451 | 38 | *v = u.v; |
brunofgc | 0:8c59a494a451 | 39 | } |
brunofgc | 0:8c59a494a451 | 40 | } |
brunofgc | 0:8c59a494a451 | 41 | |
brunofgc | 19:d52176ff683d | 42 | void uint32_t_ToBin(uint8_t dir, uint32_t *v,uint8_t *p) |
brunofgc | 0:8c59a494a451 | 43 | { |
brunofgc | 0:8c59a494a451 | 44 | //Variáveis locais |
brunofgc | 0:8c59a494a451 | 45 | union { |
brunofgc | 0:8c59a494a451 | 46 | char c[4]; |
brunofgc | 0:8c59a494a451 | 47 | uint32_t v; |
brunofgc | 0:8c59a494a451 | 48 | }u; |
brunofgc | 0:8c59a494a451 | 49 | int i; |
brunofgc | 0:8c59a494a451 | 50 | |
brunofgc | 0:8c59a494a451 | 51 | if(dir) { |
brunofgc | 0:8c59a494a451 | 52 | u.v = *v; |
brunofgc | 0:8c59a494a451 | 53 | for(i=0; i<4; i++) { |
brunofgc | 0:8c59a494a451 | 54 | p[i]=u.c[3-i]; |
brunofgc | 0:8c59a494a451 | 55 | } |
brunofgc | 0:8c59a494a451 | 56 | } else { |
brunofgc | 0:8c59a494a451 | 57 | for(i=0; i<4; i++) { |
brunofgc | 0:8c59a494a451 | 58 | u.c[3-i]=p[i]; |
brunofgc | 0:8c59a494a451 | 59 | } |
brunofgc | 0:8c59a494a451 | 60 | *v = u.v; |
brunofgc | 0:8c59a494a451 | 61 | } |
brunofgc | 0:8c59a494a451 | 62 | } |
brunofgc | 0:8c59a494a451 | 63 | |
brunofgc | 19:d52176ff683d | 64 | void uint16_t_ToBin(uint8_t dir, uint16_t *v,uint8_t *p) |
brunofgc | 0:8c59a494a451 | 65 | { |
brunofgc | 0:8c59a494a451 | 66 | //Variáveis locais |
brunofgc | 0:8c59a494a451 | 67 | union { |
brunofgc | 0:8c59a494a451 | 68 | char c[2]; |
brunofgc | 0:8c59a494a451 | 69 | uint32_t v; |
brunofgc | 0:8c59a494a451 | 70 | }u; |
brunofgc | 0:8c59a494a451 | 71 | int i; |
brunofgc | 0:8c59a494a451 | 72 | |
brunofgc | 0:8c59a494a451 | 73 | if(dir) { |
brunofgc | 0:8c59a494a451 | 74 | u.v = *v; |
brunofgc | 0:8c59a494a451 | 75 | for(i=0; i<2; i++) { |
brunofgc | 0:8c59a494a451 | 76 | p[i]=u.c[1-i]; |
brunofgc | 0:8c59a494a451 | 77 | } |
brunofgc | 0:8c59a494a451 | 78 | } else { |
brunofgc | 0:8c59a494a451 | 79 | for(i=0; i<2; i++) { |
brunofgc | 0:8c59a494a451 | 80 | u.c[1-i]=p[i]; |
brunofgc | 0:8c59a494a451 | 81 | } |
brunofgc | 0:8c59a494a451 | 82 | *v = u.v; |
brunofgc | 0:8c59a494a451 | 83 | } |
brunofgc | 0:8c59a494a451 | 84 | } |
brunofgc | 0:8c59a494a451 | 85 | |
brunofgc | 24:4d44626e5b78 | 86 | /*void mostraAlarmeEmExecucao(uint32_t indice){ |
brunofgc | 19:d52176ff683d | 87 | printf("Alarme %u:\n",indice); |
brunofgc | 19:d52176ff683d | 88 | printf("\tId <%lu>.\n",alarms[indice].id); |
brunofgc | 19:d52176ff683d | 89 | printf("\tSeconds <%lu>.\n",alarms[indice].seconds); |
brunofgc | 19:d52176ff683d | 90 | printf("\tAddr %u, func %u, reg %lu.\n",alarms[indice].addrModbusRead,alarms[indice].funcModbusRead,alarms[indice].regModbusRead); |
brunofgc | 19:d52176ff683d | 91 | if(alarms[indice].type==modBusType_float) { |
brunofgc | 19:d52176ff683d | 92 | float value; |
brunofgc | 19:d52176ff683d | 93 | floatToBin(0,&value,&alarms[indice].value[0]); |
brunofgc | 19:d52176ff683d | 94 | if(alarms[indice].max){ |
brunofgc | 24:4d44626e5b78 | 95 | printf("\tFloat max <%f>.\n",value); |
brunofgc | 19:d52176ff683d | 96 | } |
brunofgc | 24:4d44626e5b78 | 97 | else{printf("\tFloat min <%f>.\n",value);} |
brunofgc | 19:d52176ff683d | 98 | } |
brunofgc | 19:d52176ff683d | 99 | |
brunofgc | 19:d52176ff683d | 100 | if(alarms[indice].type==modBusType_uint32_t) { |
brunofgc | 19:d52176ff683d | 101 | uint32_t value; |
brunofgc | 19:d52176ff683d | 102 | uint32_t_ToBin(0,&value,&alarms[indice].value[0]); |
brunofgc | 19:d52176ff683d | 103 | if(alarms[indice].max){ |
brunofgc | 19:d52176ff683d | 104 | printf("\tuint32_t max <%lu>.\n",value); |
brunofgc | 19:d52176ff683d | 105 | } |
brunofgc | 19:d52176ff683d | 106 | else{printf("\tuint32_t min <%lu>.\n",value);} |
brunofgc | 19:d52176ff683d | 107 | } |
brunofgc | 19:d52176ff683d | 108 | |
brunofgc | 19:d52176ff683d | 109 | if(alarms[indice].type==modBusType_uint16_t) { |
brunofgc | 19:d52176ff683d | 110 | uint16_t value; |
brunofgc | 19:d52176ff683d | 111 | uint16_t_ToBin(0,&value,&alarms[indice].value[0]); |
brunofgc | 19:d52176ff683d | 112 | if(alarms[indice].max){ |
brunofgc | 19:d52176ff683d | 113 | printf("\tuint16_t max <%lu>.\n",value); |
brunofgc | 19:d52176ff683d | 114 | } |
brunofgc | 19:d52176ff683d | 115 | else{printf("\tuint16_t min <%lu>.\n",value);} |
brunofgc | 19:d52176ff683d | 116 | } |
brunofgc | 19:d52176ff683d | 117 | |
brunofgc | 19:d52176ff683d | 118 | if(alarms[indice].type==modBusType_int32_t) { |
brunofgc | 19:d52176ff683d | 119 | uint32_t value; |
brunofgc | 19:d52176ff683d | 120 | uint32_t_ToBin(0,&value,&alarms[indice].value[0]); |
brunofgc | 19:d52176ff683d | 121 | if(alarms[indice].max){ |
brunofgc | 19:d52176ff683d | 122 | printf("\tint32_t max <%ld>.\n",(int32_t)value); |
brunofgc | 19:d52176ff683d | 123 | } |
brunofgc | 19:d52176ff683d | 124 | else{printf("\tint32_t min <%ld>.\n",(int32_t)value);} |
brunofgc | 19:d52176ff683d | 125 | } |
brunofgc | 19:d52176ff683d | 126 | |
brunofgc | 19:d52176ff683d | 127 | if(alarms[indice].type==modBusType_int16_t) { |
brunofgc | 19:d52176ff683d | 128 | uint16_t value; |
brunofgc | 19:d52176ff683d | 129 | uint16_t_ToBin(0,&value,&alarms[indice].value[0]); |
brunofgc | 19:d52176ff683d | 130 | if(alarms[indice].max){ |
brunofgc | 19:d52176ff683d | 131 | printf("\tint16_t max <%ld>.\n",(int16_t)value); |
brunofgc | 19:d52176ff683d | 132 | } |
brunofgc | 19:d52176ff683d | 133 | else{printf("\tint16_t min <%ld>.\n",(int16_t)value);} |
brunofgc | 19:d52176ff683d | 134 | } |
brunofgc | 19:d52176ff683d | 135 | |
brunofgc | 19:d52176ff683d | 136 | if(alarms[indice].type==modBusType_bit) { |
brunofgc | 19:d52176ff683d | 137 | uint8_t value; |
brunofgc | 19:d52176ff683d | 138 | value=alarms[indice].value[0]; |
brunofgc | 19:d52176ff683d | 139 | if(alarms[indice].max){ |
brunofgc | 19:d52176ff683d | 140 | printf("\tBIT max <%lu>.\n",value); |
brunofgc | 19:d52176ff683d | 141 | } |
brunofgc | 19:d52176ff683d | 142 | else{printf("\tBIT min <%lu>.\n",value);} |
brunofgc | 19:d52176ff683d | 143 | } |
brunofgc | 19:d52176ff683d | 144 | |
brunofgc | 19:d52176ff683d | 145 | printf("\tact:%lu\n",alarms[indice].idAct); |
brunofgc | 24:4d44626e5b78 | 146 | }*/ |
brunofgc | 19:d52176ff683d | 147 | |
brunofgc | 1:3f9e5b86d9e4 | 148 | //Funcao restaurada |
brunofgc | 19:d52176ff683d | 149 | void verifyAlarms(){ |
brunofgc | 0:8c59a494a451 | 150 | //Inicio da verificação |
brunofgc | 0:8c59a494a451 | 151 | uint16_t alarmIndex; |
brunofgc | 0:8c59a494a451 | 152 | float leitura_float,value_float; |
brunofgc | 24:4d44626e5b78 | 153 | double leitura_double,value_double; |
brunofgc | 0:8c59a494a451 | 154 | uint32_t leitura_uint32_t,value_uint32_t; |
brunofgc | 0:8c59a494a451 | 155 | uint16_t leitura_uint16_t,value_uint16_t; |
brunofgc | 0:8c59a494a451 | 156 | bool leitura_bit,value_bit; |
brunofgc | 24:4d44626e5b78 | 157 | bool alarmFoundAntes; |
brunofgc | 0:8c59a494a451 | 158 | char aux[30]; |
brunofgc | 5:8efd609dea7d | 159 | |
brunofgc | 19:d52176ff683d | 160 | //for(alarmIndex = 0;alarmIndex < alarmes;alarmIndex++){ |
brunofgc | 19:d52176ff683d | 161 | for(alarmIndex = 0;alarmIndex < alarmes;alarmIndex++){ |
brunofgc | 22:e0d8c8fe26c6 | 162 | diversos::wdt.kick(180.0); |
brunofgc | 19:d52176ff683d | 163 | /*pc.printf("\r\nTeste Alarm, id<%lu>.\r\n",alarms[alarmIndex].id); |
brunofgc | 19:d52176ff683d | 164 | pc.printf("ON<%lu>.\r\n",alarms[alarmIndex].on); |
brunofgc | 19:d52176ff683d | 165 | pc.printf("secAlarm<%lu>.\r\n",alarms[alarmIndex].secAlarm); |
brunofgc | 19:d52176ff683d | 166 | pc.printf("alarmFound<%lu>.\r\n",alarms[alarmIndex].alarmFound);*/ |
brunofgc | 24:4d44626e5b78 | 167 | //if((alarms[alarmIndex].on)&&(alarms[alarmIndex].id<100000000)){ |
brunofgc | 19:d52176ff683d | 168 | if(alarms[alarmIndex].on){ |
brunofgc | 24:4d44626e5b78 | 169 | alarmFoundAntes = alarms[alarmIndex].alarmFound; |
brunofgc | 24:4d44626e5b78 | 170 | //if(debug){mostraAlarmeEmExecucao(alarmIndex);} |
brunofgc | 19:d52176ff683d | 171 | switch(alarms[alarmIndex].type){ |
brunofgc | 5:8efd609dea7d | 172 | case modBusType_float: |
brunofgc | 5:8efd609dea7d | 173 | //Montar float! |
brunofgc | 19:d52176ff683d | 174 | floatToBin(0,&value_float,&alarms[alarmIndex].value[0]); |
brunofgc | 24:4d44626e5b78 | 175 | value_double = (double)value_float; |
brunofgc | 19:d52176ff683d | 176 | if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; |
brunofgc | 5:8efd609dea7d | 177 | modBusMaster1::readFloat( |
brunofgc | 19:d52176ff683d | 178 | alarms[alarmIndex].addrModbusRead, |
brunofgc | 19:d52176ff683d | 179 | alarms[alarmIndex].funcModbusRead, |
brunofgc | 19:d52176ff683d | 180 | alarms[alarmIndex].regModbusRead, |
brunofgc | 5:8efd609dea7d | 181 | 1, |
brunofgc | 5:8efd609dea7d | 182 | &leitura_float |
brunofgc | 5:8efd609dea7d | 183 | ); |
brunofgc | 5:8efd609dea7d | 184 | }else{ |
brunofgc | 19:d52176ff683d | 185 | //leitura_float = temperaturas[alarms[alarmIndex].regModbusRead]; |
brunofgc | 5:8efd609dea7d | 186 | //Busca Interna |
brunofgc | 19:d52176ff683d | 187 | if(alarms[alarmIndex].regModbusRead<100){//Registradores das entradas de temperatura |
brunofgc | 5:8efd609dea7d | 188 | //Populando floatBuffer com a temperaturas |
brunofgc | 19:d52176ff683d | 189 | if(alarms[alarmIndex].regModbusRead < num_temperatureSensors){ |
brunofgc | 24:4d44626e5b78 | 190 | leitura_float = temperaturas[alarms[alarmIndex].regModbusRead]; |
brunofgc | 5:8efd609dea7d | 191 | } |
brunofgc | 5:8efd609dea7d | 192 | } |
brunofgc | 19:d52176ff683d | 193 | if((alarms[alarmIndex].regModbusRead>99)&&(alarms[alarmIndex].regModbusRead<104)){//Registradores das entradas analógicas |
brunofgc | 19:d52176ff683d | 194 | leitura_float = aiFiltrada[alarms[alarmIndex].regModbusRead - 100]; |
brunofgc | 5:8efd609dea7d | 195 | } |
brunofgc | 24:4d44626e5b78 | 196 | } |
brunofgc | 24:4d44626e5b78 | 197 | leitura_double = (double)leitura_float; |
brunofgc | 5:8efd609dea7d | 198 | break; |
brunofgc | 5:8efd609dea7d | 199 | case modBusType_uint32_t: |
brunofgc | 5:8efd609dea7d | 200 | //Montar uint32_t! |
brunofgc | 19:d52176ff683d | 201 | uint32_t_ToBin(0,&value_uint32_t,&alarms[alarmIndex].value[0]); |
brunofgc | 24:4d44626e5b78 | 202 | //value_float = (float)value_uint32_t; |
brunofgc | 24:4d44626e5b78 | 203 | value_double = (double)value_uint32_t; |
brunofgc | 19:d52176ff683d | 204 | if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; |
brunofgc | 5:8efd609dea7d | 205 | //Bloco de leitura da variável modBus; |
brunofgc | 5:8efd609dea7d | 206 | modBusMaster1::readRegister32BIT( |
brunofgc | 19:d52176ff683d | 207 | alarms[alarmIndex].addrModbusRead, |
brunofgc | 19:d52176ff683d | 208 | alarms[alarmIndex].funcModbusRead, |
brunofgc | 19:d52176ff683d | 209 | alarms[alarmIndex].regModbusRead, |
brunofgc | 5:8efd609dea7d | 210 | 1, |
brunofgc | 5:8efd609dea7d | 211 | &leitura_uint32_t |
brunofgc | 5:8efd609dea7d | 212 | ); |
brunofgc | 5:8efd609dea7d | 213 | }else{ |
brunofgc | 19:d52176ff683d | 214 | leitura_uint32_t = pulsosEDs[alarms[alarmIndex].regModbusRead]; |
brunofgc | 24:4d44626e5b78 | 215 | } |
brunofgc | 24:4d44626e5b78 | 216 | leitura_float = (float)leitura_uint32_t; |
brunofgc | 5:8efd609dea7d | 217 | break; |
brunofgc | 0:8c59a494a451 | 218 | |
brunofgc | 5:8efd609dea7d | 219 | case modBusType_uint16_t: |
brunofgc | 5:8efd609dea7d | 220 | //Montar uint16_t! |
brunofgc | 19:d52176ff683d | 221 | uint16_t_ToBin(0,&value_uint16_t,&alarms[alarmIndex].value[0]); |
brunofgc | 24:4d44626e5b78 | 222 | //value_float = (float)value_uint16_t; |
brunofgc | 24:4d44626e5b78 | 223 | value_double = (double)value_uint16_t; |
brunofgc | 19:d52176ff683d | 224 | if(alarms[alarmIndex].addrModbusRead!=enderecoControladoraVirtual){ |
brunofgc | 5:8efd609dea7d | 225 | //Bloco de leitura da variável modBus; |
brunofgc | 5:8efd609dea7d | 226 | modBusMaster1::readRegister16BIT( |
brunofgc | 19:d52176ff683d | 227 | alarms[alarmIndex].addrModbusRead, |
brunofgc | 19:d52176ff683d | 228 | alarms[alarmIndex].funcModbusRead, |
brunofgc | 19:d52176ff683d | 229 | alarms[alarmIndex].regModbusRead, |
brunofgc | 5:8efd609dea7d | 230 | 1, |
brunofgc | 5:8efd609dea7d | 231 | &leitura_uint16_t |
brunofgc | 5:8efd609dea7d | 232 | ); |
brunofgc | 5:8efd609dea7d | 233 | }else{ |
brunofgc | 19:d52176ff683d | 234 | if(alarms[alarmIndex].regModbusRead<4){ |
brunofgc | 19:d52176ff683d | 235 | leitura_uint16_t = aiFiltrada[alarms[alarmIndex].regModbusRead]; |
brunofgc | 5:8efd609dea7d | 236 | } |
brunofgc | 5:8efd609dea7d | 237 | } |
brunofgc | 5:8efd609dea7d | 238 | |
brunofgc | 24:4d44626e5b78 | 239 | //leitura_float = (float)leitura_uint16_t; |
brunofgc | 24:4d44626e5b78 | 240 | leitura_double = (double)leitura_uint16_t; |
brunofgc | 5:8efd609dea7d | 241 | break; |
brunofgc | 5:8efd609dea7d | 242 | |
brunofgc | 17:c046e4b07ded | 243 | case modBusType_int32_t: |
brunofgc | 17:c046e4b07ded | 244 | //Montar uint32_t! |
brunofgc | 19:d52176ff683d | 245 | uint32_t_ToBin(0,&value_uint32_t,&alarms[alarmIndex].value[0]); |
brunofgc | 24:4d44626e5b78 | 246 | //value_float = (float)((int32_t)value_uint32_t); |
brunofgc | 24:4d44626e5b78 | 247 | value_double = (double)((int32_t)value_uint32_t); |
brunofgc | 17:c046e4b07ded | 248 | |
brunofgc | 19:d52176ff683d | 249 | if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; |
brunofgc | 17:c046e4b07ded | 250 | //Bloco de leitura da variável modBus; |
brunofgc | 17:c046e4b07ded | 251 | modBusMaster1::readRegister32BIT( |
brunofgc | 19:d52176ff683d | 252 | alarms[alarmIndex].addrModbusRead, |
brunofgc | 19:d52176ff683d | 253 | alarms[alarmIndex].funcModbusRead, |
brunofgc | 19:d52176ff683d | 254 | alarms[alarmIndex].regModbusRead, |
brunofgc | 17:c046e4b07ded | 255 | 1, |
brunofgc | 17:c046e4b07ded | 256 | &leitura_uint32_t |
brunofgc | 17:c046e4b07ded | 257 | ); |
brunofgc | 17:c046e4b07ded | 258 | }else{ |
brunofgc | 19:d52176ff683d | 259 | leitura_uint32_t = pulsosEDs[alarms[alarmIndex].regModbusRead]; |
brunofgc | 17:c046e4b07ded | 260 | } |
brunofgc | 17:c046e4b07ded | 261 | |
brunofgc | 24:4d44626e5b78 | 262 | //leitura_float = (float)((int32_t)leitura_uint32_t); |
brunofgc | 24:4d44626e5b78 | 263 | leitura_double = (double)((int32_t)leitura_uint32_t); |
brunofgc | 17:c046e4b07ded | 264 | break; |
brunofgc | 17:c046e4b07ded | 265 | |
brunofgc | 17:c046e4b07ded | 266 | case modBusType_int16_t: |
brunofgc | 17:c046e4b07ded | 267 | //Montar uint16_t! |
brunofgc | 19:d52176ff683d | 268 | uint16_t_ToBin(0,&value_uint16_t,&alarms[alarmIndex].value[0]); |
brunofgc | 24:4d44626e5b78 | 269 | //value_float = (float)((int16_t)value_uint16_t); |
brunofgc | 24:4d44626e5b78 | 270 | value_double = (double)((int16_t)value_uint16_t); |
brunofgc | 19:d52176ff683d | 271 | if(alarms[alarmIndex].addrModbusRead!=enderecoControladoraVirtual){ |
brunofgc | 17:c046e4b07ded | 272 | //Bloco de leitura da variável modBus; |
brunofgc | 17:c046e4b07ded | 273 | modBusMaster1::readRegister16BIT( |
brunofgc | 19:d52176ff683d | 274 | alarms[alarmIndex].addrModbusRead, |
brunofgc | 19:d52176ff683d | 275 | alarms[alarmIndex].funcModbusRead, |
brunofgc | 19:d52176ff683d | 276 | alarms[alarmIndex].regModbusRead, |
brunofgc | 17:c046e4b07ded | 277 | 1, |
brunofgc | 17:c046e4b07ded | 278 | &leitura_uint16_t |
brunofgc | 17:c046e4b07ded | 279 | ); |
brunofgc | 17:c046e4b07ded | 280 | }else{ |
brunofgc | 19:d52176ff683d | 281 | if(alarms[alarmIndex].regModbusRead<4){ |
brunofgc | 19:d52176ff683d | 282 | leitura_uint16_t = aiFiltrada[alarms[alarmIndex].regModbusRead]; |
brunofgc | 17:c046e4b07ded | 283 | } |
brunofgc | 17:c046e4b07ded | 284 | } |
brunofgc | 17:c046e4b07ded | 285 | |
brunofgc | 24:4d44626e5b78 | 286 | //leitura_float = (float)((int16_t)leitura_uint16_t); |
brunofgc | 24:4d44626e5b78 | 287 | leitura_double = (double)((int16_t)leitura_uint16_t); |
brunofgc | 17:c046e4b07ded | 288 | break; |
brunofgc | 17:c046e4b07ded | 289 | |
brunofgc | 17:c046e4b07ded | 290 | |
brunofgc | 6:f20c0e094294 | 291 | case modBusType_bit: |
brunofgc | 5:8efd609dea7d | 292 | //Montar bit! |
brunofgc | 5:8efd609dea7d | 293 | //O valor de alarm para bit é o value[0]!!! |
brunofgc | 19:d52176ff683d | 294 | value_bit = (alarms[alarmIndex].value[0] > 0); //Qualquer valor maior que zero da saída 1 |
brunofgc | 24:4d44626e5b78 | 295 | //value_float = (float)value_bit; |
brunofgc | 24:4d44626e5b78 | 296 | value_double = (double)value_bit; |
brunofgc | 19:d52176ff683d | 297 | if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; |
brunofgc | 6:f20c0e094294 | 298 | //Bloco de leitura da variável modBus; |
brunofgc | 6:f20c0e094294 | 299 | modBusMaster1::readCoils( |
brunofgc | 19:d52176ff683d | 300 | alarms[alarmIndex].addrModbusRead, |
brunofgc | 19:d52176ff683d | 301 | alarms[alarmIndex].regModbusRead, |
brunofgc | 6:f20c0e094294 | 302 | 1, |
brunofgc | 6:f20c0e094294 | 303 | &leitura_bit |
brunofgc | 6:f20c0e094294 | 304 | ); |
brunofgc | 6:f20c0e094294 | 305 | }else{ |
brunofgc | 19:d52176ff683d | 306 | leitura_bit = entradasDigitais[alarms[alarmIndex].regModbusRead]; |
brunofgc | 6:f20c0e094294 | 307 | } |
brunofgc | 24:4d44626e5b78 | 308 | //leitura_float = (float)leitura_bit; |
brunofgc | 24:4d44626e5b78 | 309 | leitura_double = (double)leitura_bit; |
brunofgc | 5:8efd609dea7d | 310 | break; |
brunofgc | 0:8c59a494a451 | 311 | |
brunofgc | 5:8efd609dea7d | 312 | } |
brunofgc | 24:4d44626e5b78 | 313 | |
brunofgc | 24:4d44626e5b78 | 314 | //Teste de maquina de estados alarme alarmFound, max, seconds, secAlarm, alarmFound |
brunofgc | 24:4d44626e5b78 | 315 | /*cenários |
brunofgc | 24:4d44626e5b78 | 316 | found && max |
brunofgc | 24:4d44626e5b78 | 317 | 0 ^ 0 = 0 |
brunofgc | 24:4d44626e5b78 | 318 | 0 ^ 1 = 1 |
brunofgc | 24:4d44626e5b78 | 319 | 1 ^ 0 = 1 |
brunofgc | 24:4d44626e5b78 | 320 | 1 ^ 1 = 0*/ |
brunofgc | 24:4d44626e5b78 | 321 | |
brunofgc | 24:4d44626e5b78 | 322 | if((alarms[alarmIndex].alarmFound)^(alarms[alarmIndex].max)){ |
brunofgc | 24:4d44626e5b78 | 323 | if(leitura_double > value_double){alarms[alarmIndex].secAlarm++;} |
brunofgc | 24:4d44626e5b78 | 324 | else{alarms[alarmIndex].secAlarm=0;} |
brunofgc | 24:4d44626e5b78 | 325 | }else{ |
brunofgc | 24:4d44626e5b78 | 326 | if(leitura_double < value_double){alarms[alarmIndex].secAlarm++;} |
brunofgc | 24:4d44626e5b78 | 327 | else{alarms[alarmIndex].secAlarm=0;} |
brunofgc | 24:4d44626e5b78 | 328 | } |
brunofgc | 24:4d44626e5b78 | 329 | |
brunofgc | 24:4d44626e5b78 | 330 | |
brunofgc | 24:4d44626e5b78 | 331 | //Alterno de found pra release dependendo de como seja. |
brunofgc | 24:4d44626e5b78 | 332 | if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ |
brunofgc | 24:4d44626e5b78 | 333 | alarms[alarmIndex].alarmFound = !alarms[alarmIndex].alarmFound; |
brunofgc | 24:4d44626e5b78 | 334 | alarms[alarmIndex].secAlarm = 0; |
brunofgc | 24:4d44626e5b78 | 335 | } |
brunofgc | 24:4d44626e5b78 | 336 | |
brunofgc | 24:4d44626e5b78 | 337 | //debug |
brunofgc | 24:4d44626e5b78 | 338 | //if(debug){pc.printf("alarmIndex [%lu],leitura_double %f, value_double %f, alarmFound %u, secAlarm %lu\r\n",alarmIndex,leitura_double,value_double,alarms[alarmIndex].alarmFound,alarms[alarmIndex].secAlarm);} |
brunofgc | 24:4d44626e5b78 | 339 | //debug |
brunofgc | 24:4d44626e5b78 | 340 | |
brunofgc | 24:4d44626e5b78 | 341 | //Teste de maquina de estados alarme alarmFound, max, seconds, secAlarm, alarmFound |
brunofgc | 24:4d44626e5b78 | 342 | |
brunofgc | 5:8efd609dea7d | 343 | //Verifico se houve alarme não tratado |
brunofgc | 24:4d44626e5b78 | 344 | if(alarms[alarmIndex].alarmFound!=alarmFoundAntes){ |
brunofgc | 24:4d44626e5b78 | 345 | if(alarms[alarmIndex].alarmFound){ |
brunofgc | 24:4d44626e5b78 | 346 | printf("Alarm FOUND! Id <%lu>.\n",alarms[alarmIndex].id); |
brunofgc | 5:8efd609dea7d | 347 | sdCardBuf.fill("log{alarm:event:found;id:",25); |
brunofgc | 19:d52176ff683d | 348 | sprintf(aux,"%lu",alarms[alarmIndex].id); |
brunofgc | 5:8efd609dea7d | 349 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 5:8efd609dea7d | 350 | |
brunofgc | 5:8efd609dea7d | 351 | sdCardBuf.fill(";timestamp:",11); |
brunofgc | 5:8efd609dea7d | 352 | sprintf(aux,"%lu",time(NULL)); |
brunofgc | 5:8efd609dea7d | 353 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 5:8efd609dea7d | 354 | |
brunofgc | 5:8efd609dea7d | 355 | sdCardBuf.fill(";reading:",9); |
brunofgc | 19:d52176ff683d | 356 | if(alarms[alarmIndex].type == modBusType_float){ |
brunofgc | 5:8efd609dea7d | 357 | sprintf(aux,"%f",leitura_float); |
brunofgc | 19:d52176ff683d | 358 | }else if(alarms[alarmIndex].type == modBusType_uint32_t){ |
brunofgc | 5:8efd609dea7d | 359 | sprintf(aux,"%lu",leitura_uint32_t); |
brunofgc | 19:d52176ff683d | 360 | }else if(alarms[alarmIndex].type == modBusType_uint16_t){ |
brunofgc | 5:8efd609dea7d | 361 | sprintf(aux,"%lu",leitura_uint16_t); |
brunofgc | 19:d52176ff683d | 362 | }else if(alarms[alarmIndex].type == modBusType_int32_t){ |
brunofgc | 18:66fa6fcba212 | 363 | sprintf(aux,"%ld",(int32_t)leitura_uint32_t); |
brunofgc | 19:d52176ff683d | 364 | }else if(alarms[alarmIndex].type == modBusType_int16_t){ |
brunofgc | 18:66fa6fcba212 | 365 | sprintf(aux,"%ld",(int16_t)leitura_uint16_t); |
brunofgc | 19:d52176ff683d | 366 | }else if(alarms[alarmIndex].type == modBusType_bit){ |
brunofgc | 24:4d44626e5b78 | 367 | sprintf(aux,"%u",leitura_bit); |
brunofgc | 5:8efd609dea7d | 368 | } |
brunofgc | 24:4d44626e5b78 | 369 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 24:4d44626e5b78 | 370 | |
brunofgc | 5:8efd609dea7d | 371 | leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit |
brunofgc | 8:25061210fcc9 | 372 | sdCard::insereDadosBank(sdCardBuf.get(),leitura_uint16_t); |
brunofgc | 5:8efd609dea7d | 373 | |
brunofgc | 5:8efd609dea7d | 374 | boolExecAct=true; |
brunofgc | 19:d52176ff683d | 375 | act = alarms[alarmIndex].idAct; |
brunofgc | 5:8efd609dea7d | 376 | }else{ |
brunofgc | 24:4d44626e5b78 | 377 | printf("Alarm RELEASE! id <%lu>.\n",alarms[alarmIndex].id); |
brunofgc | 5:8efd609dea7d | 378 | sdCardBuf.fill("log{alarm:event:release;id:",27); |
brunofgc | 19:d52176ff683d | 379 | sprintf(aux,"%lu",alarms[alarmIndex].id); |
brunofgc | 5:8efd609dea7d | 380 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 5:8efd609dea7d | 381 | |
brunofgc | 5:8efd609dea7d | 382 | sdCardBuf.fill(";timestamp:",11); |
brunofgc | 5:8efd609dea7d | 383 | sprintf(aux,"%lu",time(NULL)); |
brunofgc | 5:8efd609dea7d | 384 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 5:8efd609dea7d | 385 | |
brunofgc | 24:4d44626e5b78 | 386 | sdCardBuf.fill(";reading:",9); |
brunofgc | 24:4d44626e5b78 | 387 | if(alarms[alarmIndex].type == modBusType_float){ |
brunofgc | 24:4d44626e5b78 | 388 | sprintf(aux,"%f",leitura_float); |
brunofgc | 24:4d44626e5b78 | 389 | }else if(alarms[alarmIndex].type == modBusType_uint32_t){ |
brunofgc | 24:4d44626e5b78 | 390 | sprintf(aux,"%lu",leitura_uint32_t); |
brunofgc | 24:4d44626e5b78 | 391 | }else if(alarms[alarmIndex].type == modBusType_uint16_t){ |
brunofgc | 24:4d44626e5b78 | 392 | sprintf(aux,"%lu",leitura_uint16_t); |
brunofgc | 24:4d44626e5b78 | 393 | }else if(alarms[alarmIndex].type == modBusType_int32_t){ |
brunofgc | 24:4d44626e5b78 | 394 | sprintf(aux,"%ld",(int32_t)leitura_uint32_t); |
brunofgc | 24:4d44626e5b78 | 395 | }else if(alarms[alarmIndex].type == modBusType_int16_t){ |
brunofgc | 24:4d44626e5b78 | 396 | sprintf(aux,"%ld",(int16_t)leitura_uint16_t); |
brunofgc | 24:4d44626e5b78 | 397 | }else if(alarms[alarmIndex].type == modBusType_bit){ |
brunofgc | 24:4d44626e5b78 | 398 | sprintf(aux,"%u",leitura_bit); |
brunofgc | 24:4d44626e5b78 | 399 | } |
brunofgc | 24:4d44626e5b78 | 400 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 24:4d44626e5b78 | 401 | |
brunofgc | 5:8efd609dea7d | 402 | leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit |
brunofgc | 24:4d44626e5b78 | 403 | //sdCard::arquivoAberto(40000); |
brunofgc | 8:25061210fcc9 | 404 | sdCard::insereDadosBank(sdCardBuf.get(),leitura_uint16_t); |
brunofgc | 5:8efd609dea7d | 405 | } |
brunofgc | 5:8efd609dea7d | 406 | //atrasa o envio em 3 segundos para que possa ser gravado o registro do alarme. |
brunofgc | 5:8efd609dea7d | 407 | enviaDadosPorAlarme=10; |
brunofgc | 5:8efd609dea7d | 408 | } |
brunofgc | 0:8c59a494a451 | 409 | } |
brunofgc | 0:8c59a494a451 | 410 | } |
brunofgc | 0:8c59a494a451 | 411 | } |
brunofgc | 0:8c59a494a451 | 412 | |
brunofgc | 19:d52176ff683d | 413 | void verifySchedules(){ |
brunofgc | 5:8efd609dea7d | 414 | char stringTime[30]; |
brunofgc | 0:8c59a494a451 | 415 | char *weekdayString; |
brunofgc | 5:8efd609dea7d | 416 | time_t seconds = time(NULL)+5; |
brunofgc | 0:8c59a494a451 | 417 | uint8_t hora; |
brunofgc | 0:8c59a494a451 | 418 | uint16_t minuto; |
brunofgc | 0:8c59a494a451 | 419 | uint8_t weekday=0; |
brunofgc | 5:8efd609dea7d | 420 | uint16_t anoAtual=0; |
brunofgc | 5:8efd609dea7d | 421 | uint16_t diaAtual=0; |
brunofgc | 5:8efd609dea7d | 422 | uint16_t mesAtual=0; |
brunofgc | 5:8efd609dea7d | 423 | uint16_t anoScheduleException=0; |
brunofgc | 5:8efd609dea7d | 424 | uint16_t diaScheduleException=0; |
brunofgc | 5:8efd609dea7d | 425 | uint16_t mesScheduleException=0; |
brunofgc | 0:8c59a494a451 | 426 | const char* weekdayTable[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; |
brunofgc | 0:8c59a494a451 | 427 | uint8_t indexSchedule; |
brunofgc | 5:8efd609dea7d | 428 | uint8_t indexScheduleException; |
brunofgc | 5:8efd609dea7d | 429 | bool exceptionDetected=false; |
brunofgc | 0:8c59a494a451 | 430 | |
brunofgc | 23:d3ef6bb8412a | 431 | puts("Verifing schedule.\r\n"); |
brunofgc | 0:8c59a494a451 | 432 | |
brunofgc | 5:8efd609dea7d | 433 | strftime(stringTime, 30, "%a,%R,%y,%d,%m", localtime(&seconds)); |
brunofgc | 5:8efd609dea7d | 434 | //pc.printf("VerifySchedule <%s>.\r\n",stringTime); |
brunofgc | 5:8efd609dea7d | 435 | //Thu,20:48,17,2,7 |
brunofgc | 0:8c59a494a451 | 436 | weekdayString = strtok(stringTime,","); |
brunofgc | 0:8c59a494a451 | 437 | weekday = 0; |
brunofgc | 0:8c59a494a451 | 438 | while((weekday<10)&&(strstr(weekdayTable[weekday],weekdayString)==NULL)){ |
brunofgc | 0:8c59a494a451 | 439 | weekday++; |
brunofgc | 0:8c59a494a451 | 440 | } |
brunofgc | 0:8c59a494a451 | 441 | hora = atoi(strtok(NULL,":")); |
brunofgc | 5:8efd609dea7d | 442 | minuto = atoi(strtok(NULL,",")); |
brunofgc | 5:8efd609dea7d | 443 | minuto = (hora*60)+minuto; |
brunofgc | 5:8efd609dea7d | 444 | anoAtual = atoi(strtok(NULL,",")); |
brunofgc | 5:8efd609dea7d | 445 | diaAtual = atoi(strtok(NULL,",")); |
brunofgc | 5:8efd609dea7d | 446 | mesAtual = atoi(strtok(NULL,"")); |
brunofgc | 5:8efd609dea7d | 447 | |
brunofgc | 5:8efd609dea7d | 448 | //pc.printf("Passo 1 .\r\n"); |
brunofgc | 5:8efd609dea7d | 449 | for(indexSchedule = 0; indexSchedule < qtdSchedules; indexSchedule++){ |
brunofgc | 20:e0656406d5b8 | 450 | //pc.printf("Passo 2.\r\n"); |
brunofgc | 22:e0d8c8fe26c6 | 451 | diversos::wdt.kick(180.0); |
brunofgc | 19:d52176ff683d | 452 | if(schedules[indexSchedule].weekday&(0x1<<weekday)){ |
brunofgc | 5:8efd609dea7d | 453 | //pc.printf("Passo 3.\r\n"); |
brunofgc | 19:d52176ff683d | 454 | if(schedules[indexSchedule].minute == minuto){ |
brunofgc | 5:8efd609dea7d | 455 | //pc.printf("Passo 4.\r\n"); |
brunofgc | 0:8c59a494a451 | 456 | //Verifico se já executei e então executo a ação. |
brunofgc | 19:d52176ff683d | 457 | if(!schedules[indexSchedule].actSent){ |
brunofgc | 5:8efd609dea7d | 458 | //pc.printf("Passo 5 .\r\n"); |
brunofgc | 0:8c59a494a451 | 459 | //Executa a ação; |
brunofgc | 19:d52176ff683d | 460 | for(indexScheduleException = 0;indexScheduleException<numScheduleExceptions;indexScheduleException++){ |
brunofgc | 5:8efd609dea7d | 461 | //pc.printf("Passo 6.\r\n"); |
brunofgc | 19:d52176ff683d | 462 | if(schedules[indexSchedule].id == scheduleExceptions[indexScheduleException].id_schedule){ |
brunofgc | 5:8efd609dea7d | 463 | //pc.printf("Passo 7.\r\n"); |
brunofgc | 19:d52176ff683d | 464 | seconds = scheduleExceptions[indexScheduleException].timestamp; |
brunofgc | 5:8efd609dea7d | 465 | strftime(stringTime, 30, "%y,%d,%m", localtime(&seconds)); |
brunofgc | 5:8efd609dea7d | 466 | anoScheduleException=atoi(strtok(stringTime,",")); |
brunofgc | 5:8efd609dea7d | 467 | diaScheduleException=atoi(strtok(NULL,",")); |
brunofgc | 5:8efd609dea7d | 468 | mesScheduleException=atoi(strtok(NULL,"")); |
brunofgc | 5:8efd609dea7d | 469 | /* |
brunofgc | 5:8efd609dea7d | 470 | pc.printf("\r\nDebug ScheduleException.\r\n"); |
brunofgc | 5:8efd609dea7d | 471 | pc.printf("stringTime <%s>.\r\n",stringTime); |
brunofgc | 5:8efd609dea7d | 472 | pc.printf("anoScheduleException %lu.",anoScheduleException); |
brunofgc | 5:8efd609dea7d | 473 | pc.printf("diaScheduleException %lu.",diaScheduleException); |
brunofgc | 5:8efd609dea7d | 474 | pc.printf("mesScheduleException %lu.",mesScheduleException); |
brunofgc | 5:8efd609dea7d | 475 | pc.printf("anoAtual %lu.",anoAtual); |
brunofgc | 5:8efd609dea7d | 476 | pc.printf("diaAtual %lu.",diaAtual); |
brunofgc | 5:8efd609dea7d | 477 | pc.printf("mesAtual %lu.",mesAtual); |
brunofgc | 5:8efd609dea7d | 478 | pc.printf("\r\n"); |
brunofgc | 5:8efd609dea7d | 479 | */ |
brunofgc | 5:8efd609dea7d | 480 | if( |
brunofgc | 5:8efd609dea7d | 481 | ((anoScheduleException == anoAtual) || (anoScheduleException == 70)) |
brunofgc | 5:8efd609dea7d | 482 | && |
brunofgc | 5:8efd609dea7d | 483 | (diaAtual == diaScheduleException) |
brunofgc | 5:8efd609dea7d | 484 | && |
brunofgc | 5:8efd609dea7d | 485 | (mesAtual == mesScheduleException) |
brunofgc | 5:8efd609dea7d | 486 | ) |
brunofgc | 5:8efd609dea7d | 487 | { |
brunofgc | 5:8efd609dea7d | 488 | pc.printf("ScheduleException detected.\r\n"); |
brunofgc | 5:8efd609dea7d | 489 | exceptionDetected=true; |
brunofgc | 5:8efd609dea7d | 490 | } |
brunofgc | 5:8efd609dea7d | 491 | } |
brunofgc | 5:8efd609dea7d | 492 | } |
brunofgc | 20:e0656406d5b8 | 493 | if(!exceptionDetected){ |
brunofgc | 19:d52176ff683d | 494 | execAct(schedules[indexSchedule].idAct); |
brunofgc | 19:d52176ff683d | 495 | schedules[indexSchedule].actSent = true; |
brunofgc | 5:8efd609dea7d | 496 | } |
brunofgc | 0:8c59a494a451 | 497 | } |
brunofgc | 0:8c59a494a451 | 498 | }else{ |
brunofgc | 19:d52176ff683d | 499 | schedules[indexSchedule].actSent = false; |
brunofgc | 0:8c59a494a451 | 500 | } |
brunofgc | 5:8efd609dea7d | 501 | } |
brunofgc | 0:8c59a494a451 | 502 | } |
brunofgc | 0:8c59a494a451 | 503 | } |
brunofgc | 0:8c59a494a451 | 504 | |
brunofgc | 0:8c59a494a451 | 505 | /* |
brunofgc | 19:d52176ff683d | 506 | void verifySchedules(){ |
brunofgc | 0:8c59a494a451 | 507 | char stringTime[15]; |
brunofgc | 0:8c59a494a451 | 508 | char *weekdayString; |
brunofgc | 0:8c59a494a451 | 509 | time_t seconds = time(NULL); |
brunofgc | 0:8c59a494a451 | 510 | uint8_t hora; |
brunofgc | 0:8c59a494a451 | 511 | uint16_t minuto; |
brunofgc | 0:8c59a494a451 | 512 | uint8_t weekday=0; |
brunofgc | 0:8c59a494a451 | 513 | const char* weekdayTable[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; |
brunofgc | 0:8c59a494a451 | 514 | uint8_t indexSchedule; |
brunofgc | 0:8c59a494a451 | 515 | |
brunofgc | 0:8c59a494a451 | 516 | //Tue,19:09 |
brunofgc | 0:8c59a494a451 | 517 | strftime(stringTime, 15, "%a,%R", localtime(&seconds)); |
brunofgc | 0:8c59a494a451 | 518 | |
brunofgc | 0:8c59a494a451 | 519 | weekdayString = strtok(stringTime,","); |
brunofgc | 0:8c59a494a451 | 520 | weekday = 0; |
brunofgc | 0:8c59a494a451 | 521 | while((weekday<10)&&(strstr(weekdayTable[weekday],weekdayString)==NULL)){ |
brunofgc | 0:8c59a494a451 | 522 | weekday++; |
brunofgc | 0:8c59a494a451 | 523 | } |
brunofgc | 0:8c59a494a451 | 524 | weekday++; |
brunofgc | 0:8c59a494a451 | 525 | hora = atoi(strtok(NULL,":")); |
brunofgc | 0:8c59a494a451 | 526 | minuto = atoi(strtok(NULL,"")); |
brunofgc | 0:8c59a494a451 | 527 | minuto = (hora*60)+minuto; |
brunofgc | 0:8c59a494a451 | 528 | //printf("\r\n\r\nMinuto %lu.\r\n\r\n",minuto); |
brunofgc | 0:8c59a494a451 | 529 | for(indexSchedule = 0; indexSchedule < qtdSchedules; indexSchedule++){ |
brunofgc | 0:8c59a494a451 | 530 | //printf("Executando item %u do for de schedules.\r\n\r\n",indexSchedule); |
brunofgc | 19:d52176ff683d | 531 | if((schedules[indexSchedule].weekday == weekday)||(schedules[indexSchedule].weekday == 0)){ |
brunofgc | 0:8c59a494a451 | 532 | //Se for um alarme para o dia de hoje ou o alarme independer de dia (Se o dia for zero) |
brunofgc | 0:8c59a494a451 | 533 | //printf("Hoje eh dia de executar a acão do schedule indice %u.\r\n\r\n",indexSchedule); |
brunofgc | 19:d52176ff683d | 534 | if(schedules[indexSchedule].minute == minuto){ |
brunofgc | 0:8c59a494a451 | 535 | //Verifico se já executei e então executo a ação. |
brunofgc | 19:d52176ff683d | 536 | if(!schedules[indexSchedule].actSent){ |
brunofgc | 0:8c59a494a451 | 537 | //Executa a ação; |
brunofgc | 0:8c59a494a451 | 538 | //printf("Executando a acao!\r\n\r\n"); |
brunofgc | 19:d52176ff683d | 539 | execAct(schedules[indexSchedule].idAct); |
brunofgc | 19:d52176ff683d | 540 | schedules[indexSchedule].actSent = true; |
brunofgc | 0:8c59a494a451 | 541 | } |
brunofgc | 0:8c59a494a451 | 542 | }else{ |
brunofgc | 19:d52176ff683d | 543 | schedules[indexSchedule].actSent = false; |
brunofgc | 0:8c59a494a451 | 544 | } |
brunofgc | 0:8c59a494a451 | 545 | } |
brunofgc | 0:8c59a494a451 | 546 | } |
brunofgc | 0:8c59a494a451 | 547 | } |
brunofgc | 0:8c59a494a451 | 548 | */ |
brunofgc | 0:8c59a494a451 | 549 | /* |
brunofgc | 19:d52176ff683d | 550 | uint8_t execActSchedule(uint32_t index){ |
brunofgc | 19:d52176ff683d | 551 | switch(schedules[index].actType){ |
brunofgc | 0:8c59a494a451 | 552 | case modBusType_float: |
brunofgc | 0:8c59a494a451 | 553 | float floatValueToWrite; |
brunofgc | 0:8c59a494a451 | 554 | //writeFloat(uint8_t,uint16_t,uint8_t,float*); |
brunofgc | 19:d52176ff683d | 555 | floatToBin(0,&floatValueToWrite,&schedules[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 556 | modBusMaster1::writeFloat( |
brunofgc | 19:d52176ff683d | 557 | schedules[index].param1, |
brunofgc | 19:d52176ff683d | 558 | schedules[index].param2, |
brunofgc | 19:d52176ff683d | 559 | schedules[index].param3, |
brunofgc | 0:8c59a494a451 | 560 | &floatValueToWrite |
brunofgc | 0:8c59a494a451 | 561 | ); |
brunofgc | 0:8c59a494a451 | 562 | break; |
brunofgc | 0:8c59a494a451 | 563 | case modBusType_uint32_t: |
brunofgc | 0:8c59a494a451 | 564 | uint32_t uint32ValueToWrite; |
brunofgc | 19:d52176ff683d | 565 | uint32_t_ToBin(0,&uint32ValueToWrite,&schedules[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 566 | modBusMaster1::writeRegister32BIT( |
brunofgc | 19:d52176ff683d | 567 | schedules[index].param1, |
brunofgc | 19:d52176ff683d | 568 | schedules[index].param2, |
brunofgc | 19:d52176ff683d | 569 | schedules[index].param3, |
brunofgc | 0:8c59a494a451 | 570 | &uint32ValueToWrite |
brunofgc | 0:8c59a494a451 | 571 | ); |
brunofgc | 0:8c59a494a451 | 572 | break; |
brunofgc | 0:8c59a494a451 | 573 | case modBusType_uint16_t: |
brunofgc | 0:8c59a494a451 | 574 | uint16_t uint16ValueToWrite; |
brunofgc | 19:d52176ff683d | 575 | uint16_t_ToBin(0,&uint16ValueToWrite,&schedules[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 576 | modBusMaster1::writeRegister16BIT( |
brunofgc | 19:d52176ff683d | 577 | schedules[index].param1, |
brunofgc | 19:d52176ff683d | 578 | schedules[index].param2, |
brunofgc | 19:d52176ff683d | 579 | schedules[index].param3, |
brunofgc | 0:8c59a494a451 | 580 | &uint16ValueToWrite |
brunofgc | 0:8c59a494a451 | 581 | ); |
brunofgc | 0:8c59a494a451 | 582 | break; |
brunofgc | 0:8c59a494a451 | 583 | case modBusType_bit: |
brunofgc | 0:8c59a494a451 | 584 | bool boolValueToWrite; |
brunofgc | 19:d52176ff683d | 585 | boolValueToWrite = (schedules[index].actValue[0] != 0); |
brunofgc | 0:8c59a494a451 | 586 | modBusMaster1::writeSingleCoil( |
brunofgc | 19:d52176ff683d | 587 | schedules[index].param1, |
brunofgc | 19:d52176ff683d | 588 | schedules[index].param3, |
brunofgc | 0:8c59a494a451 | 589 | boolValueToWrite |
brunofgc | 0:8c59a494a451 | 590 | ); |
brunofgc | 0:8c59a494a451 | 591 | break; |
brunofgc | 0:8c59a494a451 | 592 | case IrCommandType: |
brunofgc | 0:8c59a494a451 | 593 | //Busca no arquivo |
brunofgc | 0:8c59a494a451 | 594 | //Abre arquivo |
brunofgc | 0:8c59a494a451 | 595 | char comandoIr[1024]; |
brunofgc | 0:8c59a494a451 | 596 | char indexBusca[10]; |
brunofgc | 0:8c59a494a451 | 597 | bool endOfFile = false; |
brunofgc | 0:8c59a494a451 | 598 | |
brunofgc | 0:8c59a494a451 | 599 | //printf("\r\n===================DEBUG(Schedule)===================\r\n"); |
brunofgc | 0:8c59a494a451 | 600 | //"Ir:0,6E01FFFFS" |
brunofgc | 19:d52176ff683d | 601 | sprintf(indexBusca,"Ir:%u,",schedules[index].param3); |
brunofgc | 0:8c59a494a451 | 602 | //printf("indexBusca = <%s>.\r\n",indexBusca); |
brunofgc | 0:8c59a494a451 | 603 | if(sdCard::abreArquivo(&sdCard::devices,"r")){ |
brunofgc | 0:8c59a494a451 | 604 | do{ |
brunofgc | 0:8c59a494a451 | 605 | fgets(comandoIr,1024,sdCard::devices.fp); |
brunofgc | 0:8c59a494a451 | 606 | endOfFile = feof(sdCard::devices.fp); |
brunofgc | 0:8c59a494a451 | 607 | //printf("Linha comando Ir = <%s>.\r\nendOfFile = %u\r\n",comandoIr,endOfFile); |
brunofgc | 0:8c59a494a451 | 608 | }while((strstr(comandoIr,indexBusca)==NULL)&&(endOfFile==0)); |
brunofgc | 0:8c59a494a451 | 609 | sdCard::fechaArquivo(&sdCard::devices); |
brunofgc | 0:8c59a494a451 | 610 | |
brunofgc | 0:8c59a494a451 | 611 | //Vê se deu tudo certo, se não encontrou, retorna false |
brunofgc | 0:8c59a494a451 | 612 | if(!endOfFile){ |
brunofgc | 0:8c59a494a451 | 613 | //Encontrado o comando, vou desempacotar ele. |
brunofgc | 0:8c59a494a451 | 614 | strtok(comandoIr,","); |
brunofgc | 0:8c59a494a451 | 615 | //Neste ponto já tenho o meu comando pronto para ser executado dentro de comandoIr; |
brunofgc | 0:8c59a494a451 | 616 | //printf("\r\n\r\n\r\nEstou enviando o comando %s de dentro do schedule.\r\n\r\n\r\n",strtok(NULL,"S")); |
brunofgc | 0:8c59a494a451 | 617 | deserializaPacoteIR(strtok(NULL,"S")); |
brunofgc | 19:d52176ff683d | 618 | enviaComandoIR(schedules[index].param1,schedules[index].param2); |
brunofgc | 0:8c59a494a451 | 619 | } |
brunofgc | 0:8c59a494a451 | 620 | }else{ |
brunofgc | 0:8c59a494a451 | 621 | return false; |
brunofgc | 0:8c59a494a451 | 622 | } |
brunofgc | 0:8c59a494a451 | 623 | //printf("\r\n===================DEBUG(Schedule)===================\r\n"); |
brunofgc | 0:8c59a494a451 | 624 | break; |
brunofgc | 0:8c59a494a451 | 625 | } |
brunofgc | 0:8c59a494a451 | 626 | return true; |
brunofgc | 0:8c59a494a451 | 627 | } |
brunofgc | 0:8c59a494a451 | 628 | */ |
brunofgc | 0:8c59a494a451 | 629 | /* |
brunofgc | 19:d52176ff683d | 630 | uint8_t execActAlarm(uint32_t index){ |
brunofgc | 19:d52176ff683d | 631 | switch(alarms[index].actType){ |
brunofgc | 0:8c59a494a451 | 632 | case modBusType_float: |
brunofgc | 0:8c59a494a451 | 633 | float floatValueToWrite; |
brunofgc | 0:8c59a494a451 | 634 | //writeFloat(uint8_t,uint16_t,uint8_t,float*); |
brunofgc | 19:d52176ff683d | 635 | floatToBin(0,&floatValueToWrite,&alarms[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 636 | modBusMaster1::writeFloat( |
brunofgc | 19:d52176ff683d | 637 | alarms[index].addrModbusWrite, |
brunofgc | 19:d52176ff683d | 638 | alarms[index].funcModbusWrite, |
brunofgc | 19:d52176ff683d | 639 | alarms[index].regModbusWrite, |
brunofgc | 0:8c59a494a451 | 640 | &floatValueToWrite |
brunofgc | 0:8c59a494a451 | 641 | ); |
brunofgc | 0:8c59a494a451 | 642 | break; |
brunofgc | 0:8c59a494a451 | 643 | case modBusType_uint32_t: |
brunofgc | 0:8c59a494a451 | 644 | uint32_t uint32ValueToWrite; |
brunofgc | 19:d52176ff683d | 645 | uint32_t_ToBin(0,&uint32ValueToWrite,&alarms[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 646 | modBusMaster1::writeRegister32BIT( |
brunofgc | 19:d52176ff683d | 647 | alarms[index].addrModbusWrite, |
brunofgc | 19:d52176ff683d | 648 | alarms[index].funcModbusWrite, |
brunofgc | 19:d52176ff683d | 649 | alarms[index].regModbusWrite, |
brunofgc | 0:8c59a494a451 | 650 | &uint32ValueToWrite |
brunofgc | 0:8c59a494a451 | 651 | ); |
brunofgc | 0:8c59a494a451 | 652 | break; |
brunofgc | 0:8c59a494a451 | 653 | case modBusType_uint16_t: |
brunofgc | 0:8c59a494a451 | 654 | uint16_t uint16ValueToWrite; |
brunofgc | 19:d52176ff683d | 655 | uint16_t_ToBin(0,&uint16ValueToWrite,&alarms[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 656 | modBusMaster1::writeRegister16BIT( |
brunofgc | 19:d52176ff683d | 657 | alarms[index].addrModbusWrite, |
brunofgc | 19:d52176ff683d | 658 | alarms[index].funcModbusWrite, |
brunofgc | 19:d52176ff683d | 659 | alarms[index].regModbusWrite, |
brunofgc | 0:8c59a494a451 | 660 | &uint16ValueToWrite |
brunofgc | 0:8c59a494a451 | 661 | ); |
brunofgc | 0:8c59a494a451 | 662 | break; |
brunofgc | 0:8c59a494a451 | 663 | case modBusType_bit: |
brunofgc | 0:8c59a494a451 | 664 | bool boolValueToWrite; |
brunofgc | 19:d52176ff683d | 665 | boolValueToWrite = (alarms[index].actValue[0] != 0); |
brunofgc | 0:8c59a494a451 | 666 | modBusMaster1::writeSingleCoil( |
brunofgc | 19:d52176ff683d | 667 | alarms[index].addrModbusWrite, |
brunofgc | 19:d52176ff683d | 668 | alarms[index].regModbusWrite, |
brunofgc | 0:8c59a494a451 | 669 | boolValueToWrite |
brunofgc | 0:8c59a494a451 | 670 | ); |
brunofgc | 0:8c59a494a451 | 671 | break; |
brunofgc | 0:8c59a494a451 | 672 | } |
brunofgc | 0:8c59a494a451 | 673 | return true; |
brunofgc | 0:8c59a494a451 | 674 | } |
brunofgc | 0:8c59a494a451 | 675 | */ |
brunofgc | 0:8c59a494a451 | 676 | |
brunofgc | 19:d52176ff683d | 677 | void setReading(char *linha) |
brunofgc | 0:8c59a494a451 | 678 | { |
brunofgc | 0:8c59a494a451 | 679 | uint8_t addr=0; |
brunofgc | 0:8c59a494a451 | 680 | uint8_t func=0; |
brunofgc | 0:8c59a494a451 | 681 | uint16_t reg=0; |
brunofgc | 0:8c59a494a451 | 682 | uint8_t numRegs=0; |
brunofgc | 0:8c59a494a451 | 683 | char *pChar; |
brunofgc | 0:8c59a494a451 | 684 | bool inputData = false; |
brunofgc | 0:8c59a494a451 | 685 | |
brunofgc | 0:8c59a494a451 | 686 | //printf("Linha <%s>.\n",linha); |
brunofgc | 0:8c59a494a451 | 687 | if(strstr(linha,"readingsSet")) { |
brunofgc | 0:8c59a494a451 | 688 | return; |
brunofgc | 0:8c59a494a451 | 689 | } |
brunofgc | 0:8c59a494a451 | 690 | |
brunofgc | 19:d52176ff683d | 691 | numReadings++; |
brunofgc | 0:8c59a494a451 | 692 | |
brunofgc | 0:8c59a494a451 | 693 | if(strstr(linha,"float,")) { |
brunofgc | 19:d52176ff683d | 694 | readings[numReadings-1].type=modBusType_float; |
brunofgc | 0:8c59a494a451 | 695 | inputData = true; |
brunofgc | 0:8c59a494a451 | 696 | } |
brunofgc | 0:8c59a494a451 | 697 | |
brunofgc | 18:66fa6fcba212 | 698 | if(strstr(linha,"int32_t,")) { |
brunofgc | 19:d52176ff683d | 699 | readings[numReadings-1].type=modBusType_int32_t; |
brunofgc | 18:66fa6fcba212 | 700 | inputData = true; |
brunofgc | 18:66fa6fcba212 | 701 | } |
brunofgc | 18:66fa6fcba212 | 702 | |
brunofgc | 18:66fa6fcba212 | 703 | if(strstr(linha,"int16_t,")) { |
brunofgc | 19:d52176ff683d | 704 | readings[numReadings-1].type=modBusType_int16_t; |
brunofgc | 18:66fa6fcba212 | 705 | inputData = true; |
brunofgc | 18:66fa6fcba212 | 706 | } |
brunofgc | 18:66fa6fcba212 | 707 | |
brunofgc | 0:8c59a494a451 | 708 | if(strstr(linha,"uint32_t,")) { |
brunofgc | 19:d52176ff683d | 709 | readings[numReadings-1].type=modBusType_uint32_t; |
brunofgc | 0:8c59a494a451 | 710 | inputData = true; |
brunofgc | 0:8c59a494a451 | 711 | } |
brunofgc | 0:8c59a494a451 | 712 | |
brunofgc | 0:8c59a494a451 | 713 | if(strstr(linha,"uint16_t,")) { |
brunofgc | 19:d52176ff683d | 714 | readings[numReadings-1].type=modBusType_uint16_t; |
brunofgc | 0:8c59a494a451 | 715 | inputData = true; |
brunofgc | 17:c046e4b07ded | 716 | } |
brunofgc | 17:c046e4b07ded | 717 | |
brunofgc | 0:8c59a494a451 | 718 | if(strstr(linha,"uint8_t,")) { |
brunofgc | 19:d52176ff683d | 719 | readings[numReadings-1].type=modBusType_uint8_t; |
brunofgc | 0:8c59a494a451 | 720 | inputData = true; |
brunofgc | 0:8c59a494a451 | 721 | } |
brunofgc | 0:8c59a494a451 | 722 | |
brunofgc | 0:8c59a494a451 | 723 | if(strstr(linha,"bit,")) { |
brunofgc | 19:d52176ff683d | 724 | readings[numReadings-1].type=modBusType_bit; |
brunofgc | 0:8c59a494a451 | 725 | inputData = true; |
brunofgc | 0:8c59a494a451 | 726 | } |
brunofgc | 0:8c59a494a451 | 727 | |
brunofgc | 0:8c59a494a451 | 728 | if(inputData){ |
brunofgc | 0:8c59a494a451 | 729 | pChar = strtok(linha,","); |
brunofgc | 0:8c59a494a451 | 730 | |
brunofgc | 0:8c59a494a451 | 731 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 732 | addr = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 733 | |
brunofgc | 0:8c59a494a451 | 734 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 735 | func = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 736 | |
brunofgc | 0:8c59a494a451 | 737 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 738 | reg = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 739 | |
brunofgc | 0:8c59a494a451 | 740 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 741 | numRegs = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 742 | |
brunofgc | 19:d52176ff683d | 743 | readings[numReadings-1].addr=addr; |
brunofgc | 19:d52176ff683d | 744 | readings[numReadings-1].func=func; |
brunofgc | 19:d52176ff683d | 745 | readings[numReadings-1].reg=reg; |
brunofgc | 19:d52176ff683d | 746 | readings[numReadings-1].numRegs=numRegs; |
brunofgc | 0:8c59a494a451 | 747 | } |
brunofgc | 0:8c59a494a451 | 748 | |
brunofgc | 0:8c59a494a451 | 749 | } |
brunofgc | 0:8c59a494a451 | 750 | |
brunofgc | 19:d52176ff683d | 751 | void setAlarm(char *linha) |
brunofgc | 0:8c59a494a451 | 752 | { |
brunofgc | 0:8c59a494a451 | 753 | float floatVar; |
brunofgc | 0:8c59a494a451 | 754 | uint32_t aux32_t; |
brunofgc | 0:8c59a494a451 | 755 | uint16_t aux16_t; |
brunofgc | 0:8c59a494a451 | 756 | char *pChar; |
brunofgc | 0:8c59a494a451 | 757 | |
brunofgc | 0:8c59a494a451 | 758 | if(strstr(linha,"alarmSet")) { |
brunofgc | 2:ca3723da6668 | 759 | //Inicialização de contadores de alarme |
brunofgc | 2:ca3723da6668 | 760 | //É necessário inicializar estes dados para quando a máquina de alarmes rodar ter um inicio correto. |
brunofgc | 19:d52176ff683d | 761 | alarms[alarmes].secAlarm=0; |
brunofgc | 19:d52176ff683d | 762 | alarms[alarmes].idAct=0; |
brunofgc | 19:d52176ff683d | 763 | alarms[alarmes].on=0; |
brunofgc | 24:4d44626e5b78 | 764 | alarms[alarmes].alarmFound = true; |
brunofgc | 0:8c59a494a451 | 765 | //Inicialização de contadores de alarme |
brunofgc | 19:d52176ff683d | 766 | alarmes++; |
brunofgc | 18:66fa6fcba212 | 767 | return; |
brunofgc | 0:8c59a494a451 | 768 | } |
brunofgc | 0:8c59a494a451 | 769 | |
brunofgc | 5:8efd609dea7d | 770 | if(strstr(linha,"idAlarm:")){ |
brunofgc | 0:8c59a494a451 | 771 | strtok(linha,":"); |
brunofgc | 19:d52176ff683d | 772 | alarms[alarmes-1].id = atoi(strtok(NULL,"\r\n")); |
brunofgc | 18:66fa6fcba212 | 773 | return; |
brunofgc | 0:8c59a494a451 | 774 | } |
brunofgc | 0:8c59a494a451 | 775 | |
brunofgc | 5:8efd609dea7d | 776 | if(strstr(linha,"seconds:")) { |
brunofgc | 0:8c59a494a451 | 777 | strtok(linha,":"); |
brunofgc | 0:8c59a494a451 | 778 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 19:d52176ff683d | 779 | alarms[alarmes-1].seconds = atoi(pChar); |
brunofgc | 18:66fa6fcba212 | 780 | return; |
brunofgc | 0:8c59a494a451 | 781 | } |
brunofgc | 0:8c59a494a451 | 782 | |
brunofgc | 5:8efd609dea7d | 783 | if(strstr(linha,"on:")) { |
brunofgc | 5:8efd609dea7d | 784 | strtok(linha,":"); |
brunofgc | 5:8efd609dea7d | 785 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 19:d52176ff683d | 786 | alarms[alarmes-1].on = atoi(pChar); |
brunofgc | 18:66fa6fcba212 | 787 | return; |
brunofgc | 5:8efd609dea7d | 788 | } |
brunofgc | 5:8efd609dea7d | 789 | |
brunofgc | 5:8efd609dea7d | 790 | if(strstr(linha,"idAct:")) { |
brunofgc | 0:8c59a494a451 | 791 | strtok(linha,":"); |
brunofgc | 19:d52176ff683d | 792 | alarms[alarmes-1].idAct = atoi(strtok(NULL,"")); |
brunofgc | 0:8c59a494a451 | 793 | return; |
brunofgc | 5:8efd609dea7d | 794 | } |
brunofgc | 5:8efd609dea7d | 795 | |
brunofgc | 5:8efd609dea7d | 796 | if(strstr(linha,"bit,")){ |
brunofgc | 19:d52176ff683d | 797 | alarms[alarmes-1].type=modBusType_bit; |
brunofgc | 5:8efd609dea7d | 798 | |
brunofgc | 0:8c59a494a451 | 799 | pChar = strtok(linha,","); |
brunofgc | 0:8c59a494a451 | 800 | |
brunofgc | 0:8c59a494a451 | 801 | //Parametro1 |
brunofgc | 0:8c59a494a451 | 802 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 803 | alarms[alarmes-1].addrModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 804 | |
brunofgc | 0:8c59a494a451 | 805 | //Parametro2 |
brunofgc | 0:8c59a494a451 | 806 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 807 | alarms[alarmes-1].funcModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 808 | |
brunofgc | 0:8c59a494a451 | 809 | //Parametro3 |
brunofgc | 0:8c59a494a451 | 810 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 811 | alarms[alarmes-1].regModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 812 | |
brunofgc | 0:8c59a494a451 | 813 | //Parametro4 |
brunofgc | 0:8c59a494a451 | 814 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 815 | alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 0:8c59a494a451 | 816 | |
brunofgc | 0:8c59a494a451 | 817 | //Parametro5 |
brunofgc | 0:8c59a494a451 | 818 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 819 | alarms[alarmes-1].value[0]= (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 18:66fa6fcba212 | 820 | return; |
brunofgc | 0:8c59a494a451 | 821 | } |
brunofgc | 0:8c59a494a451 | 822 | |
brunofgc | 0:8c59a494a451 | 823 | if(strstr(linha,"float,")) { |
brunofgc | 19:d52176ff683d | 824 | alarms[alarmes-1].type=modBusType_float; |
brunofgc | 0:8c59a494a451 | 825 | strtok(linha,","); |
brunofgc | 0:8c59a494a451 | 826 | |
brunofgc | 0:8c59a494a451 | 827 | //Parametro 1; |
brunofgc | 0:8c59a494a451 | 828 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 829 | alarms[alarmes-1].addrModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 830 | |
brunofgc | 0:8c59a494a451 | 831 | //Parametro 2; |
brunofgc | 0:8c59a494a451 | 832 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 833 | alarms[alarmes-1].funcModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 834 | |
brunofgc | 0:8c59a494a451 | 835 | //Parametro 3; |
brunofgc | 0:8c59a494a451 | 836 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 837 | alarms[alarmes-1].regModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 838 | |
brunofgc | 0:8c59a494a451 | 839 | //Parametro 4; |
brunofgc | 0:8c59a494a451 | 840 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 841 | alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 0:8c59a494a451 | 842 | |
brunofgc | 0:8c59a494a451 | 843 | //Parametro 5; |
brunofgc | 0:8c59a494a451 | 844 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 0:8c59a494a451 | 845 | //floatVar = diversos::stringToFloat(pChar,100); |
brunofgc | 0:8c59a494a451 | 846 | floatVar = atof(pChar); |
brunofgc | 5:8efd609dea7d | 847 | |
brunofgc | 19:d52176ff683d | 848 | floatToBin(1,&floatVar,&alarms[alarmes-1].value[0]); |
brunofgc | 18:66fa6fcba212 | 849 | return; |
brunofgc | 0:8c59a494a451 | 850 | } |
brunofgc | 5:8efd609dea7d | 851 | |
brunofgc | 0:8c59a494a451 | 852 | if(strstr(linha,"uint32_t,")) { |
brunofgc | 19:d52176ff683d | 853 | alarms[alarmes-1].type=modBusType_uint32_t; |
brunofgc | 0:8c59a494a451 | 854 | //sscanf(linha,"max:uint32_t,%lu,%lu,%lu,%lu",&aux1,&aux2,&aux3,&aux4); |
brunofgc | 0:8c59a494a451 | 855 | strtok(linha,","); |
brunofgc | 0:8c59a494a451 | 856 | |
brunofgc | 0:8c59a494a451 | 857 | //parametro1 |
brunofgc | 5:8efd609dea7d | 858 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 859 | alarms[alarmes-1].addrModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 860 | |
brunofgc | 0:8c59a494a451 | 861 | //parametro2 |
brunofgc | 5:8efd609dea7d | 862 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 863 | alarms[alarmes-1].funcModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 864 | |
brunofgc | 0:8c59a494a451 | 865 | //parametro3 |
brunofgc | 5:8efd609dea7d | 866 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 867 | alarms[alarmes-1].regModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 868 | |
brunofgc | 0:8c59a494a451 | 869 | //Parametro 4; |
brunofgc | 0:8c59a494a451 | 870 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 871 | alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 0:8c59a494a451 | 872 | |
brunofgc | 0:8c59a494a451 | 873 | //parametro5 |
brunofgc | 5:8efd609dea7d | 874 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 0:8c59a494a451 | 875 | aux32_t = atoi(pChar); |
brunofgc | 5:8efd609dea7d | 876 | |
brunofgc | 19:d52176ff683d | 877 | uint32_t_ToBin(1,&aux32_t,&alarms[alarmes-1].value[0]); |
brunofgc | 18:66fa6fcba212 | 878 | return; |
brunofgc | 0:8c59a494a451 | 879 | } |
brunofgc | 0:8c59a494a451 | 880 | |
brunofgc | 0:8c59a494a451 | 881 | |
brunofgc | 0:8c59a494a451 | 882 | |
brunofgc | 0:8c59a494a451 | 883 | if(strstr(linha,"uint16_t,")) { |
brunofgc | 19:d52176ff683d | 884 | alarms[alarmes-1].type=modBusType_uint16_t; |
brunofgc | 0:8c59a494a451 | 885 | strtok(linha,","); |
brunofgc | 0:8c59a494a451 | 886 | |
brunofgc | 0:8c59a494a451 | 887 | //parametro1 |
brunofgc | 0:8c59a494a451 | 888 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 889 | alarms[alarmes-1].addrModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 890 | |
brunofgc | 0:8c59a494a451 | 891 | //parametro2 |
brunofgc | 5:8efd609dea7d | 892 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 893 | alarms[alarmes-1].funcModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 894 | |
brunofgc | 0:8c59a494a451 | 895 | //parametro3 |
brunofgc | 5:8efd609dea7d | 896 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 897 | alarms[alarmes-1].regModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 898 | |
brunofgc | 0:8c59a494a451 | 899 | //Parametro 4; |
brunofgc | 0:8c59a494a451 | 900 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 901 | alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 0:8c59a494a451 | 902 | |
brunofgc | 0:8c59a494a451 | 903 | //parametro5 |
brunofgc | 5:8efd609dea7d | 904 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 0:8c59a494a451 | 905 | aux16_t = atoi(pChar); |
brunofgc | 5:8efd609dea7d | 906 | |
brunofgc | 19:d52176ff683d | 907 | uint16_t_ToBin(1,&aux16_t,&alarms[alarmes-1].value[0]); |
brunofgc | 18:66fa6fcba212 | 908 | return; |
brunofgc | 5:8efd609dea7d | 909 | } |
brunofgc | 17:c046e4b07ded | 910 | |
brunofgc | 17:c046e4b07ded | 911 | if(strstr(linha,"int32_t,")) { |
brunofgc | 19:d52176ff683d | 912 | alarms[alarmes-1].type=modBusType_int32_t; |
brunofgc | 17:c046e4b07ded | 913 | //sscanf(linha,"max:uint32_t,%lu,%lu,%lu,%lu",&aux1,&aux2,&aux3,&aux4); |
brunofgc | 17:c046e4b07ded | 914 | strtok(linha,","); |
brunofgc | 17:c046e4b07ded | 915 | |
brunofgc | 17:c046e4b07ded | 916 | //parametro1 |
brunofgc | 17:c046e4b07ded | 917 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 918 | alarms[alarmes-1].addrModbusRead = atoi(pChar); |
brunofgc | 17:c046e4b07ded | 919 | |
brunofgc | 17:c046e4b07ded | 920 | //parametro2 |
brunofgc | 17:c046e4b07ded | 921 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 922 | alarms[alarmes-1].funcModbusRead = atoi(pChar); |
brunofgc | 17:c046e4b07ded | 923 | |
brunofgc | 17:c046e4b07ded | 924 | //parametro3 |
brunofgc | 17:c046e4b07ded | 925 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 926 | alarms[alarmes-1].regModbusRead = atol(pChar); |
brunofgc | 17:c046e4b07ded | 927 | |
brunofgc | 17:c046e4b07ded | 928 | //Parametro 4; |
brunofgc | 17:c046e4b07ded | 929 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 930 | alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 17:c046e4b07ded | 931 | |
brunofgc | 17:c046e4b07ded | 932 | //parametro5 |
brunofgc | 17:c046e4b07ded | 933 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 17:c046e4b07ded | 934 | aux32_t = atol(pChar); |
brunofgc | 17:c046e4b07ded | 935 | |
brunofgc | 19:d52176ff683d | 936 | uint32_t_ToBin(1,&aux32_t,&alarms[alarmes-1].value[0]); |
brunofgc | 18:66fa6fcba212 | 937 | return; |
brunofgc | 17:c046e4b07ded | 938 | } |
brunofgc | 17:c046e4b07ded | 939 | |
brunofgc | 17:c046e4b07ded | 940 | |
brunofgc | 17:c046e4b07ded | 941 | |
brunofgc | 17:c046e4b07ded | 942 | if(strstr(linha,"int16_t,")) { |
brunofgc | 19:d52176ff683d | 943 | alarms[alarmes-1].type=modBusType_int16_t; |
brunofgc | 17:c046e4b07ded | 944 | strtok(linha,","); |
brunofgc | 17:c046e4b07ded | 945 | |
brunofgc | 17:c046e4b07ded | 946 | //parametro1 |
brunofgc | 17:c046e4b07ded | 947 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 948 | alarms[alarmes-1].addrModbusRead = atoi(pChar); |
brunofgc | 17:c046e4b07ded | 949 | |
brunofgc | 17:c046e4b07ded | 950 | //parametro2 |
brunofgc | 17:c046e4b07ded | 951 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 952 | alarms[alarmes-1].funcModbusRead = atoi(pChar); |
brunofgc | 17:c046e4b07ded | 953 | |
brunofgc | 17:c046e4b07ded | 954 | //parametro3 |
brunofgc | 17:c046e4b07ded | 955 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 956 | alarms[alarmes-1].regModbusRead = atoi(pChar); |
brunofgc | 17:c046e4b07ded | 957 | |
brunofgc | 17:c046e4b07ded | 958 | //Parametro 4; |
brunofgc | 17:c046e4b07ded | 959 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 960 | alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 17:c046e4b07ded | 961 | |
brunofgc | 17:c046e4b07ded | 962 | //parametro5 |
brunofgc | 17:c046e4b07ded | 963 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 17:c046e4b07ded | 964 | aux16_t = atoi(pChar); |
brunofgc | 17:c046e4b07ded | 965 | |
brunofgc | 19:d52176ff683d | 966 | uint16_t_ToBin(1,&aux16_t,&alarms[alarmes-1].value[0]); |
brunofgc | 18:66fa6fcba212 | 967 | return; |
brunofgc | 17:c046e4b07ded | 968 | } |
brunofgc | 0:8c59a494a451 | 969 | } |
brunofgc | 0:8c59a494a451 | 970 | |
brunofgc | 19:d52176ff683d | 971 | void setSchedule(char *linha) |
brunofgc | 0:8c59a494a451 | 972 | { |
brunofgc | 0:8c59a494a451 | 973 | //float floatVar; |
brunofgc | 0:8c59a494a451 | 974 | //uint32_t aux32_t; |
brunofgc | 0:8c59a494a451 | 975 | //uint16_t aux16_t; |
brunofgc | 5:8efd609dea7d | 976 | char *pChar; |
brunofgc | 5:8efd609dea7d | 977 | |
brunofgc | 2:ca3723da6668 | 978 | if(strstr(linha,"scheduleSet")) { |
brunofgc | 0:8c59a494a451 | 979 | //Inicialização de contadores de schedules |
brunofgc | 2:ca3723da6668 | 980 | //É necessário inicializar estes dados para quando a máquina de schedules rodar ter um inicio correto. |
brunofgc | 19:d52176ff683d | 981 | schedules[numSchedules].actSent = false; |
brunofgc | 2:ca3723da6668 | 982 | //Inicialização de contadores de schedules |
brunofgc | 19:d52176ff683d | 983 | numSchedules++; |
brunofgc | 0:8c59a494a451 | 984 | } |
brunofgc | 0:8c59a494a451 | 985 | |
brunofgc | 5:8efd609dea7d | 986 | if(strstr(linha,"idSchedule:")) { |
brunofgc | 0:8c59a494a451 | 987 | strtok(linha,":"); |
brunofgc | 5:8efd609dea7d | 988 | pChar = strtok(NULL,""); |
brunofgc | 19:d52176ff683d | 989 | schedules[numSchedules-1].id = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 990 | } |
brunofgc | 0:8c59a494a451 | 991 | |
brunofgc | 5:8efd609dea7d | 992 | if(strstr(linha,"dayTime:")) { |
brunofgc | 0:8c59a494a451 | 993 | strtok(linha,":"); |
brunofgc | 0:8c59a494a451 | 994 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 995 | schedules[numSchedules-1].weekday = atoi(pChar); |
brunofgc | 5:8efd609dea7d | 996 | pChar = strtok(NULL,""); |
brunofgc | 19:d52176ff683d | 997 | schedules[numSchedules-1].minute = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 998 | } |
brunofgc | 5:8efd609dea7d | 999 | |
brunofgc | 5:8efd609dea7d | 1000 | if(strstr(linha,"idAct:")) { |
brunofgc | 0:8c59a494a451 | 1001 | strtok(linha,":"); |
brunofgc | 0:8c59a494a451 | 1002 | pChar = strtok(NULL,""); |
brunofgc | 19:d52176ff683d | 1003 | schedules[numSchedules-1].idAct = atoi(pChar); |
brunofgc | 5:8efd609dea7d | 1004 | } |
brunofgc | 5:8efd609dea7d | 1005 | /*pc.printf("Debugando erro em scheduleSet.\r\n"); |
brunofgc | 5:8efd609dea7d | 1006 | pc.printf("Linha <%s>\r\n",linha); |
brunofgc | 19:d52176ff683d | 1007 | pc.printf("actSent = %lu",schedules[numSchedules-1].actSent); |
brunofgc | 19:d52176ff683d | 1008 | pc.printf("idSchedule = %lu",schedules[numSchedules-1].id); |
brunofgc | 19:d52176ff683d | 1009 | pc.printf("dayTime = %lu",schedules[numSchedules-1].weekday); |
brunofgc | 19:d52176ff683d | 1010 | pc.printf("idAct = %lu",schedules[numSchedules-1].idAct); |
brunofgc | 5:8efd609dea7d | 1011 | pc.printf("Debugando erro em scheduleSet.\r\n");*/ |
brunofgc | 0:8c59a494a451 | 1012 | } |
brunofgc | 0:8c59a494a451 | 1013 | |
brunofgc | 19:d52176ff683d | 1014 | void setScheduleException(char *linha) |
brunofgc | 4:3f92979d1ffb | 1015 | { |
brunofgc | 4:3f92979d1ffb | 1016 | //float floatVar; |
brunofgc | 4:3f92979d1ffb | 1017 | //uint32_t aux32_t; |
brunofgc | 4:3f92979d1ffb | 1018 | //uint16_t aux16_t; |
brunofgc | 5:8efd609dea7d | 1019 | char *pChar; |
brunofgc | 5:8efd609dea7d | 1020 | if(strstr(linha,"scheduleExceptionSet")) { |
brunofgc | 4:3f92979d1ffb | 1021 | //Inicialização de contadores de schedulesExceptions |
brunofgc | 19:d52176ff683d | 1022 | numScheduleExceptions++; |
brunofgc | 4:3f92979d1ffb | 1023 | } |
brunofgc | 4:3f92979d1ffb | 1024 | |
brunofgc | 5:8efd609dea7d | 1025 | if(strstr(linha,"idSchedule:")) { |
brunofgc | 4:3f92979d1ffb | 1026 | strtok(linha,":"); |
brunofgc | 19:d52176ff683d | 1027 | scheduleExceptions[numScheduleExceptions-1].id_schedule = atoi(strtok(NULL,"\r\n")); |
brunofgc | 4:3f92979d1ffb | 1028 | } |
brunofgc | 4:3f92979d1ffb | 1029 | |
brunofgc | 5:8efd609dea7d | 1030 | if(strstr(linha,"timestamp:")) { |
brunofgc | 4:3f92979d1ffb | 1031 | strtok(linha,":"); |
brunofgc | 4:3f92979d1ffb | 1032 | pChar = strtok(NULL,","); |
brunofgc | 19:d52176ff683d | 1033 | scheduleExceptions[numScheduleExceptions-1].timestamp = atoi(pChar); |
brunofgc | 5:8efd609dea7d | 1034 | } |
brunofgc | 4:3f92979d1ffb | 1035 | } |
brunofgc | 4:3f92979d1ffb | 1036 | |
brunofgc | 19:d52176ff683d | 1037 | /*void writeReadingsToSD(){ |
brunofgc | 0:8c59a494a451 | 1038 | //Esta função faz a inserção de todos os dados no arquivo armazenamento. |
brunofgc | 5:8efd609dea7d | 1039 | uint16_t readingIndex=0; |
brunofgc | 5:8efd609dea7d | 1040 | |
brunofgc | 0:8c59a494a451 | 1041 | float *floatBuffer; |
brunofgc | 0:8c59a494a451 | 1042 | uint32_t *uint32_tBuffer; |
brunofgc | 5:8efd609dea7d | 1043 | uint16_t *uint16_tBuffer; |
brunofgc | 5:8efd609dea7d | 1044 | bool *boolBuffer; |
brunofgc | 0:8c59a494a451 | 1045 | uint32_t seconds_uint32_t; |
brunofgc | 0:8c59a494a451 | 1046 | |
brunofgc | 0:8c59a494a451 | 1047 | //char registro[1024]; |
brunofgc | 0:8c59a494a451 | 1048 | //uint16_t indexRegistro; |
brunofgc | 0:8c59a494a451 | 1049 | |
brunofgc | 0:8c59a494a451 | 1050 | seconds_uint32_t = (uint32_t) time(NULL); |
brunofgc | 0:8c59a494a451 | 1051 | |
brunofgc | 0:8c59a494a451 | 1052 | //Inicio da string dentro do arquivo binario |
brunofgc | 8:25061210fcc9 | 1053 | sdCard::insereDadosBank("log{data:",9); |
brunofgc | 0:8c59a494a451 | 1054 | |
brunofgc | 0:8c59a494a451 | 1055 | //O timestamp é posto nos 4 primeiros bytes (segundos) |
brunofgc | 8:25061210fcc9 | 1056 | sdCard::insereDadosBank(&sdCard::armazenamento,(char *)&seconds_uint32_t,4); |
brunofgc | 0:8c59a494a451 | 1057 | |
brunofgc | 19:d52176ff683d | 1058 | for(readingIndex = 0;readingIndex < numReadings;readingIndex++){ |
brunofgc | 19:d52176ff683d | 1059 | switch(readings[readingIndex].type){ |
brunofgc | 0:8c59a494a451 | 1060 | case modBusType_float: |
brunofgc | 19:d52176ff683d | 1061 | floatBuffer = (float*) malloc(readings[readingIndex].numRegs*sizeof(float)); |
brunofgc | 0:8c59a494a451 | 1062 | modBusMaster1::readFloat( |
brunofgc | 19:d52176ff683d | 1063 | readings[readingIndex].addr, |
brunofgc | 19:d52176ff683d | 1064 | readings[readingIndex].func, |
brunofgc | 19:d52176ff683d | 1065 | readings[readingIndex].reg, |
brunofgc | 19:d52176ff683d | 1066 | readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1067 | floatBuffer |
brunofgc | 0:8c59a494a451 | 1068 | ); |
brunofgc | 0:8c59a494a451 | 1069 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 19:d52176ff683d | 1070 | sdCard::insereDadosBank((char *)floatBuffer,sizeof(float)*readings[readingIndex].numRegs); |
brunofgc | 0:8c59a494a451 | 1071 | free(floatBuffer); |
brunofgc | 0:8c59a494a451 | 1072 | break; |
brunofgc | 0:8c59a494a451 | 1073 | case modBusType_uint32_t: |
brunofgc | 19:d52176ff683d | 1074 | uint32_tBuffer = (uint32_t*) malloc(readings[readingIndex].numRegs*sizeof(uint32_t)); |
brunofgc | 0:8c59a494a451 | 1075 | modBusMaster1::readRegister32BIT( |
brunofgc | 19:d52176ff683d | 1076 | readings[readingIndex].addr, |
brunofgc | 19:d52176ff683d | 1077 | readings[readingIndex].func, |
brunofgc | 19:d52176ff683d | 1078 | readings[readingIndex].reg, |
brunofgc | 19:d52176ff683d | 1079 | readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1080 | uint32_tBuffer |
brunofgc | 0:8c59a494a451 | 1081 | ); |
brunofgc | 0:8c59a494a451 | 1082 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 19:d52176ff683d | 1083 | sdCard::insereDadosBank((char *)uint32_tBuffer,sizeof(uint32_t)*readings[readingIndex].numRegs); |
brunofgc | 0:8c59a494a451 | 1084 | free(uint32_tBuffer); |
brunofgc | 0:8c59a494a451 | 1085 | break; |
brunofgc | 0:8c59a494a451 | 1086 | |
brunofgc | 0:8c59a494a451 | 1087 | case modBusType_uint16_t: |
brunofgc | 19:d52176ff683d | 1088 | uint16_tBuffer = (uint16_t*) malloc(readings[readingIndex].numRegs*sizeof(uint16_t)); |
brunofgc | 0:8c59a494a451 | 1089 | modBusMaster1::readRegister16BIT( |
brunofgc | 19:d52176ff683d | 1090 | readings[readingIndex].addr, |
brunofgc | 19:d52176ff683d | 1091 | readings[readingIndex].func, |
brunofgc | 19:d52176ff683d | 1092 | readings[readingIndex].reg, |
brunofgc | 19:d52176ff683d | 1093 | readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1094 | uint16_tBuffer |
brunofgc | 0:8c59a494a451 | 1095 | ); |
brunofgc | 0:8c59a494a451 | 1096 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 19:d52176ff683d | 1097 | sdCard::insereDadosBank((char *)uint16_tBuffer,sizeof(uint16_t)*readings[readingIndex].numRegs); |
brunofgc | 0:8c59a494a451 | 1098 | free(uint16_tBuffer); |
brunofgc | 0:8c59a494a451 | 1099 | break; |
brunofgc | 0:8c59a494a451 | 1100 | |
brunofgc | 0:8c59a494a451 | 1101 | case modBusType_bit: |
brunofgc | 19:d52176ff683d | 1102 | boolBuffer = (bool*) malloc(readings[readingIndex].numRegs*sizeof(bool)); |
brunofgc | 0:8c59a494a451 | 1103 | modBusMaster1::readCoils( |
brunofgc | 19:d52176ff683d | 1104 | readings[readingIndex].addr, |
brunofgc | 19:d52176ff683d | 1105 | readings[readingIndex].reg, |
brunofgc | 19:d52176ff683d | 1106 | readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1107 | boolBuffer |
brunofgc | 0:8c59a494a451 | 1108 | ); |
brunofgc | 0:8c59a494a451 | 1109 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 19:d52176ff683d | 1110 | sdCard::insereDadosBank((char *)boolBuffer,sizeof(bool)*readings[readingIndex].numRegs); |
brunofgc | 0:8c59a494a451 | 1111 | free(boolBuffer); |
brunofgc | 0:8c59a494a451 | 1112 | break; |
brunofgc | 0:8c59a494a451 | 1113 | |
brunofgc | 0:8c59a494a451 | 1114 | } |
brunofgc | 0:8c59a494a451 | 1115 | } |
brunofgc | 8:25061210fcc9 | 1116 | sdCard::insereDadosBank("}log",4); |
brunofgc | 0:8c59a494a451 | 1117 | }*/ |
brunofgc | 0:8c59a494a451 | 1118 | |
brunofgc | 19:d52176ff683d | 1119 | uint8_t execAct(uint32_t index){ |
brunofgc | 0:8c59a494a451 | 1120 | //Primeiro preciso decidir que tipo de act estou lidando para tanto preciso pegar a ACT inteira. |
brunofgc | 0:8c59a494a451 | 1121 | //Busca no arquivo |
brunofgc | 0:8c59a494a451 | 1122 | //Abre arquivo |
brunofgc | 0:8c59a494a451 | 1123 | char actString[1024]; |
brunofgc | 0:8c59a494a451 | 1124 | char indexBusca[15]; |
brunofgc | 5:8efd609dea7d | 1125 | char alarmsSetString[256]; |
brunofgc | 5:8efd609dea7d | 1126 | char alarmsResetString[256]; |
brunofgc | 0:8c59a494a451 | 1127 | char *pTipoComando; |
brunofgc | 18:66fa6fcba212 | 1128 | uint8_t tipoComando=0; |
brunofgc | 0:8c59a494a451 | 1129 | bool endOfFile = false; |
brunofgc | 18:66fa6fcba212 | 1130 | uint8_t indexAlarms=0; |
brunofgc | 5:8efd609dea7d | 1131 | char *pChar; |
brunofgc | 9:0230ec4d2e68 | 1132 | bool commandExecOk = true; |
brunofgc | 12:254c32b4ece7 | 1133 | uint8_t i; |
brunofgc | 12:254c32b4ece7 | 1134 | |
brunofgc | 0:8c59a494a451 | 1135 | //Variáveis comuns; |
brunofgc | 0:8c59a494a451 | 1136 | uint8_t param1; |
brunofgc | 0:8c59a494a451 | 1137 | uint8_t param2; |
brunofgc | 0:8c59a494a451 | 1138 | uint16_t param3; |
brunofgc | 0:8c59a494a451 | 1139 | uint32_t param4; |
brunofgc | 0:8c59a494a451 | 1140 | |
brunofgc | 0:8c59a494a451 | 1141 | //printf("\r\n===================DEBUG(execAct)===================\r\n"); |
brunofgc | 5:8efd609dea7d | 1142 | sprintf(indexBusca,"idAct:%lu;",index); |
brunofgc | 5:8efd609dea7d | 1143 | pc.printf("Lido <%s>.\r\n",indexBusca); |
brunofgc | 0:8c59a494a451 | 1144 | //printf("Buscando por indexBusca<%s>.\r\n",indexBusca); |
brunofgc | 0:8c59a494a451 | 1145 | if(sdCard::abreArquivo(&sdCard::devices,"r")){ |
brunofgc | 0:8c59a494a451 | 1146 | do{ |
brunofgc | 0:8c59a494a451 | 1147 | endOfFile = feof(sdCard::devices.fp); |
brunofgc | 5:8efd609dea7d | 1148 | if(!endOfFile){fgets(actString,1024,sdCard::devices.fp);} |
brunofgc | 5:8efd609dea7d | 1149 | }while((strstr(actString,"actList")==NULL)&&(endOfFile==0)); |
brunofgc | 6:f20c0e094294 | 1150 | pc.printf("Achei o actList.\r\n"); |
brunofgc | 5:8efd609dea7d | 1151 | do{ |
brunofgc | 5:8efd609dea7d | 1152 | endOfFile = feof(sdCard::devices.fp); |
brunofgc | 0:8c59a494a451 | 1153 | if(!endOfFile){fgets(actString,1024,sdCard::devices.fp);} |
brunofgc | 5:8efd609dea7d | 1154 | //printf("Linha ACT = <%s>.\r\nendOfFile = %u\r\n",actString,endOfFile); |
brunofgc | 5:8efd609dea7d | 1155 | }while((strstr(actString,indexBusca)==NULL)&&(endOfFile==0)); |
brunofgc | 0:8c59a494a451 | 1156 | sdCard::fechaArquivo(&sdCard::devices); |
brunofgc | 0:8c59a494a451 | 1157 | |
brunofgc | 0:8c59a494a451 | 1158 | //Vê se deu tudo certo, se não encontrou, retorna false |
brunofgc | 0:8c59a494a451 | 1159 | if(endOfFile){ |
brunofgc | 0:8c59a494a451 | 1160 | return false; |
brunofgc | 0:8c59a494a451 | 1161 | } |
brunofgc | 0:8c59a494a451 | 1162 | //Tudo correndo bem. |
brunofgc | 0:8c59a494a451 | 1163 | |
brunofgc | 0:8c59a494a451 | 1164 | //printf("\r\nPoint 1\r\n"); |
brunofgc | 0:8c59a494a451 | 1165 | |
brunofgc | 0:8c59a494a451 | 1166 | //Encontrado o tipo de comando reutilizando a string indexBusca; |
brunofgc | 5:8efd609dea7d | 1167 | pChar = strtok(actString,";"); |
brunofgc | 5:8efd609dea7d | 1168 | pc.printf("1- <%s>.\r\n",pChar); |
brunofgc | 5:8efd609dea7d | 1169 | pChar = strtok(NULL,";"); |
brunofgc | 5:8efd609dea7d | 1170 | strcpy(alarmsSetString,pChar); |
brunofgc | 5:8efd609dea7d | 1171 | pChar = strtok(NULL,";"); |
brunofgc | 5:8efd609dea7d | 1172 | strcpy(alarmsResetString,pChar); |
brunofgc | 5:8efd609dea7d | 1173 | |
brunofgc | 5:8efd609dea7d | 1174 | pTipoComando = strtok(NULL,","); |
brunofgc | 5:8efd609dea7d | 1175 | |
brunofgc | 5:8efd609dea7d | 1176 | printf("\r\nPoint 2 comando <%s>\r\n",pTipoComando); |
brunofgc | 0:8c59a494a451 | 1177 | if(strstr(pTipoComando,"float")){tipoComando = modBusType_float;} |
brunofgc | 0:8c59a494a451 | 1178 | if(strstr(pTipoComando,"bit")){tipoComando = modBusType_bit;} |
brunofgc | 0:8c59a494a451 | 1179 | if(strstr(pTipoComando,"uint8_t")){tipoComando = modBusType_uint8_t;} |
brunofgc | 17:c046e4b07ded | 1180 | if(strstr(pTipoComando,"int16_t")){tipoComando = modBusType_int16_t;} |
brunofgc | 17:c046e4b07ded | 1181 | if(strstr(pTipoComando,"int32_t")){tipoComando = modBusType_int32_t;} |
brunofgc | 18:66fa6fcba212 | 1182 | if(strstr(pTipoComando,"uint16_t")){tipoComando = modBusType_uint16_t;} |
brunofgc | 18:66fa6fcba212 | 1183 | if(strstr(pTipoComando,"uint32_t")){tipoComando = modBusType_uint32_t;} |
brunofgc | 1:3f9e5b86d9e4 | 1184 | if(strstr(pTipoComando,"IR")){tipoComando = IrCommandType;} |
brunofgc | 1:3f9e5b86d9e4 | 1185 | if(strstr(pTipoComando,"PWM")){tipoComando = PWMCommandType;} |
brunofgc | 0:8c59a494a451 | 1186 | }else{ |
brunofgc | 0:8c59a494a451 | 1187 | return false; |
brunofgc | 0:8c59a494a451 | 1188 | } |
brunofgc | 0:8c59a494a451 | 1189 | |
brunofgc | 0:8c59a494a451 | 1190 | switch(tipoComando){ |
brunofgc | 0:8c59a494a451 | 1191 | case modBusType_float: |
brunofgc | 0:8c59a494a451 | 1192 | float floatValueToWrite; |
brunofgc | 0:8c59a494a451 | 1193 | uint32_t auxMod; |
brunofgc | 0:8c59a494a451 | 1194 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1195 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1196 | param3 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1197 | //floatValueToWrite = diversos::stringToFloat(strtok(NULL,","),100); |
brunofgc | 0:8c59a494a451 | 1198 | floatValueToWrite = atof(strtok(NULL,",")); |
brunofgc | 5:8efd609dea7d | 1199 | |
brunofgc | 1:3f9e5b86d9e4 | 1200 | //Envio comando via Modbus |
brunofgc | 9:0230ec4d2e68 | 1201 | if(modBusMaster1::writeFloat( |
brunofgc | 1:3f9e5b86d9e4 | 1202 | param1, |
brunofgc | 1:3f9e5b86d9e4 | 1203 | param3, |
brunofgc | 10:b61bb770f7b2 | 1204 | 1, |
brunofgc | 1:3f9e5b86d9e4 | 1205 | &floatValueToWrite |
brunofgc | 9:0230ec4d2e68 | 1206 | )){commandExecOk = false;} |
brunofgc | 0:8c59a494a451 | 1207 | break; |
brunofgc | 17:c046e4b07ded | 1208 | case modBusType_uint32_t: |
brunofgc | 17:c046e4b07ded | 1209 | case modBusType_int32_t: |
brunofgc | 0:8c59a494a451 | 1210 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1211 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1212 | param3 = atoi(strtok(NULL,",")); |
brunofgc | 17:c046e4b07ded | 1213 | param4 = atol(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1214 | |
brunofgc | 9:0230ec4d2e68 | 1215 | if(modBusMaster1::writeRegister32BIT( |
brunofgc | 0:8c59a494a451 | 1216 | param1, |
brunofgc | 0:8c59a494a451 | 1217 | param3, |
brunofgc | 10:b61bb770f7b2 | 1218 | 1, |
brunofgc | 0:8c59a494a451 | 1219 | ¶m4 |
brunofgc | 9:0230ec4d2e68 | 1220 | )){commandExecOk = false;} |
brunofgc | 0:8c59a494a451 | 1221 | break; |
brunofgc | 0:8c59a494a451 | 1222 | case modBusType_uint16_t: |
brunofgc | 17:c046e4b07ded | 1223 | case modBusType_int16_t: |
brunofgc | 0:8c59a494a451 | 1224 | uint16_t param4_uint16_t; |
brunofgc | 0:8c59a494a451 | 1225 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1226 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1227 | param3 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1228 | param4_uint16_t = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1229 | |
brunofgc | 9:0230ec4d2e68 | 1230 | if(modBusMaster1::writeRegister16BIT( |
brunofgc | 0:8c59a494a451 | 1231 | param1, |
brunofgc | 0:8c59a494a451 | 1232 | param3, |
brunofgc | 10:b61bb770f7b2 | 1233 | 1, |
brunofgc | 0:8c59a494a451 | 1234 | ¶m4_uint16_t |
brunofgc | 9:0230ec4d2e68 | 1235 | )){commandExecOk = false;} |
brunofgc | 0:8c59a494a451 | 1236 | break; |
brunofgc | 0:8c59a494a451 | 1237 | case modBusType_bit: |
brunofgc | 0:8c59a494a451 | 1238 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1239 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1240 | param3 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1241 | param4 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1242 | |
brunofgc | 0:8c59a494a451 | 1243 | if(param1 != 254){ |
brunofgc | 0:8c59a494a451 | 1244 | //Envia comando via Modbus |
brunofgc | 9:0230ec4d2e68 | 1245 | if(modBusMaster1::writeSingleCoil( |
brunofgc | 0:8c59a494a451 | 1246 | param1, |
brunofgc | 0:8c59a494a451 | 1247 | param3, |
brunofgc | 0:8c59a494a451 | 1248 | (bool)param4 |
brunofgc | 9:0230ec4d2e68 | 1249 | )){commandExecOk = false;} |
brunofgc | 0:8c59a494a451 | 1250 | }else{ |
brunofgc | 0:8c59a494a451 | 1251 | //Executa comando interno; |
brunofgc | 0:8c59a494a451 | 1252 | switch(param3){ |
brunofgc | 0:8c59a494a451 | 1253 | case 0: |
brunofgc | 0:8c59a494a451 | 1254 | SD1.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1255 | break; |
brunofgc | 0:8c59a494a451 | 1256 | case 1: |
brunofgc | 0:8c59a494a451 | 1257 | SD2.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1258 | break; |
brunofgc | 0:8c59a494a451 | 1259 | case 2: |
brunofgc | 0:8c59a494a451 | 1260 | SD3.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1261 | break; |
brunofgc | 0:8c59a494a451 | 1262 | case 3: |
brunofgc | 0:8c59a494a451 | 1263 | SD4.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1264 | break; |
brunofgc | 0:8c59a494a451 | 1265 | case 4: |
brunofgc | 0:8c59a494a451 | 1266 | SD5.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1267 | break; |
brunofgc | 0:8c59a494a451 | 1268 | case 5: |
brunofgc | 0:8c59a494a451 | 1269 | SD6.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1270 | break; |
brunofgc | 0:8c59a494a451 | 1271 | case 6: |
brunofgc | 0:8c59a494a451 | 1272 | SD7 = param4; |
brunofgc | 0:8c59a494a451 | 1273 | break; |
brunofgc | 0:8c59a494a451 | 1274 | case 7: |
brunofgc | 0:8c59a494a451 | 1275 | SD8 = param4; |
brunofgc | 0:8c59a494a451 | 1276 | break; |
brunofgc | 0:8c59a494a451 | 1277 | } |
brunofgc | 0:8c59a494a451 | 1278 | } |
brunofgc | 0:8c59a494a451 | 1279 | break; |
brunofgc | 0:8c59a494a451 | 1280 | case IrCommandType: |
brunofgc | 0:8c59a494a451 | 1281 | //Busca no arquivo |
brunofgc | 0:8c59a494a451 | 1282 | //Abre arquivo |
brunofgc | 0:8c59a494a451 | 1283 | //printf("\r\nIniciando execucao de comando IR\r\n"); |
brunofgc | 0:8c59a494a451 | 1284 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1285 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1286 | //printf("\r\nparam 1 %lu, param2 %lu\r\n",param1,param2); |
brunofgc | 0:8c59a494a451 | 1287 | pTipoComando = strtok(NULL,"S");//Reaproveitando variável pTipoComando |
brunofgc | 0:8c59a494a451 | 1288 | //printf("\r\nPacote IR <%s>.\r\n",pTipoComando); |
brunofgc | 0:8c59a494a451 | 1289 | deserializaPacoteIR(pTipoComando); |
brunofgc | 5:8efd609dea7d | 1290 | //printf("Deserializado.\r\nInicio do envio.\r\n"); |
brunofgc | 12:254c32b4ece7 | 1291 | if(param1){ |
brunofgc | 12:254c32b4ece7 | 1292 | enviaComandoIR(param1,param2); |
brunofgc | 12:254c32b4ece7 | 1293 | }else{ |
brunofgc | 12:254c32b4ece7 | 1294 | for(i=33;i<41;i++){ |
brunofgc | 12:254c32b4ece7 | 1295 | enviaComandoIR(i,param2); |
brunofgc | 12:254c32b4ece7 | 1296 | if(i!=40){osDelay(100);} |
brunofgc | 12:254c32b4ece7 | 1297 | } |
brunofgc | 12:254c32b4ece7 | 1298 | } |
brunofgc | 5:8efd609dea7d | 1299 | //printf("Fim do envio.\r\n"); |
brunofgc | 0:8c59a494a451 | 1300 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1301 | |
brunofgc | 1:3f9e5b86d9e4 | 1302 | case PWMCommandType: |
brunofgc | 1:3f9e5b86d9e4 | 1303 | //float floatValuePWM; |
brunofgc | 1:3f9e5b86d9e4 | 1304 | //uint32_t auxPeriod; |
brunofgc | 1:3f9e5b86d9e4 | 1305 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 1:3f9e5b86d9e4 | 1306 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 1:3f9e5b86d9e4 | 1307 | param3 = atoi(strtok(NULL,",")); |
brunofgc | 1:3f9e5b86d9e4 | 1308 | //floatValueToWrite = diversos::stringToFloat(strtok(NULL,","),100); |
brunofgc | 1:3f9e5b86d9e4 | 1309 | floatValueToWrite = atof(strtok(NULL,",")); |
brunofgc | 1:3f9e5b86d9e4 | 1310 | |
brunofgc | 1:3f9e5b86d9e4 | 1311 | //Executa comando interno; |
brunofgc | 1:3f9e5b86d9e4 | 1312 | auxMod = floatValueToWrite/10; |
brunofgc | 1:3f9e5b86d9e4 | 1313 | floatValueToWrite = floatValueToWrite-(auxMod*10); |
brunofgc | 1:3f9e5b86d9e4 | 1314 | SD1.period_us(auxMod); |
brunofgc | 1:3f9e5b86d9e4 | 1315 | pwmPeriod = auxMod; |
brunofgc | 1:3f9e5b86d9e4 | 1316 | switch(param3){ |
brunofgc | 1:3f9e5b86d9e4 | 1317 | case 0: |
brunofgc | 1:3f9e5b86d9e4 | 1318 | SD1.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1319 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1320 | case 1: |
brunofgc | 1:3f9e5b86d9e4 | 1321 | SD2.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1322 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1323 | case 2: |
brunofgc | 1:3f9e5b86d9e4 | 1324 | SD3.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1325 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1326 | case 3: |
brunofgc | 1:3f9e5b86d9e4 | 1327 | SD4.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1328 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1329 | case 4: |
brunofgc | 1:3f9e5b86d9e4 | 1330 | SD5.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1331 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1332 | case 5: |
brunofgc | 1:3f9e5b86d9e4 | 1333 | SD6.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1334 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1335 | } |
brunofgc | 1:3f9e5b86d9e4 | 1336 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1337 | |
brunofgc | 0:8c59a494a451 | 1338 | default: |
brunofgc | 0:8c59a494a451 | 1339 | //printf("comando nao encontrado\r\n"); |
brunofgc | 0:8c59a494a451 | 1340 | break; |
brunofgc | 0:8c59a494a451 | 1341 | } |
brunofgc | 5:8efd609dea7d | 1342 | //Coletando dados do alarmSet e alarmReset |
brunofgc | 5:8efd609dea7d | 1343 | |
brunofgc | 5:8efd609dea7d | 1344 | pChar = strtok(alarmsSetString,","); |
brunofgc | 5:8efd609dea7d | 1345 | while(pChar!=NULL){ |
brunofgc | 5:8efd609dea7d | 1346 | pc.printf("Lido Alarm Set %lu - <%lu>.\r\n",indexAlarms,atoi(pChar)); |
brunofgc | 13:c1122e7b0b36 | 1347 | if(atoi(pChar)){ |
brunofgc | 19:d52176ff683d | 1348 | alarmOnOff(atoi(pChar),1); |
brunofgc | 13:c1122e7b0b36 | 1349 | } |
brunofgc | 5:8efd609dea7d | 1350 | pChar = strtok(NULL,","); |
brunofgc | 5:8efd609dea7d | 1351 | } |
brunofgc | 5:8efd609dea7d | 1352 | |
brunofgc | 5:8efd609dea7d | 1353 | pChar = strtok(alarmsResetString,","); |
brunofgc | 5:8efd609dea7d | 1354 | while(pChar!=NULL){ |
brunofgc | 5:8efd609dea7d | 1355 | pc.printf("Lido Alarm Reset %lu - <%lu>.\r\n",indexAlarms,atoi(pChar)); |
brunofgc | 13:c1122e7b0b36 | 1356 | if(atoi(pChar)){ |
brunofgc | 19:d52176ff683d | 1357 | alarmOnOff(atoi(pChar),0); |
brunofgc | 13:c1122e7b0b36 | 1358 | } |
brunofgc | 5:8efd609dea7d | 1359 | pChar = strtok(NULL,","); |
brunofgc | 5:8efd609dea7d | 1360 | } |
brunofgc | 5:8efd609dea7d | 1361 | |
brunofgc | 0:8c59a494a451 | 1362 | //printf("\r\n===================DEBUG(execAct)===================\r\n"); |
brunofgc | 9:0230ec4d2e68 | 1363 | return commandExecOk; |
brunofgc | 0:8c59a494a451 | 1364 | } |
brunofgc | 0:8c59a494a451 | 1365 | |
brunofgc | 19:d52176ff683d | 1366 | void alarmOnOff(uint32_t idAlarm,bool state){ |
brunofgc | 5:8efd609dea7d | 1367 | uint16_t alarmIndex; |
brunofgc | 5:8efd609dea7d | 1368 | char indexBusca[30]; |
brunofgc | 5:8efd609dea7d | 1369 | char alarmString[40]; |
brunofgc | 5:8efd609dea7d | 1370 | uint32_t seekPosition; |
brunofgc | 5:8efd609dea7d | 1371 | bool endOfFile; |
brunofgc | 5:8efd609dea7d | 1372 | char charState; |
brunofgc | 5:8efd609dea7d | 1373 | |
brunofgc | 5:8efd609dea7d | 1374 | for(alarmIndex = 0; alarmIndex < alarmes; alarmIndex++){ |
brunofgc | 19:d52176ff683d | 1375 | if(alarms[alarmIndex].id == idAlarm){ |
brunofgc | 19:d52176ff683d | 1376 | alarms[alarmIndex].on = state; |
brunofgc | 5:8efd609dea7d | 1377 | } |
brunofgc | 5:8efd609dea7d | 1378 | } |
brunofgc | 5:8efd609dea7d | 1379 | |
brunofgc | 5:8efd609dea7d | 1380 | if(!sdCard::abreArquivo(&sdCard::devices,"rb+")){return;} |
brunofgc | 5:8efd609dea7d | 1381 | |
brunofgc | 7:b4b2b8720166 | 1382 | sprintf(indexBusca,"idAlarm:%lu\r",idAlarm); |
brunofgc | 5:8efd609dea7d | 1383 | do{ |
brunofgc | 5:8efd609dea7d | 1384 | endOfFile = feof(sdCard::devices.fp); |
brunofgc | 5:8efd609dea7d | 1385 | if(!endOfFile){fgets(alarmString,40,sdCard::devices.fp);} |
brunofgc | 5:8efd609dea7d | 1386 | }while((strstr(alarmString,indexBusca)==NULL)&&(endOfFile==0)); |
brunofgc | 5:8efd609dea7d | 1387 | |
brunofgc | 5:8efd609dea7d | 1388 | |
brunofgc | 5:8efd609dea7d | 1389 | do{ |
brunofgc | 5:8efd609dea7d | 1390 | endOfFile = feof(sdCard::devices.fp); |
brunofgc | 5:8efd609dea7d | 1391 | if(!endOfFile){fgets(alarmString,40,sdCard::devices.fp);} |
brunofgc | 5:8efd609dea7d | 1392 | }while((strstr(alarmString,"on:")==NULL)&&(endOfFile==0)); |
brunofgc | 5:8efd609dea7d | 1393 | |
brunofgc | 5:8efd609dea7d | 1394 | |
brunofgc | 5:8efd609dea7d | 1395 | if(endOfFile){ |
brunofgc | 5:8efd609dea7d | 1396 | sdCard::fechaArquivo(&sdCard::devices); |
brunofgc | 5:8efd609dea7d | 1397 | return; |
brunofgc | 5:8efd609dea7d | 1398 | } |
brunofgc | 5:8efd609dea7d | 1399 | |
brunofgc | 5:8efd609dea7d | 1400 | seekPosition = ftell(sdCard::devices.fp)-3; |
brunofgc | 5:8efd609dea7d | 1401 | fseek(sdCard::devices.fp,seekPosition,SEEK_SET); |
brunofgc | 5:8efd609dea7d | 1402 | if(state){charState='1';}else{charState='0';} |
brunofgc | 5:8efd609dea7d | 1403 | fputc(charState,sdCard::devices.fp); |
brunofgc | 5:8efd609dea7d | 1404 | //fprintf(sdCard::devices.fp,"%u",state); |
brunofgc | 5:8efd609dea7d | 1405 | fflush(sdCard::devices.fp); |
brunofgc | 5:8efd609dea7d | 1406 | sdCard::fechaArquivo(&sdCard::devices); |
brunofgc | 5:8efd609dea7d | 1407 | } |
brunofgc | 5:8efd609dea7d | 1408 | |
brunofgc | 19:d52176ff683d | 1409 | void writeReadingsToSD(){ |
brunofgc | 0:8c59a494a451 | 1410 | //Buffering before insertData |
brunofgc | 0:8c59a494a451 | 1411 | //Esta função faz a inserção de todos os dados no arquivo armazenamento. |
brunofgc | 0:8c59a494a451 | 1412 | uint16_t readingIndex=0; |
brunofgc | 0:8c59a494a451 | 1413 | /* |
brunofgc | 0:8c59a494a451 | 1414 | union { |
brunofgc | 0:8c59a494a451 | 1415 | char c[4]; |
brunofgc | 0:8c59a494a451 | 1416 | float v; |
brunofgc | 0:8c59a494a451 | 1417 | }uFloat; |
brunofgc | 0:8c59a494a451 | 1418 | */ |
brunofgc | 0:8c59a494a451 | 1419 | float *floatBuffer; |
brunofgc | 0:8c59a494a451 | 1420 | uint32_t *uint32_tBuffer; |
brunofgc | 0:8c59a494a451 | 1421 | uint16_t *uint16_tBuffer; |
brunofgc | 0:8c59a494a451 | 1422 | bool *boolBuffer; |
brunofgc | 0:8c59a494a451 | 1423 | uint32_t seconds_uint32_t; |
brunofgc | 0:8c59a494a451 | 1424 | char seconds_char[5]; |
brunofgc | 0:8c59a494a451 | 1425 | |
brunofgc | 0:8c59a494a451 | 1426 | seconds_uint32_t = (uint32_t) time(NULL); |
brunofgc | 0:8c59a494a451 | 1427 | seconds_char[0]=(seconds_uint32_t >> 0) & 0xFF; |
brunofgc | 0:8c59a494a451 | 1428 | seconds_char[1]=(seconds_uint32_t >> 8) & 0xFF; |
brunofgc | 0:8c59a494a451 | 1429 | seconds_char[2]=(seconds_uint32_t >> 16) & 0xFF; |
brunofgc | 0:8c59a494a451 | 1430 | seconds_char[3]=(seconds_uint32_t >> 24) & 0xFF; |
brunofgc | 0:8c59a494a451 | 1431 | seconds_char[4]=0; |
brunofgc | 0:8c59a494a451 | 1432 | |
brunofgc | 0:8c59a494a451 | 1433 | |
brunofgc | 0:8c59a494a451 | 1434 | //Inicio da string dentro do arquivo binario |
brunofgc | 16:1b43a57ee676 | 1435 | sdCardBuf.del(); |
brunofgc | 0:8c59a494a451 | 1436 | sdCardBuf.fill("log{data:",9); |
brunofgc | 0:8c59a494a451 | 1437 | |
brunofgc | 0:8c59a494a451 | 1438 | //O timestamp é posto nos 4 primeiros bytes (segundos) |
brunofgc | 0:8c59a494a451 | 1439 | sdCardBuf.fill(seconds_char,5); |
brunofgc | 0:8c59a494a451 | 1440 | |
brunofgc | 19:d52176ff683d | 1441 | for(readingIndex = 0;readingIndex < numReadings;readingIndex++){ |
brunofgc | 22:e0d8c8fe26c6 | 1442 | diversos::wdt.kick(180.0); |
brunofgc | 19:d52176ff683d | 1443 | switch(readings[readingIndex].type){ |
brunofgc | 0:8c59a494a451 | 1444 | case modBusType_float: |
brunofgc | 19:d52176ff683d | 1445 | if(readings[readingIndex].addr != enderecoControladoraVirtual){ |
brunofgc | 19:d52176ff683d | 1446 | //Busca fora do drome via Modbus |
brunofgc | 19:d52176ff683d | 1447 | floatBuffer = (float*) malloc(((readings[readingIndex].numRegs*sizeof(float))+1)); //Alocando mais um byte para status |
brunofgc | 19:d52176ff683d | 1448 | ((char *)floatBuffer)[(readings[readingIndex].numRegs*sizeof(float))] = modBusMaster1::readFloat( |
brunofgc | 19:d52176ff683d | 1449 | readings[readingIndex].addr, |
brunofgc | 19:d52176ff683d | 1450 | readings[readingIndex].func, |
brunofgc | 19:d52176ff683d | 1451 | readings[readingIndex].reg, |
brunofgc | 19:d52176ff683d | 1452 | readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1453 | floatBuffer |
brunofgc | 0:8c59a494a451 | 1454 | ); |
brunofgc | 0:8c59a494a451 | 1455 | }else{ |
brunofgc | 0:8c59a494a451 | 1456 | //Busca Interna |
brunofgc | 0:8c59a494a451 | 1457 | floatBuffer = (float*) malloc(sizeof(float)); |
brunofgc | 19:d52176ff683d | 1458 | if((readings[readingIndex].reg<100)&&(readings[readingIndex].reg < num_temperatureSensors)){//Registradores das entradas de temperatura |
brunofgc | 3:75223647cf3d | 1459 | //Populando floatBuffer com a temperaturas |
brunofgc | 19:d52176ff683d | 1460 | floatBuffer[0] = temperaturas[readings[readingIndex].reg]; |
brunofgc | 1:3f9e5b86d9e4 | 1461 | } |
brunofgc | 19:d52176ff683d | 1462 | if((readings[readingIndex].reg>=100)&&(readings[readingIndex].reg<104)){ |
brunofgc | 3:75223647cf3d | 1463 | //Registradores das entradas analógicas |
brunofgc | 19:d52176ff683d | 1464 | floatBuffer[0] = aiFiltrada[readings[readingIndex].reg - 100]; |
brunofgc | 3:75223647cf3d | 1465 | } |
brunofgc | 19:d52176ff683d | 1466 | readings[readingIndex].numRegs = 1; |
brunofgc | 0:8c59a494a451 | 1467 | } |
brunofgc | 0:8c59a494a451 | 1468 | |
brunofgc | 0:8c59a494a451 | 1469 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 19:d52176ff683d | 1470 | sdCardBuf.fill((char *)floatBuffer,((sizeof(float)*readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status |
brunofgc | 0:8c59a494a451 | 1471 | free(floatBuffer); |
brunofgc | 0:8c59a494a451 | 1472 | break; |
brunofgc | 17:c046e4b07ded | 1473 | case modBusType_uint32_t: |
brunofgc | 17:c046e4b07ded | 1474 | case modBusType_int32_t: |
brunofgc | 19:d52176ff683d | 1475 | if(readings[readingIndex].addr != enderecoControladoraVirtual){ |
brunofgc | 19:d52176ff683d | 1476 | uint32_tBuffer = (uint32_t*) malloc((readings[readingIndex].numRegs*sizeof(uint32_t))+1); //Alocando mais um byte para status |
brunofgc | 19:d52176ff683d | 1477 | ((char *)uint32_tBuffer)[(readings[readingIndex].numRegs*sizeof(uint32_t))] = modBusMaster1::readRegister32BIT(//Ponho o status no ultimo byte |
brunofgc | 19:d52176ff683d | 1478 | readings[readingIndex].addr, |
brunofgc | 19:d52176ff683d | 1479 | readings[readingIndex].func, |
brunofgc | 19:d52176ff683d | 1480 | readings[readingIndex].reg, |
brunofgc | 19:d52176ff683d | 1481 | readings[readingIndex].numRegs, |
brunofgc | 3:75223647cf3d | 1482 | uint32_tBuffer |
brunofgc | 3:75223647cf3d | 1483 | ); |
brunofgc | 3:75223647cf3d | 1484 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 3:75223647cf3d | 1485 | }else{ |
brunofgc | 15:6d9e9cafe138 | 1486 | uint32_tBuffer = (uint32_t*) malloc(sizeof(uint32_t)+1); //Alocando mais um byte para status |
brunofgc | 19:d52176ff683d | 1487 | uint32_tBuffer[0] = pulsosEDs[readings[readingIndex].reg]; |
brunofgc | 19:d52176ff683d | 1488 | readings[readingIndex].numRegs = 1; |
brunofgc | 19:d52176ff683d | 1489 | pulsosEDs[readings[readingIndex].reg] = 0; |
brunofgc | 3:75223647cf3d | 1490 | } |
brunofgc | 19:d52176ff683d | 1491 | sdCardBuf.fill((char *)uint32_tBuffer,((sizeof(uint32_t)*readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status |
brunofgc | 0:8c59a494a451 | 1492 | free(uint32_tBuffer); |
brunofgc | 0:8c59a494a451 | 1493 | break; |
brunofgc | 0:8c59a494a451 | 1494 | |
brunofgc | 17:c046e4b07ded | 1495 | case modBusType_uint16_t: |
brunofgc | 17:c046e4b07ded | 1496 | case modBusType_int16_t: |
brunofgc | 19:d52176ff683d | 1497 | if(readings[readingIndex].addr!=enderecoControladoraVirtual){ |
brunofgc | 19:d52176ff683d | 1498 | uint16_tBuffer = (uint16_t*) malloc(((readings[readingIndex].numRegs*sizeof(uint16_t))+1));//Alocando mais um byte para status |
brunofgc | 19:d52176ff683d | 1499 | ((char *)uint16_tBuffer)[(readings[readingIndex].numRegs*sizeof(uint16_t))] = modBusMaster1::readRegister16BIT(// |
brunofgc | 19:d52176ff683d | 1500 | readings[readingIndex].addr, |
brunofgc | 19:d52176ff683d | 1501 | readings[readingIndex].func, |
brunofgc | 19:d52176ff683d | 1502 | readings[readingIndex].reg, |
brunofgc | 19:d52176ff683d | 1503 | readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1504 | uint16_tBuffer |
brunofgc | 0:8c59a494a451 | 1505 | ); |
brunofgc | 0:8c59a494a451 | 1506 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 19:d52176ff683d | 1507 | sdCardBuf.fill((char *)uint16_tBuffer,((sizeof(uint16_t)*readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status |
brunofgc | 0:8c59a494a451 | 1508 | free(uint16_tBuffer); |
brunofgc | 15:6d9e9cafe138 | 1509 | }else{ |
brunofgc | 19:d52176ff683d | 1510 | if((readings[readingIndex].reg>0)&&((readings[readingIndex].reg + readings[readingIndex].numRegs)<=4)){ |
brunofgc | 19:d52176ff683d | 1511 | sdCardBuf.fill((char *)&aiFiltrada[readings[readingIndex].reg-1],sizeof(uint16_t)*readings[readingIndex].numRegs); |
brunofgc | 15:6d9e9cafe138 | 1512 | seconds_char[4]=0; |
brunofgc | 15:6d9e9cafe138 | 1513 | sdCardBuf.fill((char *)&seconds_char[4],1); |
brunofgc | 0:8c59a494a451 | 1514 | } |
brunofgc | 0:8c59a494a451 | 1515 | } |
brunofgc | 0:8c59a494a451 | 1516 | break; |
brunofgc | 0:8c59a494a451 | 1517 | |
brunofgc | 0:8c59a494a451 | 1518 | case modBusType_bit: |
brunofgc | 15:6d9e9cafe138 | 1519 | boolBuffer = (bool*) malloc(sizeof(bool)+1);//Alocando mais um byte para status |
brunofgc | 19:d52176ff683d | 1520 | if(readings[readingIndex].addr != enderecoControladoraVirtual){ |
brunofgc | 19:d52176ff683d | 1521 | ((char *)boolBuffer)[(readings[readingIndex].numRegs*sizeof(bool))] = modBusMaster1::readCoils( |
brunofgc | 19:d52176ff683d | 1522 | readings[readingIndex].addr, |
brunofgc | 19:d52176ff683d | 1523 | readings[readingIndex].reg, |
brunofgc | 19:d52176ff683d | 1524 | readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1525 | boolBuffer |
brunofgc | 0:8c59a494a451 | 1526 | ); |
brunofgc | 15:6d9e9cafe138 | 1527 | }else{ |
brunofgc | 19:d52176ff683d | 1528 | boolBuffer[0] = entradasDigitais[readings[readingIndex].reg]; |
brunofgc | 15:6d9e9cafe138 | 1529 | ((char *)boolBuffer)[1]=0; |
brunofgc | 0:8c59a494a451 | 1530 | } |
brunofgc | 0:8c59a494a451 | 1531 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 19:d52176ff683d | 1532 | sdCardBuf.fill((char *)boolBuffer,((sizeof(bool)*readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status |
brunofgc | 0:8c59a494a451 | 1533 | free(boolBuffer); |
brunofgc | 0:8c59a494a451 | 1534 | break; |
brunofgc | 0:8c59a494a451 | 1535 | |
brunofgc | 0:8c59a494a451 | 1536 | } |
brunofgc | 0:8c59a494a451 | 1537 | } |
brunofgc | 0:8c59a494a451 | 1538 | readingIndex = sdCardBuf.fill("}log",4); //Reaproveitando a variável de 16bit readingIndex |
brunofgc | 5:8efd609dea7d | 1539 | sdCard::arquivoAberto(40000); |
brunofgc | 5:8efd609dea7d | 1540 | //Insiro os dados se for possível. |
brunofgc | 8:25061210fcc9 | 1541 | sdCard::insereDadosBank(sdCardBuf.get(),readingIndex); |
brunofgc | 0:8c59a494a451 | 1542 | } |
brunofgc | 0:8c59a494a451 | 1543 | |
brunofgc | 0:8c59a494a451 | 1544 | |
brunofgc | 0:8c59a494a451 | 1545 | |
brunofgc | 0:8c59a494a451 | 1546 | |
brunofgc | 0:8c59a494a451 | 1547 | |
brunofgc | 0:8c59a494a451 | 1548 | |
brunofgc | 0:8c59a494a451 | 1549 | |
brunofgc | 0:8c59a494a451 | 1550 | |
brunofgc | 19:d52176ff683d | 1551 | |
brunofgc | 0:8c59a494a451 | 1552 | |
brunofgc | 0:8c59a494a451 | 1553 | int criaDevices(FILE *devicesCfg) |
brunofgc | 0:8c59a494a451 | 1554 | { |
brunofgc | 0:8c59a494a451 | 1555 | char linha[maxCaractereLeLinha]; |
brunofgc | 0:8c59a494a451 | 1556 | int linhas = 0; |
brunofgc | 0:8c59a494a451 | 1557 | devices = 0; |
brunofgc | 0:8c59a494a451 | 1558 | |
brunofgc | 0:8c59a494a451 | 1559 | while(fgets(linha,maxCaractereLeLinha,devicesCfg)) { |
brunofgc | 0:8c59a494a451 | 1560 | if(strstr(linha,"deviceCreate\\")) { |
brunofgc | 0:8c59a494a451 | 1561 | devices++; |
brunofgc | 0:8c59a494a451 | 1562 | } |
brunofgc | 0:8c59a494a451 | 1563 | linhas++; |
brunofgc | 0:8c59a494a451 | 1564 | } |
brunofgc | 0:8c59a494a451 | 1565 | |
brunofgc | 19:d52176ff683d | 1566 | printf("Contei %u linha(s), %u device(s).\n",linhas,devices); |
brunofgc | 19:d52176ff683d | 1567 | |
brunofgc | 19:d52176ff683d | 1568 | alarmes=0; |
brunofgc | 19:d52176ff683d | 1569 | numSchedules=0; |
brunofgc | 19:d52176ff683d | 1570 | numScheduleExceptions=0; |
brunofgc | 19:d52176ff683d | 1571 | numReadings=0; |
brunofgc | 2:ca3723da6668 | 1572 | rewind(devicesCfg); |
brunofgc | 0:8c59a494a451 | 1573 | return 1; |
brunofgc | 0:8c59a494a451 | 1574 | } |
brunofgc | 0:8c59a494a451 | 1575 | |
brunofgc | 0:8c59a494a451 | 1576 | |
brunofgc | 0:8c59a494a451 | 1577 | int configuraDevices(FILE *devicesCfg) |
brunofgc | 0:8c59a494a451 | 1578 | { |
brunofgc | 21:19135fa28bc3 | 1579 | |
brunofgc | 0:8c59a494a451 | 1580 | //uint16_t leituras = 0; |
brunofgc | 2:ca3723da6668 | 1581 | char linha[maxCaractereLeLinha]; |
brunofgc | 0:8c59a494a451 | 1582 | uint8_t estadoConfiguracao=estadoConfiguracao_idle; |
brunofgc | 19:d52176ff683d | 1583 | alarmes = 0; |
brunofgc | 19:d52176ff683d | 1584 | bool condicaoDeSaida = false; |
brunofgc | 2:ca3723da6668 | 1585 | |
brunofgc | 19:d52176ff683d | 1586 | while(fgets(linha,maxCaractereLeLinha,devicesCfg)&&(!condicaoDeSaida)){ |
brunofgc | 19:d52176ff683d | 1587 | if(strstr(linha,"deviceCreate\\")) { |
brunofgc | 0:8c59a494a451 | 1588 | //printf("Configurando dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 0:8c59a494a451 | 1589 | estadoConfiguracao = estadoConfiguracao_creatingDevice; |
brunofgc | 0:8c59a494a451 | 1590 | } |
brunofgc | 0:8c59a494a451 | 1591 | |
brunofgc | 0:8c59a494a451 | 1592 | if(strstr(linha,"alarmSet")) { |
brunofgc | 0:8c59a494a451 | 1593 | //printf("Criando alarme no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 19:d52176ff683d | 1594 | //alarmes++; |
brunofgc | 0:8c59a494a451 | 1595 | estadoConfiguracao = estadoConfiguracao_alarmSet; |
brunofgc | 0:8c59a494a451 | 1596 | } |
brunofgc | 0:8c59a494a451 | 1597 | |
brunofgc | 15:6d9e9cafe138 | 1598 | if(strstr(linha,"readingsEnd")) { |
brunofgc | 0:8c59a494a451 | 1599 | //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 15:6d9e9cafe138 | 1600 | estadoConfiguracao = estadoConfiguracao_idle; |
brunofgc | 15:6d9e9cafe138 | 1601 | } |
brunofgc | 15:6d9e9cafe138 | 1602 | |
brunofgc | 15:6d9e9cafe138 | 1603 | if(estadoConfiguracao == estadoConfiguracao_readingsSet){ |
brunofgc | 2:ca3723da6668 | 1604 | leituras++; |
brunofgc | 15:6d9e9cafe138 | 1605 | } |
brunofgc | 15:6d9e9cafe138 | 1606 | |
brunofgc | 15:6d9e9cafe138 | 1607 | |
brunofgc | 15:6d9e9cafe138 | 1608 | if(strstr(linha,"readingsSet")) { |
brunofgc | 15:6d9e9cafe138 | 1609 | //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 0:8c59a494a451 | 1610 | estadoConfiguracao = estadoConfiguracao_readingsSet; |
brunofgc | 0:8c59a494a451 | 1611 | } |
brunofgc | 0:8c59a494a451 | 1612 | |
brunofgc | 4:3f92979d1ffb | 1613 | if(strstr(linha,"scheduleExceptionSet")) { |
brunofgc | 4:3f92979d1ffb | 1614 | //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 4:3f92979d1ffb | 1615 | qtdScheduleExceptions++; |
brunofgc | 4:3f92979d1ffb | 1616 | estadoConfiguracao = estadoConfiguracao_scheduleExceptionSet; |
brunofgc | 15:6d9e9cafe138 | 1617 | } |
brunofgc | 0:8c59a494a451 | 1618 | |
brunofgc | 0:8c59a494a451 | 1619 | if(strstr(linha,"scheduleSet")) { |
brunofgc | 0:8c59a494a451 | 1620 | //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 0:8c59a494a451 | 1621 | qtdSchedules++; |
brunofgc | 0:8c59a494a451 | 1622 | estadoConfiguracao = estadoConfiguracao_scheduleSet; |
brunofgc | 5:8efd609dea7d | 1623 | } |
brunofgc | 0:8c59a494a451 | 1624 | |
brunofgc | 0:8c59a494a451 | 1625 | if(strstr(linha,">")) { |
brunofgc | 0:8c59a494a451 | 1626 | //printf("Fechando dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 19:d52176ff683d | 1627 | condicaoDeSaida = true; |
brunofgc | 0:8c59a494a451 | 1628 | estadoConfiguracao = estadoConfiguracao_idle; |
brunofgc | 0:8c59a494a451 | 1629 | } |
brunofgc | 0:8c59a494a451 | 1630 | |
brunofgc | 0:8c59a494a451 | 1631 | switch(estadoConfiguracao) { |
brunofgc | 0:8c59a494a451 | 1632 | case estadoConfiguracao_creatingDevice: |
brunofgc | 0:8c59a494a451 | 1633 | /*if(strstr(linha,"id:")) { |
brunofgc | 19:d52176ff683d | 1634 | sscanf(linha,"id:%u",&id); |
brunofgc | 0:8c59a494a451 | 1635 | }*/ |
brunofgc | 0:8c59a494a451 | 1636 | break; |
brunofgc | 0:8c59a494a451 | 1637 | case estadoConfiguracao_alarmSet: |
brunofgc | 0:8c59a494a451 | 1638 | //Inclue linha nos alarmes do dispositivo em configuração |
brunofgc | 19:d52176ff683d | 1639 | setAlarm(linha); |
brunofgc | 0:8c59a494a451 | 1640 | break; |
brunofgc | 0:8c59a494a451 | 1641 | case estadoConfiguracao_readingsSet: |
brunofgc | 0:8c59a494a451 | 1642 | //Inclue linha nas leituras do dispositivo em configuração |
brunofgc | 19:d52176ff683d | 1643 | setReading(linha); |
brunofgc | 0:8c59a494a451 | 1644 | break; |
brunofgc | 0:8c59a494a451 | 1645 | case estadoConfiguracao_scheduleSet: |
brunofgc | 4:3f92979d1ffb | 1646 | //Inclue linha nos agendamentos do dispositivo em configuração |
brunofgc | 19:d52176ff683d | 1647 | setSchedule(linha); |
brunofgc | 4:3f92979d1ffb | 1648 | break; |
brunofgc | 4:3f92979d1ffb | 1649 | case estadoConfiguracao_scheduleExceptionSet: |
brunofgc | 4:3f92979d1ffb | 1650 | //Inclue linha nas excessões dos agendamentos do dispositivo em configuração |
brunofgc | 19:d52176ff683d | 1651 | setScheduleException(linha); |
brunofgc | 0:8c59a494a451 | 1652 | break; |
brunofgc | 0:8c59a494a451 | 1653 | } |
brunofgc | 19:d52176ff683d | 1654 | } |
brunofgc | 0:8c59a494a451 | 1655 | return 1; |
brunofgc | 0:8c59a494a451 | 1656 | } |
brunofgc | 0:8c59a494a451 | 1657 | |
brunofgc | 0:8c59a494a451 | 1658 | |
brunofgc | 0:8c59a494a451 | 1659 | |
brunofgc | 0:8c59a494a451 | 1660 | void testaTudoDevices() |
brunofgc | 0:8c59a494a451 | 1661 | { |
brunofgc | 0:8c59a494a451 | 1662 | int j; |
brunofgc | 19:d52176ff683d | 1663 | |
brunofgc | 19:d52176ff683d | 1664 | printf("O dispositivo tem %u alarmes sendo eles:\n",alarmes); |
brunofgc | 0:8c59a494a451 | 1665 | |
brunofgc | 19:d52176ff683d | 1666 | for(j=0; j<alarmes; j++) { |
brunofgc | 19:d52176ff683d | 1667 | printf("Alarme %u:\n",j); |
brunofgc | 19:d52176ff683d | 1668 | printf("\tId <%lu>.\n",alarms[j].id); |
brunofgc | 19:d52176ff683d | 1669 | printf("\tSeconds <%lu>.\n",alarms[j].seconds); |
brunofgc | 19:d52176ff683d | 1670 | printf("\tAddr %u, func %u, reg %lu.\n",alarms[j].addrModbusRead,alarms[j].funcModbusRead,alarms[j].regModbusRead); |
brunofgc | 19:d52176ff683d | 1671 | if(alarms[j].type==modBusType_float) { |
brunofgc | 19:d52176ff683d | 1672 | float value; |
brunofgc | 19:d52176ff683d | 1673 | floatToBin(0,&value,&alarms[j].value[0]); |
brunofgc | 19:d52176ff683d | 1674 | if(alarms[j].max){ |
brunofgc | 19:d52176ff683d | 1675 | printf("\tFloat max <%f>.\n",value); |
brunofgc | 0:8c59a494a451 | 1676 | } |
brunofgc | 19:d52176ff683d | 1677 | else{printf("\tFloat min <%f>.\n",value);} |
brunofgc | 19:d52176ff683d | 1678 | } |
brunofgc | 19:d52176ff683d | 1679 | |
brunofgc | 19:d52176ff683d | 1680 | if(alarms[j].type==modBusType_uint32_t) { |
brunofgc | 19:d52176ff683d | 1681 | uint32_t value; |
brunofgc | 19:d52176ff683d | 1682 | uint32_t_ToBin(0,&value,&alarms[j].value[0]); |
brunofgc | 19:d52176ff683d | 1683 | if(alarms[j].max){ |
brunofgc | 19:d52176ff683d | 1684 | printf("\tuint32_t max <%lu>.\n",value); |
brunofgc | 17:c046e4b07ded | 1685 | } |
brunofgc | 19:d52176ff683d | 1686 | else{printf("\tuint32_t min <%lu>.\n",value);} |
brunofgc | 19:d52176ff683d | 1687 | } |
brunofgc | 19:d52176ff683d | 1688 | |
brunofgc | 19:d52176ff683d | 1689 | if(alarms[j].type==modBusType_uint16_t) { |
brunofgc | 19:d52176ff683d | 1690 | uint16_t value; |
brunofgc | 19:d52176ff683d | 1691 | uint16_t_ToBin(0,&value,&alarms[j].value[0]); |
brunofgc | 19:d52176ff683d | 1692 | if(alarms[j].max){ |
brunofgc | 19:d52176ff683d | 1693 | printf("\tuint16_t max <%lu>.\n",value); |
brunofgc | 19:d52176ff683d | 1694 | } |
brunofgc | 19:d52176ff683d | 1695 | else{printf("\tuint16_t min <%lu>.\n",value);} |
brunofgc | 19:d52176ff683d | 1696 | } |
brunofgc | 19:d52176ff683d | 1697 | |
brunofgc | 19:d52176ff683d | 1698 | if(alarms[j].type==modBusType_int32_t) { |
brunofgc | 19:d52176ff683d | 1699 | uint32_t value; |
brunofgc | 19:d52176ff683d | 1700 | uint32_t_ToBin(0,&value,&alarms[j].value[0]); |
brunofgc | 19:d52176ff683d | 1701 | if(alarms[j].max){ |
brunofgc | 19:d52176ff683d | 1702 | printf("\tint32_t max <%ld>.\n",(int32_t)value); |
brunofgc | 19:d52176ff683d | 1703 | } |
brunofgc | 19:d52176ff683d | 1704 | else{printf("\tint32_t min <%ld>.\n",(int32_t)value);} |
brunofgc | 0:8c59a494a451 | 1705 | } |
brunofgc | 0:8c59a494a451 | 1706 | |
brunofgc | 19:d52176ff683d | 1707 | if(alarms[j].type==modBusType_int16_t) { |
brunofgc | 19:d52176ff683d | 1708 | uint16_t value; |
brunofgc | 19:d52176ff683d | 1709 | uint16_t_ToBin(0,&value,&alarms[j].value[0]); |
brunofgc | 19:d52176ff683d | 1710 | if(alarms[j].max){ |
brunofgc | 19:d52176ff683d | 1711 | printf("\tint16_t max <%ld>.\n",(int16_t)value); |
brunofgc | 19:d52176ff683d | 1712 | } |
brunofgc | 19:d52176ff683d | 1713 | else{printf("\tint16_t min <%ld>.\n",(int16_t)value);} |
brunofgc | 19:d52176ff683d | 1714 | } |
brunofgc | 19:d52176ff683d | 1715 | |
brunofgc | 19:d52176ff683d | 1716 | if(alarms[j].type==modBusType_bit) { |
brunofgc | 19:d52176ff683d | 1717 | uint8_t value; |
brunofgc | 19:d52176ff683d | 1718 | value=alarms[j].value[0]; |
brunofgc | 19:d52176ff683d | 1719 | if(alarms[j].max){ |
brunofgc | 19:d52176ff683d | 1720 | printf("\tBIT max <%lu>.\n",value); |
brunofgc | 19:d52176ff683d | 1721 | } |
brunofgc | 19:d52176ff683d | 1722 | else{printf("\tBIT min <%lu>.\n",value);} |
brunofgc | 19:d52176ff683d | 1723 | } |
brunofgc | 4:3f92979d1ffb | 1724 | |
brunofgc | 19:d52176ff683d | 1725 | printf("\tact:%lu\n",alarms[j].idAct); |
brunofgc | 19:d52176ff683d | 1726 | } |
brunofgc | 19:d52176ff683d | 1727 | |
brunofgc | 21:19135fa28bc3 | 1728 | printf("O dispositivo tem %u schedules sendo eles:\n",numSchedules); |
brunofgc | 19:d52176ff683d | 1729 | for(j=0; j<numSchedules; j++) { |
brunofgc | 19:d52176ff683d | 1730 | printf("Schedule %u:\n",j); |
brunofgc | 19:d52176ff683d | 1731 | printf("\tId <%lu>.\n",schedules[j].id); |
brunofgc | 19:d52176ff683d | 1732 | printf("\tweekday <%lu>.\n",schedules[j].weekday); |
brunofgc | 19:d52176ff683d | 1733 | printf("\tminute <%lu>.\n",schedules[j].minute); |
brunofgc | 19:d52176ff683d | 1734 | printf("\tidAct <%lu>.\n",schedules[j].idAct); |
brunofgc | 19:d52176ff683d | 1735 | } |
brunofgc | 19:d52176ff683d | 1736 | |
brunofgc | 19:d52176ff683d | 1737 | printf("O dispositivo tem %u scheduleExceptions sendo eles:\n",numScheduleExceptions); |
brunofgc | 19:d52176ff683d | 1738 | for(j=0; j<numScheduleExceptions; j++) { |
brunofgc | 19:d52176ff683d | 1739 | printf("scheduleException %u:\n",j); |
brunofgc | 19:d52176ff683d | 1740 | printf("\tid_schedule <%lu>.\n",scheduleExceptions[j].id_schedule); |
brunofgc | 19:d52176ff683d | 1741 | printf("\ttimestamp <%lu>.\n",scheduleExceptions[j].timestamp); |
brunofgc | 19:d52176ff683d | 1742 | } |
brunofgc | 0:8c59a494a451 | 1743 | |
brunofgc | 19:d52176ff683d | 1744 | printf("O dispositivo tem %u leituras sendo elas:\n",numReadings); |
brunofgc | 19:d52176ff683d | 1745 | for(j=0; j<numReadings; j++) { |
brunofgc | 19:d52176ff683d | 1746 | printf("Leitura %u:",j); |
brunofgc | 19:d52176ff683d | 1747 | switch(readings[j].type) { |
brunofgc | 19:d52176ff683d | 1748 | case modBusType_float: |
brunofgc | 19:d52176ff683d | 1749 | printf("Tipo float "); |
brunofgc | 19:d52176ff683d | 1750 | break; |
brunofgc | 19:d52176ff683d | 1751 | case modBusType_uint32_t: |
brunofgc | 19:d52176ff683d | 1752 | printf("Tipo uint32_t "); |
brunofgc | 19:d52176ff683d | 1753 | break; |
brunofgc | 19:d52176ff683d | 1754 | case modBusType_uint16_t: |
brunofgc | 19:d52176ff683d | 1755 | printf("Tipo uint16_t "); |
brunofgc | 19:d52176ff683d | 1756 | break; |
brunofgc | 19:d52176ff683d | 1757 | case modBusType_int32_t: |
brunofgc | 19:d52176ff683d | 1758 | printf("Tipo int32_t "); |
brunofgc | 19:d52176ff683d | 1759 | break; |
brunofgc | 19:d52176ff683d | 1760 | case modBusType_int16_t: |
brunofgc | 19:d52176ff683d | 1761 | printf("Tipo int16_t "); |
brunofgc | 19:d52176ff683d | 1762 | break; |
brunofgc | 19:d52176ff683d | 1763 | case modBusType_uint8_t: |
brunofgc | 19:d52176ff683d | 1764 | printf("Tipo uint8_t "); |
brunofgc | 19:d52176ff683d | 1765 | break; |
brunofgc | 19:d52176ff683d | 1766 | case modBusType_bit: |
brunofgc | 19:d52176ff683d | 1767 | printf("Tipo bool "); |
brunofgc | 19:d52176ff683d | 1768 | break; |
brunofgc | 0:8c59a494a451 | 1769 | } |
brunofgc | 19:d52176ff683d | 1770 | printf("addr %u, func %u, reg %u, numRegs %u.\n",readings[j].addr,readings[j].func,readings[j].reg,readings[j].numRegs); |
brunofgc | 0:8c59a494a451 | 1771 | } |
brunofgc | 0:8c59a494a451 | 1772 | } |
brunofgc | 19:d52176ff683d | 1773 |