Fork for Get Started Demo
Dependencies: DebouncedInterrupt dash7-alp mbed-rtos mbed wizzi-utils
Fork of D7A_Demo_full by
main.cpp
- Committer:
- mikl_andre
- Date:
- 2015-11-25
- Revision:
- 8:9beb450228d4
- Parent:
- 7:4226c77951a4
- Child:
- 10:e16d5cfd4198
File content as of revision 8:9beb450228d4:
#include "mbed.h" #include "rtos.h" #include "dbg.h" #include "DebouncedInterrupt.h" #include "system.h" #include "file_sys.h" #include "alp.h" // Semaphore for notifiying button presses Semaphore button_user(1); revision_t revision = { .nw_stat = 0, .nw_seq = 255, // These data are parsed to identify the device .manufacturer_id = __MANUFACTURER_ID__, .device_id = __DEVICE_ID__, .fw_version.fw_id = __FW_ID__, .fw_version.major = __FW_MAJOR__, .fw_version.minor = __FW_MINOR__, .fw_version.patch = __FW_PATCH__, .fw_version.hash = __FW_HASH__, .hw_version = __HW_ID__, .fs_crc = 0, }; alarm_data_t alarm_data = { .nw_stat = 0, .nw_seq = 255, .status = true, }; alarm_cmd_t alarm_cmd = { .cmd = 0, }; temp_data_t temp_data = { .nw_stat = 0, .nw_seq = 255, .value = 0, }; alp_err_t d7_write_file(const uint8_t file_id, const uint16_t offset, const uint16_t file_size, const uint8_t* const content) { alp_err_t err = ALP_ERR_NO; void* file = NULL; //err = write_file(file_id, offset, file_size, content); PRINT("D7 File %d Write (offset %d, size %d)\r\n", file_id, offset, file_size); switch (file_id) { case REVISION_DEVICE_FILE_ID: file = &revision; break; case ALARM_DATA_FILE_ID: file = &alarm_data; break; case ALARM_CMD_FILE_ID: file = &alarm_cmd; // Update alarm file alarm_data.status = !content[0]; // Simulate button press button_user.release(); break; case TEMP_DATA_FILE_ID: file = &temp_data; break; default: PRINT("Unknown file id %d\r\n", file_id); err = ALP_ERR_FILE_NOT_FOUND; break; } if (file != NULL) { memcpy(file+offset, content, file_size); } return err; } alp_err_t d7_read_file( const uint8_t file_id, const uint16_t offset, const uint16_t file_size, uint8_t* buf) { alp_err_t err = ALP_ERR_NO; void* file = NULL; PRINT("D7 File %d Read (offset %d, size %d)\r\n", file_id, offset, file_size); switch (file_id) { case REVISION_DEVICE_FILE_ID: file = &revision; break; case ALARM_DATA_FILE_ID: file = &alarm_data; break; case ALARM_CMD_FILE_ID: file = &alarm_cmd; break; case TEMP_DATA_FILE_ID: file = &temp_data; break; default: PRINT("Unknown file id %d\r\n", file_id); err = ALP_ERR_FILE_NOT_FOUND; break; } if (file != NULL) { memcpy(buf, file+offset, file_size); } return err; } const d7_config_t shield1001_config = { .tx = PB_6, .rx = PA_10, .rts = PA_5, .cts = PC_7, .rx_buffer_size = 256, .local_timeout = 500, .distant_timeout = 2000, .write = d7_write_file, .read = d7_read_file, }; const d7_file_header_t revision_device_fh = { .access_class = ACCESS_CLASS_GW, .notify = true, .file_size = REVISION_DEVICE_FILE_SIZE, .file_id = REVISION_DEVICE_FILE_ID, }; const d7_file_header_t alarm_data_fh = { .access_class = ACCESS_CLASS_GW, .notify = true, .file_size = ALARM_DATA_FILE_SIZE, .file_id = ALARM_DATA_FILE_ID, }; const d7_file_header_t alarm_cmd_fh = { .access_class = ACCESS_CLASS_GW, .notify = false, .file_size = ALARM_CMD_FILE_SIZE, .file_id = ALARM_CMD_FILE_ID, }; const d7_file_header_t temp_data_fh = { .access_class = ACCESS_CLASS_GW, .notify = true, .file_size = TEMP_DATA_FILE_SIZE, .file_id = TEMP_DATA_FILE_ID, }; // Checks the status of the report send. void check_status( uint8_t status, char* message ) { switch (status) { case ALP_ERR_NO: // message is send and acknowleged PRINT("%s OK\r\n", message); break; default: PRINT("%s ERROR %d\r\n", message, status); break; } } // Interrupt Service Routine on button press. void button_push_isr( void ) { button_user.release(); } // This Thread monitors the user button // and reports the alarm status void alarm_thread( const void* args ) { // Get modem D7_modem* wm1001 = (D7_modem*)args; // Enable interrupt on User button DebouncedInterrupt button(USER_BUTTON); button.attach(&button_push_isr, IRQ_FALL, 200); while(true) { // Wait for button press button_user.wait(); // Invert alarm status alarm_data.status = !alarm_data.status; PRINT("NOTIFY ALARM STATE CHANGE %d\r\n", alarm_data.status); // Notify alarm status check_status(wm1001->notify_filechange(ALARM_DATA_FILE_ID), "ALARM REPORT"); } } // This Thread reads the chip temperature // and reports it every 5 minutes void temp_thread( const void* args ) { // Get modem D7_modem* wm1001 = (D7_modem*)args; while(true) { temp_data.value = system_get_temperature(); PRINT("NOTIFY TEMPERATURE %d C\r\n", temp_data.value); // Notify temp value check_status(wm1001->notify_filechange(TEMP_DATA_FILE_ID), "TEMP REPORT"); // Wait 5 minutes // The function Thread::wait(...) takes a uin32_t as parameter // but the maximum value is uin16_t (65535) for (uint8_t i=0 ; i<5 ; i++) { Thread::wait(60000); } } } int main() { // ----- Debug session over USB Serial ----- // DBG_OPEN(); // Clear some lines on the terminal PRINT("\r\n\nBOOT\r\n"); // Initialize system functions system_open(); // Initialize ALP modem D7_modem wm1001(&shield1001_config, PB_0); // Reset modem wm1001.reset(); // Check the boot packets ASSERT(wm1001.wait_boot() == ALP_ERR_NO, "BOOT FAIL!\r\n"); // Wait for modem to notify its files if there is any Thread::wait(200); // Register the files wm1001.register_file(&revision_device_fh); wm1001.register_file(&alarm_data_fh); wm1001.register_file(&alarm_cmd_fh); wm1001.register_file(&temp_data_fh); // Send the Revision report PRINT("NOTIFY REVISION\r\n"); check_status(wm1001.notify_filechange(REVISION_DEVICE_FILE_ID), "REPORT REVISION"); // Init Threads Thread t_alarm_thread(alarm_thread, &wm1001); Thread t_temp_thread(temp_thread, &wm1001); // Set main task to lowest priority osThreadSetPriority(osThreadGetId(), osPriorityIdle); while(true) { // Wait to avoid beeing stuck in loop Thread::wait(osWaitForever); } }