Cortex Challenge Team / Mbed 2 deprecated Nucleo_read_logic_probe

Dependencies:   mbed

main.cpp

Committer:
dousape2
Date:
2015-03-22
Revision:
2:10add9ee8c4b
Parent:
1:533ef4c5f7ab
Child:
3:29a231eba2e0

File content as of revision 2:10add9ee8c4b:

/**********************************************************************************
* @file    main.cpp
* @author  Petr Dousa
* @version V0.1
* @date    09-March-2015
* @brief   Read value from pins and write it to serial.
*          It could be set some settings through serial.
*          Serial speed is set to 115200.
***********************************************************************************/

/**************************************************************************************************************************************************/
/*                                                                    Table how to find 0xE000                                                    */
/**************************************************************************************************************************************************/
/* Num.        |   15  |   14  |   13  |   12  ||   11  |   10  |   9   |   8   ||   7   |   6   |   5   |   4   ||   3   |   2   |   1   |   0   */
/* Num. of pin | PC_15 | PC_14 | PC_13 | PC_12 || PC_11 | PC_10 | PC_9  | PC_8  || PC_7  | PC_6  | PC_5  | PC_4  || PC_3  | PC_2  | PC_1  | PC_0  */
/* Num. in BIN |   1   |   1   |   1   |   0   ||   0   |   0   |   0   |   0   ||   0   |   0   |   0   |   0   ||   0   |   0   |   0   |   0   */
/* Num. in HEX |               E               ||               0               ||               0               ||               0               */
/**************************************************************************************************************************************************/

/* Includes ----------------------------------------------------------------------*/
#include "mbed.h"

/* Defines -----------------------------------------------------------------------*/

/* Function prototypes -----------------------------------------------------------*/

/* Variables ---------------------------------------------------------------------*/
int pin =0;             //pins to compare and start print
int vypis =0;           // which output will be provided
bool start = true;      // if true it print value to serial

//mbed - initialization of peripherals
PortIn myIOs(PortC, 0xFFFF);        // PC_all
Serial pc(SERIAL_TX, SERIAL_RX);    // inicialize Serial to connect to PC
Ticker toggle_ticker;               // inicialize ticker
DigitalOut led(LED1);               // inicialize LED

/* Functions----------------------------------------------------------------------*/

/***********************************************************************************
* Function Name  : toggle.
* Description    : Read value from pins, blink with LED and send value to serial.
* Input          : None.
* Output         : None.
* Return         : None.
***********************************************************************************/
void toggle()
{
    int meas,z;
    meas=myIOs.read();  // read pins

    led= !led;          //blink led
    if (meas == pin ) { // If the value is as we want 
        start = true;  //start send data to Serial
    }
    if(start) {
        if(vypis==1) {                      // write value in bin
            while(!pc.writeable());
            for (z =32768; z > 0; z >>= 1) {
                while(!pc.writeable());
                pc.printf(((meas & z) == z) ? "1" : "0");
            }
            while(!pc.writeable());
            pc.printf("\n");
        } else if(vypis==2) {           // write value in decimal
            while(!pc.writeable());
            pc.printf("%d\n", meas);
        } else if(vypis==3) {           // write value in oct
            while(!pc.writeable());
            pc.printf("%o\n", meas);
        } else if(vypis==4) {           // write value in hex
            while(!pc.writeable());
            pc.printf("%x\n", meas);
        } else {                        // write value in all options
            while(!pc.writeable());
            pc.printf("DEC: %d, HEX: %x, OCT: %o, BIN: ", meas,meas,meas);
            for (z =32768; z > 0; z >>= 1) {
                while(!pc.writeable());
                pc.printf(((meas & z) == z) ? "1" : "0");
            }
            while(!pc.writeable());
            pc.printf("\n");
        }
    }
}

/***********************************************************************************
* Function Name  : flushSerialPort.
* Description    : Serial flush rountine.
* Input          : None.
* Output         : None.
* Return         : None.
***********************************************************************************/
void flushSerialPort()
{
    while(pc.readable())
        pc.getc();
    return;
}

/***********************************************************************************
* Function Name  : menu.
* Description    : Print menu to serial.
* Input          : None.
* Output         : None.
* Return         : None.
***********************************************************************************/
void menu()
{
    while(!pc.writeable()); // wait to be serial available for sending data
    pc.printf("HELP - MENU\n");// send text to serial
    while(!pc.writeable());
    pc.printf("Data send to PC is in dec format.\n");
    while(!pc.writeable());
    pc.printf("Set data exactly.\n");
    while(!pc.writeable());
    pc.printf("Write to console: \"xx yy\", where xx is a code of seting and yy his value.\n");
    while(!pc.writeable());
    pc.printf("01 y.yyy - set period[s] to send data to PC and start reading value, example:01 0.01\n");
    while(!pc.writeable());
    pc.printf("02 0 - stop reading value, example:02 0\n");
    while(!pc.writeable());
    pc.printf("03 yyyy - Wait for pin to get to log.1. yyyy write in dec example:03 2000\n");
    while(!pc.writeable());
    pc.printf("04 yy - print yy: 00-default all, 01-bin, 02-dec, 03-oct, 04-hex, example:04 00\n");
    while(!pc.writeable());
    pc.printf("end HELP\n");
}

/***********************************************************************************
* Function Name  : main.
* Description    : Main routine.
* Input          : None.
* Output         : None.
* Return         : None.
***********************************************************************************/
int main()
{
    myIOs.mode(PullNone); // Modes: PullDown PullUp PullNone OpenDrain

    // variables to read from serials
    int prijData=0;
    int Data1=0;
    float Data2=0;

    //set serial
    pc.baud(115200);
    pc.printf("\nLogic sond.\n");
    menu(); //print menu

    toggle_ticker.detach();             // do NOT call function
    toggle_ticker.attach(&toggle, 1);   // 1 second was passed, call function toggle


    while(1) {
        //accepted data from serial
        prijData=pc.scanf("%d",&Data1);             // read number from serial
        if(prijData==1 && (Data1>=1 && Data1<=3)) { // test if number was read and it is between 1 and 3
            prijData=pc.scanf("%f",&Data2);         // read float number from serial
            if(prijData==1) {
                prijData=2;         // set variable to set measure of values
            } else {
                flushSerialPort();  // iscard data from serial
                prijData=0;         // set variable to print menu
            }
        } else {
            flushSerialPort();      // discard data from serial
            prijData=0;             // set variable to print menu
        }

        if(prijData==2) {
            if(Data1==1) {
                toggle_ticker.detach();                 // do NOT call function
                toggle_ticker.attach(&toggle, Data2);   // Data2 seconds was passed, call function toggle
            } else if(Data1==2) {
                toggle_ticker.detach();                 // do NOT call function
            } else if(Data1==3) {
                pin=(int)Data2;                         //set variables, which are use in toggle
                start = false;
            } else if(Data1==4) {
                vypis=(int)Data2;                       //set variables to set printf, which are use in toggle
            }
        } else {
            toggle_ticker.detach();                     // do NOT call function
            menu();                                     //print menu
            flushSerialPort();                          // discard data from serial
        }
    }
}