Complete sensor demo.

Dependencies:   modem_ref_helper CRC X_NUCLEO_IKS01A1 DebouncedInterrupt

Revision:
1:4d3968b2941b
Parent:
0:87c57e1b1e1c
Child:
2:cbe6dca8a632
--- a/main.cpp	Mon May 15 16:00:25 2017 +0000
+++ b/main.cpp	Thu May 18 10:44:26 2017 +0000
@@ -2,50 +2,21 @@
 // @autor: jeremie@wizzilab.com
 // @date: 2016-12-20
 
-#include "mbed.h"
-#include "rtos.h"
+#include "DebouncedInterrupt.h"
 #include "sensors.h"
-#include "WizziDebug.h"
-#include "WizziCom.h"
-#include "DebouncedInterrupt.h"
-
+#include "sensors_cfg.h"
+#include "simul.h"
+#include "modem_ref_helper.h"
+#include "modem_callbacks.h"
 #include "files.h"
-#include "ram_fs.h"
-#include "hwcfg.h"
-#include "modem_callbacks.h"
-#include "simul.h"
-
-#include "revision.h"
-#include "alp_spec.h"
-#include "alp_helpers.h"
-#include "modem_ref.h"
-#include "d7a_1x.h"
-#include "d7a_1x_fs.h"
-#include "alp.h"
 
 
-WizziCom* g_modem_com;
 Semaphore button_user(0);
 Semaphore modem_ready[MAX_USER_NB];
 Semaphore thread_ready(0);
 sensor_config_t g_light_config;
 Queue<void, 8> g_file_modified;
 
-TYPEDEF_STRUCT_PACKED {
-    uint8_t type;
-    d7a_sp_cfg_t cfg;
-} alp_d7a_itf_t;
-
-#define D7A_CTF_VAL(mant,exp)       ((uint8_t)(mant|(exp<<5)))
-#define ALP_ITF_TYPE_D7A            0xD7
-#define MY_D7_ITF_SIZE(_itf)        (1+my_alp_itf_d7a_cfg_size(&(_itf)->cfg))
-int my_alp_itf_d7a_cfg_size(d7a_sp_cfg_t* cfg)
-{
-    int size = sizeof(d7a_sp_cfg_t) - sizeof(d7a_addressee_t);
-    size += D7A_ADDR_LEN(cfg->addressee.ctrl);
-    return size;
-}
-
 // 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)
 {
@@ -94,11 +65,6 @@
     button_user.release();
 }
 
-void my_get_alp_file_props(uint8_t fid, alp_file_header_t* hdr)
-{
-    memcpy(hdr, ram_fs_get_header(fid), sizeof(alp_file_header_t));
-}
-
 void print_status(int status)
 {
     switch (status)
@@ -131,33 +97,13 @@
     }
 }
 
-// ============================================================}}}
-
-// Serial adapters to WizziLab's own architecture
-// ============================================================{{{
-
-void my_serial_input(WizziCom* com, WizziComPacket_t* pkt)
-{
-    modem_input(wizzicom_type_to_flow(pkt->type), pkt->data, pkt->length);
-    FREE(pkt);
-}
-
-int my_serial_send(uint8_t* data1, uint8_t size1, uint8_t* data2, uint8_t size2)
-{
-    (void)size1;
-    
-    // Retrieve Flow ID from header and send packet 
-    g_modem_com->send((WizziComPacketType)wizzicom_flow_to_type(data1[4]), size2, data2);
-
-    return (size1 + size2);
-}
-
 modem_callbacks_t callbacks = {
     .read       = my_read,
     .write      = my_write,
     .read_fprop = my_read_fprop,
     .flush      = my_flush,
     .remove     = my_delete,
+    .udata      = my_udata,
     .lqual      = my_lqual,
     .ldown      = my_ldown,
     .reset      = my_reset,
@@ -229,9 +175,6 @@
 SENSOR_THREAD_CTX(tem2, TEM2, 1);
 SENSOR_THREAD_CTX(light, LIGHT, 1);
 
-    sensor_thread_ctx_t* ctx = g_thread_ctx;
-
-
 void thread_sensor()
 {
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
@@ -254,12 +197,12 @@
         
         ASSERT(err == 0, "Failed to read sensor\n");
         
-        PRINT("Got %3d: ", ctx->value_file_id);
-        for (uint8_t i = 0; i < ctx->nb_values; i++)
-        {
-            PRINT("%9ld ", (int32_t)ctx->current_value[i]);
-        }
-        PRINT("\r\n");
+        //PRINT("Got %3d: ", ctx->value_file_id);
+        //for (uint8_t i = 0; i < ctx->nb_values; i++)
+        //{
+        //    PRINT("%9ld ", (int32_t)ctx->current_value[i]);
+        //}
+        //PRINT("\r\n");
         
         for (uint8_t i = 0; i < ctx->nb_values; i++)
         {
@@ -356,7 +299,7 @@
     ram_fs_read(FID_ALARM, 0, 1, &alarm);
     
     // Send initial value
-    modem_send_file_content((uint8_t*)&alarm_itf, MY_D7_ITF_SIZE(&alarm_itf), (void*)&istat, FID_ALARM, &alarm, 0, 1, user_id);
+    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)
@@ -374,7 +317,7 @@
         
         PRINT("BUTTON ALARM %d\r\n", alarm);
                     
-        modem_send_file_content((uint8_t*)&alarm_itf, MY_D7_ITF_SIZE(&alarm_itf), (void*)&istat, FID_ALARM, &alarm, 0, 1, user_id);
+        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();
     }
 }
@@ -431,53 +374,17 @@
 int main()
 {
     // Start & initialize
+#ifdef DEBUG_LED
     DBG_OPEN(DEBUG_LED);
+#else
+    DBG_OPEN(NC);
+#endif
     PRINT("\r\n--- Starting new run ---\r\n");
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
     
-    static union {
-        uint8_t      b[8];
-        uint32_t     w[2];
-    } uid;
-    revision_t rev;
-    uint8_t main_id;
-    
-    // Hardware reset
-    DigitalOut reset_low(MODEM_PIN_RESET, 0);
-    Thread::wait(100);
-    
-    // Release reset
-    DigitalIn reset_release(MODEM_PIN_RESET);
-    Thread::wait(2000);
-    
-    // Open modem Com port
-    g_modem_com = new WizziCom(MODEM_PIN_TX, MODEM_PIN_RX, MODEM_PIN_IRQ_OUT, MODEM_PIN_IRQ_IN);
+    modem_helper_open(&callbacks);
     
-    // Redirect All Port traffic to my_serial_input
-    g_modem_com->attach(my_serial_input, WizziComPacketOther);
-
-    // Open driver
-    modem_open(my_serial_send, &callbacks);
-    
-    main_id = modem_get_id(my_main_callback);
-
-    PRINT("Start Modem Process (id=%d)\n", main_id);
-    Thread::wait(1000);
-    
-    modem_read_file(D7A_FID_UID, uid.b, 0, 8, main_id);
-    modem_ready[main_id].wait();
-    
-    modem_read_file(D7A_FID_FIRMWARE_VERSION, &rev, 0, sizeof(revision_t), main_id);
-    modem_ready[main_id].wait();
-    
-    PRINT("------------ D7A Modem infos ------------\r\n");
-    PRINT_DATA(" - UID:              ", "%02X", uid.b, 8, "\r\n");
-    PRINT(" - Manufacturer ID:  %08X\r\n", rev.manufacturer_id);
-    PRINT(" - Device ID:        %08X\r\n", rev.device_id);
-    PRINT(" - Hardware version: %08X\r\n", rev.hw_version);
-    PRINT(" - Firmware version: v%d.%d.%d\r\n", rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch);
-    PRINT(" - File system CRC:  0x%08x\r\n", rev.fs_crc);
-    PRINT("-----------------------------------------\r\n");
+    uint8_t main_id = modem_get_id(my_main_callback);
     
     PRINT("Register Files\n");
     modem_update_file(FID_HOST_REV, &h_rev, (uint8_t*)&f_rev, main_id);
@@ -579,7 +486,7 @@
     ASSERT(status == osOK, "Failed to start but thread (err: %d)\r\n", status);
 #endif
 
-#if (DEBUG_LED != NC)
+#ifdef DEBUG_LED
     DigitalOut my_led(DEBUG_LED);
 #endif
 
@@ -589,7 +496,7 @@
     {
         // Wait to avoid beeing stuck in loop
         Thread::wait(500);
-#if (DEBUG_LED != NC)
+#ifdef DEBUG_LED
         my_led = !my_led;
 #endif
     }