Working read code with mode button

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Testnew by BAT

Committer:
aismail1997
Date:
Wed Oct 18 14:06:19 2017 +0000
Revision:
13:b302c71e300f
Parent:
12:21268d8bf979
Child:
14:7ff1f6022694
Child:
15:b80dde24e9bc
test - Azra

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aismail1997 2:9eda4611081a 1 #include "mbed.h"
aismail1997 8:1aa86ad19af9 2 #include "rtos.h"
aismail1997 11:418a4437a693 3 #include "wave_player.h"
aismail1997 11:418a4437a693 4 #include "SDFileSystem.h"
aismail1997 12:21268d8bf979 5 #include "button.h"
aismail1997 5:9ed43e974156 6
aismail1997 13:b302c71e300f 7 // Azra
aismail1997 13:b302c71e300f 8
aismail1997 2:9eda4611081a 9 PwmOut myservo(p21);
aismail1997 2:9eda4611081a 10 DigitalIn pb1 (p20);
aismail1997 2:9eda4611081a 11 //AnalogIn linpot(p20);
aismail1997 2:9eda4611081a 12 Serial pc(USBTX, USBRX);
aismail1997 6:cc181f8f2bd1 13 //DigitalOut myled(LED1);
aismail1997 6:cc181f8f2bd1 14 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
aismail1997 6:cc181f8f2bd1 15 AnalogOut DACout(p26);
aismail1997 6:cc181f8f2bd1 16 wave_player waver(&DACout);
aismail1997 12:21268d8bf979 17 button button1(myservo, pb1);
aismail1997 6:cc181f8f2bd1 18
aismail1997 8:1aa86ad19af9 19 // add start, mode, reset buttons
aismail1997 11:418a4437a693 20 int start = 0;
aismail1997 11:418a4437a693 21 int submit = 0;
aismail1997 12:21268d8bf979 22 // FIX THIS: button up: state = 2, button halfway: state = 0; button down: state = 1
aismail1997 12:21268d8bf979 23 int state = 2;
aismail1997 8:1aa86ad19af9 24
aismail1997 11:418a4437a693 25 // FUNCTIONS
aismail1997 11:418a4437a693 26 void playSound(wave_player waver)
aismail1997 11:418a4437a693 27 {
aismail1997 11:418a4437a693 28 FILE *wave_file;
aismail1997 11:418a4437a693 29 wave_file=fopen("/sd/police_siren.wav","r");
aismail1997 11:418a4437a693 30 waver.play(wave_file);
aismail1997 11:418a4437a693 31 fclose(wave_file);
aismail1997 11:418a4437a693 32 }
aismail1997 8:1aa86ad19af9 33
aismail1997 8:1aa86ad19af9 34 // THREADS
aismail1997 8:1aa86ad19af9 35 void button_thread()
aismail1997 6:cc181f8f2bd1 36 {
aismail1997 12:21268d8bf979 37 // button was up and is moving down, move servo in
aismail1997 12:21268d8bf979 38 if (pb1 == 1 && state == 2) {
aismail1997 12:21268d8bf979 39 button1.moveServoIn();
aismail1997 12:21268d8bf979 40 state = 1;
aismail1997 12:21268d8bf979 41 }
aismail1997 12:21268d8bf979 42 // button was down and is being pushed again, move servo out
aismail1997 12:21268d8bf979 43 if (pb1 == 1 && state == 1) {
aismail1997 12:21268d8bf979 44 button1.moveServoOut();
aismail1997 12:21268d8bf979 45 state = 2;
aismail1997 12:21268d8bf979 46 }
aismail1997 6:cc181f8f2bd1 47 }
aismail1997 6:cc181f8f2bd1 48
aismail1997 8:1aa86ad19af9 49 void submit_thread()
aismail1997 6:cc181f8f2bd1 50 {
aismail1997 8:1aa86ad19af9 51
aismail1997 6:cc181f8f2bd1 52 }
aismail1997 6:cc181f8f2bd1 53
aismail1997 8:1aa86ad19af9 54 void start_thread()
aismail1997 6:cc181f8f2bd1 55 {
aismail1997 8:1aa86ad19af9 56 // read pb_start
aismail1997 8:1aa86ad19af9 57 // if 1
aismail1997 8:1aa86ad19af9 58 start == 1;
aismail1997 8:1aa86ad19af9 59 // else 0
aismail1997 6:cc181f8f2bd1 60 }
aismail1997 2:9eda4611081a 61
aismail1997 2:9eda4611081a 62 int main()
aismail1997 2:9eda4611081a 63 {
aismail1997 8:1aa86ad19af9 64 // SETUP
aismail1997 6:cc181f8f2bd1 65 // pull up the pushbutton to prevent bouncing
aismail1997 2:9eda4611081a 66 pb1.mode(PullUp);
aismail1997 2:9eda4611081a 67 wait(.001);
aismail1997 6:cc181f8f2bd1 68 // servo begins at 30 degrees
aismail1997 12:21268d8bf979 69 // replace with a button setup function
aismail1997 2:9eda4611081a 70 for(int i=0; i<=3; i++) {
aismail1997 2:9eda4611081a 71 myservo = i/100.0;
aismail1997 2:9eda4611081a 72 wait(0.01);
aismail1997 2:9eda4611081a 73 }
aismail1997 6:cc181f8f2bd1 74
aismail1997 8:1aa86ad19af9 75 // MAIN THREAD
aismail1997 8:1aa86ad19af9 76 while(true) {
aismail1997 8:1aa86ad19af9 77
aismail1997 8:1aa86ad19af9 78 // start threads for reset, mode, start
aismail1997 8:1aa86ad19af9 79 Thread t1(start_thread);
aismail1997 8:1aa86ad19af9 80 // setup SDcard and Speaker
aismail1997 12:21268d8bf979 81
aismail1997 8:1aa86ad19af9 82 // when started
aismail1997 8:1aa86ad19af9 83 while (start == 0) {}
aismail1997 8:1aa86ad19af9 84 Thread t2(button_thread);
aismail1997 12:21268d8bf979 85
aismail1997 12:21268d8bf979 86 //Thread t3(submit_thread);
aismail1997 8:1aa86ad19af9 87
aismail1997 8:1aa86ad19af9 88 // when submitted
aismail1997 12:21268d8bf979 89 //while (submit == 0) {}
aismail1997 12:21268d8bf979 90
aismail1997 8:1aa86ad19af9 91
aismail1997 8:1aa86ad19af9 92 // start button threads and submit thread
aismail1997 8:1aa86ad19af9 93 // if submit close button threads and submit thread
aismail1997 8:1aa86ad19af9 94 // check result, speaker result
aismail1997 8:1aa86ad19af9 95 // save results
aismail1997 8:1aa86ad19af9 96
aismail1997 6:cc181f8f2bd1 97 // SD card code
aismail1997 6:cc181f8f2bd1 98
aismail1997 6:cc181f8f2bd1 99 // speaker code
aismail1997 2:9eda4611081a 100
aismail1997 2:9eda4611081a 101 // read linear potentiometer
aismail1997 2:9eda4611081a 102 //if (linpot < 0.5) {
aismail1997 2:9eda4611081a 103 //float potval = linpot;
aismail1997 2:9eda4611081a 104 //pc.printf("linear pot: %f\n", potval);
aismail1997 2:9eda4611081a 105 }
aismail1997 2:9eda4611081a 106 }