lab7

Dependencies:   SDFileSystem mbed

Committer:
philliptran011
Date:
Thu Nov 09 19:02:08 2017 +0000
Revision:
0:fa63e288286a
lab7

Who changed what in which revision?

UserRevisionLine numberNew contents of line
philliptran011 0:fa63e288286a 1 #include "mbed.h"
philliptran011 0:fa63e288286a 2 #include "SDFileSystem.h"
philliptran011 0:fa63e288286a 3 #include <stdint.h>
philliptran011 0:fa63e288286a 4
philliptran011 0:fa63e288286a 5
philliptran011 0:fa63e288286a 6 Serial pc(USBTX, USBRX);
philliptran011 0:fa63e288286a 7 InterruptIn R_Button(SW1);
philliptran011 0:fa63e288286a 8 AnalogIn measure(PTB0);
philliptran011 0:fa63e288286a 9 SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd");
philliptran011 0:fa63e288286a 10 Ticker t;
philliptran011 0:fa63e288286a 11 float buffer;
philliptran011 0:fa63e288286a 12 bool flag = 0;
philliptran011 0:fa63e288286a 13
philliptran011 0:fa63e288286a 14 void active() {
philliptran011 0:fa63e288286a 15 FILE *samples = fopen("/sd/samples.csv", "a");
philliptran011 0:fa63e288286a 16 buffer = measure.read();
philliptran011 0:fa63e288286a 17 if(buffer!=0) {
philliptran011 0:fa63e288286a 18 fprintf(samples, "%f\n", buffer);
philliptran011 0:fa63e288286a 19 pc.printf("Reading %f\n", buffer);
philliptran011 0:fa63e288286a 20 buffer = 0;
philliptran011 0:fa63e288286a 21 }
philliptran011 0:fa63e288286a 22 fclose(samples);
philliptran011 0:fa63e288286a 23 }
philliptran011 0:fa63e288286a 24
philliptran011 0:fa63e288286a 25 void change() {
philliptran011 0:fa63e288286a 26 pc.printf("In change function\n");
philliptran011 0:fa63e288286a 27 if (flag == 0) {
philliptran011 0:fa63e288286a 28 printf("Enabling Oscilloscope\n");
philliptran011 0:fa63e288286a 29 t.attach(&active, .01);
philliptran011 0:fa63e288286a 30 flag = 1;
philliptran011 0:fa63e288286a 31 printf("Flag set to 1\n");
philliptran011 0:fa63e288286a 32 }
philliptran011 0:fa63e288286a 33 else {
philliptran011 0:fa63e288286a 34 printf("Disabling Oscilloscope\n");
philliptran011 0:fa63e288286a 35 t.detach();
philliptran011 0:fa63e288286a 36 flag = 0;
philliptran011 0:fa63e288286a 37 printf("Flag set to 0\n");
philliptran011 0:fa63e288286a 38 }
philliptran011 0:fa63e288286a 39 }
philliptran011 0:fa63e288286a 40
philliptran011 0:fa63e288286a 41 int main() {
philliptran011 0:fa63e288286a 42 pc.baud(115200);
philliptran011 0:fa63e288286a 43 pc.printf("Connection Established\n");
philliptran011 0:fa63e288286a 44 pc.printf("File should be created\n");
philliptran011 0:fa63e288286a 45 FILE *samples = fopen("/sd/samples.csv", "w");
philliptran011 0:fa63e288286a 46 if(samples == NULL) {
philliptran011 0:fa63e288286a 47 pc.printf("But the file doesn't exist \n");
philliptran011 0:fa63e288286a 48 }
philliptran011 0:fa63e288286a 49
philliptran011 0:fa63e288286a 50 fclose(samples);
philliptran011 0:fa63e288286a 51 R_Button.mode(PullUp);
philliptran011 0:fa63e288286a 52 R_Button.fall(&change);
philliptran011 0:fa63e288286a 53 while(true) {
philliptran011 0:fa63e288286a 54 }
philliptran011 0:fa63e288286a 55 }