Erick / Mbed 2 deprecated ICE-F412

Dependencies:   mbed-rtos mbed

ICE-Application/main.cpp

Committer:
jmarkel44
Date:
2017-01-24
Revision:
0:61364762ee0e

File content as of revision 0:61364762ee0e:

/******************************************************************************
 *
 * File:                main.cpp
 * Desciption:          Ground Zero
 *
 *****************************************************************************/
#include <time.h>
#include <iostream>

#include "global.h"
#include "mbed.h"
#include "rtos.h"
#include "ntshell.h"
#include "version.h"
#include "AnalyticsLogger.h"
#include "ConfigurationHandler.h"
#include "ControlTask.h"
#include "DataHandler.h"
#include "ModbusMaster.h"
#include "OutputTask.h"
#include "ConfigFs.h"
#include "rtc.h"
#include "mfs.h"

// threading data
osThreadId mainThreadId = NULL;

Thread *GLOBAL_analyticsLogger_thread = NULL;
Thread *GLOBAL_modbusMaster_thread = NULL;
Thread *GLOBAL_BLE_thread = NULL;
Thread *GLOBAL_CDH_thread = NULL;
Thread *GLOBAL_configHandler_thread = NULL;
Thread *GLOBAL_controlTask_thread = NULL;
Thread *GLOBAL_outputTask_thread = NULL;
Thread *GLOBAL_dataHandler_thread = NULL;

// mailboxes
Mail<ConfigMessage_t, 16>       ConfigHandlerMailBox;   
Mail<ModbusMasterReq_t, 2>      ModbusMasterMailBox;
Mail<OutputControlMsg_t, 16>    OutputMasterMailBox;
Mail<AnalyticsLoggerReq_t, 16>  AnalyticsLoggerMailBox;
Mail<BLEHandlerReq_t, 1>        BLEHandlerMailBox;

// bootup sequence signals
int sig_output_continue     = 0x1;      // allow the output handler to start 
int sig_config_continue     = 0x2;      // allow the config handler to start
int sig_control_continue    = 0x3;      // allow the control task to start

// some digitals...for fun, whilst developing 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

DigitalIn rClk(D7);

// console communication settings 
Serial console(USBTX, USBRX, 115200);

I2C* i2c;

ConfigFs EEP_FileSystem;
ConfigFs *GLOBAL_mdot = &EEP_FileSystem;

//MTSLog *GLOBAL_logger;

#undef TODO_ICE // move this, doesn't belong here...
// store modbus register information
std::map<std::string,ModbusRegister>    ModbusRegisterMap;
std::map<std::string,RegisterValue>     RegisterValueMap;
std::map<std::string,SimulateInput>     SimulateInputMap;

// local prototypes
static void banner(void);

/*****************************************************************************
 * Function:            timestamp_boot_record()
 * Description:         timestamp the boot record with current time
 *
 * @param               none
 * @return              none
 *****************************************************************************/
static void timestamp_boot_record(void)
{
    char time_string[80];
    time_t curr_sec;
    struct tm *ts;

    curr_sec = time(0);
    ts = localtime(&curr_sec);
    strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ts);

    GLOBAL_mdot->saveUserFile("boot.time", time_string, sizeof(time_string));
    return;
}


/**
 * @brief           the main software entry point
 *
 * @param           none
 * @return          0
 */



int main(void)
{    
    // light 'em up! -- remove this once we go to a real board!
    led1 = led2 = led3 = 1;

    struct tm   rtc_time;
    time_t      curr_sec;
    int         year = 0;

    mainThreadId = osThreadGetId();

    i2c = &i2c_instance;
    
    // intialize the real-time clock
    rtc_init();

    rtc_get_time(&year, &rtc_time.tm_mon, &rtc_time.tm_mday, &rtc_time.tm_hour, &rtc_time.tm_min, &rtc_time.tm_sec);
    rtc_time.tm_mon = rtc_time.tm_mon - 1;
    rtc_time.tm_year = year - 1900;
    curr_sec =  mktime( &rtc_time );

    set_time(curr_sec);

    timestamp_boot_record();
    
    mainThreadId = osThreadGetId();
     
    // display the startup banner 
    banner();
    
    // the Modbus master task
    Thread modbusMaster_thread(ModbusMaster, NULL, osPriorityHigh, MODBUS_MASTER_STACK_SIZE, NULL);
    osSignalWait(sig_output_continue, osWaitForever);
    
    // the Output task
    Thread outputTask_thread(OutputTask, NULL, osPriorityNormal, OUTPUT_TASK_STACK_SIZE, NULL);
    osSignalWait(sig_config_continue, osWaitForever);
   
    // the Configuration Handler task
    Thread configHandler_thread(ConfigurationHandler, NULL, osPriorityNormal, CONFIG_HANDLER_STACK_SIZE,  NULL);
    osSignalWait(sig_control_continue, osWaitForever);

    // the Control task
    Thread controlTask_thread(ControlTask, NULL, osPriorityNormal, CONTROL_TASK_STACK_SIZE,  NULL);
    
    // the Data Handler
    Thread dataHandler_thread(DataHandler, NULL, osPriorityNormal, DATA_HANDLER_STACK_SIZE, NULL);
    
    // the Analytics Logger task
    Thread analyticsLoggerThread(AnalyticsLogger, NULL, osPriorityHigh, ANALYTICS_LOGGER_STACK_SIZE, NULL);
    
    GLOBAL_analyticsLogger_thread = &analyticsLoggerThread;
    GLOBAL_modbusMaster_thread    = &modbusMaster_thread;
    GLOBAL_configHandler_thread   = &configHandler_thread;
    GLOBAL_controlTask_thread     = &controlTask_thread;
    GLOBAL_outputTask_thread      = &outputTask_thread;
    GLOBAL_dataHandler_thread     = &dataHandler_thread;

    Thread::wait(2500);
    
    // start the command shell -- this will loop forever
    console.printf("\r\nInvoking the command shell\n\r\n");
    ntshell_execute(&ntshell, func_read, func_write, func_cb_ntshell);
}

/**
 * @brief           the bootup banner
 * 
 * @param           none
 * @return          none
 */
static void banner(void)
{
    struct tm       *ts;
    time_t          curr_sec;
    char            time_string[80];

    curr_sec = time(0);
    ts       = localtime(&curr_sec);
    
    strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ts);

    printf("\n\n\r\nProject: ICE v%d.%d.%d (%s)\n", 
        MAJOR_VERSION_NUMBER, 
        MINOR_VERSION_NUMBER, 
        PATCH_VERSION_NUMBER, 
        PROJECT_CODE_NAME);
    
    printf("\rThe Intelligent Connected Experience\n");
    printf("\rCopyright 2017 Nalco Water, an Ecolab Company\n");
    
#ifdef __ICCARM__
    printf("\rToolchain: IAR\n");
#else 
    printf("\rToolchain: MBED\n");
#endif 

    printf("\r _________ _______  _______ \n");
    printf("\r \\__   __/(  ____ \\(  ____ \\ \n");
    printf("\r    ) (   | (    \\/| (    \\/ \n");
    printf("\r    | |   | |      | (__     \n");
    printf("\r    | |   | |      |  __)   \n");
    printf("\r    | |   | |      | (      \n");
    printf("\r ___) (___| (____/\\| (____/\\ \n");
    printf("\r \\_______/(_______/(_______/  \n");

    printf("\r\nCurrent time is: %s\r\n", time_string);
}