Complete sensor demo.
Dependencies: modem_ref_helper CRC X_NUCLEO_IKS01A1 DebouncedInterrupt
Diff: main.cpp
- Revision:
- 6:c17f7cbdeb1a
- Parent:
- 4:27a3b9db1754
- Child:
- 7:b94971f119a5
--- a/main.cpp Wed Sep 20 14:41:54 2017 +0000 +++ b/main.cpp Thu Sep 21 11:23:13 2017 +0000 @@ -11,26 +11,32 @@ #include "files.h" -Semaphore button_user(0); +enum { + MODEM_RESP_NO, + MODEM_RESP_TERMINAL, + MODEM_RESP_DONE, +}; + +Semaphore button_user(1); Semaphore modem_ready[MAX_USER_NB]; Semaphore thread_ready(0); sensor_config_t g_light_config; Queue<void, 8> g_file_modified; // Check parameters to see if data should be send -static bool report_needed(sensor_config_t* config, int32_t value, int32_t last_value, uint32_t last_report_time) +static bool report_needed(sensor_config_t* config, int32_t value, int32_t last_value, uint32_t last_report_time, uint8_t user_id) { switch (config->report_type) { case REPORT_ALWAYS: // Send a report at each measure - IPRINT("Report always\r\n"); + PRINT("Report[%d] always\r\n", user_id); return true; case REPORT_ON_DIFFERENCE: // Send a report when the difference between the last reported measure and the current mesure is greater than max_diff if (abs(last_value - value) >= config->max_diff && config->max_diff) { - IPRINT("Report on difference (last:%d new:%d max_diff:%d)\r\n", last_value, value, config->max_diff); + PRINT("Report[%d] on difference (last:%d new:%d max_diff:%d)\r\n", user_id, last_value, value, config->max_diff); return true; } break; @@ -41,7 +47,7 @@ || (value < config->threshold_high && last_value >= config->threshold_high) || (value > config->threshold_low && last_value <= config->threshold_low)) { - IPRINT("Rerport on threshold (last:%d new:%d th:%d tl:%d)\r\n", last_value, value, config->threshold_high, config->threshold_low); + PRINT("Report[%d] on threshold (last:%d new:%d th:%d tl:%d)\r\n", user_id, last_value, value, config->threshold_high, config->threshold_low); return true; } break; @@ -52,7 +58,7 @@ // Send a report if it's been more than max_period since the last report if (((last_report_time/1000) >= config->max_period) && config->max_period) { - IPRINT("Report on period (max_period:%d time:%d)\r\n", config->max_period, last_report_time); + PRINT("Report[%d] on period (max_period:%d time:%d)\r\n", user_id, config->max_period, last_report_time); return true; } @@ -206,10 +212,7 @@ for (uint8_t i = 0; i < ctx->nb_values; i++) { - if (report_needed(&(ctx->config), - ctx->current_value[i], - ctx->last_report_value[i], - last_report_time)) + if (report_needed(&(ctx->config), ctx->current_value[i], ctx->last_report_value[i], last_report_time, user_id)) { // Send notification modem_write_file(ctx->value_file_id, ctx->current_value, 0, ctx->data_size, user_id); @@ -272,14 +275,38 @@ } } +Queue<void, 8> modem_resp; + +// Callback for button +void my_response_callback(uint8_t terminal, int8_t err, uint8_t id) +{ + (void)id; + + if (terminal) + { + print_status(err, id); + modem_resp.put((void*)MODEM_RESP_TERMINAL); + } + else + { + print_resp(err, id); + if (ALP_ERR_NONE == err) + { + modem_resp.put((void*)MODEM_RESP_DONE); + } + } +} + void thread_user_button() { FPRINT("(id:0x%08x)\r\n", osThreadGetId()); + osEvent evt; + uint32_t resp; uint8_t alarm; d7a_sp_res_t istat; - uint8_t user_id = modem_get_id(my_main_callback); - + uint8_t user_id = modem_get_id(my_response_callback); + uint8_t nb = 0; alp_d7a_itf_t alarm_itf = { .type = ALP_ITF_TYPE_D7A, @@ -298,27 +325,51 @@ // Load alarm value ram_fs_read(FID_ALARM, 0, 1, &alarm); - // Send initial value - modem_send_file_content((uint8_t*)&alarm_itf, D7_ITF_SIZE(&alarm_itf), (void*)&istat, FID_ALARM, &alarm, 0, 1, user_id); - modem_ready[user_id].wait(); - while (true) { // Wait for button press button_user.wait(); - // load/save value to keep choerency in case of remote access... + // load/save value to keep coherence in case of remote access... ram_fs_read(FID_ALARM, 0, 1, &alarm); - // Toggle alarm state - alarm = !alarm; - - ram_fs_write(FID_ALARM, 0, 1, &alarm); + // Initial value + if (alarm != 255) + { + // Toggle alarm state + alarm = !alarm; + ram_fs_write(FID_ALARM, 0, 1, &alarm); + } PRINT("BUTTON ALARM %d\r\n", alarm); - + + nb = 0; modem_send_file_content((uint8_t*)&alarm_itf, D7_ITF_SIZE(&alarm_itf), (void*)&istat, FID_ALARM, &alarm, 0, 1, user_id); - modem_ready[user_id].wait(); + + do + { + evt = modem_resp.get(); + resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_NO; + + if (MODEM_RESP_DONE == resp) + { + nb++; + PRINT("%d: XCL:%02X ", nb, istat.addressee.xcl.byte); + PRINT_DATA("UID:", "%02X", istat.addressee.id, 8, " "); + PRINT("snr:%d rxlev:%d lb:%d\n", istat.snr, istat.rxlev, istat.lb); + + // Reset variable + memset(&istat, 0, sizeof(d7a_sp_res_t)); + } + + } while (MODEM_RESP_TERMINAL != resp); + + if (alarm == 255) + { + // Toggle alarm state + alarm = !!alarm; + ram_fs_write(FID_ALARM, 0, 1, &alarm); + } } }