Colin Monroe
/
CKM_HW_12_2
homework 12.2 for SSD applied algorithms and architecture
Revision 0:a4095918961b, committed 2016-11-06
- Comitter:
- CKMonroe
- Date:
- Sun Nov 06 07:06:44 2016 +0000
- Commit message:
- V1 of homework 12.2 - recursion on the KL board
Changed in this revision
diff -r 000000000000 -r a4095918961b SLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SLCD.lib Sun Nov 06 07:06:44 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/Sissors/code/SLCD/#ef2b3b7f1b01
diff -r 000000000000 -r a4095918961b TSI.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TSI.lib Sun Nov 06 07:06:44 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/TSI/#1a60ef257879
diff -r 000000000000 -r a4095918961b main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Nov 06 07:06:44 2016 +0000 @@ -0,0 +1,120 @@ +#include "mbed.h" +#include <math.h> +#include "TSISensor.h" +#include "SLCD.h" + +#define NUMBUTS 2 +#define LBUT PTC12 // port addresses for buttons +#define RBUT PTC3 + +#define ARGUMENTSTATE 0 //states to operate in +#define COUNTSTATE 1 + +#define TSILIMIT 0.01 +#define LCDCHARLEN 10 +#define DATAINTERVAL 0.1 +#define BUTTONTIME 0.1 +#define PROGNAME "kl46z_recursion_v1\n\r" + +SLCD slcd; //define LCD display +Serial pc(USBTX, USBRX); + +Timer dataTimer; +Timer ButtonTimer; // for reading button states +DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; +float tsidata; +int displayState = 0; //start program in argument state +bool recursion = false; + +void initialize_global_vars(){ + pc.printf(PROGNAME); + // set up DAQ timers + ButtonTimer.start(); + ButtonTimer.reset(); + dataTimer.start(); + dataTimer.reset(); +} + +void LCDMess(char *lMess){ + slcd.Home(); + slcd.clear(); + slcd.printf(lMess); +} + +void backward_count(float n,int delta,int min_num){ + pc.printf("\n Number: %2.0f\n\r", n); + if (n <= min_num){ + recursion = false; //set flag to false to prevent repeats + return; + } + else { + backward_count(n-delta, delta, min_num); + } +} + +int main(void) { + int i; + char lcdData[LCDCHARLEN]; + TSISensor tsi; + float tempTSI; + PwmOut gled(LED_GREEN); + PwmOut rled(LED_RED); + + initialize_global_vars(); + + while (true) { + if (ButtonTimer > BUTTONTIME){ + for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 + if(!buttons[i]) { + displayState = i; //set display state + } // if ! buttons + }// for loop to look at buttons + + ButtonTimer.reset(); + }//end if buttonTimer + + //right button - ARGUMENT STATE(red led) + //this state uses the slider to read a value, which + //will be passed into a simple recursive function later + if (displayState == ARGUMENTSTATE){ + + //turn on red led + rled = 0.0; + gled = 1.0; + + //if statement to read tsi + if(dataTimer.read() > DATAINTERVAL){ + dataTimer.reset(); + tempTSI = tsi.readPercentage(); + if (tempTSI > TSILIMIT){ + tsidata = (tempTSI*100)/2; + } + }// end if statement to read tsi + + //send data to LCD + //range 2-50 + sprintf (lcdData,"%2.0f", tsidata ); + + recursion = true; //set bool to true so that the function can run + //in count mode + } //end argument state + + //left button - COUNT STATE (green led) + else if (displayState == COUNTSTATE){ + //turn on green led + rled = 1.0; + gled = 0.0; + + //code for recursion goes here + //2nd and 3rd passed variables are hardcoded here + //2nd variable: delta + if (recursion == true){ + recursion = false; //set recursion bool to false to prevent + //repeat function calls from here + backward_count(tsidata, 2, -5); + } + } //end count state + + LCDMess(lcdData); + } //end while true +}//end main \ No newline at end of file
diff -r 000000000000 -r a4095918961b mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Nov 06 07:06:44 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9bcdf88f62b0 \ No newline at end of file