teste de publish

Files at this revision

API Documentation at this revision

Comitter:
brunofgc
Date:
Fri Jun 08 22:12:18 2018 +0000
Parent:
23:d3ef6bb8412a
Commit message:
teste de publish

Changed in this revision

devices.cpp Show annotated file Show diff for this revision Revisions of this file
devices.h Show annotated file Show diff for this revision Revisions of this file
diff -r d3ef6bb8412a -r 4d44626e5b78 devices.cpp
--- a/devices.cpp	Thu Jun 07 16:18:50 2018 +0000
+++ b/devices.cpp	Fri Jun 08 22:12:18 2018 +0000
@@ -1,5 +1,4 @@
 #include "devices.h"
-
 enum {
     estadoConfiguracao_idle = 0,
     estadoConfiguracao_creatingDevice,
@@ -84,7 +83,7 @@
     }
 }
 
-void mostraAlarmeEmExecucao(uint32_t indice){
+/*void mostraAlarmeEmExecucao(uint32_t indice){
     printf("Alarme %u:\n",indice);
     printf("\tId <%lu>.\n",alarms[indice].id);
     printf("\tSeconds <%lu>.\n",alarms[indice].seconds);
@@ -93,9 +92,9 @@
         float value;
         floatToBin(0,&value,&alarms[indice].value[0]);                
         if(alarms[indice].max){
-            //printf("\tFloat max <%f>.\n",value);
+            printf("\tFloat max <%f>.\n",value);
         }
-       // else{printf("\tFloat min <%f>.\n",value);}
+        else{printf("\tFloat min <%f>.\n",value);}
     }
     
     if(alarms[indice].type==modBusType_uint32_t) {
@@ -144,17 +143,18 @@
     }            
     
     printf("\tact:%lu\n",alarms[indice].idAct);    
-}
+}*/
 
 //Funcao restaurada
 void verifyAlarms(){
     //Inicio da verificação
     uint16_t alarmIndex;
     float leitura_float,value_float;
+    double leitura_double,value_double;
     uint32_t leitura_uint32_t,value_uint32_t;
     uint16_t leitura_uint16_t,value_uint16_t;
     bool leitura_bit,value_bit;    
-    bool alarmFound;
+    bool alarmFoundAntes;
     char aux[30];
         
     //for(alarmIndex = 0;alarmIndex < alarmes;alarmIndex++){        
@@ -164,15 +164,15 @@
         pc.printf("ON<%lu>.\r\n",alarms[alarmIndex].on);
         pc.printf("secAlarm<%lu>.\r\n",alarms[alarmIndex].secAlarm);
         pc.printf("alarmFound<%lu>.\r\n",alarms[alarmIndex].alarmFound);*/
-        //if((alarms[alarmIndex].on)&&(alarms[alarmIndex].id<100000000)){
+        //if((alarms[alarmIndex].on)&&(alarms[alarmIndex].id<100000000)){       
         if(alarms[alarmIndex].on){
-            if(debug){mostraAlarmeEmExecucao(alarmIndex);}
-            alarmFound=alarms[alarmIndex].alarmFound;
+            alarmFoundAntes = alarms[alarmIndex].alarmFound;
+            //if(debug){mostraAlarmeEmExecucao(alarmIndex);}            
             switch(alarms[alarmIndex].type){
                 case modBusType_float:                                        
                         //Montar float!
                         floatToBin(0,&value_float,&alarms[alarmIndex].value[0]);                    
-                        
+                        value_double = (double)value_float;
                         if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus;
                             modBusMaster1::readFloat(
                                 alarms[alarmIndex].addrModbusRead,
@@ -187,62 +187,20 @@
                             if(alarms[alarmIndex].regModbusRead<100){//Registradores das entradas de temperatura
                                 //Populando floatBuffer com a temperaturas
                                 if(alarms[alarmIndex].regModbusRead < num_temperatureSensors){
-                                    leitura_float = temperaturas[alarms[alarmIndex].regModbusRead];
+                                    leitura_float = temperaturas[alarms[alarmIndex].regModbusRead];                                    
                                 }    
                             }
                             if((alarms[alarmIndex].regModbusRead>99)&&(alarms[alarmIndex].regModbusRead<104)){//Registradores das entradas analógicas
                                 leitura_float = aiFiltrada[alarms[alarmIndex].regModbusRead - 100];                            
                             }
-                         }
-                
-                        if(alarms[alarmIndex].max!=0){
-                            if(leitura_float > value_float){
-                                alarms[alarmIndex].secAlarm++;
-                                if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
-                                    alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds;
-                                    alarmFound = true;
-                                }
-                            }else{
-                                if(alarms[alarmIndex].secAlarm==1){
-                                    alarms[alarmIndex].secAlarm=0;
-                                    alarmFound = false;    
-                                }else{
-                                    if(alarms[alarmIndex].secAlarm){
-                                        if(alarmFound){
-                                         alarms[alarmIndex].secAlarm--;   
-                                        }else{
-                                            alarms[alarmIndex].secAlarm=0;
-                                         }
-                                    }
-                                 }
-                             }
-                        }else{
-                            if(leitura_float < value_float){
-                                alarms[alarmIndex].secAlarm++;
-                                if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
-                                    alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds;
-                                    alarmFound = true;
-                                }
-                            }else{
-                                if(alarms[alarmIndex].secAlarm==1){
-                                    alarms[alarmIndex].secAlarm=0;
-                                    alarmFound = false;    
-                                }else{
-                                    if(alarms[alarmIndex].secAlarm){
-                                        if(alarmFound){
-                                         alarms[alarmIndex].secAlarm--;   
-                                        }else{
-                                            alarms[alarmIndex].secAlarm=0;
-                                         }    
-                                    }
-                                 }
-                             }
-                         }                                                                             
+                         }                        
+                        leitura_double = (double)leitura_float;                        
                     break;
                 case modBusType_uint32_t:
                         //Montar uint32_t!
                         uint32_t_ToBin(0,&value_uint32_t,&alarms[alarmIndex].value[0]);                    
-                        
+                        //value_float = (float)value_uint32_t;
+                        value_double = (double)value_uint32_t;
                         if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus;
                             //Bloco de leitura da variável modBus;
                             modBusMaster1::readRegister32BIT(
@@ -254,59 +212,15 @@
                             );
                         }else{
                             leitura_uint32_t = pulsosEDs[alarms[alarmIndex].regModbusRead];
-                         }                                       
-                
-                        if(alarms[alarmIndex].max!=0){
-                            if(leitura_uint32_t > value_uint32_t){
-                                alarms[alarmIndex].secAlarm++;
-                                if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
-                                    alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds;
-                                    alarmFound = true;
-                                    pulsosEDs[alarms[alarmIndex].regModbusRead]=0;
-                                }
-                            }else{
-                                if(alarms[alarmIndex].secAlarm==1){
-                                    alarms[alarmIndex].secAlarm=0;
-                                    alarmFound = false;    
-                                }else{
-                                    if(alarms[alarmIndex].secAlarm){
-                                        if(alarmFound){
-                                         alarms[alarmIndex].secAlarm--;   
-                                        }else{
-                                            alarms[alarmIndex].secAlarm=0;
-                                         }
-                                    }
-                                 }
-                             }
-                        }else{
-                            if(leitura_uint32_t < value_uint32_t){
-                                alarms[alarmIndex].secAlarm++;
-                                if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
-                                    alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds;
-                                    alarmFound = true;
-                                    pulsosEDs[alarms[alarmIndex].regModbusRead]=0;
-                                }
-                            }else{
-                                if(alarms[alarmIndex].secAlarm==1){
-                                    alarms[alarmIndex].secAlarm=0;
-                                    alarmFound = false;    
-                                }else{
-                                    if(alarms[alarmIndex].secAlarm){
-                                        if(alarmFound){
-                                         alarms[alarmIndex].secAlarm--;   
-                                        }else{
-                                            alarms[alarmIndex].secAlarm=0;
-                                         }    
-                                    }
-                                 }
-                             }
-                         }                    
+                         }                
+                        leitura_float = (float)leitura_uint32_t;
                     break;
                     
                 case modBusType_uint16_t:
                         //Montar uint16_t!
                         uint16_t_ToBin(0,&value_uint16_t,&alarms[alarmIndex].value[0]);                    
-                        
+                        //value_float = (float)value_uint16_t;
+                        value_double = (double)value_uint16_t;
                         if(alarms[alarmIndex].addrModbusRead!=enderecoControladoraVirtual){
                             //Bloco de leitura da variável modBus;
                             modBusMaster1::readRegister16BIT(
@@ -322,54 +236,15 @@
                             }
                          }
                 
-                        if(alarms[alarmIndex].max!=0){
-                            if(leitura_uint16_t > value_uint16_t){
-                                alarms[alarmIndex].secAlarm++;
-                                if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
-                                    alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds;
-                                    alarmFound = true;
-                                }
-                            }else{
-                                if(alarms[alarmIndex].secAlarm==1){
-                                    alarms[alarmIndex].secAlarm=0;
-                                    alarmFound = false;    
-                                }else{
-                                    if(alarms[alarmIndex].secAlarm){
-                                        if(alarmFound){
-                                         alarms[alarmIndex].secAlarm--;   
-                                        }else{
-                                            alarms[alarmIndex].secAlarm=0;
-                                         }
-                                    }
-                                 }
-                             }
-                        }else{
-                            if(leitura_uint16_t < value_uint16_t){
-                                alarms[alarmIndex].secAlarm++;
-                                if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
-                                    alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds;
-                                    alarmFound = true;
-                                }
-                            }else{
-                                if(alarms[alarmIndex].secAlarm==1){
-                                    alarms[alarmIndex].secAlarm=0;
-                                    alarmFound = false;    
-                                }else{
-                                    if(alarms[alarmIndex].secAlarm){
-                                        if(alarmFound){
-                                         alarms[alarmIndex].secAlarm--;   
-                                        }else{
-                                            alarms[alarmIndex].secAlarm=0;
-                                         }    
-                                    }
-                                 }
-                             }
-                         } 
+                        //leitura_float = (float)leitura_uint16_t;                      
+                        leitura_double = (double)leitura_uint16_t;                      
                     break;
                 
                 case modBusType_int32_t:
                         //Montar uint32_t!
                         uint32_t_ToBin(0,&value_uint32_t,&alarms[alarmIndex].value[0]);                    
+                        //value_float = (float)((int32_t)value_uint32_t);
+                        value_double = (double)((int32_t)value_uint32_t);
                         
                         if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus;
                             //Bloco de leitura da variável modBus;
@@ -384,57 +259,15 @@
                             leitura_uint32_t = pulsosEDs[alarms[alarmIndex].regModbusRead];
                          }                                       
                 
-                        if(alarms[alarmIndex].max!=0){
-                            if(((int32_t)leitura_uint32_t) > ((int32_t)value_uint32_t)){
-                                alarms[alarmIndex].secAlarm++;
-                                if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
-                                    alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds;
-                                    alarmFound = true;
-                                    pulsosEDs[alarms[alarmIndex].regModbusRead]=0;
-                                }
-                            }else{
-                                if(alarms[alarmIndex].secAlarm==1){
-                                    alarms[alarmIndex].secAlarm=0;
-                                    alarmFound = false;    
-                                }else{
-                                    if(alarms[alarmIndex].secAlarm){
-                                        if(alarmFound){
-                                         alarms[alarmIndex].secAlarm--;   
-                                        }else{
-                                            alarms[alarmIndex].secAlarm=0;
-                                         }
-                                    }
-                                 }
-                             }
-                        }else{
-                            if(((int32_t)leitura_uint32_t) < ((int32_t)value_uint32_t)){
-                                alarms[alarmIndex].secAlarm++;
-                                if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
-                                    alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds;
-                                    alarmFound = true;
-                                    pulsosEDs[alarms[alarmIndex].regModbusRead]=0;
-                                }
-                            }else{
-                                if(alarms[alarmIndex].secAlarm==1){
-                                    alarms[alarmIndex].secAlarm=0;
-                                    alarmFound = false;    
-                                }else{
-                                    if(alarms[alarmIndex].secAlarm){
-                                        if(alarmFound){
-                                         alarms[alarmIndex].secAlarm--;   
-                                        }else{
-                                            alarms[alarmIndex].secAlarm=0;
-                                         }    
-                                    }
-                                 }
-                             }
-                         }                    
+                        //leitura_float = (float)((int32_t)leitura_uint32_t);
+                        leitura_double = (double)((int32_t)leitura_uint32_t);
                     break;
                     
                 case modBusType_int16_t:
                         //Montar uint16_t!
                         uint16_t_ToBin(0,&value_uint16_t,&alarms[alarmIndex].value[0]);                    
-                        
+                        //value_float = (float)((int16_t)value_uint16_t);
+                        value_double = (double)((int16_t)value_uint16_t);
                         if(alarms[alarmIndex].addrModbusRead!=enderecoControladoraVirtual){
                             //Bloco de leitura da variável modBus;
                             modBusMaster1::readRegister16BIT(
@@ -450,49 +283,8 @@
                             }
                          }
                 
-                        if(alarms[alarmIndex].max!=0){
-                            if(((int16_t)leitura_uint16_t) > ((int16_t)value_uint16_t)){
-                                alarms[alarmIndex].secAlarm++;
-                                if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
-                                    alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds;
-                                    alarmFound = true;
-                                }
-                            }else{
-                                if(alarms[alarmIndex].secAlarm==1){
-                                    alarms[alarmIndex].secAlarm=0;
-                                    alarmFound = false;    
-                                }else{
-                                    if(alarms[alarmIndex].secAlarm){
-                                        if(alarmFound){
-                                         alarms[alarmIndex].secAlarm--;   
-                                        }else{
-                                            alarms[alarmIndex].secAlarm=0;
-                                         }
-                                    }
-                                 }
-                             }
-                        }else{
-                            if(((int16_t)leitura_uint16_t) < ((int16_t)value_uint16_t)){
-                                alarms[alarmIndex].secAlarm++;
-                                if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
-                                    alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds;
-                                    alarmFound = true;
-                                }
-                            }else{
-                                if(alarms[alarmIndex].secAlarm==1){
-                                    alarms[alarmIndex].secAlarm=0;
-                                    alarmFound = false;    
-                                }else{
-                                    if(alarms[alarmIndex].secAlarm){
-                                        if(alarmFound){
-                                         alarms[alarmIndex].secAlarm--;   
-                                        }else{
-                                            alarms[alarmIndex].secAlarm=0;
-                                         }    
-                                    }
-                                 }
-                             }
-                         } 
+                        //leitura_float = (float)((int16_t)leitura_uint16_t);
+                        leitura_double = (double)((int16_t)leitura_uint16_t);
                     break;
                 
                 
@@ -500,7 +292,8 @@
                         //Montar bit!
                         //O valor de alarm para bit é o value[0]!!!
                         value_bit = (alarms[alarmIndex].value[0] > 0); //Qualquer valor maior que zero da saída 1
-                        
+                        //value_float = (float)value_bit;
+                        value_double = (double)value_bit;
                         if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus;
                             //Bloco de leitura da variável modBus;
                             modBusMaster1::readCoils(
@@ -512,35 +305,45 @@
                         }else{
                             leitura_bit = entradasDigitais[alarms[alarmIndex].regModbusRead];
                          }
-                        if(leitura_bit == value_bit){
-                            alarms[alarmIndex].secAlarm++;
-                            if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
-                                alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds;
-                                alarmFound = true;
-                            }
-                        }else{
-                            if(alarms[alarmIndex].secAlarm==1){
-                                alarms[alarmIndex].secAlarm=0;
-                                alarmFound = false;    
-                            }else{
-                                if(alarms[alarmIndex].secAlarm){
-                                    if(alarmFound){
-                                     alarms[alarmIndex].secAlarm--;   
-                                    }else{
-                                        alarms[alarmIndex].secAlarm=0;
-                                     }
-                                }
-                             }
-                         }                    
+                        //leitura_float = (float)leitura_bit;                    
+                        leitura_double = (double)leitura_bit;                    
                     break;
                 
             }
-        
+            
+            //Teste de maquina de estados alarme alarmFound, max, seconds, secAlarm, alarmFound                     
+            /*cenários
+            found   &&  max
+            0       ^   0   =   0
+            0       ^   1   =   1
+            1       ^   0   =   1
+            1       ^   1   =   0*/
+            
+            if((alarms[alarmIndex].alarmFound)^(alarms[alarmIndex].max)){
+                if(leitura_double > value_double){alarms[alarmIndex].secAlarm++;}
+                    else{alarms[alarmIndex].secAlarm=0;}
+            }else{
+                if(leitura_double < value_double){alarms[alarmIndex].secAlarm++;}
+                    else{alarms[alarmIndex].secAlarm=0;}
+            }
+            
+            
+            //Alterno de found pra release dependendo de como seja.
+            if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){
+                alarms[alarmIndex].alarmFound = !alarms[alarmIndex].alarmFound;
+                alarms[alarmIndex].secAlarm = 0;
+            }            
+            
+            //debug
+                //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);}
+            //debug
+            
+            //Teste de maquina de estados alarme alarmFound, max, seconds, secAlarm, alarmFound                     
+            
             //Verifico se houve alarme não tratado
-            if(alarms[alarmIndex].alarmFound!=alarmFound){
-               alarms[alarmIndex].alarmFound=alarmFound;
-               if(alarmFound){
-                   printf("Alarm FOUND! Id <%lu>.\n",alarms[alarmIndex].id);               
+            if(alarms[alarmIndex].alarmFound!=alarmFoundAntes){               
+               if(alarms[alarmIndex].alarmFound){
+                   printf("Alarm FOUND! Id <%lu>.\n",alarms[alarmIndex].id);
                    sdCardBuf.fill("log{alarm:event:found;id:",25);
                    sprintf(aux,"%lu",alarms[alarmIndex].id);
                    sdCardBuf.fill(aux,strlen(aux));               
@@ -561,16 +364,17 @@
                    }else if(alarms[alarmIndex].type == modBusType_int16_t){
                        sprintf(aux,"%ld",(int16_t)leitura_uint16_t);
                    }else if(alarms[alarmIndex].type == modBusType_bit){
-                       sprintf(aux,"%lu",leitura_bit);
+                       sprintf(aux,"%u",leitura_bit);
                    }
-                   sdCardBuf.fill(aux,strlen(aux));                            
+                   sdCardBuf.fill(aux,strlen(aux));   
+                                            
                    leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit               
                    sdCard::insereDadosBank(sdCardBuf.get(),leitura_uint16_t);                   
                    
                    boolExecAct=true;
                    act = alarms[alarmIndex].idAct;
                }else{
-                   printf("Alarm RELEASE! id <%lu>.\n",alarms[alarmIndex].id);               
+                   printf("Alarm RELEASE! id <%lu>.\n",alarms[alarmIndex].id);
                    sdCardBuf.fill("log{alarm:event:release;id:",27);
                    sprintf(aux,"%lu",alarms[alarmIndex].id);
                    sdCardBuf.fill(aux,strlen(aux));                              
@@ -579,8 +383,24 @@
                    sprintf(aux,"%lu",time(NULL));
                    sdCardBuf.fill(aux,strlen(aux));               
                    
+                   sdCardBuf.fill(";reading:",9);
+                   if(alarms[alarmIndex].type == modBusType_float){
+                       sprintf(aux,"%f",leitura_float);
+                   }else if(alarms[alarmIndex].type == modBusType_uint32_t){
+                       sprintf(aux,"%lu",leitura_uint32_t);
+                   }else if(alarms[alarmIndex].type == modBusType_uint16_t){
+                       sprintf(aux,"%lu",leitura_uint16_t);
+                   }else if(alarms[alarmIndex].type == modBusType_int32_t){
+                       sprintf(aux,"%ld",(int32_t)leitura_uint32_t);
+                   }else if(alarms[alarmIndex].type == modBusType_int16_t){
+                       sprintf(aux,"%ld",(int16_t)leitura_uint16_t);
+                   }else if(alarms[alarmIndex].type == modBusType_bit){
+                       sprintf(aux,"%u",leitura_bit);
+                   }
+                   sdCardBuf.fill(aux,strlen(aux)); 
+                   
                    leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit
-                   sdCard::arquivoAberto(40000);
+                   //sdCard::arquivoAberto(40000);
                    sdCard::insereDadosBank(sdCardBuf.get(),leitura_uint16_t);                   
                 }                        
                 //atrasa o envio em 3 segundos para que possa ser gravado o registro do alarme.
@@ -941,7 +761,7 @@
             alarms[alarmes].secAlarm=0;
             alarms[alarmes].idAct=0;
             alarms[alarmes].on=0;
-            alarms[alarmes].alarmFound = false;
+            alarms[alarmes].alarmFound = true;
             //Inicialização de contadores de alarme            
             alarmes++;            
             return;
diff -r d3ef6bb8412a -r 4d44626e5b78 devices.h
--- a/devices.h	Thu Jun 07 16:18:50 2018 +0000
+++ b/devices.h	Fri Jun 08 22:12:18 2018 +0000
@@ -1,9 +1,10 @@
+#ifndef _devices_h_
+#define _devices_h_
 #include "mbed.h"
 #include "funcoesSDCard.h"
 #include "modbusMaster1.h"
 #include "SDFileSystem.h"
-#ifndef _devices_h_
-#define _devices_h_
+#include "DS18B20_SensorTemperatura.h"
 #define maxCaractereLeLinha 40
 #include <cstdlib>