Teste Flash

Dependencies:   pulga-lorawan-drv Si1133 BME280

main.cpp

Committer:
pancotinho
Date:
2021-06-01
Revision:
69:2d56b571c78e
Parent:
68:fc357095c8ef
Child:
70:99b7a15c09da

File content as of revision 69:2d56b571c78e:

/**
 * Copyright (c) 2017, Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include "mbed.h"

// Application helpers

#include "trace_helper.h"

// Application peripherals

#include "serial.h"
#include "gps.h"
#include "lora_radio.h"


using namespace events;


/*
 * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
 */
#define TX_TIMER                        60000

/**
 * Maximum number of events for the event queue.
 * 10 is the safe number for the stack events, however, if application
 * also uses the queue for whatever purposes, this number should be increased.
 */
#define MAX_NUMBER_OF_EVENTS            10

/**
 * Maximum number of retries for CONFIRMED messages before giving up
 */
#define CONFIRMED_MSG_RETRY_COUNTER     3


void serial_post_to_queue(void);

/**
* This event queue is the global event queue for both the
* application and stack. To conserve memory, the stack is designed to run
* in the same thread as the application and the application is responsible for
* providing an event queue to the stack that will be used for ISR deferment as
* well as application information event queuing.
*/
static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE);

/**
 * Application specific callbacks
 */
static lorawan_app_callbacks_t callbacks;

/**
 * Entry point for application
 */
 
mbed::DigitalOut _alive_led(P1_13, 0);
mbed::DigitalOut _actuated_led(P1_14,1);
int latitude=0;
int longitude=0;


void GPS_Read(void)
{
    uint8_t tx_buffer[256];
    uint16_t packet_len;
    int16_t retcode;
    
    gps_print_local();
 
    packet_len = sprintf((char *) tx_buffer, "%d, %d\n", get_latitude(), get_longitude());
    retcode = lora_send_message(tx_buffer, packet_len);

}

void serial_rx(){
    if(pc.readable()){
        pc.printf("rx: %c\n", pc.getc());
    } 
    pc.attach(&serial_post_to_queue, RawSerial::RxIrq);
    return;
}
 
void serial_post_to_queue(void){
    //disable serial rx interrupt
    pc.attach(NULL, RawSerial::RxIrq);
    //enqueue the serial rx reception as a normal task
    ev_queue.call(SerialRx);
    return;
}

int main(void)
{
    pc.printf("init\n");
    pc.baud(9600);
    pc.printf("config9600\n");
    //enable serial rx interrupt
    pc.attach(&serial_post_to_queue, RawSerial::RxIrq);
    gps_config();
    gps_leBootMsg();
    gps_config_gnss ();
    
    wait_ms(250);

    //########################################################
    // setup tracing
    setup_trace();

    // stores the status of a call to LoRaWAN protocol
    lorawan_status_t retcode;

    // Initialize LoRaWAN stack
    /*if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
        pc.printf("\r\n LoRa initialization failed! \r\n");
        return -1;
    }*/
    
    if(lorawan_initialize_stack(&ev_queue) != 0){
        return -1;
    }

    pc.printf("\r\n Mbed LoRaWANStack initialized \r\n");

    // prepare application callbacks
    /*callbacks.events = mbed::callback(lora_event_handler);
    lorawan.add_app_callbacks(&callbacks);
    */
    
    callbacks.events = mbed::callback(lora_event_handler);
    lorawan_add_callbacks(callbacks);
    
    // Set number of retries in case of CONFIRMED message
    if(lorawan_set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) != 0){
        pc.printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
        return -1;
    }
    

    pc.printf("\r\n CONFIRMED message retries : %d \r\n",
           CONFIRMED_MSG_RETRY_COUNTER);
           
    // Enable adaptive data rate
    if(lorawan_enable_adaptive_datarate() != 0){
        pc.printf("\r\n enable_adaptive_datarate failed! \r\n");
        return -1;
    }
    

    pc.printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");

    
    if(lorawan_connect() != 0){
        pc.printf("\r\n Connection error, code = %d \r\n", retcode);
        return -1;
    }
    
    pc.printf("\r\n Connection - In Progress ...\r\n");
    
    _actuated_led =0;
    
    //
    // make your event queue dispatching events forever
    ev_queue.call_every(TX_TIMER, GPS_Read);
    ev_queue.dispatch_forever();
    return 0;
}


/**
 * Event handler
 */
void lora_event_handler(lorawan_event_t event)
{
    switch (event) {
        case CONNECTED:
            pc.printf("\r\n Connection - Successful \r\n");
            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                //send_message();
                //lora_send_message((uint8_t*)"testeLora", (uint16_t)10);
            } //else {
                //ev_queue.call_every(TX_TIMER, (void)lora_send_message((uint8_t*)"testeLoraEvery", (uint16_t)15));
            //}

            break;
        case DISCONNECTED:
            ev_queue.break_dispatch();
            pc.printf("\r\n Disconnected Successfully \r\n");
            break;
        case TX_DONE:
//            printf("\r\n Message Sent to Network Server \r\n");
            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                //lora_send_message((uint8_t*)"teste", (uint16_t)6);
            }
            break;
        case TX_TIMEOUT:
//        printf("\r\n Transmission Error TX_Timeout");
            break;
        case TX_ERROR:
//        printf("\r\n Transmission Error TX_Error");
            break;
        case TX_CRYPTO_ERROR:
//        printf("\r\n Transmission Error TX_Crypto_Error");
            break;
        case TX_SCHEDULING_ERROR:
//            printf("\r\n Transmission Error - EventCode = %d \r\n", event);
            // try again
            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                //lora_send_message((uint8_t*)"teste2", (uint16_t)7);
            }
            break;
        case RX_DONE:
//            printf("\r\n Received message from Network Server \r\n");
            lora_receive_message();
            break;
        case RX_TIMEOUT:
//        printf("\r\n Transmission Error RX_Timeout");
            break;
        case RX_ERROR:
//            printf("\r\n Error in reception - Code = %d \r\n", event);
            break;
        case JOIN_FAILURE:
//            printf("\r\n OTAA Failed - Check Keys \r\n");
            break;
        case UPLINK_REQUIRED:
//            printf("\r\n Uplink required by NS \r\n");
            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                //lora_send_message((uint8_t*)"uplink", (uint16_t)7);
            }
            break;
        default:
            MBED_ASSERT("Unknown Event");
            break;
    }
}

// EOF