teste de publish

Committer:
brunofgc
Date:
Fri Mar 24 15:54:14 2017 +0000
Revision:
0:8c59a494a451
Child:
1:3f9e5b86d9e4
Novas funcionalidades. Saidas Digitais com pwm, entradas analogicas.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
brunofgc 0:8c59a494a451 1 #include "devices.h"
brunofgc 0:8c59a494a451 2
brunofgc 0:8c59a494a451 3 enum {
brunofgc 0:8c59a494a451 4 estadoConfiguracao_idle = 0,
brunofgc 0:8c59a494a451 5 estadoConfiguracao_creatingDevice,
brunofgc 0:8c59a494a451 6 estadoConfiguracao_alarmSet,
brunofgc 0:8c59a494a451 7 estadoConfiguracao_readingsSet,
brunofgc 0:8c59a494a451 8 estadoConfiguracao_scheduleSet
brunofgc 0:8c59a494a451 9 };
brunofgc 0:8c59a494a451 10
brunofgc 0:8c59a494a451 11 uint8_t resizeArray(uint32_t atual, uint32_t novo, char **array,uint16_t sizeUnit)
brunofgc 0:8c59a494a451 12 {
brunofgc 0:8c59a494a451 13 uint32_t i;
brunofgc 0:8c59a494a451 14 char *p = (char *) malloc(sizeUnit*novo);
brunofgc 0:8c59a494a451 15 char *aux;
brunofgc 0:8c59a494a451 16
brunofgc 0:8c59a494a451 17 if(p==NULL) {
brunofgc 0:8c59a494a451 18 return 0;
brunofgc 0:8c59a494a451 19 }
brunofgc 0:8c59a494a451 20
brunofgc 0:8c59a494a451 21 aux = *array;
brunofgc 0:8c59a494a451 22
brunofgc 0:8c59a494a451 23 if(atual) {
brunofgc 0:8c59a494a451 24 //So sigo em frente se tinha algo dentro deste array
brunofgc 0:8c59a494a451 25 for(i=0; i<(novo*sizeUnit); i++) {
brunofgc 0:8c59a494a451 26 p[i]=aux[i];
brunofgc 0:8c59a494a451 27 }
brunofgc 0:8c59a494a451 28 free(aux);
brunofgc 0:8c59a494a451 29 }
brunofgc 0:8c59a494a451 30
brunofgc 0:8c59a494a451 31 *array = p;
brunofgc 0:8c59a494a451 32 return 1;
brunofgc 0:8c59a494a451 33 }
brunofgc 0:8c59a494a451 34
brunofgc 0:8c59a494a451 35 void writeReadingsToSD(){
brunofgc 0:8c59a494a451 36 int i;
brunofgc 0:8c59a494a451 37 for(i=0;i<devices;i++){
brunofgc 0:8c59a494a451 38 dispositivos[i]->writeReadingsToSD();
brunofgc 0:8c59a494a451 39 }
brunofgc 0:8c59a494a451 40 }
brunofgc 0:8c59a494a451 41
brunofgc 0:8c59a494a451 42 void verifyAlarms(){
brunofgc 0:8c59a494a451 43 int i;
brunofgc 0:8c59a494a451 44 for(i=0;i<devices;i++){
brunofgc 0:8c59a494a451 45 dispositivos[i]->verifyAlarms();
brunofgc 0:8c59a494a451 46 }
brunofgc 0:8c59a494a451 47 }
brunofgc 0:8c59a494a451 48
brunofgc 0:8c59a494a451 49 void verifySchedules(){
brunofgc 0:8c59a494a451 50 int i;
brunofgc 0:8c59a494a451 51 for(i=0;i<devices;i++){
brunofgc 0:8c59a494a451 52 dispositivos[i]->verifySchedules();
brunofgc 0:8c59a494a451 53 }
brunofgc 0:8c59a494a451 54 }
brunofgc 0:8c59a494a451 55
brunofgc 0:8c59a494a451 56 void device::floatToBin(uint8_t dir, float *v,uint8_t *p)
brunofgc 0:8c59a494a451 57 {
brunofgc 0:8c59a494a451 58 //Variáveis locais
brunofgc 0:8c59a494a451 59 union {
brunofgc 0:8c59a494a451 60 char c[4];
brunofgc 0:8c59a494a451 61 float v;
brunofgc 0:8c59a494a451 62 }u;
brunofgc 0:8c59a494a451 63 int i;
brunofgc 0:8c59a494a451 64
brunofgc 0:8c59a494a451 65 if(dir) {
brunofgc 0:8c59a494a451 66 u.v = *v;
brunofgc 0:8c59a494a451 67 for(i=0; i<4; i++) {
brunofgc 0:8c59a494a451 68 p[i]=u.c[3-i];
brunofgc 0:8c59a494a451 69 }
brunofgc 0:8c59a494a451 70 } else {
brunofgc 0:8c59a494a451 71 for(i=0; i<4; i++) {
brunofgc 0:8c59a494a451 72 u.c[3-i]=p[i];
brunofgc 0:8c59a494a451 73 }
brunofgc 0:8c59a494a451 74 *v = u.v;
brunofgc 0:8c59a494a451 75 }
brunofgc 0:8c59a494a451 76 }
brunofgc 0:8c59a494a451 77
brunofgc 0:8c59a494a451 78 void device::uint32_t_ToBin(uint8_t dir, uint32_t *v,uint8_t *p)
brunofgc 0:8c59a494a451 79 {
brunofgc 0:8c59a494a451 80 //Variáveis locais
brunofgc 0:8c59a494a451 81 union {
brunofgc 0:8c59a494a451 82 char c[4];
brunofgc 0:8c59a494a451 83 uint32_t v;
brunofgc 0:8c59a494a451 84 }u;
brunofgc 0:8c59a494a451 85 int i;
brunofgc 0:8c59a494a451 86
brunofgc 0:8c59a494a451 87 if(dir) {
brunofgc 0:8c59a494a451 88 u.v = *v;
brunofgc 0:8c59a494a451 89 for(i=0; i<4; i++) {
brunofgc 0:8c59a494a451 90 p[i]=u.c[3-i];
brunofgc 0:8c59a494a451 91 }
brunofgc 0:8c59a494a451 92 } else {
brunofgc 0:8c59a494a451 93 for(i=0; i<4; i++) {
brunofgc 0:8c59a494a451 94 u.c[3-i]=p[i];
brunofgc 0:8c59a494a451 95 }
brunofgc 0:8c59a494a451 96 *v = u.v;
brunofgc 0:8c59a494a451 97 }
brunofgc 0:8c59a494a451 98 }
brunofgc 0:8c59a494a451 99
brunofgc 0:8c59a494a451 100 void device::uint16_t_ToBin(uint8_t dir, uint16_t *v,uint8_t *p)
brunofgc 0:8c59a494a451 101 {
brunofgc 0:8c59a494a451 102 //Variáveis locais
brunofgc 0:8c59a494a451 103 union {
brunofgc 0:8c59a494a451 104 char c[2];
brunofgc 0:8c59a494a451 105 uint32_t v;
brunofgc 0:8c59a494a451 106 }u;
brunofgc 0:8c59a494a451 107 int i;
brunofgc 0:8c59a494a451 108
brunofgc 0:8c59a494a451 109 if(dir) {
brunofgc 0:8c59a494a451 110 u.v = *v;
brunofgc 0:8c59a494a451 111 for(i=0; i<2; i++) {
brunofgc 0:8c59a494a451 112 p[i]=u.c[1-i];
brunofgc 0:8c59a494a451 113 }
brunofgc 0:8c59a494a451 114 } else {
brunofgc 0:8c59a494a451 115 for(i=0; i<2; i++) {
brunofgc 0:8c59a494a451 116 u.c[1-i]=p[i];
brunofgc 0:8c59a494a451 117 }
brunofgc 0:8c59a494a451 118 *v = u.v;
brunofgc 0:8c59a494a451 119 }
brunofgc 0:8c59a494a451 120 }
brunofgc 0:8c59a494a451 121
brunofgc 0:8c59a494a451 122 void device::verifyAlarms(){
brunofgc 0:8c59a494a451 123 //Inicio da verificação
brunofgc 0:8c59a494a451 124 uint16_t alarmIndex;
brunofgc 0:8c59a494a451 125 float leitura_float,value_float;
brunofgc 0:8c59a494a451 126 uint32_t leitura_uint32_t,value_uint32_t;
brunofgc 0:8c59a494a451 127 uint16_t leitura_uint16_t,value_uint16_t;
brunofgc 0:8c59a494a451 128 bool leitura_bit,value_bit;
brunofgc 0:8c59a494a451 129 bool alarmFound;
brunofgc 0:8c59a494a451 130 char aux[30];
brunofgc 0:8c59a494a451 131
brunofgc 0:8c59a494a451 132 for(alarmIndex = 0;alarmIndex < this->numAlarms;alarmIndex++){
brunofgc 0:8c59a494a451 133 alarmFound=this->alarms[alarmIndex].alarmFound;
brunofgc 0:8c59a494a451 134 switch(this->alarms[alarmIndex].type){
brunofgc 0:8c59a494a451 135 case modBusType_float:
brunofgc 0:8c59a494a451 136 //Montar float!
brunofgc 0:8c59a494a451 137 this->floatToBin(0,&value_float,&this->alarms[alarmIndex].value[0]);
brunofgc 0:8c59a494a451 138
brunofgc 0:8c59a494a451 139 if(this->alarms[alarmIndex].addrModbusRead != 254){
brunofgc 0:8c59a494a451 140 //Bloco de leitura da variável modBus;
brunofgc 0:8c59a494a451 141 modBusMaster1::readFloat(
brunofgc 0:8c59a494a451 142 this->alarms[alarmIndex].addrModbusRead,
brunofgc 0:8c59a494a451 143 this->alarms[alarmIndex].funcModbusRead,
brunofgc 0:8c59a494a451 144 this->alarms[alarmIndex].regModbusRead,
brunofgc 0:8c59a494a451 145 1,
brunofgc 0:8c59a494a451 146 &leitura_float
brunofgc 0:8c59a494a451 147 );
brunofgc 0:8c59a494a451 148 }else{
brunofgc 0:8c59a494a451 149 leitura_float = aiFiltrada[this->alarms[alarmIndex].regModbusRead];
brunofgc 0:8c59a494a451 150 }
brunofgc 0:8c59a494a451 151
brunofgc 0:8c59a494a451 152 if(this->alarms[alarmIndex].max!=0){
brunofgc 0:8c59a494a451 153 if(leitura_float > value_float){
brunofgc 0:8c59a494a451 154 this->alarms[alarmIndex].secAlarm++;
brunofgc 0:8c59a494a451 155 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 0:8c59a494a451 156 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 0:8c59a494a451 157 alarmFound = true;
brunofgc 0:8c59a494a451 158 }
brunofgc 0:8c59a494a451 159 }else{
brunofgc 0:8c59a494a451 160 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 0:8c59a494a451 161 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 162 alarmFound = false;
brunofgc 0:8c59a494a451 163 }else{
brunofgc 0:8c59a494a451 164 if(this->alarms[alarmIndex].secAlarm){
brunofgc 0:8c59a494a451 165 if(alarmFound){
brunofgc 0:8c59a494a451 166 this->alarms[alarmIndex].secAlarm--;
brunofgc 0:8c59a494a451 167 }else{
brunofgc 0:8c59a494a451 168 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 169 }
brunofgc 0:8c59a494a451 170 }
brunofgc 0:8c59a494a451 171 }
brunofgc 0:8c59a494a451 172 }
brunofgc 0:8c59a494a451 173 }else{
brunofgc 0:8c59a494a451 174 if(leitura_float < value_float){
brunofgc 0:8c59a494a451 175 this->alarms[alarmIndex].secAlarm++;
brunofgc 0:8c59a494a451 176 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 0:8c59a494a451 177 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 0:8c59a494a451 178 alarmFound = true;
brunofgc 0:8c59a494a451 179 }
brunofgc 0:8c59a494a451 180 }else{
brunofgc 0:8c59a494a451 181 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 0:8c59a494a451 182 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 183 alarmFound = false;
brunofgc 0:8c59a494a451 184 }else{
brunofgc 0:8c59a494a451 185 if(this->alarms[alarmIndex].secAlarm){
brunofgc 0:8c59a494a451 186 if(alarmFound){
brunofgc 0:8c59a494a451 187 this->alarms[alarmIndex].secAlarm--;
brunofgc 0:8c59a494a451 188 }else{
brunofgc 0:8c59a494a451 189 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 190 }
brunofgc 0:8c59a494a451 191 }
brunofgc 0:8c59a494a451 192 }
brunofgc 0:8c59a494a451 193 }
brunofgc 0:8c59a494a451 194 }
brunofgc 0:8c59a494a451 195 break;
brunofgc 0:8c59a494a451 196 case modBusType_uint32_t:
brunofgc 0:8c59a494a451 197 //Montar uint32_t!
brunofgc 0:8c59a494a451 198 this->uint32_t_ToBin(0,&value_uint32_t,&this->alarms[alarmIndex].value[0]);
brunofgc 0:8c59a494a451 199
brunofgc 0:8c59a494a451 200 //Bloco de leitura da variável modBus;
brunofgc 0:8c59a494a451 201 modBusMaster1::readRegister32BIT(
brunofgc 0:8c59a494a451 202 this->alarms[alarmIndex].addrModbusRead,
brunofgc 0:8c59a494a451 203 this->alarms[alarmIndex].funcModbusRead,
brunofgc 0:8c59a494a451 204 this->alarms[alarmIndex].regModbusRead,
brunofgc 0:8c59a494a451 205 1,
brunofgc 0:8c59a494a451 206 &leitura_uint32_t
brunofgc 0:8c59a494a451 207 );
brunofgc 0:8c59a494a451 208
brunofgc 0:8c59a494a451 209 if(this->alarms[alarmIndex].max!=0){
brunofgc 0:8c59a494a451 210 if(leitura_uint32_t > value_uint32_t){
brunofgc 0:8c59a494a451 211 this->alarms[alarmIndex].secAlarm++;
brunofgc 0:8c59a494a451 212 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 0:8c59a494a451 213 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 0:8c59a494a451 214 alarmFound = true;
brunofgc 0:8c59a494a451 215 }
brunofgc 0:8c59a494a451 216 }else{
brunofgc 0:8c59a494a451 217 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 0:8c59a494a451 218 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 219 alarmFound = false;
brunofgc 0:8c59a494a451 220 }else{
brunofgc 0:8c59a494a451 221 if(this->alarms[alarmIndex].secAlarm){
brunofgc 0:8c59a494a451 222 if(alarmFound){
brunofgc 0:8c59a494a451 223 this->alarms[alarmIndex].secAlarm--;
brunofgc 0:8c59a494a451 224 }else{
brunofgc 0:8c59a494a451 225 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 226 }
brunofgc 0:8c59a494a451 227 }
brunofgc 0:8c59a494a451 228 }
brunofgc 0:8c59a494a451 229 }
brunofgc 0:8c59a494a451 230 }else{
brunofgc 0:8c59a494a451 231 if(leitura_uint32_t < value_uint32_t){
brunofgc 0:8c59a494a451 232 this->alarms[alarmIndex].secAlarm++;
brunofgc 0:8c59a494a451 233 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 0:8c59a494a451 234 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 0:8c59a494a451 235 alarmFound = true;
brunofgc 0:8c59a494a451 236 }
brunofgc 0:8c59a494a451 237 }else{
brunofgc 0:8c59a494a451 238 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 0:8c59a494a451 239 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 240 alarmFound = false;
brunofgc 0:8c59a494a451 241 }else{
brunofgc 0:8c59a494a451 242 if(this->alarms[alarmIndex].secAlarm){
brunofgc 0:8c59a494a451 243 if(alarmFound){
brunofgc 0:8c59a494a451 244 this->alarms[alarmIndex].secAlarm--;
brunofgc 0:8c59a494a451 245 }else{
brunofgc 0:8c59a494a451 246 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 247 }
brunofgc 0:8c59a494a451 248 }
brunofgc 0:8c59a494a451 249 }
brunofgc 0:8c59a494a451 250 }
brunofgc 0:8c59a494a451 251 }
brunofgc 0:8c59a494a451 252 break;
brunofgc 0:8c59a494a451 253
brunofgc 0:8c59a494a451 254 case modBusType_uint16_t:
brunofgc 0:8c59a494a451 255 //Montar uint16_t!
brunofgc 0:8c59a494a451 256 this->uint16_t_ToBin(0,&value_uint16_t,&this->alarms[alarmIndex].value[0]);
brunofgc 0:8c59a494a451 257
brunofgc 0:8c59a494a451 258 if(this->alarms[alarmIndex].addrModbusRead!=255){
brunofgc 0:8c59a494a451 259 //Bloco de leitura da variável modBus;
brunofgc 0:8c59a494a451 260 modBusMaster1::readRegister16BIT(
brunofgc 0:8c59a494a451 261 this->alarms[alarmIndex].addrModbusRead,
brunofgc 0:8c59a494a451 262 this->alarms[alarmIndex].funcModbusRead,
brunofgc 0:8c59a494a451 263 this->alarms[alarmIndex].regModbusRead,
brunofgc 0:8c59a494a451 264 1,
brunofgc 0:8c59a494a451 265 &leitura_uint16_t
brunofgc 0:8c59a494a451 266 );
brunofgc 0:8c59a494a451 267 }else{
brunofgc 0:8c59a494a451 268 if((this->alarms[alarmIndex].regModbusRead > 0) &&(this->alarms[alarmIndex].regModbusRead<4)){
brunofgc 0:8c59a494a451 269 leitura_uint16_t = aiFiltrada[this->alarms[alarmIndex].regModbusRead-1];
brunofgc 0:8c59a494a451 270 }
brunofgc 0:8c59a494a451 271 }
brunofgc 0:8c59a494a451 272
brunofgc 0:8c59a494a451 273 if(this->alarms[alarmIndex].max!=0){
brunofgc 0:8c59a494a451 274 if(leitura_uint16_t > value_uint16_t){
brunofgc 0:8c59a494a451 275 this->alarms[alarmIndex].secAlarm++;
brunofgc 0:8c59a494a451 276 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 0:8c59a494a451 277 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 0:8c59a494a451 278 alarmFound = true;
brunofgc 0:8c59a494a451 279 }
brunofgc 0:8c59a494a451 280 }else{
brunofgc 0:8c59a494a451 281 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 0:8c59a494a451 282 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 283 alarmFound = false;
brunofgc 0:8c59a494a451 284 }else{
brunofgc 0:8c59a494a451 285 if(this->alarms[alarmIndex].secAlarm){
brunofgc 0:8c59a494a451 286 if(alarmFound){
brunofgc 0:8c59a494a451 287 this->alarms[alarmIndex].secAlarm--;
brunofgc 0:8c59a494a451 288 }else{
brunofgc 0:8c59a494a451 289 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 290 }
brunofgc 0:8c59a494a451 291 }
brunofgc 0:8c59a494a451 292 }
brunofgc 0:8c59a494a451 293 }
brunofgc 0:8c59a494a451 294 }else{
brunofgc 0:8c59a494a451 295 if(leitura_uint16_t < value_uint16_t){
brunofgc 0:8c59a494a451 296 this->alarms[alarmIndex].secAlarm++;
brunofgc 0:8c59a494a451 297 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 0:8c59a494a451 298 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 0:8c59a494a451 299 alarmFound = true;
brunofgc 0:8c59a494a451 300 }
brunofgc 0:8c59a494a451 301 }else{
brunofgc 0:8c59a494a451 302 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 0:8c59a494a451 303 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 304 alarmFound = false;
brunofgc 0:8c59a494a451 305 }else{
brunofgc 0:8c59a494a451 306 if(this->alarms[alarmIndex].secAlarm){
brunofgc 0:8c59a494a451 307 if(alarmFound){
brunofgc 0:8c59a494a451 308 this->alarms[alarmIndex].secAlarm--;
brunofgc 0:8c59a494a451 309 }else{
brunofgc 0:8c59a494a451 310 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 311 }
brunofgc 0:8c59a494a451 312 }
brunofgc 0:8c59a494a451 313 }
brunofgc 0:8c59a494a451 314 }
brunofgc 0:8c59a494a451 315 }
brunofgc 0:8c59a494a451 316 break;
brunofgc 0:8c59a494a451 317
brunofgc 0:8c59a494a451 318 case modBusType_bit:
brunofgc 0:8c59a494a451 319 //Montar bit!
brunofgc 0:8c59a494a451 320 //O valor de alarm para bit é o value[0]!!!
brunofgc 0:8c59a494a451 321 value_bit = (this->alarms[alarmIndex].value[0] > 0); //Qualquer valor maior que zero da saída 1
brunofgc 0:8c59a494a451 322
brunofgc 0:8c59a494a451 323 if(this->alarms[alarmIndex].addrModbusRead != 254){
brunofgc 0:8c59a494a451 324 //Bloco de leitura da variável modBus;
brunofgc 0:8c59a494a451 325 modBusMaster1::readCoils(
brunofgc 0:8c59a494a451 326 this->alarms[alarmIndex].addrModbusRead,
brunofgc 0:8c59a494a451 327 this->alarms[alarmIndex].regModbusRead,
brunofgc 0:8c59a494a451 328 1,
brunofgc 0:8c59a494a451 329 &leitura_bit
brunofgc 0:8c59a494a451 330 );
brunofgc 0:8c59a494a451 331 }else{
brunofgc 0:8c59a494a451 332 leitura_bit = entradasDigitais[this->alarms[alarmIndex].regModbusRead];
brunofgc 0:8c59a494a451 333 }
brunofgc 0:8c59a494a451 334
brunofgc 0:8c59a494a451 335 if(leitura_bit == value_bit){
brunofgc 0:8c59a494a451 336 this->alarms[alarmIndex].secAlarm++;
brunofgc 0:8c59a494a451 337 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 0:8c59a494a451 338 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 0:8c59a494a451 339 alarmFound = true;
brunofgc 0:8c59a494a451 340 }
brunofgc 0:8c59a494a451 341 }else{
brunofgc 0:8c59a494a451 342 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 0:8c59a494a451 343 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 344 alarmFound = false;
brunofgc 0:8c59a494a451 345 }else{
brunofgc 0:8c59a494a451 346 if(this->alarms[alarmIndex].secAlarm){
brunofgc 0:8c59a494a451 347 if(alarmFound){
brunofgc 0:8c59a494a451 348 this->alarms[alarmIndex].secAlarm--;
brunofgc 0:8c59a494a451 349 }else{
brunofgc 0:8c59a494a451 350 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 351 }
brunofgc 0:8c59a494a451 352 }
brunofgc 0:8c59a494a451 353 }
brunofgc 0:8c59a494a451 354 }
brunofgc 0:8c59a494a451 355 break;
brunofgc 0:8c59a494a451 356
brunofgc 0:8c59a494a451 357 }
brunofgc 0:8c59a494a451 358
brunofgc 0:8c59a494a451 359 //Verifico se houve alarme não tratado
brunofgc 0:8c59a494a451 360 if(this->alarms[alarmIndex].alarmFound!=alarmFound){
brunofgc 0:8c59a494a451 361 this->alarms[alarmIndex].alarmFound=alarmFound;
brunofgc 0:8c59a494a451 362 if(alarmFound){
brunofgc 0:8c59a494a451 363 printf("Alarm FOUND! Id <%lu>.\n",this->alarms[alarmIndex].id);
brunofgc 0:8c59a494a451 364 sdCardBuf.fill("log{alarm:event:found;id:",25);
brunofgc 0:8c59a494a451 365 sprintf(aux,"%lu",this->alarms[alarmIndex].id);
brunofgc 0:8c59a494a451 366 sdCardBuf.fill(aux,strlen(aux));
brunofgc 0:8c59a494a451 367
brunofgc 0:8c59a494a451 368 sdCardBuf.fill(";timestamp:",11);
brunofgc 0:8c59a494a451 369 sprintf(aux,"%lu",time(NULL));
brunofgc 0:8c59a494a451 370 sdCardBuf.fill(aux,strlen(aux));
brunofgc 0:8c59a494a451 371
brunofgc 0:8c59a494a451 372 sdCardBuf.fill(";reading:",9);
brunofgc 0:8c59a494a451 373 if(this->alarms[alarmIndex].type == modBusType_float){
brunofgc 0:8c59a494a451 374 sprintf(aux,"%f",leitura_float);
brunofgc 0:8c59a494a451 375 }else if(this->alarms[alarmIndex].type == modBusType_uint32_t){
brunofgc 0:8c59a494a451 376 sprintf(aux,"%lu",leitura_uint32_t);
brunofgc 0:8c59a494a451 377 }else if(this->alarms[alarmIndex].type == modBusType_uint16_t){
brunofgc 0:8c59a494a451 378 sprintf(aux,"%lu",leitura_uint16_t);
brunofgc 0:8c59a494a451 379 }else if(this->alarms[alarmIndex].type == modBusType_bit){
brunofgc 0:8c59a494a451 380 sprintf(aux,"%lu",leitura_bit);
brunofgc 0:8c59a494a451 381 }
brunofgc 0:8c59a494a451 382 sdCardBuf.fill(aux,strlen(aux));
brunofgc 0:8c59a494a451 383 leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit
brunofgc 0:8c59a494a451 384 if((!modemCom::status.emComunicacao)&&(!sdCard::arquivoAberto())){
brunofgc 0:8c59a494a451 385 sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),leitura_uint16_t);
brunofgc 0:8c59a494a451 386 }
brunofgc 0:8c59a494a451 387
brunofgc 0:8c59a494a451 388 this->execAct(this->alarms[alarmIndex].idAct);
brunofgc 0:8c59a494a451 389 }else{
brunofgc 0:8c59a494a451 390 printf("Alarm RELEASE! id <%lu>.\n",this->alarms[alarmIndex].id);
brunofgc 0:8c59a494a451 391 sdCardBuf.fill("log{alarm:event:release;id:",27);
brunofgc 0:8c59a494a451 392 sprintf(aux,"%lu",this->alarms[alarmIndex].id);
brunofgc 0:8c59a494a451 393 sdCardBuf.fill(aux,strlen(aux));
brunofgc 0:8c59a494a451 394
brunofgc 0:8c59a494a451 395 sdCardBuf.fill(";timestamp:",11);
brunofgc 0:8c59a494a451 396 sprintf(aux,"%lu",time(NULL));
brunofgc 0:8c59a494a451 397 sdCardBuf.fill(aux,strlen(aux));
brunofgc 0:8c59a494a451 398
brunofgc 0:8c59a494a451 399 leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit
brunofgc 0:8c59a494a451 400 if((!modemCom::status.emComunicacao)&&(!sdCard::arquivoAberto())){
brunofgc 0:8c59a494a451 401 sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),leitura_uint16_t);
brunofgc 0:8c59a494a451 402 }
brunofgc 0:8c59a494a451 403 }
brunofgc 0:8c59a494a451 404 //atrasa o envio em 3 segundos para que possa ser gravado o registro do alarme.
brunofgc 0:8c59a494a451 405 enviaDadosPorAlarme=10;
brunofgc 0:8c59a494a451 406 }
brunofgc 0:8c59a494a451 407
brunofgc 0:8c59a494a451 408 /*printf("\n\nAlarme \"%s\".\n",this->alarms[alarmIndex].name);
brunofgc 0:8c59a494a451 409 printf("\nsecAlarme <%lu>.\n",this->alarms[alarmIndex].secAlarm);
brunofgc 0:8c59a494a451 410 printf("\nthis->alarms[alarmIndex].alarmFound <%lu>.\n",this->alarms[alarmIndex].alarmFound);
brunofgc 0:8c59a494a451 411 printf("\nalarmFound <%lu>.\n",alarmFound);
brunofgc 0:8c59a494a451 412 printf("\nmax <%lu>.\n",this->alarms[alarmIndex].max);*/
brunofgc 0:8c59a494a451 413 }
brunofgc 0:8c59a494a451 414 }
brunofgc 0:8c59a494a451 415
brunofgc 0:8c59a494a451 416 void device::verifySchedules(){
brunofgc 0:8c59a494a451 417 char stringTime[15];
brunofgc 0:8c59a494a451 418 char *weekdayString;
brunofgc 0:8c59a494a451 419 time_t seconds = time(NULL);
brunofgc 0:8c59a494a451 420 uint8_t hora;
brunofgc 0:8c59a494a451 421 uint16_t minuto;
brunofgc 0:8c59a494a451 422 uint8_t weekday=0;
brunofgc 0:8c59a494a451 423 const char* weekdayTable[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
brunofgc 0:8c59a494a451 424 uint8_t indexSchedule;
brunofgc 0:8c59a494a451 425
brunofgc 0:8c59a494a451 426 //Tue,19:09
brunofgc 0:8c59a494a451 427 strftime(stringTime, 15, "%a,%R", localtime(&seconds));
brunofgc 0:8c59a494a451 428
brunofgc 0:8c59a494a451 429 weekdayString = strtok(stringTime,",");
brunofgc 0:8c59a494a451 430 weekday = 0;
brunofgc 0:8c59a494a451 431 while((weekday<10)&&(strstr(weekdayTable[weekday],weekdayString)==NULL)){
brunofgc 0:8c59a494a451 432 weekday++;
brunofgc 0:8c59a494a451 433 }
brunofgc 0:8c59a494a451 434 hora = atoi(strtok(NULL,":"));
brunofgc 0:8c59a494a451 435 minuto = atoi(strtok(NULL,""));
brunofgc 0:8c59a494a451 436 minuto = (hora*60)+minuto;
brunofgc 0:8c59a494a451 437 for(indexSchedule = 0; indexSchedule < qtdSchedules; indexSchedule++){
brunofgc 0:8c59a494a451 438 //this->schedules[indexSchedule].weekday (Sun bit0, Mon bit1, Tue bit2, etc...)
brunofgc 0:8c59a494a451 439 if(this->schedules[indexSchedule].weekday&(0x1<<weekday)){
brunofgc 0:8c59a494a451 440 if(this->schedules[indexSchedule].minute == minuto){
brunofgc 0:8c59a494a451 441 //Verifico se já executei e então executo a ação.
brunofgc 0:8c59a494a451 442 if(!this->schedules[indexSchedule].actSent){
brunofgc 0:8c59a494a451 443 //Executa a ação;
brunofgc 0:8c59a494a451 444 this->execAct(this->schedules[indexSchedule].idAct);
brunofgc 0:8c59a494a451 445 this->schedules[indexSchedule].actSent = true;
brunofgc 0:8c59a494a451 446 }
brunofgc 0:8c59a494a451 447 }else{
brunofgc 0:8c59a494a451 448 this->schedules[indexSchedule].actSent = false;
brunofgc 0:8c59a494a451 449 }
brunofgc 0:8c59a494a451 450 }
brunofgc 0:8c59a494a451 451 }
brunofgc 0:8c59a494a451 452 }
brunofgc 0:8c59a494a451 453
brunofgc 0:8c59a494a451 454 /*
brunofgc 0:8c59a494a451 455 void device::verifySchedules(){
brunofgc 0:8c59a494a451 456 char stringTime[15];
brunofgc 0:8c59a494a451 457 char *weekdayString;
brunofgc 0:8c59a494a451 458 time_t seconds = time(NULL);
brunofgc 0:8c59a494a451 459 uint8_t hora;
brunofgc 0:8c59a494a451 460 uint16_t minuto;
brunofgc 0:8c59a494a451 461 uint8_t weekday=0;
brunofgc 0:8c59a494a451 462 const char* weekdayTable[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
brunofgc 0:8c59a494a451 463 uint8_t indexSchedule;
brunofgc 0:8c59a494a451 464
brunofgc 0:8c59a494a451 465 //Tue,19:09
brunofgc 0:8c59a494a451 466 strftime(stringTime, 15, "%a,%R", localtime(&seconds));
brunofgc 0:8c59a494a451 467
brunofgc 0:8c59a494a451 468 weekdayString = strtok(stringTime,",");
brunofgc 0:8c59a494a451 469 weekday = 0;
brunofgc 0:8c59a494a451 470 while((weekday<10)&&(strstr(weekdayTable[weekday],weekdayString)==NULL)){
brunofgc 0:8c59a494a451 471 weekday++;
brunofgc 0:8c59a494a451 472 }
brunofgc 0:8c59a494a451 473 weekday++;
brunofgc 0:8c59a494a451 474 hora = atoi(strtok(NULL,":"));
brunofgc 0:8c59a494a451 475 minuto = atoi(strtok(NULL,""));
brunofgc 0:8c59a494a451 476 minuto = (hora*60)+minuto;
brunofgc 0:8c59a494a451 477 //printf("\r\n\r\nMinuto %lu.\r\n\r\n",minuto);
brunofgc 0:8c59a494a451 478 for(indexSchedule = 0; indexSchedule < qtdSchedules; indexSchedule++){
brunofgc 0:8c59a494a451 479 //printf("Executando item %u do for de schedules.\r\n\r\n",indexSchedule);
brunofgc 0:8c59a494a451 480 if((this->schedules[indexSchedule].weekday == weekday)||(this->schedules[indexSchedule].weekday == 0)){
brunofgc 0:8c59a494a451 481 //Se for um alarme para o dia de hoje ou o alarme independer de dia (Se o dia for zero)
brunofgc 0:8c59a494a451 482 //printf("Hoje eh dia de executar a acão do schedule indice %u.\r\n\r\n",indexSchedule);
brunofgc 0:8c59a494a451 483 if(this->schedules[indexSchedule].minute == minuto){
brunofgc 0:8c59a494a451 484 //Verifico se já executei e então executo a ação.
brunofgc 0:8c59a494a451 485 if(!this->schedules[indexSchedule].actSent){
brunofgc 0:8c59a494a451 486 //Executa a ação;
brunofgc 0:8c59a494a451 487 //printf("Executando a acao!\r\n\r\n");
brunofgc 0:8c59a494a451 488 this->execAct(this->schedules[indexSchedule].idAct);
brunofgc 0:8c59a494a451 489 this->schedules[indexSchedule].actSent = true;
brunofgc 0:8c59a494a451 490 }
brunofgc 0:8c59a494a451 491 }else{
brunofgc 0:8c59a494a451 492 this->schedules[indexSchedule].actSent = false;
brunofgc 0:8c59a494a451 493 }
brunofgc 0:8c59a494a451 494 }
brunofgc 0:8c59a494a451 495 }
brunofgc 0:8c59a494a451 496 }
brunofgc 0:8c59a494a451 497 */
brunofgc 0:8c59a494a451 498 /*
brunofgc 0:8c59a494a451 499 uint8_t device::execActSchedule(uint32_t index){
brunofgc 0:8c59a494a451 500 switch(this->schedules[index].actType){
brunofgc 0:8c59a494a451 501 case modBusType_float:
brunofgc 0:8c59a494a451 502 float floatValueToWrite;
brunofgc 0:8c59a494a451 503 //writeFloat(uint8_t,uint16_t,uint8_t,float*);
brunofgc 0:8c59a494a451 504 this->floatToBin(0,&floatValueToWrite,&this->schedules[index].actValue[0]);
brunofgc 0:8c59a494a451 505 modBusMaster1::writeFloat(
brunofgc 0:8c59a494a451 506 this->schedules[index].param1,
brunofgc 0:8c59a494a451 507 this->schedules[index].param2,
brunofgc 0:8c59a494a451 508 this->schedules[index].param3,
brunofgc 0:8c59a494a451 509 &floatValueToWrite
brunofgc 0:8c59a494a451 510 );
brunofgc 0:8c59a494a451 511 break;
brunofgc 0:8c59a494a451 512 case modBusType_uint32_t:
brunofgc 0:8c59a494a451 513 uint32_t uint32ValueToWrite;
brunofgc 0:8c59a494a451 514 this->uint32_t_ToBin(0,&uint32ValueToWrite,&this->schedules[index].actValue[0]);
brunofgc 0:8c59a494a451 515 modBusMaster1::writeRegister32BIT(
brunofgc 0:8c59a494a451 516 this->schedules[index].param1,
brunofgc 0:8c59a494a451 517 this->schedules[index].param2,
brunofgc 0:8c59a494a451 518 this->schedules[index].param3,
brunofgc 0:8c59a494a451 519 &uint32ValueToWrite
brunofgc 0:8c59a494a451 520 );
brunofgc 0:8c59a494a451 521 break;
brunofgc 0:8c59a494a451 522 case modBusType_uint16_t:
brunofgc 0:8c59a494a451 523 uint16_t uint16ValueToWrite;
brunofgc 0:8c59a494a451 524 this->uint16_t_ToBin(0,&uint16ValueToWrite,&this->schedules[index].actValue[0]);
brunofgc 0:8c59a494a451 525 modBusMaster1::writeRegister16BIT(
brunofgc 0:8c59a494a451 526 this->schedules[index].param1,
brunofgc 0:8c59a494a451 527 this->schedules[index].param2,
brunofgc 0:8c59a494a451 528 this->schedules[index].param3,
brunofgc 0:8c59a494a451 529 &uint16ValueToWrite
brunofgc 0:8c59a494a451 530 );
brunofgc 0:8c59a494a451 531 break;
brunofgc 0:8c59a494a451 532 case modBusType_bit:
brunofgc 0:8c59a494a451 533 bool boolValueToWrite;
brunofgc 0:8c59a494a451 534 boolValueToWrite = (this->schedules[index].actValue[0] != 0);
brunofgc 0:8c59a494a451 535 modBusMaster1::writeSingleCoil(
brunofgc 0:8c59a494a451 536 this->schedules[index].param1,
brunofgc 0:8c59a494a451 537 this->schedules[index].param3,
brunofgc 0:8c59a494a451 538 boolValueToWrite
brunofgc 0:8c59a494a451 539 );
brunofgc 0:8c59a494a451 540 break;
brunofgc 0:8c59a494a451 541 case IrCommandType:
brunofgc 0:8c59a494a451 542 //Busca no arquivo
brunofgc 0:8c59a494a451 543 //Abre arquivo
brunofgc 0:8c59a494a451 544 char comandoIr[1024];
brunofgc 0:8c59a494a451 545 char indexBusca[10];
brunofgc 0:8c59a494a451 546 bool endOfFile = false;
brunofgc 0:8c59a494a451 547
brunofgc 0:8c59a494a451 548 //printf("\r\n===================DEBUG(Schedule)===================\r\n");
brunofgc 0:8c59a494a451 549 //"Ir:0,6E01FFFFS"
brunofgc 0:8c59a494a451 550 sprintf(indexBusca,"Ir:%u,",this->schedules[index].param3);
brunofgc 0:8c59a494a451 551 //printf("indexBusca = <%s>.\r\n",indexBusca);
brunofgc 0:8c59a494a451 552 if(sdCard::abreArquivo(&sdCard::devices,"r")){
brunofgc 0:8c59a494a451 553 do{
brunofgc 0:8c59a494a451 554 fgets(comandoIr,1024,sdCard::devices.fp);
brunofgc 0:8c59a494a451 555 endOfFile = feof(sdCard::devices.fp);
brunofgc 0:8c59a494a451 556 //printf("Linha comando Ir = <%s>.\r\nendOfFile = %u\r\n",comandoIr,endOfFile);
brunofgc 0:8c59a494a451 557 }while((strstr(comandoIr,indexBusca)==NULL)&&(endOfFile==0));
brunofgc 0:8c59a494a451 558 sdCard::fechaArquivo(&sdCard::devices);
brunofgc 0:8c59a494a451 559
brunofgc 0:8c59a494a451 560 //Vê se deu tudo certo, se não encontrou, retorna false
brunofgc 0:8c59a494a451 561 if(!endOfFile){
brunofgc 0:8c59a494a451 562 //Encontrado o comando, vou desempacotar ele.
brunofgc 0:8c59a494a451 563 strtok(comandoIr,",");
brunofgc 0:8c59a494a451 564 //Neste ponto já tenho o meu comando pronto para ser executado dentro de comandoIr;
brunofgc 0:8c59a494a451 565 //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 566 deserializaPacoteIR(strtok(NULL,"S"));
brunofgc 0:8c59a494a451 567 enviaComandoIR(this->schedules[index].param1,this->schedules[index].param2);
brunofgc 0:8c59a494a451 568 }
brunofgc 0:8c59a494a451 569 }else{
brunofgc 0:8c59a494a451 570 return false;
brunofgc 0:8c59a494a451 571 }
brunofgc 0:8c59a494a451 572 //printf("\r\n===================DEBUG(Schedule)===================\r\n");
brunofgc 0:8c59a494a451 573 break;
brunofgc 0:8c59a494a451 574 }
brunofgc 0:8c59a494a451 575 return true;
brunofgc 0:8c59a494a451 576 }
brunofgc 0:8c59a494a451 577 */
brunofgc 0:8c59a494a451 578 /*
brunofgc 0:8c59a494a451 579 uint8_t device::execActAlarm(uint32_t index){
brunofgc 0:8c59a494a451 580 switch(this->alarms[index].actType){
brunofgc 0:8c59a494a451 581 case modBusType_float:
brunofgc 0:8c59a494a451 582 float floatValueToWrite;
brunofgc 0:8c59a494a451 583 //writeFloat(uint8_t,uint16_t,uint8_t,float*);
brunofgc 0:8c59a494a451 584 this->floatToBin(0,&floatValueToWrite,&this->alarms[index].actValue[0]);
brunofgc 0:8c59a494a451 585 modBusMaster1::writeFloat(
brunofgc 0:8c59a494a451 586 this->alarms[index].addrModbusWrite,
brunofgc 0:8c59a494a451 587 this->alarms[index].funcModbusWrite,
brunofgc 0:8c59a494a451 588 this->alarms[index].regModbusWrite,
brunofgc 0:8c59a494a451 589 &floatValueToWrite
brunofgc 0:8c59a494a451 590 );
brunofgc 0:8c59a494a451 591 break;
brunofgc 0:8c59a494a451 592 case modBusType_uint32_t:
brunofgc 0:8c59a494a451 593 uint32_t uint32ValueToWrite;
brunofgc 0:8c59a494a451 594 this->uint32_t_ToBin(0,&uint32ValueToWrite,&this->alarms[index].actValue[0]);
brunofgc 0:8c59a494a451 595 modBusMaster1::writeRegister32BIT(
brunofgc 0:8c59a494a451 596 this->alarms[index].addrModbusWrite,
brunofgc 0:8c59a494a451 597 this->alarms[index].funcModbusWrite,
brunofgc 0:8c59a494a451 598 this->alarms[index].regModbusWrite,
brunofgc 0:8c59a494a451 599 &uint32ValueToWrite
brunofgc 0:8c59a494a451 600 );
brunofgc 0:8c59a494a451 601 break;
brunofgc 0:8c59a494a451 602 case modBusType_uint16_t:
brunofgc 0:8c59a494a451 603 uint16_t uint16ValueToWrite;
brunofgc 0:8c59a494a451 604 this->uint16_t_ToBin(0,&uint16ValueToWrite,&this->alarms[index].actValue[0]);
brunofgc 0:8c59a494a451 605 modBusMaster1::writeRegister16BIT(
brunofgc 0:8c59a494a451 606 this->alarms[index].addrModbusWrite,
brunofgc 0:8c59a494a451 607 this->alarms[index].funcModbusWrite,
brunofgc 0:8c59a494a451 608 this->alarms[index].regModbusWrite,
brunofgc 0:8c59a494a451 609 &uint16ValueToWrite
brunofgc 0:8c59a494a451 610 );
brunofgc 0:8c59a494a451 611 break;
brunofgc 0:8c59a494a451 612 case modBusType_bit:
brunofgc 0:8c59a494a451 613 bool boolValueToWrite;
brunofgc 0:8c59a494a451 614 boolValueToWrite = (this->alarms[index].actValue[0] != 0);
brunofgc 0:8c59a494a451 615 modBusMaster1::writeSingleCoil(
brunofgc 0:8c59a494a451 616 this->alarms[index].addrModbusWrite,
brunofgc 0:8c59a494a451 617 this->alarms[index].regModbusWrite,
brunofgc 0:8c59a494a451 618 boolValueToWrite
brunofgc 0:8c59a494a451 619 );
brunofgc 0:8c59a494a451 620 break;
brunofgc 0:8c59a494a451 621 }
brunofgc 0:8c59a494a451 622 return true;
brunofgc 0:8c59a494a451 623 }
brunofgc 0:8c59a494a451 624 */
brunofgc 0:8c59a494a451 625
brunofgc 0:8c59a494a451 626 void device::setReading(char *linha)
brunofgc 0:8c59a494a451 627 {
brunofgc 0:8c59a494a451 628 uint8_t addr=0;
brunofgc 0:8c59a494a451 629 uint8_t func=0;
brunofgc 0:8c59a494a451 630 uint16_t reg=0;
brunofgc 0:8c59a494a451 631 uint8_t numRegs=0;
brunofgc 0:8c59a494a451 632 char *pChar;
brunofgc 0:8c59a494a451 633 bool inputData = false;
brunofgc 0:8c59a494a451 634
brunofgc 0:8c59a494a451 635 //printf("Linha <%s>.\n",linha);
brunofgc 0:8c59a494a451 636 if(strstr(linha,"readingsSet")) {
brunofgc 0:8c59a494a451 637 return;
brunofgc 0:8c59a494a451 638 }
brunofgc 0:8c59a494a451 639
brunofgc 0:8c59a494a451 640 //printf("O tamanho da struct eh <%lu>.\n",sizeof(reading));
brunofgc 0:8c59a494a451 641
brunofgc 0:8c59a494a451 642
brunofgc 0:8c59a494a451 643 if(resizeArray(this->numReadings, this->numReadings+1, (char **)&this->readings,sizeof(reading))) {
brunofgc 0:8c59a494a451 644 //printf("Resize em readings beleza!\n");
brunofgc 0:8c59a494a451 645 this->numReadings++;
brunofgc 0:8c59a494a451 646 }
brunofgc 0:8c59a494a451 647
brunofgc 0:8c59a494a451 648 if(strstr(linha,"float,")) {
brunofgc 0:8c59a494a451 649 this->readings[this->numReadings-1].type=modBusType_float;
brunofgc 0:8c59a494a451 650 inputData = true;
brunofgc 0:8c59a494a451 651 }
brunofgc 0:8c59a494a451 652
brunofgc 0:8c59a494a451 653 if(strstr(linha,"uint32_t,")) {
brunofgc 0:8c59a494a451 654 this->readings[this->numReadings-1].type=modBusType_uint32_t;
brunofgc 0:8c59a494a451 655 inputData = true;
brunofgc 0:8c59a494a451 656 }
brunofgc 0:8c59a494a451 657
brunofgc 0:8c59a494a451 658 if(strstr(linha,"uint16_t,")) {
brunofgc 0:8c59a494a451 659 this->readings[this->numReadings-1].type=modBusType_uint16_t;
brunofgc 0:8c59a494a451 660 inputData = true;
brunofgc 0:8c59a494a451 661 }
brunofgc 0:8c59a494a451 662
brunofgc 0:8c59a494a451 663 if(strstr(linha,"uint8_t,")) {
brunofgc 0:8c59a494a451 664 this->readings[this->numReadings-1].type=modBusType_uint8_t;
brunofgc 0:8c59a494a451 665 inputData = true;
brunofgc 0:8c59a494a451 666 }
brunofgc 0:8c59a494a451 667
brunofgc 0:8c59a494a451 668 if(strstr(linha,"bit,")) {
brunofgc 0:8c59a494a451 669 this->readings[this->numReadings-1].type=modBusType_bit;
brunofgc 0:8c59a494a451 670 inputData = true;
brunofgc 0:8c59a494a451 671 }
brunofgc 0:8c59a494a451 672
brunofgc 0:8c59a494a451 673 if(inputData){
brunofgc 0:8c59a494a451 674 pChar = strtok(linha,",");
brunofgc 0:8c59a494a451 675
brunofgc 0:8c59a494a451 676 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 677 addr = atoi(pChar);
brunofgc 0:8c59a494a451 678
brunofgc 0:8c59a494a451 679 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 680 func = atoi(pChar);
brunofgc 0:8c59a494a451 681
brunofgc 0:8c59a494a451 682 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 683 reg = atoi(pChar);
brunofgc 0:8c59a494a451 684
brunofgc 0:8c59a494a451 685 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 686 numRegs = atoi(pChar);
brunofgc 0:8c59a494a451 687
brunofgc 0:8c59a494a451 688 this->readings[this->numReadings-1].addr=addr;
brunofgc 0:8c59a494a451 689 this->readings[this->numReadings-1].func=func;
brunofgc 0:8c59a494a451 690 this->readings[this->numReadings-1].reg=reg;
brunofgc 0:8c59a494a451 691 this->readings[this->numReadings-1].numRegs=numRegs;
brunofgc 0:8c59a494a451 692 }
brunofgc 0:8c59a494a451 693
brunofgc 0:8c59a494a451 694 }
brunofgc 0:8c59a494a451 695
brunofgc 0:8c59a494a451 696 void device::setAlarm(char *linha)
brunofgc 0:8c59a494a451 697 {
brunofgc 0:8c59a494a451 698 float floatVar;
brunofgc 0:8c59a494a451 699 uint32_t aux32_t;
brunofgc 0:8c59a494a451 700 uint16_t aux16_t;
brunofgc 0:8c59a494a451 701 char *pChar;
brunofgc 0:8c59a494a451 702
brunofgc 0:8c59a494a451 703 if(strstr(linha,"alarmSet")) {
brunofgc 0:8c59a494a451 704 if(resizeArray(this->numAlarms, this->numAlarms+1, (char **)&this->alarms,sizeof(alarm))) {
brunofgc 0:8c59a494a451 705
brunofgc 0:8c59a494a451 706 //Inicialização de contadores de alarme
brunofgc 0:8c59a494a451 707 //É necessário inicializar estes dados para quando a máquina de alarmes rodar ter um inicio correto.
brunofgc 0:8c59a494a451 708 this->alarms[this->numAlarms].secAlarm=0;
brunofgc 0:8c59a494a451 709 this->alarms[this->numAlarms].idAct=0;
brunofgc 0:8c59a494a451 710 this->alarms[this->numAlarms].alarmFound = false;
brunofgc 0:8c59a494a451 711 //Inicialização de contadores de alarme
brunofgc 0:8c59a494a451 712
brunofgc 0:8c59a494a451 713 this->numAlarms++;
brunofgc 0:8c59a494a451 714 }
brunofgc 0:8c59a494a451 715 }
brunofgc 0:8c59a494a451 716
brunofgc 0:8c59a494a451 717 if(strstr(linha,"id:")) {
brunofgc 0:8c59a494a451 718 strtok(linha,":");
brunofgc 0:8c59a494a451 719 this->alarms[this->numAlarms-1].id = atoi(strtok(NULL,"\r\n"));
brunofgc 0:8c59a494a451 720 }
brunofgc 0:8c59a494a451 721
brunofgc 0:8c59a494a451 722
brunofgc 0:8c59a494a451 723 if(strstr(linha,"seconds:")) {
brunofgc 0:8c59a494a451 724 //sscanf(linha,"seconds:%lu",&this->alarms[this->numAlarms-1].seconds);
brunofgc 0:8c59a494a451 725 strtok(linha,":");
brunofgc 0:8c59a494a451 726 pChar = strtok(NULL,"\r\n");
brunofgc 0:8c59a494a451 727 this->alarms[this->numAlarms-1].seconds = atoi(pChar);
brunofgc 0:8c59a494a451 728 }
brunofgc 0:8c59a494a451 729
brunofgc 0:8c59a494a451 730 if(strstr(linha,"act:")) {
brunofgc 0:8c59a494a451 731 strtok(linha,":");
brunofgc 0:8c59a494a451 732 this->alarms[this->numAlarms-1].idAct = atoi(strtok(NULL,""));
brunofgc 0:8c59a494a451 733 return;
brunofgc 0:8c59a494a451 734 }
brunofgc 0:8c59a494a451 735
brunofgc 0:8c59a494a451 736
brunofgc 0:8c59a494a451 737 if(strstr(linha,"bit,")){
brunofgc 0:8c59a494a451 738 this->alarms[this->numAlarms-1].type=modBusType_bit;
brunofgc 0:8c59a494a451 739
brunofgc 0:8c59a494a451 740 pChar = strtok(linha,",");
brunofgc 0:8c59a494a451 741
brunofgc 0:8c59a494a451 742 //Parametro1
brunofgc 0:8c59a494a451 743 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 744 this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 745
brunofgc 0:8c59a494a451 746 //Parametro2
brunofgc 0:8c59a494a451 747 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 748 this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 749
brunofgc 0:8c59a494a451 750 //Parametro3
brunofgc 0:8c59a494a451 751 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 752 this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 753
brunofgc 0:8c59a494a451 754 //Parametro4
brunofgc 0:8c59a494a451 755 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 756 this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 0:8c59a494a451 757
brunofgc 0:8c59a494a451 758 //Parametro5
brunofgc 0:8c59a494a451 759 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 760 this->alarms[this->numAlarms-1].value[0]= (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 0:8c59a494a451 761 }
brunofgc 0:8c59a494a451 762
brunofgc 0:8c59a494a451 763
brunofgc 0:8c59a494a451 764 if(strstr(linha,"float,")) {
brunofgc 0:8c59a494a451 765 this->alarms[this->numAlarms-1].type=modBusType_float;
brunofgc 0:8c59a494a451 766 strtok(linha,",");
brunofgc 0:8c59a494a451 767
brunofgc 0:8c59a494a451 768 //Parametro 1;
brunofgc 0:8c59a494a451 769 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 770 this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 771
brunofgc 0:8c59a494a451 772 //Parametro 2;
brunofgc 0:8c59a494a451 773 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 774 this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 775
brunofgc 0:8c59a494a451 776 //Parametro 3;
brunofgc 0:8c59a494a451 777 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 778 this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 779
brunofgc 0:8c59a494a451 780 //Parametro 4;
brunofgc 0:8c59a494a451 781 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 782 this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 0:8c59a494a451 783
brunofgc 0:8c59a494a451 784 //Parametro 5;
brunofgc 0:8c59a494a451 785 pChar = strtok(NULL,"\r\n");
brunofgc 0:8c59a494a451 786 //floatVar = diversos::stringToFloat(pChar,100);
brunofgc 0:8c59a494a451 787 floatVar = atof(pChar);
brunofgc 0:8c59a494a451 788
brunofgc 0:8c59a494a451 789 this->floatToBin(1,&floatVar,&this->alarms[this->numAlarms-1].value[0]);
brunofgc 0:8c59a494a451 790 }
brunofgc 0:8c59a494a451 791
brunofgc 0:8c59a494a451 792 if(strstr(linha,"uint32_t,")) {
brunofgc 0:8c59a494a451 793 this->alarms[this->numAlarms-1].type=modBusType_uint32_t;
brunofgc 0:8c59a494a451 794 //sscanf(linha,"max:uint32_t,%lu,%lu,%lu,%lu",&aux1,&aux2,&aux3,&aux4);
brunofgc 0:8c59a494a451 795 strtok(linha,",");
brunofgc 0:8c59a494a451 796
brunofgc 0:8c59a494a451 797 //parametro1
brunofgc 0:8c59a494a451 798 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 799 this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 800
brunofgc 0:8c59a494a451 801 //parametro2
brunofgc 0:8c59a494a451 802 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 803 this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 804
brunofgc 0:8c59a494a451 805 //parametro3
brunofgc 0:8c59a494a451 806 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 807 this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 808
brunofgc 0:8c59a494a451 809 //Parametro 4;
brunofgc 0:8c59a494a451 810 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 811 this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 0:8c59a494a451 812
brunofgc 0:8c59a494a451 813 //parametro5
brunofgc 0:8c59a494a451 814 pChar = strtok(NULL,"\r\n");
brunofgc 0:8c59a494a451 815 aux32_t = atoi(pChar);
brunofgc 0:8c59a494a451 816
brunofgc 0:8c59a494a451 817 this->uint32_t_ToBin(1,&aux32_t,&this->alarms[this->numAlarms-1].value[0]);
brunofgc 0:8c59a494a451 818 }
brunofgc 0:8c59a494a451 819
brunofgc 0:8c59a494a451 820
brunofgc 0:8c59a494a451 821
brunofgc 0:8c59a494a451 822 if(strstr(linha,"uint16_t,")) {
brunofgc 0:8c59a494a451 823 this->alarms[this->numAlarms-1].type=modBusType_uint16_t;
brunofgc 0:8c59a494a451 824 strtok(linha,",");
brunofgc 0:8c59a494a451 825
brunofgc 0:8c59a494a451 826 //parametro1
brunofgc 0:8c59a494a451 827 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 828 this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 829
brunofgc 0:8c59a494a451 830 //parametro2
brunofgc 0:8c59a494a451 831 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 832 this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 833
brunofgc 0:8c59a494a451 834 //parametro3
brunofgc 0:8c59a494a451 835 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 836 this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 837
brunofgc 0:8c59a494a451 838 //Parametro 4;
brunofgc 0:8c59a494a451 839 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 840 this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 0:8c59a494a451 841
brunofgc 0:8c59a494a451 842 //parametro5
brunofgc 0:8c59a494a451 843 pChar = strtok(NULL,"\r\n");
brunofgc 0:8c59a494a451 844 aux16_t = atoi(pChar);
brunofgc 0:8c59a494a451 845
brunofgc 0:8c59a494a451 846 this->uint16_t_ToBin(1,&aux16_t,&this->alarms[this->numAlarms-1].value[0]);
brunofgc 0:8c59a494a451 847 }
brunofgc 0:8c59a494a451 848
brunofgc 0:8c59a494a451 849
brunofgc 0:8c59a494a451 850 }
brunofgc 0:8c59a494a451 851
brunofgc 0:8c59a494a451 852 void device::setSchedule(char *linha)
brunofgc 0:8c59a494a451 853 {
brunofgc 0:8c59a494a451 854 //float floatVar;
brunofgc 0:8c59a494a451 855 //uint32_t aux32_t;
brunofgc 0:8c59a494a451 856 //uint16_t aux16_t;
brunofgc 0:8c59a494a451 857 char *pChar;
brunofgc 0:8c59a494a451 858
brunofgc 0:8c59a494a451 859 if(strstr(linha,"scheduleSet")) {
brunofgc 0:8c59a494a451 860 if(resizeArray(this->numSchedules, this->numSchedules+1, (char **)&this->schedules,sizeof(schedule))) {
brunofgc 0:8c59a494a451 861 //Inicialização de contadores de schedules
brunofgc 0:8c59a494a451 862 //É necessário inicializar estes dados para quando a máquina de schedules rodar ter um inicio correto.
brunofgc 0:8c59a494a451 863 this->schedules[this->numSchedules].actSent = false;
brunofgc 0:8c59a494a451 864 //Inicialização de contadores de schedules
brunofgc 0:8c59a494a451 865 this->numSchedules++;
brunofgc 0:8c59a494a451 866 }
brunofgc 0:8c59a494a451 867 }
brunofgc 0:8c59a494a451 868
brunofgc 0:8c59a494a451 869 if(strstr(linha,"id:")) {
brunofgc 0:8c59a494a451 870 strtok(linha,":");
brunofgc 0:8c59a494a451 871 this->schedules[this->numSchedules-1].id = atoi(strtok(NULL,"\r\n"));
brunofgc 0:8c59a494a451 872 }
brunofgc 0:8c59a494a451 873
brunofgc 0:8c59a494a451 874
brunofgc 0:8c59a494a451 875 if(strstr(linha,"dayTime:")) {
brunofgc 0:8c59a494a451 876 strtok(linha,":");
brunofgc 0:8c59a494a451 877 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 878 this->schedules[this->numSchedules-1].weekday = atoi(pChar);
brunofgc 0:8c59a494a451 879 pChar = strtok(NULL,"\r\n");
brunofgc 0:8c59a494a451 880 this->schedules[this->numSchedules-1].minute = atoi(pChar);
brunofgc 0:8c59a494a451 881 }
brunofgc 0:8c59a494a451 882
brunofgc 0:8c59a494a451 883 if(strstr(linha,"act:")) {
brunofgc 0:8c59a494a451 884 strtok(linha,":");
brunofgc 0:8c59a494a451 885 pChar = strtok(NULL,"");
brunofgc 0:8c59a494a451 886 this->schedules[this->numSchedules-1].idAct = atoi(pChar);
brunofgc 0:8c59a494a451 887 }
brunofgc 0:8c59a494a451 888 }
brunofgc 0:8c59a494a451 889
brunofgc 0:8c59a494a451 890 /*void device::writeReadingsToSD(){
brunofgc 0:8c59a494a451 891 //Esta função faz a inserção de todos os dados no arquivo armazenamento.
brunofgc 0:8c59a494a451 892 uint16_t readingIndex=0;
brunofgc 0:8c59a494a451 893
brunofgc 0:8c59a494a451 894 float *floatBuffer;
brunofgc 0:8c59a494a451 895 uint32_t *uint32_tBuffer;
brunofgc 0:8c59a494a451 896 uint16_t *uint16_tBuffer;
brunofgc 0:8c59a494a451 897 bool *boolBuffer;
brunofgc 0:8c59a494a451 898 uint32_t seconds_uint32_t;
brunofgc 0:8c59a494a451 899
brunofgc 0:8c59a494a451 900 //char registro[1024];
brunofgc 0:8c59a494a451 901 //uint16_t indexRegistro;
brunofgc 0:8c59a494a451 902
brunofgc 0:8c59a494a451 903 seconds_uint32_t = (uint32_t) time(NULL);
brunofgc 0:8c59a494a451 904
brunofgc 0:8c59a494a451 905 //Inicio da string dentro do arquivo binario
brunofgc 0:8c59a494a451 906 sdCard::insereDadosArquivo(&sdCard::armazenamento,"log{data:",9);
brunofgc 0:8c59a494a451 907
brunofgc 0:8c59a494a451 908 //O timestamp é posto nos 4 primeiros bytes (segundos)
brunofgc 0:8c59a494a451 909 sdCard::insereDadosArquivo(&sdCard::armazenamento,(char *)&seconds_uint32_t,4);
brunofgc 0:8c59a494a451 910
brunofgc 0:8c59a494a451 911 for(readingIndex = 0;readingIndex < this->numReadings;readingIndex++){
brunofgc 0:8c59a494a451 912 switch(this->readings[readingIndex].type){
brunofgc 0:8c59a494a451 913 case modBusType_float:
brunofgc 0:8c59a494a451 914 floatBuffer = (float*) malloc(this->readings[readingIndex].numRegs*sizeof(float));
brunofgc 0:8c59a494a451 915 modBusMaster1::readFloat(
brunofgc 0:8c59a494a451 916 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 917 this->readings[readingIndex].func,
brunofgc 0:8c59a494a451 918 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 919 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 920 floatBuffer
brunofgc 0:8c59a494a451 921 );
brunofgc 0:8c59a494a451 922 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 0:8c59a494a451 923 sdCard::insereDadosArquivo(&sdCard::armazenamento,(char *)floatBuffer,sizeof(float)*this->readings[readingIndex].numRegs);
brunofgc 0:8c59a494a451 924 free(floatBuffer);
brunofgc 0:8c59a494a451 925 break;
brunofgc 0:8c59a494a451 926 case modBusType_uint32_t:
brunofgc 0:8c59a494a451 927 uint32_tBuffer = (uint32_t*) malloc(this->readings[readingIndex].numRegs*sizeof(uint32_t));
brunofgc 0:8c59a494a451 928 modBusMaster1::readRegister32BIT(
brunofgc 0:8c59a494a451 929 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 930 this->readings[readingIndex].func,
brunofgc 0:8c59a494a451 931 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 932 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 933 uint32_tBuffer
brunofgc 0:8c59a494a451 934 );
brunofgc 0:8c59a494a451 935 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 0:8c59a494a451 936 sdCard::insereDadosArquivo(&sdCard::armazenamento,(char *)uint32_tBuffer,sizeof(uint32_t)*this->readings[readingIndex].numRegs);
brunofgc 0:8c59a494a451 937 free(uint32_tBuffer);
brunofgc 0:8c59a494a451 938 break;
brunofgc 0:8c59a494a451 939
brunofgc 0:8c59a494a451 940 case modBusType_uint16_t:
brunofgc 0:8c59a494a451 941 uint16_tBuffer = (uint16_t*) malloc(this->readings[readingIndex].numRegs*sizeof(uint16_t));
brunofgc 0:8c59a494a451 942 modBusMaster1::readRegister16BIT(
brunofgc 0:8c59a494a451 943 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 944 this->readings[readingIndex].func,
brunofgc 0:8c59a494a451 945 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 946 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 947 uint16_tBuffer
brunofgc 0:8c59a494a451 948 );
brunofgc 0:8c59a494a451 949 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 0:8c59a494a451 950 sdCard::insereDadosArquivo(&sdCard::armazenamento,(char *)uint16_tBuffer,sizeof(uint16_t)*this->readings[readingIndex].numRegs);
brunofgc 0:8c59a494a451 951 free(uint16_tBuffer);
brunofgc 0:8c59a494a451 952 break;
brunofgc 0:8c59a494a451 953
brunofgc 0:8c59a494a451 954 case modBusType_bit:
brunofgc 0:8c59a494a451 955 boolBuffer = (bool*) malloc(this->readings[readingIndex].numRegs*sizeof(bool));
brunofgc 0:8c59a494a451 956 modBusMaster1::readCoils(
brunofgc 0:8c59a494a451 957 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 958 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 959 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 960 boolBuffer
brunofgc 0:8c59a494a451 961 );
brunofgc 0:8c59a494a451 962 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 0:8c59a494a451 963 sdCard::insereDadosArquivo(&sdCard::armazenamento,(char *)boolBuffer,sizeof(bool)*this->readings[readingIndex].numRegs);
brunofgc 0:8c59a494a451 964 free(boolBuffer);
brunofgc 0:8c59a494a451 965 break;
brunofgc 0:8c59a494a451 966
brunofgc 0:8c59a494a451 967 }
brunofgc 0:8c59a494a451 968 }
brunofgc 0:8c59a494a451 969 sdCard::insereDadosArquivo(&sdCard::armazenamento,"}log",4);
brunofgc 0:8c59a494a451 970 }*/
brunofgc 0:8c59a494a451 971
brunofgc 0:8c59a494a451 972 uint8_t device::execAct(uint32_t index){
brunofgc 0:8c59a494a451 973 //Primeiro preciso decidir que tipo de act estou lidando para tanto preciso pegar a ACT inteira.
brunofgc 0:8c59a494a451 974 //Busca no arquivo
brunofgc 0:8c59a494a451 975 //Abre arquivo
brunofgc 0:8c59a494a451 976 char actString[1024];
brunofgc 0:8c59a494a451 977 char indexBusca[15];
brunofgc 0:8c59a494a451 978 char *pTipoComando;
brunofgc 0:8c59a494a451 979 uint8_t tipoComando;
brunofgc 0:8c59a494a451 980 bool endOfFile = false;
brunofgc 0:8c59a494a451 981
brunofgc 0:8c59a494a451 982 //Variáveis comuns;
brunofgc 0:8c59a494a451 983 uint8_t param1;
brunofgc 0:8c59a494a451 984 uint8_t param2;
brunofgc 0:8c59a494a451 985 uint16_t param3;
brunofgc 0:8c59a494a451 986 uint32_t param4;
brunofgc 0:8c59a494a451 987
brunofgc 0:8c59a494a451 988 //printf("\r\n===================DEBUG(execAct)===================\r\n");
brunofgc 0:8c59a494a451 989 sprintf(indexBusca,"actId:%lu,",index);
brunofgc 0:8c59a494a451 990 //printf("Buscando por indexBusca<%s>.\r\n",indexBusca);
brunofgc 0:8c59a494a451 991 if(sdCard::abreArquivo(&sdCard::devices,"r")){
brunofgc 0:8c59a494a451 992 do{
brunofgc 0:8c59a494a451 993 endOfFile = feof(sdCard::devices.fp);
brunofgc 0:8c59a494a451 994 if(!endOfFile){fgets(actString,1024,sdCard::devices.fp);}
brunofgc 0:8c59a494a451 995 //printf("Linha ACT = <%s>.\r\nendOfFile = %u\r\n",actString,endOfFile);
brunofgc 0:8c59a494a451 996 }while((strstr(actString,indexBusca)==NULL)&&(endOfFile==0));
brunofgc 0:8c59a494a451 997 sdCard::fechaArquivo(&sdCard::devices);
brunofgc 0:8c59a494a451 998
brunofgc 0:8c59a494a451 999 //Vê se deu tudo certo, se não encontrou, retorna false
brunofgc 0:8c59a494a451 1000 if(endOfFile){
brunofgc 0:8c59a494a451 1001 return false;
brunofgc 0:8c59a494a451 1002 }
brunofgc 0:8c59a494a451 1003 //Tudo correndo bem.
brunofgc 0:8c59a494a451 1004
brunofgc 0:8c59a494a451 1005 //printf("\r\nPoint 1\r\n");
brunofgc 0:8c59a494a451 1006
brunofgc 0:8c59a494a451 1007 //Encontrado o tipo de comando reutilizando a string indexBusca;
brunofgc 0:8c59a494a451 1008 strtok(actString,",");
brunofgc 0:8c59a494a451 1009 pTipoComando = strtok(NULL,",");
brunofgc 0:8c59a494a451 1010 //printf("\r\nPoint 2 comando <%s>\r\n",pTipoComando);
brunofgc 0:8c59a494a451 1011 if(strstr(pTipoComando,"float")){tipoComando = modBusType_float;}
brunofgc 0:8c59a494a451 1012 if(strstr(pTipoComando,"bit")){tipoComando = modBusType_bit;}
brunofgc 0:8c59a494a451 1013 if(strstr(pTipoComando,"uint8_t")){tipoComando = modBusType_uint8_t;}
brunofgc 0:8c59a494a451 1014 if(strstr(pTipoComando,"uint16_t")){tipoComando = modBusType_uint16_t;}
brunofgc 0:8c59a494a451 1015 if(strstr(pTipoComando,"uint32_t")){tipoComando = modBusType_uint32_t;}
brunofgc 0:8c59a494a451 1016 if(strstr(pTipoComando,"ir")){tipoComando = IrCommandType;}
brunofgc 0:8c59a494a451 1017 }else{
brunofgc 0:8c59a494a451 1018 return false;
brunofgc 0:8c59a494a451 1019 }
brunofgc 0:8c59a494a451 1020
brunofgc 0:8c59a494a451 1021 switch(tipoComando){
brunofgc 0:8c59a494a451 1022 case modBusType_float:
brunofgc 0:8c59a494a451 1023 float floatValueToWrite;
brunofgc 0:8c59a494a451 1024 uint32_t auxMod;
brunofgc 0:8c59a494a451 1025 param1 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1026 param2 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1027 param3 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1028 //floatValueToWrite = diversos::stringToFloat(strtok(NULL,","),100);
brunofgc 0:8c59a494a451 1029 floatValueToWrite = atof(strtok(NULL,","));
brunofgc 0:8c59a494a451 1030
brunofgc 0:8c59a494a451 1031 if(param1 != 254){
brunofgc 0:8c59a494a451 1032 //Envio comando via Modbus
brunofgc 0:8c59a494a451 1033 modBusMaster1::writeFloat(
brunofgc 0:8c59a494a451 1034 param1,
brunofgc 0:8c59a494a451 1035 param2,
brunofgc 0:8c59a494a451 1036 param3,
brunofgc 0:8c59a494a451 1037 &floatValueToWrite
brunofgc 0:8c59a494a451 1038 );
brunofgc 0:8c59a494a451 1039 }else{
brunofgc 0:8c59a494a451 1040 //Executa comando interno;
brunofgc 0:8c59a494a451 1041 auxMod = floatValueToWrite/10;
brunofgc 0:8c59a494a451 1042 floatValueToWrite = floatValueToWrite-(auxMod*10);
brunofgc 0:8c59a494a451 1043 SD1.period_us(auxMod);
brunofgc 0:8c59a494a451 1044 switch(param3){
brunofgc 0:8c59a494a451 1045 case 0:
brunofgc 0:8c59a494a451 1046 SD1.write(floatValueToWrite);
brunofgc 0:8c59a494a451 1047 break;
brunofgc 0:8c59a494a451 1048 case 1:
brunofgc 0:8c59a494a451 1049 SD2.write(floatValueToWrite);
brunofgc 0:8c59a494a451 1050 break;
brunofgc 0:8c59a494a451 1051 case 2:
brunofgc 0:8c59a494a451 1052 SD3.write(floatValueToWrite);
brunofgc 0:8c59a494a451 1053 break;
brunofgc 0:8c59a494a451 1054 case 3:
brunofgc 0:8c59a494a451 1055 SD4.write(floatValueToWrite);
brunofgc 0:8c59a494a451 1056 break;
brunofgc 0:8c59a494a451 1057 case 4:
brunofgc 0:8c59a494a451 1058 SD5.write(floatValueToWrite);
brunofgc 0:8c59a494a451 1059 break;
brunofgc 0:8c59a494a451 1060 case 5:
brunofgc 0:8c59a494a451 1061 SD6.write(floatValueToWrite);
brunofgc 0:8c59a494a451 1062 break;
brunofgc 0:8c59a494a451 1063 }
brunofgc 0:8c59a494a451 1064 }
brunofgc 0:8c59a494a451 1065 break;
brunofgc 0:8c59a494a451 1066 case modBusType_uint32_t:
brunofgc 0:8c59a494a451 1067 param1 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1068 param2 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1069 param3 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1070 param4 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1071
brunofgc 0:8c59a494a451 1072 modBusMaster1::writeRegister32BIT(
brunofgc 0:8c59a494a451 1073 param1,
brunofgc 0:8c59a494a451 1074 param2,
brunofgc 0:8c59a494a451 1075 param3,
brunofgc 0:8c59a494a451 1076 &param4
brunofgc 0:8c59a494a451 1077 );
brunofgc 0:8c59a494a451 1078 break;
brunofgc 0:8c59a494a451 1079 case modBusType_uint16_t:
brunofgc 0:8c59a494a451 1080 uint16_t param4_uint16_t;
brunofgc 0:8c59a494a451 1081 param1 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1082 param2 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1083 param3 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1084 param4_uint16_t = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1085
brunofgc 0:8c59a494a451 1086 modBusMaster1::writeRegister16BIT(
brunofgc 0:8c59a494a451 1087 param1,
brunofgc 0:8c59a494a451 1088 param2,
brunofgc 0:8c59a494a451 1089 param3,
brunofgc 0:8c59a494a451 1090 &param4_uint16_t
brunofgc 0:8c59a494a451 1091 );
brunofgc 0:8c59a494a451 1092 break;
brunofgc 0:8c59a494a451 1093 case modBusType_bit:
brunofgc 0:8c59a494a451 1094 param1 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1095 param2 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1096 param3 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1097 param4 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1098
brunofgc 0:8c59a494a451 1099 if(param1 != 254){
brunofgc 0:8c59a494a451 1100 //Envia comando via Modbus
brunofgc 0:8c59a494a451 1101 modBusMaster1::writeSingleCoil(
brunofgc 0:8c59a494a451 1102 param1,
brunofgc 0:8c59a494a451 1103 param3,
brunofgc 0:8c59a494a451 1104 (bool)param4
brunofgc 0:8c59a494a451 1105 );
brunofgc 0:8c59a494a451 1106 }else{
brunofgc 0:8c59a494a451 1107 //Executa comando interno;
brunofgc 0:8c59a494a451 1108 switch(param3){
brunofgc 0:8c59a494a451 1109 case 0:
brunofgc 0:8c59a494a451 1110 SD1.write(param4*1.0f);
brunofgc 0:8c59a494a451 1111 break;
brunofgc 0:8c59a494a451 1112 case 1:
brunofgc 0:8c59a494a451 1113 SD2.write(param4*1.0f);
brunofgc 0:8c59a494a451 1114 break;
brunofgc 0:8c59a494a451 1115 case 2:
brunofgc 0:8c59a494a451 1116 SD3.write(param4*1.0f);
brunofgc 0:8c59a494a451 1117 break;
brunofgc 0:8c59a494a451 1118 case 3:
brunofgc 0:8c59a494a451 1119 SD4.write(param4*1.0f);
brunofgc 0:8c59a494a451 1120 break;
brunofgc 0:8c59a494a451 1121 case 4:
brunofgc 0:8c59a494a451 1122 SD5.write(param4*1.0f);
brunofgc 0:8c59a494a451 1123 break;
brunofgc 0:8c59a494a451 1124 case 5:
brunofgc 0:8c59a494a451 1125 SD6.write(param4*1.0f);
brunofgc 0:8c59a494a451 1126 break;
brunofgc 0:8c59a494a451 1127 case 6:
brunofgc 0:8c59a494a451 1128 SD7 = param4;
brunofgc 0:8c59a494a451 1129 break;
brunofgc 0:8c59a494a451 1130 case 7:
brunofgc 0:8c59a494a451 1131 SD8 = param4;
brunofgc 0:8c59a494a451 1132 break;
brunofgc 0:8c59a494a451 1133 }
brunofgc 0:8c59a494a451 1134 }
brunofgc 0:8c59a494a451 1135 break;
brunofgc 0:8c59a494a451 1136 case IrCommandType:
brunofgc 0:8c59a494a451 1137 //Busca no arquivo
brunofgc 0:8c59a494a451 1138 //Abre arquivo
brunofgc 0:8c59a494a451 1139 //printf("\r\nIniciando execucao de comando IR\r\n");
brunofgc 0:8c59a494a451 1140 param1 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1141 param2 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1142 //printf("\r\nparam 1 %lu, param2 %lu\r\n",param1,param2);
brunofgc 0:8c59a494a451 1143 pTipoComando = strtok(NULL,"S");//Reaproveitando variável pTipoComando
brunofgc 0:8c59a494a451 1144 //printf("\r\nPacote IR <%s>.\r\n",pTipoComando);
brunofgc 0:8c59a494a451 1145 deserializaPacoteIR(pTipoComando);
brunofgc 0:8c59a494a451 1146 enviaComandoIR(param1,param2);
brunofgc 0:8c59a494a451 1147 break;
brunofgc 0:8c59a494a451 1148 default:
brunofgc 0:8c59a494a451 1149 //printf("comando nao encontrado\r\n");
brunofgc 0:8c59a494a451 1150 break;
brunofgc 0:8c59a494a451 1151 }
brunofgc 0:8c59a494a451 1152 //printf("\r\n===================DEBUG(execAct)===================\r\n");
brunofgc 0:8c59a494a451 1153 return true;
brunofgc 0:8c59a494a451 1154 }
brunofgc 0:8c59a494a451 1155
brunofgc 0:8c59a494a451 1156 void device::writeReadingsToSD(){
brunofgc 0:8c59a494a451 1157 //Buffering before insertData
brunofgc 0:8c59a494a451 1158 //Esta função faz a inserção de todos os dados no arquivo armazenamento.
brunofgc 0:8c59a494a451 1159 uint16_t readingIndex=0;
brunofgc 0:8c59a494a451 1160 /*
brunofgc 0:8c59a494a451 1161 union {
brunofgc 0:8c59a494a451 1162 char c[4];
brunofgc 0:8c59a494a451 1163 float v;
brunofgc 0:8c59a494a451 1164 }uFloat;
brunofgc 0:8c59a494a451 1165 */
brunofgc 0:8c59a494a451 1166 float *floatBuffer;
brunofgc 0:8c59a494a451 1167 uint32_t *uint32_tBuffer;
brunofgc 0:8c59a494a451 1168 uint16_t *uint16_tBuffer;
brunofgc 0:8c59a494a451 1169 bool *boolBuffer;
brunofgc 0:8c59a494a451 1170 uint32_t seconds_uint32_t;
brunofgc 0:8c59a494a451 1171 char seconds_char[5];
brunofgc 0:8c59a494a451 1172
brunofgc 0:8c59a494a451 1173 seconds_uint32_t = (uint32_t) time(NULL);
brunofgc 0:8c59a494a451 1174 seconds_char[0]=(seconds_uint32_t >> 0) & 0xFF;
brunofgc 0:8c59a494a451 1175 seconds_char[1]=(seconds_uint32_t >> 8) & 0xFF;
brunofgc 0:8c59a494a451 1176 seconds_char[2]=(seconds_uint32_t >> 16) & 0xFF;
brunofgc 0:8c59a494a451 1177 seconds_char[3]=(seconds_uint32_t >> 24) & 0xFF;
brunofgc 0:8c59a494a451 1178 seconds_char[4]=0;
brunofgc 0:8c59a494a451 1179
brunofgc 0:8c59a494a451 1180
brunofgc 0:8c59a494a451 1181 //Inicio da string dentro do arquivo binario
brunofgc 0:8c59a494a451 1182 //sdCardBuf.del();
brunofgc 0:8c59a494a451 1183 sdCardBuf.fill("log{data:",9);
brunofgc 0:8c59a494a451 1184
brunofgc 0:8c59a494a451 1185 //O timestamp é posto nos 4 primeiros bytes (segundos)
brunofgc 0:8c59a494a451 1186 sdCardBuf.fill(seconds_char,5);
brunofgc 0:8c59a494a451 1187
brunofgc 0:8c59a494a451 1188 for(readingIndex = 0;readingIndex < this->numReadings;readingIndex++){
brunofgc 0:8c59a494a451 1189 switch(this->readings[readingIndex].type){
brunofgc 0:8c59a494a451 1190 case modBusType_float:
brunofgc 0:8c59a494a451 1191 if(this->readings[readingIndex].addr != 0){
brunofgc 0:8c59a494a451 1192 //Busca externa via Modbus
brunofgc 0:8c59a494a451 1193 floatBuffer = (float*) malloc(((this->readings[readingIndex].numRegs*sizeof(float))+1)); //Alocando mais um byte para status
brunofgc 0:8c59a494a451 1194 ((char *)floatBuffer)[this->readings[readingIndex].numRegs*sizeof(float)]=modBusMaster1::readFloat(
brunofgc 0:8c59a494a451 1195 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 1196 this->readings[readingIndex].func,
brunofgc 0:8c59a494a451 1197 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 1198 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 1199 floatBuffer
brunofgc 0:8c59a494a451 1200 );
brunofgc 0:8c59a494a451 1201 }else{
brunofgc 0:8c59a494a451 1202 //Busca Interna
brunofgc 0:8c59a494a451 1203 floatBuffer = (float*) malloc(sizeof(float));
brunofgc 0:8c59a494a451 1204 //Populando floatBuffer com a entrada analógica
brunofgc 0:8c59a494a451 1205 floatBuffer[0] = aiFiltrada[this->readings[readingIndex].reg];
brunofgc 0:8c59a494a451 1206 }
brunofgc 0:8c59a494a451 1207
brunofgc 0:8c59a494a451 1208 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 0:8c59a494a451 1209 sdCardBuf.fill((char *)floatBuffer,((sizeof(float)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status
brunofgc 0:8c59a494a451 1210 free(floatBuffer);
brunofgc 0:8c59a494a451 1211 break;
brunofgc 0:8c59a494a451 1212 case modBusType_uint32_t:
brunofgc 0:8c59a494a451 1213 uint32_tBuffer = (uint32_t*) malloc((this->readings[readingIndex].numRegs*sizeof(uint32_t))+1); //Alocando mais um byte para status
brunofgc 0:8c59a494a451 1214 ((char *) uint32_tBuffer)[(this->readings[readingIndex].numRegs*sizeof(uint32_t))]=modBusMaster1::readRegister32BIT(//Ponho o status no ultimo byte
brunofgc 0:8c59a494a451 1215 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 1216 this->readings[readingIndex].func,
brunofgc 0:8c59a494a451 1217 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 1218 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 1219 uint32_tBuffer
brunofgc 0:8c59a494a451 1220 );
brunofgc 0:8c59a494a451 1221 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 0:8c59a494a451 1222 sdCardBuf.fill((char *)uint32_tBuffer,((sizeof(uint32_t)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status
brunofgc 0:8c59a494a451 1223 free(uint32_tBuffer);
brunofgc 0:8c59a494a451 1224 break;
brunofgc 0:8c59a494a451 1225
brunofgc 0:8c59a494a451 1226 case modBusType_uint16_t:
brunofgc 0:8c59a494a451 1227 if(this->readings[readingIndex].addr!=255){
brunofgc 0:8c59a494a451 1228 uint16_tBuffer = (uint16_t*) malloc(((this->readings[readingIndex].numRegs*sizeof(uint16_t))+1));//Alocando mais um byte para status
brunofgc 0:8c59a494a451 1229 ((char *) uint16_tBuffer)[this->readings[readingIndex].numRegs*sizeof(uint16_t)]=modBusMaster1::readRegister16BIT(//
brunofgc 0:8c59a494a451 1230 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 1231 this->readings[readingIndex].func,
brunofgc 0:8c59a494a451 1232 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 1233 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 1234 uint16_tBuffer
brunofgc 0:8c59a494a451 1235 );
brunofgc 0:8c59a494a451 1236 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 0:8c59a494a451 1237 sdCardBuf.fill((char *)uint16_tBuffer,((sizeof(uint16_t)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status
brunofgc 0:8c59a494a451 1238 free(uint16_tBuffer);
brunofgc 0:8c59a494a451 1239 }else{
brunofgc 0:8c59a494a451 1240 if((this->readings[readingIndex].reg>0)&&((this->readings[readingIndex].reg + this->readings[readingIndex].numRegs)<=4)){
brunofgc 0:8c59a494a451 1241 sdCardBuf.fill((char *)&aiFiltrada[this->readings[readingIndex].reg-1],sizeof(uint16_t)*this->readings[readingIndex].numRegs);
brunofgc 0:8c59a494a451 1242 }
brunofgc 0:8c59a494a451 1243 }
brunofgc 0:8c59a494a451 1244 break;
brunofgc 0:8c59a494a451 1245
brunofgc 0:8c59a494a451 1246 case modBusType_bit:
brunofgc 0:8c59a494a451 1247 if(this->readings[readingIndex].addr != 0){
brunofgc 0:8c59a494a451 1248 boolBuffer = (bool*) malloc(((this->readings[readingIndex].numRegs*sizeof(bool))+1));//Alocando mais um byte para status
brunofgc 0:8c59a494a451 1249 ((char *)boolBuffer)[this->readings[readingIndex].numRegs*sizeof(bool)]=modBusMaster1::readCoils(
brunofgc 0:8c59a494a451 1250 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 1251 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 1252 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 1253 boolBuffer
brunofgc 0:8c59a494a451 1254 );
brunofgc 0:8c59a494a451 1255 }else{
brunofgc 0:8c59a494a451 1256 boolBuffer = (bool*) malloc(sizeof(bool));
brunofgc 0:8c59a494a451 1257 boolBuffer[0] = entradasDigitais[this->readings[readingIndex].reg];
brunofgc 0:8c59a494a451 1258 }
brunofgc 0:8c59a494a451 1259 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 0:8c59a494a451 1260 sdCardBuf.fill((char *)boolBuffer,((sizeof(bool)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status
brunofgc 0:8c59a494a451 1261 free(boolBuffer);
brunofgc 0:8c59a494a451 1262 break;
brunofgc 0:8c59a494a451 1263
brunofgc 0:8c59a494a451 1264 }
brunofgc 0:8c59a494a451 1265 }
brunofgc 0:8c59a494a451 1266 readingIndex = sdCardBuf.fill("}log",4); //Reaproveitando a variável de 16bit readingIndex
brunofgc 0:8c59a494a451 1267 if((!modemCom::status.emComunicacao)&&(!sdCard::arquivoAberto())){
brunofgc 0:8c59a494a451 1268 //Insiro os dados se for possível.
brunofgc 0:8c59a494a451 1269 sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),readingIndex);
brunofgc 0:8c59a494a451 1270 }
brunofgc 0:8c59a494a451 1271 }
brunofgc 0:8c59a494a451 1272
brunofgc 0:8c59a494a451 1273
brunofgc 0:8c59a494a451 1274
brunofgc 0:8c59a494a451 1275
brunofgc 0:8c59a494a451 1276
brunofgc 0:8c59a494a451 1277
brunofgc 0:8c59a494a451 1278
brunofgc 0:8c59a494a451 1279
brunofgc 0:8c59a494a451 1280 device::device()
brunofgc 0:8c59a494a451 1281 {
brunofgc 0:8c59a494a451 1282 this->numAlarms=0;
brunofgc 0:8c59a494a451 1283 this->numReadings=0;
brunofgc 0:8c59a494a451 1284 }
brunofgc 0:8c59a494a451 1285
brunofgc 0:8c59a494a451 1286 int criaDevices(FILE *devicesCfg)
brunofgc 0:8c59a494a451 1287 {
brunofgc 0:8c59a494a451 1288 char linha[maxCaractereLeLinha];
brunofgc 0:8c59a494a451 1289 int i;
brunofgc 0:8c59a494a451 1290 int linhas = 0;
brunofgc 0:8c59a494a451 1291 devices = 0;
brunofgc 0:8c59a494a451 1292
brunofgc 0:8c59a494a451 1293 while(fgets(linha,maxCaractereLeLinha,devicesCfg)) {
brunofgc 0:8c59a494a451 1294 if(strstr(linha,"deviceCreate\\")) {
brunofgc 0:8c59a494a451 1295 devices++;
brunofgc 0:8c59a494a451 1296 }
brunofgc 0:8c59a494a451 1297 linhas++;
brunofgc 0:8c59a494a451 1298 }
brunofgc 0:8c59a494a451 1299
brunofgc 0:8c59a494a451 1300 printf("Contei %u linha(s), %u device(s).\n",linhas,devices);
brunofgc 0:8c59a494a451 1301
brunofgc 0:8c59a494a451 1302 //Criando vetor de objetos dispositivos;
brunofgc 0:8c59a494a451 1303
brunofgc 0:8c59a494a451 1304 dispositivos = (device **) malloc(sizeof(device)*devices);
brunofgc 0:8c59a494a451 1305
brunofgc 0:8c59a494a451 1306 for(i=0; i<devices; i++) {
brunofgc 0:8c59a494a451 1307 dispositivos[i] = new device();
brunofgc 0:8c59a494a451 1308 //dispositivos[i]->id=i;
brunofgc 0:8c59a494a451 1309 }
brunofgc 0:8c59a494a451 1310
brunofgc 0:8c59a494a451 1311 rewind(devicesCfg);
brunofgc 0:8c59a494a451 1312 return 1;
brunofgc 0:8c59a494a451 1313 }
brunofgc 0:8c59a494a451 1314
brunofgc 0:8c59a494a451 1315
brunofgc 0:8c59a494a451 1316
brunofgc 0:8c59a494a451 1317
brunofgc 0:8c59a494a451 1318
brunofgc 0:8c59a494a451 1319 int configuraDevices(FILE *devicesCfg)
brunofgc 0:8c59a494a451 1320 {
brunofgc 0:8c59a494a451 1321 uint16_t linhas=0;
brunofgc 0:8c59a494a451 1322 //uint16_t leituras = 0;
brunofgc 0:8c59a494a451 1323 char linha[40];
brunofgc 0:8c59a494a451 1324 uint8_t estadoConfiguracao=estadoConfiguracao_idle;
brunofgc 0:8c59a494a451 1325 int8_t dispositivoEmConfiguracao = -1;
brunofgc 0:8c59a494a451 1326
brunofgc 0:8c59a494a451 1327 while(fgets(linha,maxCaractereLeLinha,devicesCfg)) {
brunofgc 0:8c59a494a451 1328 if(strstr(linha,"deviceCreate\\")) {
brunofgc 0:8c59a494a451 1329 dispositivoEmConfiguracao++;
brunofgc 0:8c59a494a451 1330 //printf("Configurando dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1331 estadoConfiguracao = estadoConfiguracao_creatingDevice;
brunofgc 0:8c59a494a451 1332 }
brunofgc 0:8c59a494a451 1333
brunofgc 0:8c59a494a451 1334 if(strstr(linha,"alarmSet")) {
brunofgc 0:8c59a494a451 1335 //printf("Criando alarme no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1336 alarmes++;
brunofgc 0:8c59a494a451 1337 estadoConfiguracao = estadoConfiguracao_alarmSet;
brunofgc 0:8c59a494a451 1338 }
brunofgc 0:8c59a494a451 1339
brunofgc 0:8c59a494a451 1340 if(strstr(linha,"readingsSet")) {
brunofgc 0:8c59a494a451 1341 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1342 estadoConfiguracao = estadoConfiguracao_readingsSet;
brunofgc 0:8c59a494a451 1343 }
brunofgc 0:8c59a494a451 1344
brunofgc 0:8c59a494a451 1345 if(strstr(linha,"readingsEnd")) {
brunofgc 0:8c59a494a451 1346 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1347 estadoConfiguracao = estadoConfiguracao_idle;
brunofgc 0:8c59a494a451 1348 }
brunofgc 0:8c59a494a451 1349
brunofgc 0:8c59a494a451 1350 if(strstr(linha,"scheduleSet")) {
brunofgc 0:8c59a494a451 1351 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1352 qtdSchedules++;
brunofgc 0:8c59a494a451 1353 estadoConfiguracao = estadoConfiguracao_scheduleSet;
brunofgc 0:8c59a494a451 1354 }
brunofgc 0:8c59a494a451 1355
brunofgc 0:8c59a494a451 1356 if(strstr(linha,"schedulesEnd")) {
brunofgc 0:8c59a494a451 1357 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1358 estadoConfiguracao = estadoConfiguracao_idle;
brunofgc 0:8c59a494a451 1359 }
brunofgc 0:8c59a494a451 1360
brunofgc 0:8c59a494a451 1361 if(strstr(linha,">")) {
brunofgc 0:8c59a494a451 1362 //printf("Fechando dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1363 estadoConfiguracao = estadoConfiguracao_idle;
brunofgc 0:8c59a494a451 1364 }
brunofgc 0:8c59a494a451 1365
brunofgc 0:8c59a494a451 1366 switch(estadoConfiguracao) {
brunofgc 0:8c59a494a451 1367 case estadoConfiguracao_creatingDevice:
brunofgc 0:8c59a494a451 1368 /*if(strstr(linha,"id:")) {
brunofgc 0:8c59a494a451 1369 sscanf(linha,"id:%u",&dispositivos[dispositivoEmConfiguracao]->id);
brunofgc 0:8c59a494a451 1370 }*/
brunofgc 0:8c59a494a451 1371 break;
brunofgc 0:8c59a494a451 1372 case estadoConfiguracao_alarmSet:
brunofgc 0:8c59a494a451 1373 //Inclue linha nos alarmes do dispositivo em configuração
brunofgc 0:8c59a494a451 1374 dispositivos[dispositivoEmConfiguracao]->setAlarm(linha);
brunofgc 0:8c59a494a451 1375 break;
brunofgc 0:8c59a494a451 1376 case estadoConfiguracao_readingsSet:
brunofgc 0:8c59a494a451 1377 //Inclue linha nas leituras do dispositivo em configuração
brunofgc 0:8c59a494a451 1378 dispositivos[dispositivoEmConfiguracao]->setReading(linha);
brunofgc 0:8c59a494a451 1379 break;
brunofgc 0:8c59a494a451 1380 case estadoConfiguracao_scheduleSet:
brunofgc 0:8c59a494a451 1381 //Inclue linha nas leituras do dispositivo em configuração
brunofgc 0:8c59a494a451 1382 dispositivos[dispositivoEmConfiguracao]->setSchedule(linha);
brunofgc 0:8c59a494a451 1383 break;
brunofgc 0:8c59a494a451 1384 }
brunofgc 0:8c59a494a451 1385
brunofgc 0:8c59a494a451 1386 linhas++;
brunofgc 0:8c59a494a451 1387 }
brunofgc 0:8c59a494a451 1388 printf("Contei %u linha(s), configurei %u dispositivo(s), instalei %u alarme(s) e %u schedule(s) nos dispositivo(s).\n",linhas,devices,alarmes,qtdSchedules);
brunofgc 0:8c59a494a451 1389 return 1;
brunofgc 0:8c59a494a451 1390 }
brunofgc 0:8c59a494a451 1391
brunofgc 0:8c59a494a451 1392
brunofgc 0:8c59a494a451 1393
brunofgc 0:8c59a494a451 1394 void testaTudoDevices()
brunofgc 0:8c59a494a451 1395 {
brunofgc 0:8c59a494a451 1396 int i;
brunofgc 0:8c59a494a451 1397 int j;
brunofgc 0:8c59a494a451 1398
brunofgc 0:8c59a494a451 1399 for(i=0; i<devices; i++) {
brunofgc 0:8c59a494a451 1400 printf("Dispositivo %u tem %u alarmes sendo eles:\n",i,dispositivos[i]->numAlarms);
brunofgc 0:8c59a494a451 1401
brunofgc 0:8c59a494a451 1402 for(j=0; j<dispositivos[i]->numAlarms; j++) {
brunofgc 0:8c59a494a451 1403 printf("Alarme %u:\n",j);
brunofgc 0:8c59a494a451 1404 printf("\tId <%lu>.\n",dispositivos[i]->alarms[j].id);
brunofgc 0:8c59a494a451 1405 printf("\tSeconds <%lu>.\n",dispositivos[i]->alarms[j].seconds);
brunofgc 0:8c59a494a451 1406 printf("\tAddr %u, func %u, reg %lu.\n",dispositivos[i]->alarms[j].addrModbusRead,dispositivos[i]->alarms[j].funcModbusRead,dispositivos[i]->alarms[j].regModbusRead);
brunofgc 0:8c59a494a451 1407 if(dispositivos[i]->alarms[j].type==modBusType_float) {
brunofgc 0:8c59a494a451 1408 float value;
brunofgc 0:8c59a494a451 1409 dispositivos[i]->floatToBin(0,&value,&dispositivos[i]->alarms[j].value[0]);
brunofgc 0:8c59a494a451 1410 if(dispositivos[i]->alarms[j].max){
brunofgc 0:8c59a494a451 1411 printf("\tFloat max <%f>.\n",value);
brunofgc 0:8c59a494a451 1412 }
brunofgc 0:8c59a494a451 1413 else{printf("\tFloat min <%f>.\n",value);}
brunofgc 0:8c59a494a451 1414 }
brunofgc 0:8c59a494a451 1415
brunofgc 0:8c59a494a451 1416 if(dispositivos[i]->alarms[j].type==modBusType_uint32_t) {
brunofgc 0:8c59a494a451 1417 uint32_t value;
brunofgc 0:8c59a494a451 1418 dispositivos[i]->uint32_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]);
brunofgc 0:8c59a494a451 1419 if(dispositivos[i]->alarms[j].max){
brunofgc 0:8c59a494a451 1420 printf("\tuint32_t max <%lu>.\n",value);
brunofgc 0:8c59a494a451 1421 }
brunofgc 0:8c59a494a451 1422 else{printf("\tuint32_t min <%lu>.\n",value);}
brunofgc 0:8c59a494a451 1423 }
brunofgc 0:8c59a494a451 1424
brunofgc 0:8c59a494a451 1425 if(dispositivos[i]->alarms[j].type==modBusType_uint16_t) {
brunofgc 0:8c59a494a451 1426 uint16_t value;
brunofgc 0:8c59a494a451 1427 dispositivos[i]->uint16_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]);
brunofgc 0:8c59a494a451 1428 if(dispositivos[i]->alarms[j].max){
brunofgc 0:8c59a494a451 1429 printf("\tuint16_t max <%lu>.\n",value);
brunofgc 0:8c59a494a451 1430 }
brunofgc 0:8c59a494a451 1431 else{printf("\tuint16_t min <%lu>.\n",value);}
brunofgc 0:8c59a494a451 1432 }
brunofgc 0:8c59a494a451 1433
brunofgc 0:8c59a494a451 1434 if(dispositivos[i]->alarms[j].type==modBusType_bit) {
brunofgc 0:8c59a494a451 1435 uint8_t value;
brunofgc 0:8c59a494a451 1436 value=dispositivos[i]->alarms[j].value[0];
brunofgc 0:8c59a494a451 1437 if(dispositivos[i]->alarms[j].max){
brunofgc 0:8c59a494a451 1438 printf("\tBIT max <%lu>.\n",value);
brunofgc 0:8c59a494a451 1439 }
brunofgc 0:8c59a494a451 1440 else{printf("\tBIT min <%lu>.\n",value);}
brunofgc 0:8c59a494a451 1441 }
brunofgc 0:8c59a494a451 1442
brunofgc 0:8c59a494a451 1443 printf("\tact:%lu\n",dispositivos[i]->alarms[j].idAct);
brunofgc 0:8c59a494a451 1444 }
brunofgc 0:8c59a494a451 1445
brunofgc 0:8c59a494a451 1446 printf("Dispositivo %u tem %u schedules sendo eles:\n",i,dispositivos[i]->numSchedules);
brunofgc 0:8c59a494a451 1447
brunofgc 0:8c59a494a451 1448 for(j=0; j<dispositivos[i]->numSchedules; j++) {
brunofgc 0:8c59a494a451 1449 printf("Schedule %u:\n",j);
brunofgc 0:8c59a494a451 1450 printf("\tId <%lu>.\n",dispositivos[i]->schedules[j].id);
brunofgc 0:8c59a494a451 1451 printf("\tweekday <%lu>.\n",dispositivos[i]->schedules[j].weekday);
brunofgc 0:8c59a494a451 1452 printf("\tminute <%lu>.\n",dispositivos[i]->schedules[j].minute);
brunofgc 0:8c59a494a451 1453 printf("\tidAct <%lu>.\n",dispositivos[i]->schedules[j].idAct);
brunofgc 0:8c59a494a451 1454 }
brunofgc 0:8c59a494a451 1455
brunofgc 0:8c59a494a451 1456 printf("Dispositivo %u tem %u leituras sendo elas:\n",i,dispositivos[i]->numReadings);
brunofgc 0:8c59a494a451 1457
brunofgc 0:8c59a494a451 1458 for(j=0; j<dispositivos[i]->numReadings; j++) {
brunofgc 0:8c59a494a451 1459 printf("Leitura %u:",j);
brunofgc 0:8c59a494a451 1460 switch(dispositivos[i]->readings[j].type) {
brunofgc 0:8c59a494a451 1461 case modBusType_float:
brunofgc 0:8c59a494a451 1462 printf("Tipo float ");
brunofgc 0:8c59a494a451 1463 break;
brunofgc 0:8c59a494a451 1464 case modBusType_uint32_t:
brunofgc 0:8c59a494a451 1465 printf("Tipo uint32_t ");
brunofgc 0:8c59a494a451 1466 break;
brunofgc 0:8c59a494a451 1467
brunofgc 0:8c59a494a451 1468 case modBusType_uint16_t:
brunofgc 0:8c59a494a451 1469 printf("Tipo uint16_t ");
brunofgc 0:8c59a494a451 1470 break;
brunofgc 0:8c59a494a451 1471
brunofgc 0:8c59a494a451 1472 case modBusType_uint8_t:
brunofgc 0:8c59a494a451 1473 printf("Tipo uint8_t ");
brunofgc 0:8c59a494a451 1474 break;
brunofgc 0:8c59a494a451 1475
brunofgc 0:8c59a494a451 1476 case modBusType_bit:
brunofgc 0:8c59a494a451 1477 printf("Tipo bool ");
brunofgc 0:8c59a494a451 1478 break;
brunofgc 0:8c59a494a451 1479
brunofgc 0:8c59a494a451 1480 }
brunofgc 0:8c59a494a451 1481
brunofgc 0:8c59a494a451 1482 printf("addr %u, func %u, reg %u, numRegs %u.\n",dispositivos[i]->readings[j].addr,dispositivos[i]->readings[j].func,dispositivos[i]->readings[j].reg,dispositivos[i]->readings[j].numRegs);
brunofgc 0:8c59a494a451 1483 }
brunofgc 0:8c59a494a451 1484 }
brunofgc 0:8c59a494a451 1485 }