demo new haven display

Dependencies:   LCD Menu ButtonCtrl TimeManagement EventLog AddressMap emic2

ESCM 2000 Control and Display application provides interface for the LPC1768 processor boards with the ECSM 2000 system.

This application implements SW interface : - RX 485 Receive from physical system - RX 485 Interface to send toECOM / ESCM board - CAN Interface to send to ECOM / ESCM board - 4x40 LCD with menu controls - RTC configuration -EMIC2 Sound Card - GPIO Extender to push buttons etc

ESCMControlApp.cpp

Committer:
foxbrianr
Date:
2019-07-25
Revision:
3:ecaf9963341b
Child:
5:65f21c0b6b79

File content as of revision 3:ecaf9963341b:


#include "mbed.h"
#include "ESCMControlApp.h"


Serial rs485port1(p9, p10, 9600); //tx,rx,baud
DigitalOut rs485port1mode (p11); //Transmit = 1, Receive = 0

Serial rs485port2(p13, p14, 9600); //tx,rx,baud
DigitalOut rs485port2mode (p12); //Transmit = 1, Receive = 0

CAN canport (p30, p29);       // rx,tx
DigitalOut canportmode (p25); //Silent Mode = 1, Normal = 0


emic2 speaker(p28, p27); //serial RX,TX pins to emic

Mutex  sound_mutex;


ESCM_EventLog escmEventLog;

static int dataRxCnt    = 0; 
static int cur_address  = 0;    


void setCurrentTime (char* timeBuf)
{
    
    time_t rawtime;
    struct tm * timeinfo;
    
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
                
    int hours = timeinfo->tm_hour;
    int mins  = timeinfo->tm_min;
    int secs  = timeinfo->tm_sec;
    
    int years   = timeinfo->tm_year + 1900;
    int months  = timeinfo->tm_mon + 1 ;
    int days    = timeinfo->tm_mday;
    
    sprintf(timeBuf,"%0d/%0d/%04d %02d:%02d:%02d\0", 
        months,days,years,hours,mins,secs );
    
}

void rx485Message() {
    // Note: you need to actually read from the serial to clear the RX interrupt
    int dataRxBuffer[4];
    char timeBuffer[40];
    
    int value = rs485port1.getc();
    
    if (value){
        dataRxBuffer[dataRxCnt++]=value;
        
        if(dataRxCnt==4) {
            cur_address = 10*(dataRxBuffer[0] -0x30) + (dataRxBuffer[1] -0x30);
            memset(dataRxBuffer,0,sizeof(dataRxBuffer));
            dataRxCnt=0;
            printf("ADDR=%d\n",cur_address);
            
            setCurrentTime(timeBuffer);
            escmEventLog.add(cur_address, timeBuffer);
            ESCMControlApp::say("Unit %d is open",cur_address);
        }
    }
}



void ESCMControlApp::init()
{
    rs485port1mode = 0; // Receive
    rs485port2mode = 1; // Transmit
    canportmode = 0;    // Normal mode
    
    
    rs485port1.attach(&rx485Message); 
}


void ESCMControlApp::update(void)
{
    #if 0
    if(rs485port1.readable() ) 
    {
        rx485Message();
    }
    #endif
        
    if (cur_address)
    {
        tx485Message(cur_address);
        say("%d is open", cur_address);
    }
}


void ESCMControlApp::say (char *format, ...)
{
    char buffer[128];
    va_list args;
    va_start(args, format);
    vsprintf(buffer,format,args);
    
    speaker.speakf("S");//Speak command starts with "S"
    speaker.speakf(buffer);  
    speaker.speakf("\r"); 
    speaker.ready(); 
    
    
    va_end(args);
}

void ESCMControlApp::tx485Message(int address) {
    
    int sum =0;
    
    char dataTxBuffer[12];
    sum += dataTxBuffer[0] = 0x40;
    sum += dataTxBuffer[1]  = 0x01;
    sum += dataTxBuffer[2]  = address; // floor number
    sum += dataTxBuffer[3]  = 0x0;
    sum += dataTxBuffer[4]  = 0x0;
    sum += dataTxBuffer[5]  = 0x0;
    sum += dataTxBuffer[6]  = (0x30 + address / 10); 
    sum += dataTxBuffer[7]  = (0x30 + address % 10); 
    ; //
    sum += dataTxBuffer[8]  = 0x0;
    sum += dataTxBuffer[9]  = 0x0;
    sum += dataTxBuffer[10] = 0x0;
    sum += dataTxBuffer[11] = (char)(~sum +1);
    
    for(int i= 0;i<12;i++){
        rs485port2.putc(dataTxBuffer[i]);
    }
    
}

void ESCMControlApp::txCanMessage502 (int address)
{
    CANMessage canMessage(502);
    
    canMessage.len = 8;
    
    canMessage.data[0]  = 0x0;
    canMessage.data[1]  = (0x30 + address / 10); 
    canMessage.data[2]  = (0x30 + address % 10);
    canMessage.data[3]  = 0x0;
    canMessage.data[4]  = 0x0;
    canMessage.data[5]  = 0x0;
    canMessage.data[6]  = 0x0;
    canMessage.data[7]  = 0x0;
    
    wait_ms(5); //Wait a bit for the mode the first time
    if(canport.write(canMessage)) {
        printf("Message sent via CAN: %d\n\r", address);
    } 
    
}



Menu ESCMControlApp::getAddressMenu()
{
    return NULL;
}