version1

Dependencies:   mbed

main.cpp

Committer:
moove1334
Date:
2019-03-19
Revision:
0:ac7bd75171ef
Child:
1:8e615c17b2b3

File content as of revision 0:ac7bd75171ef:

#include "mbed.h"

Serial device(D1, D0);
DigitalOut led1(LED1);
DigitalIn button0(USER_BUTTON);

void Rx_interrupt();
void SetSerial(int c);

int data_size = 5;
char data[5] = {};
char package = 0;
char num_data = 0;

int main()
{
    device.attach(&Rx_interrupt);
    
    led1 = 0;
    while(true)
    {
        if (package == 1)
        {
            package = 0;
            led1 = 1;
            wait(1);
            led1 = 0;
            wait(0.5);
        }
    }
}

void Rx_interrupt()
{
    char c = device.getc();
    int i = (int)c;
    SetSerial(i);
}
void SetSerial(int c)
{
    if (num_data < 2){
        if (c == 255){
            data[num_data] = c;
            num_data++;
        }else{
            data[num_data] = c;
            num_data = 0;
        }
    }
    else if (num_data < data_size){
        data[num_data] = c;
        num_data++;
        if (num_data >= data_size){
            if (data[data_size-1]==255){
                num_data = 0;
                package = 1;
            }   
        }
    }
}