Data reception demo.

Dependencies:   modem_ref_helper

main.cpp

Committer:
Jeej
Date:
11 months ago
Revision:
11:7df24b241621
Parent:
10:c262c5feac24
Child:
12:404a3ca64a44

File content as of revision 11:7df24b241621:

// @autor: jeremie@wizzilab.com
// @date: 2017-05-02

#include "modem_ref_helper.h"
#include "modem_callbacks.h"
#include "files.h"
#include "CriusOLED.h"

#define RESTORE_TIME    30
#define KEEP_ALIVE      3600
#define MIN_REPORT_PERIOD   (10) // Seconds

Semaphore modem_ready(0);
Queue<void, 8> g_file_modified;

uint8_t id;
uint32_t g_restore_time = RESTORE_TIME + 1;

void display_logo(void)
{
    clear_display();
    sendImage();
}

static void my_main_callback(uint8_t terminal, int8_t err, uint8_t id)
{
    (void)id;
    
    if (ALP_ERR_NONE != err)
    {
        modem_print_error(ALP_ITF_TYPE_D7A, err);
    }
    
    if (terminal)
    {
        modem_ready.release();
    }
}

void thread_reset_display()
{
    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
    
    uint32_t time = 0;
    uint8_t status = 0;
    uint8_t id = modem_get_id(my_main_callback);
    
    while (true)
    {
        if (g_restore_time++ == RESTORE_TIME)
        {
            display_logo();
        }
       
        if ((time++ % KEEP_ALIVE) == 0)
        {
            // Keep alive: Report status
            modem_write_file(FID_STATUS_FILE, &status, 0, SIZE_STATUS_FILE, id);
            modem_ready.wait();
        }
        
        Thread::wait(1000);
    }
}

void thread_file_modified()
{
    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
    
    uint8_t fid;
    osEvent evt;
    
    while (true)
    {
        evt = g_file_modified.get();
        fid = (evt.status == osEventMessage)? (uint8_t)(uint32_t)evt.value.p : NULL;
        
        switch (fid)
        {
            case FID_STRING_FILE:
                // Print string
                uint8_t str[SIZE_STRING_FILE];
                ram_fs_read(FID_STRING_FILE, 0, SIZE_STRING_FILE, (uint8_t*)&str);
                PRINT("STRING: ");
                PRINT((char*)str);
                PRINT("\r\n");
                
                clear_display();
                sendStrXY((char*)str,0,0);
                
                g_restore_time = 0;
                break;
            default:
                PRINT("Unknown file %d\n", fid);
                break;
        }
    }
}

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,
    .boot       = my_boot,
    .busy       = my_busy,
};

/*** Main function ------------------------------------------------------------- ***/
int main()
{
    // Start & initialize
#ifdef DEBUG_LED
    DBG_OPEN(DEBUG_LED);
#else
    DBG_OPEN(NC);
#endif
    PRINT("\n"
          "-----------------------------------------\n"
          "-------- Demo Receive file data ---------\n"
          "-----------------------------------------\n");
          
    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
    
    modem_helper_open(&callbacks);
    
    uint8_t id = modem_get_id(my_main_callback);
        
    PRINT("Register Files\n");
    // Allow remote access.
    // The string file data is on this host, specify the data buffer.
    modem_update_file(FID_STRING_FILE, (alp_file_header_t*)&h_string_file, (uint8_t*)&f_string_file);
    // The status file data is in the modem, do not specify a data buffer.
    modem_update_file(FID_STATUS_FILE, (alp_file_header_t*)&h_status_file, NULL);

    // Put modem to listen to this access class
    d7a_xcl_t my_xcl = { .bf.s = 0, .bf.m = 2 };
    modem_write_file(D7A_FID_DLL_CFG, &my_xcl, 0, sizeof(d7a_xcl_t), id);
    modem_ready.wait();

    // Configure URC: LQUAL on report file notification every 10 reports
    PRINT("Setup URCs\n");
    modem_enable_urc(ALP_URC_TYPE_LQUAL, IFID_REPORT, 10, true, id);
    modem_ready.wait();
    
    PRINT("Start D7A Stack\n");
    modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP | ALP_D7A_ISTAT_UNS | ALP_D7A_ISTAT_EOP, true, id);
    modem_ready.wait();
    
    PRINT("Notify Modem Version\n");
    modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, id);
    modem_ready.wait();
    
    PRINT("Notify FW Version\n");
    uint8_t default_root_key[16] = DEFAULT_ROOT_KEY;
    modem_notify_host_rev((revision_t*)&f_rev, (alp_file_header_t*)&h_rev, default_root_key);
    
    modem_free_id(id);
    
    // Start file modified thread
    Thread th_file_modified(osPriorityNormal, 512, NULL);
    osStatus status = th_file_modified.start(thread_file_modified);
    ASSERT(status == osOK, "Failed to start thread_file_modified (err: %d)\r\n", status);
    
    // Start reset display thread
    Thread th_reset_display(osPriorityLow, 1024, NULL);
    status = th_reset_display.start(thread_reset_display);
    ASSERT(status == osOK, "Failed to start thread_reset_display (err: %d)\r\n", status);

    // Initialize I2C and OLED Display
    i2c.frequency(400000);
    init_OLED();
    displayOn();
    reset_display();
    
    display_logo();

#ifdef DEBUG_LED
    DigitalOut my_led(DEBUG_LED);
#endif
    
    // Set main task to lowest priority
    osThreadSetPriority(osThreadGetId(), osPriorityIdle);
    while(true)
    {
        Thread::wait(500);
#ifdef DEBUG_LED
        my_led = !my_led;
#endif
    }
}