Complete sensor demo.

Dependencies:   modem_ref_helper X_NUCLEO_IKS01A1 DebouncedInterrupt

Committer:
Jeej
Date:
Thu Oct 11 15:15:30 2018 +0000
Revision:
14:8fd5405f7ab2
Parent:
12:7a56e0705c9d
Child:
15:1271f3566b98
Relaxed modem version check.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jeej 0:87c57e1b1e1c 1 // This project is a demo of the DASH7 1.x stack
Jeej 0:87c57e1b1e1c 2 // @autor: jeremie@wizzilab.com
Jeej 0:87c57e1b1e1c 3 // @date: 2016-12-20
Jeej 0:87c57e1b1e1c 4
Jeej 1:4d3968b2941b 5 #include "DebouncedInterrupt.h"
Jeej 0:87c57e1b1e1c 6 #include "sensors.h"
Jeej 1:4d3968b2941b 7 #include "sensors_cfg.h"
Jeej 1:4d3968b2941b 8 #include "simul.h"
Jeej 1:4d3968b2941b 9 #include "modem_ref_helper.h"
Jeej 1:4d3968b2941b 10 #include "modem_callbacks.h"
Jeej 0:87c57e1b1e1c 11 #include "files.h"
Jeej 0:87c57e1b1e1c 12
Jeej 7:b94971f119a5 13 #define MIN_REPORT_PERIOD (10) // Seconds
Jeej 0:87c57e1b1e1c 14
Jeej 6:c17f7cbdeb1a 15 enum {
Jeej 6:c17f7cbdeb1a 16 MODEM_RESP_NO,
Jeej 6:c17f7cbdeb1a 17 MODEM_RESP_TERMINAL,
Jeej 6:c17f7cbdeb1a 18 MODEM_RESP_DONE,
Jeej 6:c17f7cbdeb1a 19 };
Jeej 6:c17f7cbdeb1a 20
Jeej 6:c17f7cbdeb1a 21 Semaphore button_user(1);
Jeej 0:87c57e1b1e1c 22 Semaphore modem_ready[MAX_USER_NB];
Jeej 0:87c57e1b1e1c 23 Semaphore thread_ready(0);
Jeej 0:87c57e1b1e1c 24 sensor_config_t g_light_config;
Jeej 0:87c57e1b1e1c 25 Queue<void, 8> g_file_modified;
Jeej 0:87c57e1b1e1c 26
Jeej 7:b94971f119a5 27 static bool report_ok(uint32_t last_report_time)
Jeej 7:b94971f119a5 28 {
Jeej 7:b94971f119a5 29 // Do not send a report if it's been less than MIN_REPORT_PERIOD since the last report
Jeej 7:b94971f119a5 30 if ((last_report_time/1000) < MIN_REPORT_PERIOD)
Jeej 7:b94971f119a5 31 {
Jeej 11:9683d014dece 32 PRINT("Report Skipped. (Locked for %ds)\n", MIN_REPORT_PERIOD - (last_report_time/1000));
Jeej 7:b94971f119a5 33 return false;
Jeej 7:b94971f119a5 34 }
Jeej 7:b94971f119a5 35
Jeej 7:b94971f119a5 36 return true;
Jeej 7:b94971f119a5 37 }
Jeej 7:b94971f119a5 38
Jeej 0:87c57e1b1e1c 39 // Check parameters to see if data should be send
Jeej 6:c17f7cbdeb1a 40 static bool report_needed(sensor_config_t* config, int32_t value, int32_t last_value, uint32_t last_report_time, uint8_t user_id)
Jeej 0:87c57e1b1e1c 41 {
Jeej 7:b94971f119a5 42
Jeej 0:87c57e1b1e1c 43 switch (config->report_type)
Jeej 0:87c57e1b1e1c 44 {
Jeej 0:87c57e1b1e1c 45 case REPORT_ALWAYS:
Jeej 0:87c57e1b1e1c 46 // Send a report at each measure
Jeej 6:c17f7cbdeb1a 47 PRINT("Report[%d] always\r\n", user_id);
Jeej 7:b94971f119a5 48 return report_ok(last_report_time);
Jeej 0:87c57e1b1e1c 49 case REPORT_ON_DIFFERENCE:
Jeej 0:87c57e1b1e1c 50 // Send a report when the difference between the last reported measure and the current mesure is greater than max_diff
Jeej 0:87c57e1b1e1c 51 if (abs(last_value - value) >= config->max_diff && config->max_diff)
Jeej 0:87c57e1b1e1c 52 {
Jeej 6:c17f7cbdeb1a 53 PRINT("Report[%d] on difference (last:%d new:%d max_diff:%d)\r\n", user_id, last_value, value, config->max_diff);
Jeej 7:b94971f119a5 54 return report_ok(last_report_time);
Jeej 0:87c57e1b1e1c 55 }
Jeej 0:87c57e1b1e1c 56 break;
Jeej 0:87c57e1b1e1c 57 case REPORT_ON_THRESHOLD:
Jeej 0:87c57e1b1e1c 58 // Send a report when crossing a threshold
Jeej 0:87c57e1b1e1c 59 if ( (value >= config->threshold_high && last_value < config->threshold_high)
Jeej 0:87c57e1b1e1c 60 || (value <= config->threshold_low && last_value > config->threshold_low)
Jeej 0:87c57e1b1e1c 61 || (value < config->threshold_high && last_value >= config->threshold_high)
Jeej 0:87c57e1b1e1c 62 || (value > config->threshold_low && last_value <= config->threshold_low))
Jeej 0:87c57e1b1e1c 63 {
Jeej 6:c17f7cbdeb1a 64 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);
Jeej 7:b94971f119a5 65 return report_ok(last_report_time);
Jeej 0:87c57e1b1e1c 66 }
Jeej 0:87c57e1b1e1c 67 break;
Jeej 0:87c57e1b1e1c 68 default:
Jeej 0:87c57e1b1e1c 69 break;
Jeej 0:87c57e1b1e1c 70 }
Jeej 0:87c57e1b1e1c 71
Jeej 0:87c57e1b1e1c 72 // Send a report if it's been more than max_period since the last report
Jeej 0:87c57e1b1e1c 73 if (((last_report_time/1000) >= config->max_period) && config->max_period)
Jeej 0:87c57e1b1e1c 74 {
Jeej 6:c17f7cbdeb1a 75 PRINT("Report[%d] on period (max_period:%d time:%d)\r\n", user_id, config->max_period, last_report_time);
Jeej 7:b94971f119a5 76 return report_ok(last_report_time);
Jeej 0:87c57e1b1e1c 77 }
Jeej 0:87c57e1b1e1c 78
Jeej 0:87c57e1b1e1c 79 return false;
Jeej 0:87c57e1b1e1c 80 }
Jeej 0:87c57e1b1e1c 81
Jeej 0:87c57e1b1e1c 82 // Interrupt Service Routine on button press.
Jeej 0:87c57e1b1e1c 83 void button_push_isr( void )
Jeej 0:87c57e1b1e1c 84 {
Jeej 0:87c57e1b1e1c 85 button_user.release();
Jeej 0:87c57e1b1e1c 86 }
Jeej 0:87c57e1b1e1c 87
Jeej 0:87c57e1b1e1c 88 modem_callbacks_t callbacks = {
Jeej 0:87c57e1b1e1c 89 .read = my_read,
Jeej 0:87c57e1b1e1c 90 .write = my_write,
Jeej 0:87c57e1b1e1c 91 .read_fprop = my_read_fprop,
Jeej 0:87c57e1b1e1c 92 .flush = my_flush,
Jeej 0:87c57e1b1e1c 93 .remove = my_delete,
Jeej 1:4d3968b2941b 94 .udata = my_udata,
Jeej 0:87c57e1b1e1c 95 .lqual = my_lqual,
Jeej 0:87c57e1b1e1c 96 .ldown = my_ldown,
Jeej 0:87c57e1b1e1c 97 .reset = my_reset,
Jeej 11:9683d014dece 98 .boot = my_boot,
Jeej 11:9683d014dece 99 .busy = my_busy,
Jeej 0:87c57e1b1e1c 100 };
Jeej 0:87c57e1b1e1c 101
Jeej 4:27a3b9db1754 102 // Callback for Users
Jeej 0:87c57e1b1e1c 103 void my_main_callback(uint8_t terminal, int8_t err, uint8_t id)
Jeej 14:8fd5405f7ab2 104 {
Jeej 14:8fd5405f7ab2 105 if (ALP_ERR_NONE != err)
Jeej 14:8fd5405f7ab2 106 {
Jeej 14:8fd5405f7ab2 107 PRINT("Status[%d]: ", id);
Jeej 14:8fd5405f7ab2 108 modem_print_error(ALP_ITF_TYPE_D7A, err);
Jeej 14:8fd5405f7ab2 109 }
Jeej 14:8fd5405f7ab2 110
Jeej 0:87c57e1b1e1c 111 if (terminal)
Jeej 4:27a3b9db1754 112 {
Jeej 0:87c57e1b1e1c 113 modem_ready[id].release();
Jeej 0:87c57e1b1e1c 114 }
Jeej 0:87c57e1b1e1c 115 }
Jeej 0:87c57e1b1e1c 116
Jeej 0:87c57e1b1e1c 117 // -----------------------------------------------
Jeej 0:87c57e1b1e1c 118 // Sensor Threads
Jeej 0:87c57e1b1e1c 119 // -----------------------------------------------
Jeej 0:87c57e1b1e1c 120 typedef struct
Jeej 0:87c57e1b1e1c 121 {
Jeej 0:87c57e1b1e1c 122 // Number of data fields
Jeej 0:87c57e1b1e1c 123 uint32_t nb_values;
Jeej 0:87c57e1b1e1c 124 // Total size of data
Jeej 0:87c57e1b1e1c 125 uint32_t data_size;
Jeej 0:87c57e1b1e1c 126 // Read value function
Jeej 0:87c57e1b1e1c 127 bool (*read_value)(int32_t*);
Jeej 0:87c57e1b1e1c 128 // Last reported value
Jeej 0:87c57e1b1e1c 129 int32_t* last_report_value;
Jeej 0:87c57e1b1e1c 130 // Current measured value
Jeej 0:87c57e1b1e1c 131 int32_t* current_value;
Jeej 0:87c57e1b1e1c 132
Jeej 0:87c57e1b1e1c 133 // File ID of the sensor value file
Jeej 0:87c57e1b1e1c 134 uint8_t value_file_id;
Jeej 0:87c57e1b1e1c 135 // Sensor configuration file ID
Jeej 0:87c57e1b1e1c 136 uint8_t config_file_id;
Jeej 0:87c57e1b1e1c 137 // Sensor configuration context
Jeej 0:87c57e1b1e1c 138 sensor_config_t config;
Jeej 0:87c57e1b1e1c 139
Jeej 0:87c57e1b1e1c 140 } sensor_thread_ctx_t;
Jeej 0:87c57e1b1e1c 141
Jeej 0:87c57e1b1e1c 142 sensor_thread_ctx_t* g_thread_ctx;
Jeej 0:87c57e1b1e1c 143
Jeej 0:87c57e1b1e1c 144 // Initialisation of the sensor thread's context
Jeej 0:87c57e1b1e1c 145 #define SENSOR_THREAD_CTX(name,NAME,_nb_values) \
Jeej 0:87c57e1b1e1c 146 int32_t name##_last_report[_nb_values];\
Jeej 0:87c57e1b1e1c 147 int32_t name##_current_value[_nb_values];\
Jeej 0:87c57e1b1e1c 148 sensor_thread_ctx_t name##_thread_ctx = {\
Jeej 0:87c57e1b1e1c 149 .nb_values = _nb_values,\
Jeej 0:87c57e1b1e1c 150 .data_size = _nb_values * sizeof(int32_t),\
Jeej 0:87c57e1b1e1c 151 .read_value = name##_get_value,\
Jeej 0:87c57e1b1e1c 152 .last_report_value = (int32_t*)&name##_last_report,\
Jeej 0:87c57e1b1e1c 153 .current_value = (int32_t*)&name##_current_value,\
Jeej 0:87c57e1b1e1c 154 .value_file_id = FID_SENSOR_VALUE_##NAME,\
Jeej 0:87c57e1b1e1c 155 .config_file_id = FID_SENSOR_CONFIG_##NAME\
Jeej 0:87c57e1b1e1c 156 }
Jeej 0:87c57e1b1e1c 157
Jeej 0:87c57e1b1e1c 158
Jeej 0:87c57e1b1e1c 159 SENSOR_THREAD_CTX(mag, MAG, 3);
Jeej 0:87c57e1b1e1c 160 SENSOR_THREAD_CTX(acc, ACC, 3);
Jeej 0:87c57e1b1e1c 161 SENSOR_THREAD_CTX(gyr, GYR, 3);
Jeej 0:87c57e1b1e1c 162 SENSOR_THREAD_CTX(pre, PRE, 1);
Jeej 0:87c57e1b1e1c 163 SENSOR_THREAD_CTX(hum, HUM, 1);
Jeej 0:87c57e1b1e1c 164 SENSOR_THREAD_CTX(tem1, TEM1, 1);
Jeej 0:87c57e1b1e1c 165 SENSOR_THREAD_CTX(tem2, TEM2, 1);
Jeej 0:87c57e1b1e1c 166 SENSOR_THREAD_CTX(light, LIGHT, 1);
Jeej 0:87c57e1b1e1c 167
Jeej 0:87c57e1b1e1c 168 void thread_sensor()
Jeej 0:87c57e1b1e1c 169 {
Jeej 0:87c57e1b1e1c 170 FPRINT("(id:0x%08x)\r\n", osThreadGetId());
Jeej 0:87c57e1b1e1c 171
Jeej 0:87c57e1b1e1c 172 // To force a first report
Jeej 0:87c57e1b1e1c 173 uint32_t last_report_time = 0xFFFFFFFF;
Jeej 0:87c57e1b1e1c 174 sensor_thread_ctx_t* ctx = g_thread_ctx;
Jeej 0:87c57e1b1e1c 175 uint8_t user_id = modem_get_id(my_main_callback);
Jeej 0:87c57e1b1e1c 176
Jeej 0:87c57e1b1e1c 177 // Get the sensor configuration
Jeej 0:87c57e1b1e1c 178 ram_fs_read(ctx->config_file_id, 0, sizeof(sensor_config_t), (uint8_t*)&(ctx->config));
Jeej 0:87c57e1b1e1c 179
Jeej 0:87c57e1b1e1c 180 PRINT("Start sensor thread %d\n", user_id);
Jeej 0:87c57e1b1e1c 181
Jeej 0:87c57e1b1e1c 182 thread_ready.release();
Jeej 0:87c57e1b1e1c 183
Jeej 0:87c57e1b1e1c 184 while (true)
Jeej 0:87c57e1b1e1c 185 {
Jeej 0:87c57e1b1e1c 186 bool err = ctx->read_value(ctx->current_value);
Jeej 0:87c57e1b1e1c 187
Jeej 0:87c57e1b1e1c 188 ASSERT(err == 0, "Failed to read sensor\n");
Jeej 0:87c57e1b1e1c 189
Jeej 1:4d3968b2941b 190 //PRINT("Got %3d: ", ctx->value_file_id);
Jeej 1:4d3968b2941b 191 //for (uint8_t i = 0; i < ctx->nb_values; i++)
Jeej 1:4d3968b2941b 192 //{
Jeej 1:4d3968b2941b 193 // PRINT("%9ld ", (int32_t)ctx->current_value[i]);
Jeej 1:4d3968b2941b 194 //}
Jeej 1:4d3968b2941b 195 //PRINT("\r\n");
Jeej 0:87c57e1b1e1c 196
Jeej 0:87c57e1b1e1c 197 for (uint8_t i = 0; i < ctx->nb_values; i++)
Jeej 0:87c57e1b1e1c 198 {
Jeej 6:c17f7cbdeb1a 199 if (report_needed(&(ctx->config), ctx->current_value[i], ctx->last_report_value[i], last_report_time, user_id))
Jeej 0:87c57e1b1e1c 200 {
Jeej 0:87c57e1b1e1c 201 // Send notification
Jeej 0:87c57e1b1e1c 202 modem_write_file(ctx->value_file_id, ctx->current_value, 0, ctx->data_size, user_id);
Jeej 0:87c57e1b1e1c 203 modem_ready[user_id].wait();
Jeej 0:87c57e1b1e1c 204
Jeej 0:87c57e1b1e1c 205 // Update last report value
Jeej 0:87c57e1b1e1c 206 memcpy(ctx->last_report_value, ctx->current_value, ctx->data_size);
Jeej 0:87c57e1b1e1c 207 // Reset last report time
Jeej 0:87c57e1b1e1c 208 last_report_time = 0;
Jeej 0:87c57e1b1e1c 209 }
Jeej 0:87c57e1b1e1c 210 }
Jeej 0:87c57e1b1e1c 211
Jeej 0:87c57e1b1e1c 212 // Update last report time
Jeej 0:87c57e1b1e1c 213 last_report_time += ctx->config.read_period;
Jeej 0:87c57e1b1e1c 214
Jeej 0:87c57e1b1e1c 215 Thread::wait(ctx->config.read_period);
Jeej 0:87c57e1b1e1c 216 }
Jeej 0:87c57e1b1e1c 217 }
Jeej 0:87c57e1b1e1c 218
Jeej 0:87c57e1b1e1c 219 void thread_file_modified()
Jeej 0:87c57e1b1e1c 220 {
Jeej 0:87c57e1b1e1c 221 uint8_t fid;
Jeej 0:87c57e1b1e1c 222 osEvent evt;
Jeej 0:87c57e1b1e1c 223
Jeej 0:87c57e1b1e1c 224 while (true)
Jeej 0:87c57e1b1e1c 225 {
Jeej 0:87c57e1b1e1c 226 evt = g_file_modified.get();
Jeej 0:87c57e1b1e1c 227 fid = (evt.status == osEventMessage)? (uint8_t)(uint32_t)evt.value.p : NULL;
Jeej 0:87c57e1b1e1c 228
Jeej 0:87c57e1b1e1c 229 switch (fid)
Jeej 0:87c57e1b1e1c 230 {
Jeej 0:87c57e1b1e1c 231 // If a configuration file has been modified, update the context
Jeej 0:87c57e1b1e1c 232 case FID_SENSOR_CONFIG_MAG:
Jeej 0:87c57e1b1e1c 233 ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(mag_thread_ctx.config));
Jeej 0:87c57e1b1e1c 234 break;
Jeej 0:87c57e1b1e1c 235 case FID_SENSOR_CONFIG_ACC:
Jeej 0:87c57e1b1e1c 236 ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(acc_thread_ctx.config));
Jeej 0:87c57e1b1e1c 237 break;
Jeej 0:87c57e1b1e1c 238 case FID_SENSOR_CONFIG_GYR:
Jeej 0:87c57e1b1e1c 239 ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(gyr_thread_ctx.config));
Jeej 0:87c57e1b1e1c 240 break;
Jeej 0:87c57e1b1e1c 241 case FID_SENSOR_CONFIG_PRE:
Jeej 0:87c57e1b1e1c 242 ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(pre_thread_ctx.config));
Jeej 0:87c57e1b1e1c 243 break;
Jeej 0:87c57e1b1e1c 244 case FID_SENSOR_CONFIG_HUM:
Jeej 0:87c57e1b1e1c 245 ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(hum_thread_ctx.config));
Jeej 0:87c57e1b1e1c 246 break;
Jeej 0:87c57e1b1e1c 247 case FID_SENSOR_CONFIG_TEM1:
Jeej 0:87c57e1b1e1c 248 ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(tem1_thread_ctx.config));
Jeej 0:87c57e1b1e1c 249 break;
Jeej 0:87c57e1b1e1c 250 case FID_SENSOR_CONFIG_TEM2:
Jeej 0:87c57e1b1e1c 251 ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(tem2_thread_ctx.config));
Jeej 0:87c57e1b1e1c 252 break;
Jeej 0:87c57e1b1e1c 253 case FID_SENSOR_CONFIG_LIGHT:
Jeej 0:87c57e1b1e1c 254 ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(light_thread_ctx.config));
Jeej 0:87c57e1b1e1c 255 break;
Jeej 0:87c57e1b1e1c 256 default:
Jeej 0:87c57e1b1e1c 257 break;
Jeej 0:87c57e1b1e1c 258 }
Jeej 0:87c57e1b1e1c 259 }
Jeej 0:87c57e1b1e1c 260 }
Jeej 0:87c57e1b1e1c 261
Jeej 6:c17f7cbdeb1a 262 Queue<void, 8> modem_resp;
Jeej 6:c17f7cbdeb1a 263
Jeej 6:c17f7cbdeb1a 264 // Callback for button
Jeej 6:c17f7cbdeb1a 265 void my_response_callback(uint8_t terminal, int8_t err, uint8_t id)
Jeej 6:c17f7cbdeb1a 266 {
Jeej 14:8fd5405f7ab2 267 UNUSED(id);
Jeej 14:8fd5405f7ab2 268
Jeej 14:8fd5405f7ab2 269 if (ALP_ERR_NONE != err)
Jeej 14:8fd5405f7ab2 270 {
Jeej 14:8fd5405f7ab2 271 modem_print_error(ALP_ITF_TYPE_D7A, err);
Jeej 14:8fd5405f7ab2 272 }
Jeej 6:c17f7cbdeb1a 273
Jeej 6:c17f7cbdeb1a 274 if (terminal)
Jeej 6:c17f7cbdeb1a 275 {
Jeej 6:c17f7cbdeb1a 276 modem_resp.put((void*)MODEM_RESP_TERMINAL);
Jeej 6:c17f7cbdeb1a 277 }
Jeej 6:c17f7cbdeb1a 278 else
Jeej 6:c17f7cbdeb1a 279 {
Jeej 6:c17f7cbdeb1a 280 if (ALP_ERR_NONE == err)
Jeej 6:c17f7cbdeb1a 281 {
Jeej 6:c17f7cbdeb1a 282 modem_resp.put((void*)MODEM_RESP_DONE);
Jeej 6:c17f7cbdeb1a 283 }
Jeej 6:c17f7cbdeb1a 284 }
Jeej 6:c17f7cbdeb1a 285 }
Jeej 6:c17f7cbdeb1a 286
Jeej 0:87c57e1b1e1c 287 void thread_user_button()
Jeej 0:87c57e1b1e1c 288 {
Jeej 0:87c57e1b1e1c 289 FPRINT("(id:0x%08x)\r\n", osThreadGetId());
Jeej 0:87c57e1b1e1c 290
Jeej 6:c17f7cbdeb1a 291 osEvent evt;
Jeej 6:c17f7cbdeb1a 292 uint32_t resp;
Jeej 0:87c57e1b1e1c 293 uint8_t alarm;
Jeej 0:87c57e1b1e1c 294 d7a_sp_res_t istat;
Jeej 6:c17f7cbdeb1a 295 uint8_t user_id = modem_get_id(my_response_callback);
Jeej 6:c17f7cbdeb1a 296 uint8_t nb = 0;
Jeej 0:87c57e1b1e1c 297
Jeej 0:87c57e1b1e1c 298 alp_d7a_itf_t alarm_itf = {
Jeej 0:87c57e1b1e1c 299 .type = ALP_ITF_TYPE_D7A,
Jeej 0:87c57e1b1e1c 300 .cfg.to = 0,
Jeej 0:87c57e1b1e1c 301 .cfg.te = 0,
Jeej 0:87c57e1b1e1c 302 .cfg.qos.bf.resp = D7A_RESP_PREFERRED,
Jeej 0:87c57e1b1e1c 303 .cfg.qos.bf.retry = 1, // XXX WM_RPOL_RARE_SINGLE_CHECK,
Jeej 0:87c57e1b1e1c 304 .cfg.addressee.ctrl.bf.nls = D7A_NLS_AES_CCM_64,
Jeej 0:87c57e1b1e1c 305 .cfg.addressee.ctrl.bf.idf = D7A_ID_NBID,
Jeej 14:8fd5405f7ab2 306 .cfg.addressee.xcl.bf = {.s = 2, .m = 0x1},// XXX D7A_XCL_GW,
Jeej 14:8fd5405f7ab2 307 .cfg.addressee.id[0] = D7A_CTF_ENCODE(4),
Jeej 0:87c57e1b1e1c 308 };
Jeej 0:87c57e1b1e1c 309
Jeej 0:87c57e1b1e1c 310 // Load alarm value
Jeej 0:87c57e1b1e1c 311 ram_fs_read(FID_ALARM, 0, 1, &alarm);
Jeej 0:87c57e1b1e1c 312
Jeej 0:87c57e1b1e1c 313 while (true)
Jeej 0:87c57e1b1e1c 314 {
Jeej 0:87c57e1b1e1c 315 // Wait for button press
Jeej 0:87c57e1b1e1c 316 button_user.wait();
Jeej 0:87c57e1b1e1c 317
Jeej 6:c17f7cbdeb1a 318 // load/save value to keep coherence in case of remote access...
Jeej 0:87c57e1b1e1c 319 ram_fs_read(FID_ALARM, 0, 1, &alarm);
Jeej 0:87c57e1b1e1c 320
Jeej 6:c17f7cbdeb1a 321 // Initial value
Jeej 6:c17f7cbdeb1a 322 if (alarm != 255)
Jeej 6:c17f7cbdeb1a 323 {
Jeej 6:c17f7cbdeb1a 324 // Toggle alarm state
Jeej 6:c17f7cbdeb1a 325 alarm = !alarm;
Jeej 6:c17f7cbdeb1a 326 ram_fs_write(FID_ALARM, 0, 1, &alarm);
Jeej 6:c17f7cbdeb1a 327 }
Jeej 0:87c57e1b1e1c 328
Jeej 0:87c57e1b1e1c 329 PRINT("BUTTON ALARM %d\r\n", alarm);
Jeej 6:c17f7cbdeb1a 330
Jeej 6:c17f7cbdeb1a 331 nb = 0;
Jeej 1:4d3968b2941b 332 modem_send_file_content((uint8_t*)&alarm_itf, D7_ITF_SIZE(&alarm_itf), (void*)&istat, FID_ALARM, &alarm, 0, 1, user_id);
Jeej 6:c17f7cbdeb1a 333
Jeej 6:c17f7cbdeb1a 334 do
Jeej 6:c17f7cbdeb1a 335 {
Jeej 6:c17f7cbdeb1a 336 evt = modem_resp.get();
Jeej 6:c17f7cbdeb1a 337 resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_NO;
Jeej 6:c17f7cbdeb1a 338
Jeej 6:c17f7cbdeb1a 339 if (MODEM_RESP_DONE == resp)
Jeej 6:c17f7cbdeb1a 340 {
Jeej 6:c17f7cbdeb1a 341 nb++;
Jeej 6:c17f7cbdeb1a 342 PRINT("%d: XCL:%02X ", nb, istat.addressee.xcl.byte);
Jeej 6:c17f7cbdeb1a 343 PRINT_DATA("UID:", "%02X", istat.addressee.id, 8, " ");
Jeej 11:9683d014dece 344 PRINT("SNR: %3ddB RXLEV: -%-3ddBm LB: %3ddB\n", istat.snr, istat.rxlev, istat.lb);
Jeej 6:c17f7cbdeb1a 345
Jeej 6:c17f7cbdeb1a 346 // Reset variable
Jeej 6:c17f7cbdeb1a 347 memset(&istat, 0, sizeof(d7a_sp_res_t));
Jeej 6:c17f7cbdeb1a 348 }
Jeej 6:c17f7cbdeb1a 349
Jeej 6:c17f7cbdeb1a 350 } while (MODEM_RESP_TERMINAL != resp);
Jeej 6:c17f7cbdeb1a 351
Jeej 6:c17f7cbdeb1a 352 if (alarm == 255)
Jeej 6:c17f7cbdeb1a 353 {
Jeej 6:c17f7cbdeb1a 354 // Toggle alarm state
Jeej 6:c17f7cbdeb1a 355 alarm = !!alarm;
Jeej 6:c17f7cbdeb1a 356 ram_fs_write(FID_ALARM, 0, 1, &alarm);
Jeej 6:c17f7cbdeb1a 357 }
Jeej 0:87c57e1b1e1c 358 }
Jeej 0:87c57e1b1e1c 359 }
Jeej 0:87c57e1b1e1c 360
Jeej 0:87c57e1b1e1c 361 // Todo for each sensor
Jeej 2:cbe6dca8a632 362 #define SENSOR_SETUP(NAME,name) modem_update_file(FID_SENSOR_VALUE_##NAME, (alp_file_header_t*)&h_sensor_value_##name, NULL);\
Jeej 2:cbe6dca8a632 363 modem_update_file(FID_SENSOR_CONFIG_##NAME, (alp_file_header_t*)&h_sensor_config_##name, (uint8_t*)&f_sensor_config_##name);\
Jeej 0:87c57e1b1e1c 364 g_thread_ctx = &name##_thread_ctx;\
Jeej 0:87c57e1b1e1c 365 Thread th_##name(osPriorityNormal, 1024, NULL);\
Jeej 0:87c57e1b1e1c 366 status = th_##name.start(thread_sensor);\
Jeej 0:87c57e1b1e1c 367 ASSERT(status == osOK, "Failed to start thread (err: %d)\r\n", status);\
Jeej 0:87c57e1b1e1c 368 thread_ready.wait()
Jeej 0:87c57e1b1e1c 369
Jeej 0:87c57e1b1e1c 370 /*** Main function ------------------------------------------------------------- ***/
Jeej 0:87c57e1b1e1c 371 int main()
Jeej 0:87c57e1b1e1c 372 {
Jeej 0:87c57e1b1e1c 373 // Start & initialize
Jeej 1:4d3968b2941b 374 #ifdef DEBUG_LED
Jeej 0:87c57e1b1e1c 375 DBG_OPEN(DEBUG_LED);
Jeej 1:4d3968b2941b 376 #else
Jeej 1:4d3968b2941b 377 DBG_OPEN(NC);
Jeej 1:4d3968b2941b 378 #endif
Jeej 7:b94971f119a5 379 PRINT("\n"
Jeej 7:b94971f119a5 380 "-----------------------------------------\n"
Jeej 7:b94971f119a5 381 "------------- Demo Sensors --------------\n"
Jeej 7:b94971f119a5 382 "-----------------------------------------\n");
Jeej 7:b94971f119a5 383
Jeej 7:b94971f119a5 384
Jeej 0:87c57e1b1e1c 385 FPRINT("(id:0x%08x)\r\n", osThreadGetId());
Jeej 0:87c57e1b1e1c 386
Jeej 1:4d3968b2941b 387 modem_helper_open(&callbacks);
Jeej 0:87c57e1b1e1c 388
Jeej 1:4d3968b2941b 389 uint8_t main_id = modem_get_id(my_main_callback);
Jeej 0:87c57e1b1e1c 390
Jeej 0:87c57e1b1e1c 391 PRINT("Register Files\n");
Jeej 2:cbe6dca8a632 392 modem_update_file(FID_ALARM, &h_alarm, (uint8_t*)&f_alarm);
Jeej 0:87c57e1b1e1c 393
Jeej 0:87c57e1b1e1c 394 // Configure URC: LQUAL on report file notification every 10 reports
Jeej 0:87c57e1b1e1c 395 PRINT("Setup URCs\n");
Jeej 0:87c57e1b1e1c 396 modem_enable_urc(ALP_URC_TYPE_LQUAL, IFID_REPORT, 10, true, main_id);
Jeej 0:87c57e1b1e1c 397 modem_ready[main_id].wait();
Jeej 0:87c57e1b1e1c 398
Jeej 10:c01db64b9e68 399 // Put modem to listen to downlink access class
Jeej 10:c01db64b9e68 400 d7a_xcl_t xcl = { .bf.s = 0, .bf.m = 0x1 };
Jeej 10:c01db64b9e68 401 modem_write_file(D7A_FID_DLL_CFG, (void*)&xcl, offsetof(d7a_dll_cfg_t, xcl), sizeof(d7a_xcl_t), main_id);
Jeej 10:c01db64b9e68 402 modem_ready[main_id].wait();
Jeej 10:c01db64b9e68 403
Jeej 10:c01db64b9e68 404 modem_flush_file(D7A_FID_DLL_CFG, main_id);
Jeej 10:c01db64b9e68 405 modem_ready[main_id].wait();
Jeej 10:c01db64b9e68 406
Jeej 0:87c57e1b1e1c 407 PRINT("Start D7A Stack\n");
Jeej 14:8fd5405f7ab2 408 modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP | ALP_D7A_ISTAT_UNS | ALP_D7A_ISTAT_EOP, true, main_id);
Jeej 0:87c57e1b1e1c 409 modem_ready[main_id].wait();
Jeej 0:87c57e1b1e1c 410
Jeej 0:87c57e1b1e1c 411 PRINT("Notify Modem Version\n");
Jeej 0:87c57e1b1e1c 412 modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, main_id);
Jeej 0:87c57e1b1e1c 413 modem_ready[main_id].wait();
Jeej 0:87c57e1b1e1c 414
Jeej 0:87c57e1b1e1c 415 PRINT("Notify FW Version\n");
Jeej 12:7a56e0705c9d 416 uint8_t default_root_key[16] = DEFAULT_ROOT_KEY;
Jeej 12:7a56e0705c9d 417 modem_notify_host_rev(&f_rev, &h_rev, default_root_key);
Jeej 0:87c57e1b1e1c 418
Jeej 0:87c57e1b1e1c 419 // Start file modified thread
Jeej 0:87c57e1b1e1c 420 Thread th_file_modified(osPriorityNormal, 1024, NULL);
Jeej 0:87c57e1b1e1c 421 osStatus status = th_file_modified.start(thread_file_modified);
Jeej 0:87c57e1b1e1c 422 ASSERT(status == osOK, "Failed to start thread_file_modified (err: %d)\r\n", status);
Jeej 0:87c57e1b1e1c 423
Jeej 0:87c57e1b1e1c 424 #if (_HUM_EN_ > 0 || _TEM1_EN_ > 0 || _MAG_EN_ > 0 || _ACC_EN_ > 0 || _GYR_EN_ > 0 || _PRE_EN_ > 0 || _TEM2_EN_ > 0)
Jeej 0:87c57e1b1e1c 425 // Open I2C and initialise the sensors
Jeej 0:87c57e1b1e1c 426 DevI2C ext_i2c(SENSOR_I2C_SDA, SENSOR_I2C_SCL);
Jeej 0:87c57e1b1e1c 427 #endif
Jeej 0:87c57e1b1e1c 428
Jeej 0:87c57e1b1e1c 429 #if (_HUM_EN_ > 0 || _TEM1_EN_ > 0)
Jeej 0:87c57e1b1e1c 430 humidity_sensor = new HTS221(ext_i2c);
Jeej 0:87c57e1b1e1c 431 ASSERT(Init_HTS221(humidity_sensor), "Failed to init HTS221\r\n");
Jeej 0:87c57e1b1e1c 432 temp_sensor1 = humidity_sensor;
Jeej 0:87c57e1b1e1c 433 #endif // _TEM_EN_
Jeej 0:87c57e1b1e1c 434 #if (_PRE_EN_ > 0 || _TEM2_EN_ > 0)
Jeej 0:87c57e1b1e1c 435 pressure_sensor = new LPS25H(ext_i2c);
Jeej 0:87c57e1b1e1c 436 ASSERT(Init_LPS25H(pressure_sensor), "Failed to init LPS25H\r\n");
Jeej 0:87c57e1b1e1c 437 temp_sensor2 = pressure_sensor;
Jeej 0:87c57e1b1e1c 438 #endif // _PRE_EN_
Jeej 0:87c57e1b1e1c 439
Jeej 0:87c57e1b1e1c 440 #if defined(TARGET_STM32L152RE)
Jeej 0:87c57e1b1e1c 441 #if (_MAG_EN_ > 0)
Jeej 0:87c57e1b1e1c 442 magnetometer = new LIS3MDL(ext_i2c);
Jeej 0:87c57e1b1e1c 443 ASSERT(Init_LIS3MDL(magnetometer), "Failed to init LIS3MDL\r\n");
Jeej 0:87c57e1b1e1c 444 #endif // _MAG_EN_
Jeej 0:87c57e1b1e1c 445 #if (_ACC_EN_ > 0 || _GYR_EN_ > 0)
Jeej 0:87c57e1b1e1c 446 accelerometer = new LSM6DS0(ext_i2c);
Jeej 0:87c57e1b1e1c 447 ASSERT(Init_LSM6DS0(accelerometer), "Failed to init LSM6DS0\r\n");
Jeej 0:87c57e1b1e1c 448 gyroscope = accelerometer;
Jeej 0:87c57e1b1e1c 449 #endif // _ACC_EN_ || _GYR_EN_
Jeej 0:87c57e1b1e1c 450 #elif defined(TARGET_STM32L432KC)
Jeej 0:87c57e1b1e1c 451 #if (_ACC_EN_ > 0)
Jeej 0:87c57e1b1e1c 452 accelerometer = new LSM303C_ACC_Sensor(ext_i2c);
Jeej 0:87c57e1b1e1c 453 ASSERT(Init_LSM303C_ACC(accelerometer), "Failed to init LSM303C_ACC\r\n");
Jeej 0:87c57e1b1e1c 454 #endif // _ACC_EN_
Jeej 0:87c57e1b1e1c 455 #if (_MAG_EN_ > 0)
Jeej 0:87c57e1b1e1c 456 magnetometer = new LSM303C_MAG_Sensor(ext_i2c);
Jeej 0:87c57e1b1e1c 457 ASSERT(Init_LSM303C_MAG(magnetometer), "Failed to init LSM303C_MAG\r\n");
Jeej 0:87c57e1b1e1c 458 #endif // _MAG_EN_
Jeej 0:87c57e1b1e1c 459 #endif
Jeej 0:87c57e1b1e1c 460
Jeej 0:87c57e1b1e1c 461 #if (_MAG_EN_ > 0)
Jeej 0:87c57e1b1e1c 462 SENSOR_SETUP(MAG,mag);
Jeej 0:87c57e1b1e1c 463 #endif
Jeej 0:87c57e1b1e1c 464 #if (_ACC_EN_ > 0)
Jeej 0:87c57e1b1e1c 465 SENSOR_SETUP(ACC,acc);
Jeej 0:87c57e1b1e1c 466 #endif
Jeej 0:87c57e1b1e1c 467 #if (_GYR_EN_ > 0)
Jeej 0:87c57e1b1e1c 468 SENSOR_SETUP(GYR,gyr);
Jeej 0:87c57e1b1e1c 469 #endif
Jeej 0:87c57e1b1e1c 470 #if (_PRE_EN_ > 0)
Jeej 0:87c57e1b1e1c 471 SENSOR_SETUP(PRE,pre);
Jeej 0:87c57e1b1e1c 472 #endif
Jeej 0:87c57e1b1e1c 473 #if (_HUM_EN_ > 0)
Jeej 0:87c57e1b1e1c 474 SENSOR_SETUP(HUM,hum);
Jeej 0:87c57e1b1e1c 475 #endif
Jeej 0:87c57e1b1e1c 476 #if (_TEM1_EN_ > 0)
Jeej 0:87c57e1b1e1c 477 SENSOR_SETUP(TEM1,tem1);
Jeej 0:87c57e1b1e1c 478 #endif
Jeej 0:87c57e1b1e1c 479 #if (_TEM2_EN_ > 0)
Jeej 0:87c57e1b1e1c 480 SENSOR_SETUP(TEM2,tem2);
Jeej 0:87c57e1b1e1c 481 #endif
Jeej 0:87c57e1b1e1c 482 #if (_LIGHT_EN_ > 0)
Jeej 0:87c57e1b1e1c 483 SENSOR_SETUP(LIGHT,light);
Jeej 0:87c57e1b1e1c 484 #endif
Jeej 0:87c57e1b1e1c 485
Jeej 0:87c57e1b1e1c 486 modem_free_id(main_id);
Jeej 0:87c57e1b1e1c 487
Jeej 0:87c57e1b1e1c 488 // For button
Jeej 0:87c57e1b1e1c 489 #ifdef DEBUG_BUTTON
Jeej 0:87c57e1b1e1c 490 DebouncedInterrupt user_interrupt(DEBUG_BUTTON);
Jeej 0:87c57e1b1e1c 491 user_interrupt.attach(button_push_isr, IRQ_FALL, 500, true);
Jeej 0:87c57e1b1e1c 492
Jeej 0:87c57e1b1e1c 493 Thread but_th(osPriorityNormal, 1024, NULL);
Jeej 0:87c57e1b1e1c 494 status = but_th.start(thread_user_button);
Jeej 0:87c57e1b1e1c 495 ASSERT(status == osOK, "Failed to start but thread (err: %d)\r\n", status);
Jeej 0:87c57e1b1e1c 496 #endif
Jeej 0:87c57e1b1e1c 497
Jeej 1:4d3968b2941b 498 #ifdef DEBUG_LED
Jeej 0:87c57e1b1e1c 499 DigitalOut my_led(DEBUG_LED);
Jeej 0:87c57e1b1e1c 500 #endif
Jeej 0:87c57e1b1e1c 501
Jeej 0:87c57e1b1e1c 502 // Set main task to lowest priority
Jeej 0:87c57e1b1e1c 503 osThreadSetPriority(osThreadGetId(), osPriorityIdle);
Jeej 0:87c57e1b1e1c 504 while(true)
Jeej 0:87c57e1b1e1c 505 {
Jeej 0:87c57e1b1e1c 506 // Wait to avoid beeing stuck in loop
Jeej 0:87c57e1b1e1c 507 Thread::wait(500);
Jeej 1:4d3968b2941b 508 #ifdef DEBUG_LED
Jeej 0:87c57e1b1e1c 509 my_led = !my_led;
Jeej 0:87c57e1b1e1c 510 #endif
Jeej 0:87c57e1b1e1c 511 }
Jeej 0:87c57e1b1e1c 512 }