APP4 S5

Dependencies:   mbed-rtos mbed

radio.cpp

Committer:
JayMcGee
Date:
2017-10-11
Revision:
7:d25bcde7dcf1
Parent:
6:f3f442d43c8f
Child:
8:a878763b0ae3

File content as of revision 7:d25bcde7dcf1:


#include "radio.h"

#include "mbed.h"
#include "rtos.h"

#include <LPC17xx.h>

#define MESSAGE_BUFFER_SIZE 16

#define MANCHESTER_SPEED_OUT 50
    
#define INPUT_RADIO p18
#define OUTPUT_RADIO p6

radio_message_t out_messages[MESSAGE_BUFFER_SIZE];
byte out_message_in;
byte out_message_out;

radio_message_t in_messages[MESSAGE_BUFFER_SIZE];
byte in_message_in;
byte in_message_out;

InterruptIn input(INPUT_RADIO);

RtosTimer out_timer(radio_out, osTimerPeriodic, (void *)out_messages);

DigitalOut output(OUTPUT_RADIO);

DigitalOut in_debug_led(LED4);
DigitalOut out_debug_led(LED3);

DigitalOut frame_end_led(LED2);

// API functions
void init_radio_system()
{
   setup_radio_in();   
   setup_radio_out(); 
   output = 0;
}

// Private functions

void in_rise()
{
    in_debug_led = 1;   
}

void in_fall()
{
    in_debug_led = 0;
}

void setup_radio_in()
{
    in_message_in = 0;
    in_message_out = 0;
    
    in_debug_led = 0;
    
    input.rise(&in_rise);
    input.fall(&in_fall);   
}


void setup_radio_out()
{
    out_message_in = 0;
    out_message_out = 0;
        
    out_debug_led = 0;    
    frame_end_led = 1;
    
    //////////////////////////////////////////////////////
    // Creation d'un message et insertion dans le buffer
    radio_message_t* message = (out_messages + out_message_in);
    
    message->preambule = HEADER_DELIMITER;
    message->start = HEADER_START;
    message->options = HEADER_DELIMITER;
    message->length = 0x3;
    
    message->data[0] = 0xC0;
    message->data[1] = 0xFF;    
    message->data[2] = 0xEE;    
    
    // Ajouter calcul
    message->control = 0xCE;
    
    message->end = FOOTER_END;
    // On avance dans le buffer;
    out_message_in++;
    //////////////////////////////////////////////////////   
    output = 0;
    wait(1);
    out_timer.start(MANCHESTER_SPEED_OUT);       
}

typedef enum {
    preambule = 0,
    start,
    options,
    length,
    data,
    crc,
    end
} out_state_t;

void radio_out(void const *args)
{
    static byte current_byte_progress = 0;    
    static byte current_byte = 0;    
    static byte current_state = 0;    
    static bool IsBitTransition = false;
    static byte next_value = 0;
    
    radio_message_t* message = (radio_message_t*)(out_messages + out_message_out);   
    
    #define OUTPUT_HIGH output = 1;
    #define OUTPUT_LOW output = 0;
    
    #define SET_VAL_BIT_MASK(val) next_value = 0x1 & val; 
    #define SET_VAL_SHIFT(val, shift) SET_VAL_BIT_MASK(val >> (7 - shift))
    
    
    #define CHECK_NEXT_STATE if (current_byte_progress > 7)     \
                             {                                  \
                                    current_state++;            \
                                    current_byte_progress = 0;  \
                             }
    
    out_debug_led = !out_debug_led;
    
    if (!IsBitTransition)
    {
        // Dependant du state, on progresse dans l'envoi du message
        switch (current_state) 
        {
            case preambule: // preambule
            {
                SET_VAL_SHIFT(message->preambule, current_byte_progress++);
                CHECK_NEXT_STATE
                break;
            }        
            case start: // start
            {
                SET_VAL_SHIFT(message->start, current_byte_progress++);
                CHECK_NEXT_STATE
                break;
            }   
            case options: // entete options
            {
                SET_VAL_SHIFT(message->options, current_byte_progress++);
                CHECK_NEXT_STATE
                break;
            }             
            case length: // entete lenght
            {
                SET_VAL_SHIFT(message->length, current_byte_progress++);
                CHECK_NEXT_STATE
                break;
            }        
            case data: // charge utile
            {
                SET_VAL_SHIFT(message->data[current_byte], current_byte_progress++)
                if (current_byte_progress > 7)
                {
                    current_byte++;
                    current_byte_progress = 0;                 
                    if (current_byte >= message->length)
                    {
                        current_byte = 0;
                        current_state++;
                    }                 
                }
                //CHECK_NEXT_STATE
                break;
            }        
            case crc: // controle
            {
                SET_VAL_SHIFT(message->control, current_byte_progress++);
                CHECK_NEXT_STATE
                break;
            }        
            case end: // end
            {
                SET_VAL_SHIFT(message->end, current_byte_progress++);
                CHECK_NEXT_STATE
                break; 
            }
            default:
            {
                //current_state = 0;
                current_byte = 0;
                current_byte_progress = 0;
                break;
            }                                         
        }
        
        if (next_value != output)
        {
            output = !output;
        }
        
        if (current_state > end)
        {
            frame_end_led = 0;
            //current_state = preambule;
        }
    }
    else
    {
        output = !output;
    }
    
    IsBitTransition = !IsBitTransition;
    
}