Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Lab2 by
Diff: main.cpp
- Revision:
- 0:9439aa91d4cb
- Child:
- 1:9fe22ab77011
diff -r 000000000000 -r 9439aa91d4cb main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Feb 06 10:34:11 2018 +0000 @@ -0,0 +1,152 @@ +#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 + */ + +#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 + { + pc.printf("Voltag is %f\n\r", volt); + my_output.write_u16(volt*19720); + 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++; + } +} + +