Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/main.cpp@279:b60379a9eb1a, 2016-10-26 (annotated)
- Committer:
- davidjhoward
- Date:
- Wed Oct 26 21:02:04 2016 +0000
- Revision:
- 279:b60379a9eb1a
- Parent:
- 273:0373b2a15f8a
- Child:
- 282:2dc06137f1ec
Updates for BLE and added mailbox for BLE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 0:65cfa4873284 | 1 | /****************************************************************************** |
jmarkel44 | 3:8ea4db957749 | 2 | * |
jmarkel44 | 0:65cfa4873284 | 3 | * File: main.cpp |
jmarkel44 | 103:109fba818301 | 4 | * Desciption: Ground Zero |
jmarkel44 | 0:65cfa4873284 | 5 | * |
jmarkel44 | 0:65cfa4873284 | 6 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 7 | #include "mbed.h" |
jmarkel44 | 0:65cfa4873284 | 8 | #include "rtos.h" |
jmarkel44 | 0:65cfa4873284 | 9 | #include <stdio.h> |
jmarkel44 | 0:65cfa4873284 | 10 | #include "mDot.h" |
jmarkel44 | 0:65cfa4873284 | 11 | #include "global.h" |
jmarkel44 | 0:65cfa4873284 | 12 | #include "ConfigurationHandler.h" |
jmarkel44 | 0:65cfa4873284 | 13 | #include "AnalyticsLogger.h" |
jmarkel44 | 0:65cfa4873284 | 14 | #include "ModbusMaster.h" |
jmarkel44 | 0:65cfa4873284 | 15 | #include "BLEDataHandler.h" |
davidjhoward | 16:7f6599312962 | 16 | #include "LoRaInit.h" |
jmarkel44 | 20:653923c2f37a | 17 | #include "ControlTask.h" |
jmarkel44 | 48:1c7861d80d16 | 18 | #include "OutputTask.h" |
jmarkel44 | 20:653923c2f37a | 19 | #include "CloudDataHandler.h" |
davidjhoward | 59:c68c5bc9d077 | 20 | #include "rtc.h" |
jmarkel44 | 0:65cfa4873284 | 21 | |
jmarkel44 | 0:65cfa4873284 | 22 | // main thread identifier (for signaling) |
jmarkel44 | 0:65cfa4873284 | 23 | osThreadId mainThreadId = NULL; |
jmarkel44 | 75:96512ccc0443 | 24 | |
jmarkel44 | 103:109fba818301 | 25 | // bootup sequence signals |
jmarkel44 | 75:96512ccc0443 | 26 | int sig_output_continue = 0x1; |
jmarkel44 | 75:96512ccc0443 | 27 | int sig_config_continue = 0x2; |
jmarkel44 | 0:65cfa4873284 | 28 | |
jmarkel44 | 0:65cfa4873284 | 29 | // data handler to configuration hanlder mailbox |
jmarkel44 | 0:65cfa4873284 | 30 | Mail<Message_t, 16> MailBox; |
davidjhoward | 31:4b1587034318 | 31 | Mail<Message_t, 16> ModbusMasterMailBox; |
jmarkel44 | 65:be025ac18fb5 | 32 | Mail<OutputControlMsg_t, 16> OutputMasterMailBox; |
davidjhoward | 58:a4422d19b2ea | 33 | Mail<AnalyticsLoggerReq_t, 16> AnalyticsLoggerMailBox; |
davidjhoward | 279:b60379a9eb1a | 34 | Mail<BLEHandlerReq_t, 1> BLEHandlerMailBox; |
jmarkel44 | 0:65cfa4873284 | 35 | |
jmarkel44 | 0:65cfa4873284 | 36 | // local function prototypes |
jmarkel44 | 0:65cfa4873284 | 37 | static void banner(void); |
jmarkel44 | 0:65cfa4873284 | 38 | |
jmarkel44 | 77:43e0a3d9e536 | 39 | // for object (singleton) access outside of main() |
jmarkel44 | 13:c80c283f9db2 | 40 | mDot *GLOBAL_mdot; |
jmarkel44 | 13:c80c283f9db2 | 41 | |
jmarkel44 | 34:f345fdec711d | 42 | Thread *GLOBAL_analyticsLogger_thread = NULL; |
jmarkel44 | 34:f345fdec711d | 43 | Thread *GLOBAL_modbusMaster_thread = NULL; |
jmarkel44 | 256:296934e592ef | 44 | Thread *GLOBAL_BLE_thread = NULL; |
jmarkel44 | 34:f345fdec711d | 45 | Thread *GLOBAL_CDH_thread = NULL; |
jmarkel44 | 34:f345fdec711d | 46 | Thread *GLOBAL_configHandler_thread = NULL; |
jmarkel44 | 34:f345fdec711d | 47 | Thread *GLOBAL_controlTask_thread = NULL; |
jmarkel44 | 48:1c7861d80d16 | 48 | Thread *GLOBAL_outputTask_thread = NULL; |
jmarkel44 | 34:f345fdec711d | 49 | |
jmarkel44 | 13:c80c283f9db2 | 50 | // store modbus register information |
jmarkel44 | 13:c80c283f9db2 | 51 | std::map<std::string,ModbusRegister> ModbusRegisterMap; |
davidjhoward | 82:f3e495a98877 | 52 | std::map<std::string,SimulateInput> SimulateInputMap; |
davidjhoward | 7:c0c03193612d | 53 | |
davidjhoward | 58:a4422d19b2ea | 54 | I2C i2c_instance(I2C_SDA, I2C_SCL); |
davidjhoward | 58:a4422d19b2ea | 55 | I2C* i2c; |
davidjhoward | 58:a4422d19b2ea | 56 | |
jmarkel44 | 0:65cfa4873284 | 57 | /***************************************************************************** |
jmarkel44 | 103:109fba818301 | 58 | * Function: timestamp_boot_record() |
davidjhoward | 151:9d47fe5ba805 | 59 | * Description: timestamp the boot record with current time |
jmarkel44 | 103:109fba818301 | 60 | * |
jmarkel44 | 103:109fba818301 | 61 | * @param none |
jmarkel44 | 103:109fba818301 | 62 | * @return none |
jmarkel44 | 103:109fba818301 | 63 | *****************************************************************************/ |
jmarkel44 | 103:109fba818301 | 64 | static void timestamp_boot_record(void) |
jmarkel44 | 103:109fba818301 | 65 | { |
jmarkel44 | 113:001ad47df8ec | 66 | char time_string[80]; |
jmarkel44 | 103:109fba818301 | 67 | time_t curr_sec; |
jmarkel44 | 103:109fba818301 | 68 | struct tm *ts; |
davidjhoward | 151:9d47fe5ba805 | 69 | |
jmarkel44 | 103:109fba818301 | 70 | curr_sec = time(0); |
jmarkel44 | 103:109fba818301 | 71 | ts = localtime(&curr_sec); |
jmarkel44 | 103:109fba818301 | 72 | strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ts); |
davidjhoward | 151:9d47fe5ba805 | 73 | |
jmarkel44 | 103:109fba818301 | 74 | // see if we can open the boot record |
jmarkel44 | 103:109fba818301 | 75 | mDot::mdot_file file = GLOBAL_mdot->openUserFile("boot.time", mDot::FM_RDWR); |
jmarkel44 | 103:109fba818301 | 76 | if ( file.fd < 0 ) { |
jmarkel44 | 103:109fba818301 | 77 | file = GLOBAL_mdot->openUserFile("boot.time", mDot::FM_CREAT|mDot::FM_RDWR); |
jmarkel44 | 103:109fba818301 | 78 | GLOBAL_mdot->writeUserFile(file, time_string, sizeof(time_string)); |
jmarkel44 | 103:109fba818301 | 79 | } else { |
jmarkel44 | 103:109fba818301 | 80 | GLOBAL_mdot->seekUserFile(file, 0, SEEK_CUR); |
jmarkel44 | 103:109fba818301 | 81 | GLOBAL_mdot->writeUserFile(file, time_string, sizeof(time_string)); |
jmarkel44 | 103:109fba818301 | 82 | } |
jmarkel44 | 103:109fba818301 | 83 | GLOBAL_mdot->closeUserFile(file); |
jmarkel44 | 103:109fba818301 | 84 | return; |
jmarkel44 | 103:109fba818301 | 85 | } |
jmarkel44 | 103:109fba818301 | 86 | |
jmarkel44 | 103:109fba818301 | 87 | /***************************************************************************** |
jmarkel44 | 0:65cfa4873284 | 88 | * Function: banner() |
jmarkel44 | 0:65cfa4873284 | 89 | * Description: Display the application boot banner |
jmarkel44 | 0:65cfa4873284 | 90 | * |
jmarkel44 | 0:65cfa4873284 | 91 | * @param none |
jmarkel44 | 0:65cfa4873284 | 92 | * @return none |
jmarkel44 | 0:65cfa4873284 | 93 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 94 | static void banner( void ) |
jmarkel44 | 0:65cfa4873284 | 95 | { |
davidjhoward | 101:8a25782572a3 | 96 | struct tm *ts; |
davidjhoward | 101:8a25782572a3 | 97 | time_t curr_sec; |
davidjhoward | 101:8a25782572a3 | 98 | char time_string[80]; |
davidjhoward | 59:c68c5bc9d077 | 99 | |
davidjhoward | 101:8a25782572a3 | 100 | curr_sec = time(0); |
davidjhoward | 101:8a25782572a3 | 101 | ts = localtime(&curr_sec); |
davidjhoward | 101:8a25782572a3 | 102 | strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ts); |
davidjhoward | 60:96e17fb215a6 | 103 | |
jmarkel44 | 75:96512ccc0443 | 104 | printf("\n\n\r\nWelcome to Project: ICE v0.0.69\n"); |
jmarkel44 | 0:65cfa4873284 | 105 | printf("\rThe Intelligent Connected Experience\n"); |
jmarkel44 | 0:65cfa4873284 | 106 | printf("\rCopyright 2016 Nalco Water, an Ecolab Company\n"); |
jmarkel44 | 0:65cfa4873284 | 107 | |
jmarkel44 | 200:f83fefd6180a | 108 | printf("\r _________ _______ _______ \n"); |
jmarkel44 | 200:f83fefd6180a | 109 | printf("\r \\__ __/( ____ \\( ____ \\ \n"); |
jmarkel44 | 200:f83fefd6180a | 110 | printf("\r ) ( | ( \\/| ( \\/ \n"); |
jmarkel44 | 200:f83fefd6180a | 111 | printf("\r | | | | | (__ \n"); |
jmarkel44 | 200:f83fefd6180a | 112 | printf("\r | | | | | __) \n"); |
jmarkel44 | 200:f83fefd6180a | 113 | printf("\r | | | | | ( \n"); |
jmarkel44 | 200:f83fefd6180a | 114 | printf("\r ___) (___| (____/\\| (____/\\ \n"); |
jmarkel44 | 200:f83fefd6180a | 115 | printf("\r \\_______/(_______/(_______/ \n"); |
davidjhoward | 60:96e17fb215a6 | 116 | |
davidjhoward | 101:8a25782572a3 | 117 | printf("\r\nCurrent time is: %s\r\n", time_string); |
jmarkel44 | 0:65cfa4873284 | 118 | |
jmarkel44 | 200:f83fefd6180a | 119 | printf("\r\n\r\n"); |
jmarkel44 | 200:f83fefd6180a | 120 | printf("\rMultiTech mDot library version: %s\n\r\n\r\n", GLOBAL_mdot->getId().c_str()); |
jmarkel44 | 75:96512ccc0443 | 121 | |
jmarkel44 | 0:65cfa4873284 | 122 | } |
jmarkel44 | 0:65cfa4873284 | 123 | |
jmarkel44 | 0:65cfa4873284 | 124 | /***************************************************************************** |
jmarkel44 | 0:65cfa4873284 | 125 | * Function: banner() |
jmarkel44 | 0:65cfa4873284 | 126 | * Description: Display the application boot banner |
jmarkel44 | 0:65cfa4873284 | 127 | * |
jmarkel44 | 0:65cfa4873284 | 128 | * @param none |
jmarkel44 | 0:65cfa4873284 | 129 | * @return none |
jmarkel44 | 0:65cfa4873284 | 130 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 131 | int main( void ) |
jmarkel44 | 0:65cfa4873284 | 132 | { |
jmarkel44 | 0:65cfa4873284 | 133 | mDot *dot; |
davidjhoward | 101:8a25782572a3 | 134 | struct tm rtc_time; |
davidjhoward | 101:8a25782572a3 | 135 | time_t curr_sec; |
davidjhoward | 101:8a25782572a3 | 136 | int year=0; |
davidjhoward | 60:96e17fb215a6 | 137 | |
jmarkel44 | 273:0373b2a15f8a | 138 | // singleton object instantiation |
davidjhoward | 21:85c69494c0ff | 139 | GLOBAL_mdot = dot = mDot::getInstance(); |
davidjhoward | 21:85c69494c0ff | 140 | mDotRadioInit( dot ); |
davidjhoward | 60:96e17fb215a6 | 141 | |
davidjhoward | 58:a4422d19b2ea | 142 | i2c = &i2c_instance; |
davidjhoward | 60:96e17fb215a6 | 143 | |
davidjhoward | 101:8a25782572a3 | 144 | rtc_init(); |
davidjhoward | 101:8a25782572a3 | 145 | |
davidjhoward | 60:96e17fb215a6 | 146 | rtc_get_time(&year, &rtc_time.tm_mon, &rtc_time.tm_mday, &rtc_time.tm_hour, &rtc_time.tm_min, &rtc_time.tm_sec); |
davidjhoward | 60:96e17fb215a6 | 147 | rtc_time.tm_mon = rtc_time.tm_mon - 1; |
davidjhoward | 60:96e17fb215a6 | 148 | rtc_time.tm_year = year - 1900; |
davidjhoward | 60:96e17fb215a6 | 149 | curr_sec = mktime( &rtc_time ); |
davidjhoward | 101:8a25782572a3 | 150 | |
davidjhoward | 101:8a25782572a3 | 151 | set_time(curr_sec); |
jmarkel44 | 103:109fba818301 | 152 | timestamp_boot_record(); |
davidjhoward | 60:96e17fb215a6 | 153 | |
jmarkel44 | 13:c80c283f9db2 | 154 | // for signaling from the configuration handler |
jmarkel44 | 0:65cfa4873284 | 155 | mainThreadId = osThreadGetId(); |
jmarkel44 | 3:8ea4db957749 | 156 | |
jmarkel44 | 0:65cfa4873284 | 157 | banner(); |
jmarkel44 | 103:109fba818301 | 158 | |
jmarkel44 | 218:e0c05b4f470b | 159 | Thread modbusMaster_thread(ModbusMaster, NULL, osPriorityHigh, MODBUS_MASTER_STACK_SIZE, NULL); |
davidjhoward | 151:9d47fe5ba805 | 160 | osSignalWait(sig_output_continue, osWaitForever); |
davidjhoward | 151:9d47fe5ba805 | 161 | |
jmarkel44 | 103:109fba818301 | 162 | // start the output task |
jmarkel44 | 218:e0c05b4f470b | 163 | Thread outputTask_thread(OutputTask, NULL, osPriorityNormal, OUTPUT_TASK_STACK_SIZE, NULL); |
jmarkel44 | 75:96512ccc0443 | 164 | osSignalWait(sig_output_continue, osWaitForever); |
jmarkel44 | 103:109fba818301 | 165 | |
jmarkel44 | 3:8ea4db957749 | 166 | // start the configuration handler |
jmarkel44 | 218:e0c05b4f470b | 167 | Thread configHandler_thread(ConfigurationHandler, NULL, osPriorityNormal, CONFIG_HANDLER_STACK_SIZE, NULL); |
jmarkel44 | 75:96512ccc0443 | 168 | osSignalWait(sig_config_continue, osWaitForever); |
jmarkel44 | 172:51dfb4aabc57 | 169 | |
jmarkel44 | 103:109fba818301 | 170 | // we're clear to start running the controls |
jmarkel44 | 218:e0c05b4f470b | 171 | Thread controlTask_thread(ControlTask, NULL, osPriorityNormal, CONTROL_TASK_STACK_SIZE, NULL); |
jmarkel44 | 195:21df85341cb3 | 172 | logInfo("\r%s:%d: continuing to initialize...\n", __func__, __LINE__); |
jmarkel44 | 3:8ea4db957749 | 173 | |
davidjhoward | 249:68ed571e0002 | 174 | Thread BLE_thread(BLEDataHandler, NULL, osPriorityNormal, BLE_DATA_HANDLER_STACK_SIZE, NULL); |
davidjhoward | 162:5e8948b8044d | 175 | printf("\r%s:%d: continuing to initialize...\n", __func__, __LINE__); |
jmarkel44 | 219:2b6c5782f291 | 176 | |
jmarkel44 | 218:e0c05b4f470b | 177 | Thread CDH_thread(CloudDataHandler, NULL, osPriorityNormal, CLOUD_DATA_HANDLER_STACK_SIZE, NULL); |
jmarkel44 | 195:21df85341cb3 | 178 | logInfo("\r%s:%d: continuing to initialize...\n", __func__, __LINE__); |
jmarkel44 | 219:2b6c5782f291 | 179 | |
jmarkel44 | 218:e0c05b4f470b | 180 | Thread analyticsLoggerThread(AnalyticsLogger, NULL, osPriorityHigh, ANALYTICS_LOGGER_STACK_SIZE, NULL); |
jmarkel44 | 195:21df85341cb3 | 181 | logInfo("\r%s:%d: continuing to initialize...\n", __func__, __LINE__); |
davidjhoward | 151:9d47fe5ba805 | 182 | |
davidjhoward | 60:96e17fb215a6 | 183 | |
davidjhoward | 60:96e17fb215a6 | 184 | // assign globals |
jmarkel44 | 34:f345fdec711d | 185 | GLOBAL_analyticsLogger_thread = &analyticsLoggerThread; |
jmarkel44 | 34:f345fdec711d | 186 | GLOBAL_modbusMaster_thread = &modbusMaster_thread; |
jmarkel44 | 256:296934e592ef | 187 | GLOBAL_BLE_thread = & BLE_thread; |
jmarkel44 | 34:f345fdec711d | 188 | GLOBAL_CDH_thread = & CDH_thread; |
jmarkel44 | 34:f345fdec711d | 189 | GLOBAL_configHandler_thread = &configHandler_thread; |
jmarkel44 | 34:f345fdec711d | 190 | GLOBAL_controlTask_thread = &controlTask_thread; |
jmarkel44 | 48:1c7861d80d16 | 191 | GLOBAL_outputTask_thread = &outputTask_thread; |
jmarkel44 | 3:8ea4db957749 | 192 | |
jmarkel44 | 1:057d8fc6cb2f | 193 | Thread::wait(1000); |
jmarkel44 | 0:65cfa4873284 | 194 | printf("\r\n"); |
davidjhoward | 60:96e17fb215a6 | 195 | |
jmarkel44 | 3:8ea4db957749 | 196 | // start the command shell |
jmarkel44 | 0:65cfa4873284 | 197 | ntshell_execute(&ntshell, func_read, func_write, func_cb_ntshell); |
jmarkel44 | 3:8ea4db957749 | 198 | } |