Tom Kelliher
/
Interrupts
Starting point code for CS 220 Lab 5.
Fork of FRDM_TSI by
Diff: main.cpp
- Revision:
- 6:1096b1fd8d0a
- Parent:
- 1:51b1b688179a
--- a/main.cpp Thu May 09 09:26:46 2013 +0000 +++ b/main.cpp Mon Jun 26 16:05:16 2017 +0000 @@ -1,12 +1,57 @@ #include "mbed.h" #include "TSISensor.h" -int main(void) { - PwmOut led(LED_GREEN); - TSISensor tsi; - - while (true) { - led = 1.0 - tsi.readPercentage(); - wait(0.1); + +#define LED_OFF 1.0 +// Using pulse width modulation, the closer this value is to 0.0, the +// brighter the LED will be. +#define LED_ON 0.9 + + +// Use pulse width modulation to decrease the LED's brightness. +PwmOut greenLed(LED_GREEN); +PwmOut redLed(LED_RED); +PwmOut blueLed(LED_BLUE); + +// The blue LED is controlled by the button. +// The green LED is controlled by the KL25Z's touch sensor. +// The red LED is controlled by the cpuIntensive() function. +DigitalIn button(PTA1); +TSISensor tsi; + +// The serial connection is available, if necessary, for debugging purposes. +Serial pc(USBTX,USBRX); + + +// Function prototype. +void cpuIntensive(); + + +int main(void) +{ + button.mode(PullUp); + redLed = greenLed = blueLed = LED_OFF; + + while (1) { + cpuIntensive(); + + // Use the touch sensor to scale the green LED's brightness + // to be between LED_ON and LED_OFF. + greenLed = LED_ON + (LED_OFF - LED_ON) * (1.0 - tsi.readPercentage()); + + if (button) + blueLed = LED_OFF; + else + blueLed = LED_ON; } +} + + +// Using the long wait()s, simulate a CPU-intensive process. +void cpuIntensive() +{ + redLed = LED_ON; + wait(2.0); + redLed = LED_OFF; + wait(3.0); } \ No newline at end of file