CPS-Lab / Mbed 2 deprecated p442-lab02

Dependencies:   mbed

Fork of Lab2 by CPS Lab

main.cpp

Committer:
kmhatre
Date:
2018-03-03
Revision:
2:b641c02f2844
Parent:
1:9fe22ab77011
Child:
3:8489a61207fa

File content as of revision 2:b641c02f2844:

#include "mbed.h"

#if !DEVICE_ANALOGOUT
#error You cannot use this example as the AnalogOut is not supported on this device.
#else
AnalogOut my_output(PA_4);
#endif
#define OFFSET    (0x7FFF)

// Configuration for sinewave output
#define BUFFER_SIZE (360)
/* 
 * ENGR E 210 - CYBER PHYSICAL SYSTEMS (DIGITAL SYSTEMS)
 * LAB 1 - LED/BUTTON/SERIAL
 * CODE BY KRISH HEMANT MHATRE AND ETHAN ZHANG
 */

#include "mbed.h"
#include <string.h>

Serial pc(USBTX, USBRX);
DigitalOut led1(LED1);
DigitalIn button(PC_13);

void Led(int a)
{
    led1 = a;
}

void Blink(int n)
{
    int i = 0;
    for(i=0; i<n; i++)
    {
        led1 = 1;
        wait(0.5);
        led1= 0;
        wait(0.5);
    }
}


void Button()
{
    if(button.read() == 1)
    {
        pc.printf("RELEASED\n\r");
    }
    else
    {
        pc.printf("PRESSED\n\r");
    }
}


int main() 
{
    pc.printf("Nucleo is running\n\r");
    pc.printf("cps%\n\r");
    int size = 32;
    char str[size];
    int i = 0;
    while(1)
    {
        str[i] = pc.getc();
        pc.putc(str[i]);
        
        if (i >= size)
        {
            pc.printf("OVER BUFFER\n\r");
            i = 0;
            for(int j = 0; j < size; j++)
            {
                str[j] = 0;
            }
            pc.printf("cps%\n\r");
        }
        if(str[i] == 13)
        {
            pc.printf("\n\rOK\n\r");
            str[i] = '\0';
            pc.printf("Command Recieved: %s\n\r", str);
            char s[5];
            sscanf(str, "%s", s);
            if(strcmp(str, "BUTTON") == 0)
            {
                Button();
                pc.printf("Button checked.\n\r");
            }
            else if(strcmp("LED ON", str) == 0)
            {
                Led(1);
                pc.printf("LED is on\n\r");
            }
            else if(strcmp("LED OFF", str) == 0)
            {
                Led(0);
                pc.printf("LED is off\n\r");
            }
            else if(strcmp(s, "BLINK") == 0)
            {
                pc.printf("Reading a blink\n\r");
                char *a = strtok(str, " ");
                a = strtok(NULL, " ");
                pc.printf("Start blink\n\r");
                int n;
                sscanf(a, " %d", &n);
                if(n>=1 && n<=10)
                {
                   Blink(n);
                   pc.printf("Blink complete\n\r");
                }
                else
                {
                    pc.printf("ERROR: Blink cycle should be between 1 and 10\n\r");
                }
            }
            else if(strncmp(str, "DC", 2) == 0)
            {
                   pc.printf("Reading DC\n\r");
                   char *s1 = strtok(str, " ");
                   s1 = strtok(NULL, " ");
                   float volt;
                   sscanf(s1, " %f", &volt);
                   
                   if((volt < (float)0.5) || (volt > (float)3.0))
                   {
                       pc.printf("Voltage out of range\n\r");    
                   }
                   else 
                   {  
                       float adjust = (0x7FFF/2)+(0x7FFF/10);
                       
                       pc.printf("Voltage is %f\n\r", volt);
                       my_output.write_u16(volt*adjust);
                       pc.printf("Voltage changed\n\r");
                   }
            }
            else
            {
                pc.printf("ERR\n\r");
            }
            i = 0;
            for(int j = 0; j < size; j++)
            {
                str[j] = '\0';
            }
            pc.printf("cps%\n\r");
        }
        i++;
    }
}