EZR

Dependencies:   CRC16 FreescaleIAP FreescaleWatchdog GGSProtocol LM75B PIMA Parameters PersistentCircularQueue SerialNumberV2COM mbed-dev-watchdog_2016_03_04

Fork of smartRamalKW by Equipe Firmware V2COM

main.cpp

Committer:
tpadovani
Date:
2015-10-13
Revision:
21:62316df0c24b
Parent:
19:1f623070b9dd
Child:
22:b66e36356dd4

File content as of revision 21:62316df0c24b:

#include "main.h"

static Watchdog wd;
static Ticker tickerWd;
SerialNumber sn;
Parameters param ((char *)&APP_PARAMETERS, sizeof(APP_PARAMETERS));
CommandExecutor exec;
MemoriaMassa mm;
Alarm alarm;
BoxAlarm boxAlarm;
Pima pima;

int main()
{
    iniciaWd();
    blinkLeds();
    
    printf("##### %s #####\r\n", version);
    printSerialNumber();

    param.loadParameters();
    printParameters();
    
    boxAlarm.init();
    alarm.loadQueue(APP_PARAMETERS.ALARMS_QUEUE_SIZE, 3);
    mm.iniciaLeituras(APP_PARAMETERS.INTERVALO_MM_S, APP_PARAMETERS.MM_SIZE, 4, 5);
    
    pima.setExpirationTimeout(APP_PARAMETERS.PIMA_VALIDADE_MEDIDOR_S, APP_PARAMETERS.PIMA_VALIDADE_ENERGIA_ATIVA_S, APP_PARAMETERS.PIMA_VALIDADE_ENERGIA_REATIVA_INDUTIVA_S, APP_PARAMETERS.PIMA_VALIDADE_ENERGIA_REATIVA_CAPACITIVA_S);
    if(APP_PARAMETERS.PROTOCOLO_MEDIDOR == PROTOCOLO_PIMINHA){
        pima.iniciaLeituraPiminha(APP_PARAMETERS.PIMA_AUTOBAUD_ENABLE, APP_PARAMETERS.PIMA_AUTOBAUD_TIMEOUT_S, APP_PARAMETERS.INTERVALO_WD_METER_S, APP_PARAMETERS.PIMA_TIMEOUT_PACOTE_S);
    } else{
        pima.iniciaLeituraPima(APP_PARAMETERS.PIMA_AUTOBAUD_ENABLE, APP_PARAMETERS.PIMA_AUTOBAUD_TIMEOUT_S, APP_PARAMETERS.INTERVALO_WD_METER_S);
    }
    
    ggsStream.baud(APP_PARAMETERS.RADIO_BAUDRATE_BPS);
    exec.carregaEndereco(sn.get());
    exec.iniciaExecutorComandos(APP_PARAMETERS.INTERVALO_WD_NETWORK_S, APP_PARAMETERS.INTERVALO_SILENCIO_S);
    detectaCargaSemCorte();
    
    while(1){
        exec.trataPacoteGGS();
        pima.trataPacotePiminha();
        mm.executaLeitura();
        boxAlarm.checkState();
    }
    
}

void iniciaWd(){
    wd.Configure();
    tickerWd.attach(&feedWd, WATCHDOG_INTERVAL);
}

void feedWd(){
    wd.Service();
}

void softReset(){
    printf("[main] SoftReset\r\n");
    tickerWd.detach();
}

void blinkLeds(){
    ledON = LED_ON;
    ledNetwork = LED_OFF;
    ledMeter = LED_OFF;
    ledBox = LED_OFF;
    ledLoad = LED_OFF;
    
    wait_ms(200);
    ledNetwork = LED_ON;
    wait_ms(200);
    ledNetwork = LED_OFF;
    ledMeter = LED_ON;
    wait_ms(200);
    ledMeter = LED_OFF;
    ledBox = LED_ON;
    wait_ms(200);
    ledBox = LED_OFF;
    ledLoad = LED_ON;
    wait_ms(200);
    ledLoad = LED_OFF;
}

void printSerialNumber(){
    printf("[main] SN: ");
    for(int i=0; i < SERIAL_LENGTH; i++){
        printf("%c", sn.get()[i]);
    }
    printf("\r\n");
}

void printParameters(){
    printf("[main] Parameters:");
    for(int i=0; i < sizeof(APP_PARAMETERS); i++){
        printf(" %02x", ((char *)&APP_PARAMETERS)[i]);
    }
    printf("\r\n");
}

void detectaCargaSemCorte(){
    bool ch0Status = getEstadoSensor(0);
    bool ch1Status = getEstadoSensor(1);
    
    if(!ch0Status && ch1Status){    // Canal 0 nao esta alimentado e Canal 1 esta: entao a carga certamente e o canal 0
        printf("[main] Load channel detected: CH0\r\n");
        if(APP_PARAMETERS.LOAD_CHANNEL != 0){
            APP_PARAMETERS.LOAD_CHANNEL = 0;
            param.saveParameters();
        }
    } else if(ch0Status && !ch1Status){    // Canal 1 nao esta alimentado e Canal 0 esta: entao a carga certamente e o canal 1
        printf("[main] Load channel detected: CH1\r\n");
        if(APP_PARAMETERS.LOAD_CHANNEL != 1){
            APP_PARAMETERS.LOAD_CHANNEL = 1;
            param.saveParameters();
        }
    } else{
        printf("[main] Unable to detect load channel\r\n");
    }
}