Nicholas Outram
/
Task525Solution
Task 5.2.5 Solution
main.cpp@1:128ce9204051, 2017-07-13 (annotated)
- Committer:
- noutram
- Date:
- Thu Jul 13 14:56:36 2017 +0000
- Revision:
- 1:128ce9204051
- Parent:
- 0:3506f73fe611
updated for mbed-os 5.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:3506f73fe611 | 1 | #include "mbed.h" |
noutram | 0:3506f73fe611 | 2 | |
noutram | 0:3506f73fe611 | 3 | //Global PWM object |
noutram | 0:3506f73fe611 | 4 | PwmOut pwmRed(D7); |
noutram | 0:3506f73fe611 | 5 | |
noutram | 0:3506f73fe611 | 6 | InterruptIn SWOn(D4); //Fade ON |
noutram | 0:3506f73fe611 | 7 | InterruptIn SWOff(D3); //Fade OFF |
noutram | 0:3506f73fe611 | 8 | |
noutram | 0:3506f73fe611 | 9 | int T = 100; //100uS |
noutram | 0:3506f73fe611 | 10 | volatile int Tmark = 0; //0us |
noutram | 0:3506f73fe611 | 11 | volatile int delta = 1; //1us |
noutram | 0:3506f73fe611 | 12 | |
noutram | 0:3506f73fe611 | 13 | //Timer |
noutram | 0:3506f73fe611 | 14 | Ticker t; |
noutram | 0:3506f73fe611 | 15 | |
noutram | 0:3506f73fe611 | 16 | //Function prototype |
noutram | 0:3506f73fe611 | 17 | void doTwinkle(); |
noutram | 0:3506f73fe611 | 18 | void doSwitchON(); |
noutram | 0:3506f73fe611 | 19 | void doSwitchOFF(); |
noutram | 0:3506f73fe611 | 20 | |
noutram | 0:3506f73fe611 | 21 | //Flags |
noutram | 0:3506f73fe611 | 22 | volatile int switchONFlag=0; |
noutram | 0:3506f73fe611 | 23 | volatile int switchOFFFlag=0; |
noutram | 0:3506f73fe611 | 24 | volatile int tickerFlag = 0; |
noutram | 0:3506f73fe611 | 25 | |
noutram | 0:3506f73fe611 | 26 | int main() { |
noutram | 0:3506f73fe611 | 27 | //Setup switch interrupts |
noutram | 0:3506f73fe611 | 28 | SWOn.rise(doSwitchON); |
noutram | 0:3506f73fe611 | 29 | SWOff.rise(doSwitchOFF); |
noutram | 0:3506f73fe611 | 30 | |
noutram | 0:3506f73fe611 | 31 | //Initial PWM state |
noutram | 0:3506f73fe611 | 32 | pwmRed.period_us(T); |
noutram | 0:3506f73fe611 | 33 | pwmRed.pulsewidth_us(Tmark); |
noutram | 0:3506f73fe611 | 34 | |
noutram | 0:3506f73fe611 | 35 | |
noutram | 0:3506f73fe611 | 36 | printf("Ready\n"); |
noutram | 0:3506f73fe611 | 37 | |
noutram | 0:3506f73fe611 | 38 | while(1) { |
noutram | 0:3506f73fe611 | 39 | sleep(); |
noutram | 0:3506f73fe611 | 40 | |
noutram | 0:3506f73fe611 | 41 | //Which interrupt occured? |
noutram | 0:3506f73fe611 | 42 | if (switchONFlag == 1) { |
noutram | 0:3506f73fe611 | 43 | switchONFlag = 0; |
noutram | 0:3506f73fe611 | 44 | delta = 5; |
noutram | 0:3506f73fe611 | 45 | t.attach(doTwinkle, 0.1); |
noutram | 0:3506f73fe611 | 46 | printf("ON button pressed\n"); |
noutram | 0:3506f73fe611 | 47 | } |
noutram | 0:3506f73fe611 | 48 | else if (switchOFFFlag == 1) { |
noutram | 0:3506f73fe611 | 49 | switchOFFFlag = 0; |
noutram | 0:3506f73fe611 | 50 | delta = -5; |
noutram | 0:3506f73fe611 | 51 | t.attach(doTwinkle, 0.1); |
noutram | 0:3506f73fe611 | 52 | printf("OFF button pressed\n"); |
noutram | 0:3506f73fe611 | 53 | } |
noutram | 0:3506f73fe611 | 54 | |
noutram | 0:3506f73fe611 | 55 | //Has ticker hit the end stop? |
noutram | 0:3506f73fe611 | 56 | if (tickerFlag == 1) { |
noutram | 0:3506f73fe611 | 57 | tickerFlag = 0; |
noutram | 0:3506f73fe611 | 58 | delta = 0; //Pedantic |
noutram | 0:3506f73fe611 | 59 | t.detach(); //Turn off ticker |
noutram | 0:3506f73fe611 | 60 | printf("Timer finished\n"); |
noutram | 0:3506f73fe611 | 61 | } |
noutram | 0:3506f73fe611 | 62 | //Update PWM |
noutram | 0:3506f73fe611 | 63 | pwmRed.pulsewidth_us(Tmark); |
noutram | 0:3506f73fe611 | 64 | |
noutram | 0:3506f73fe611 | 65 | printf("Tmark = %d\n", Tmark); //Debug |
noutram | 0:3506f73fe611 | 66 | } |
noutram | 0:3506f73fe611 | 67 | } |
noutram | 0:3506f73fe611 | 68 | |
noutram | 0:3506f73fe611 | 69 | void doSwitchON() |
noutram | 0:3506f73fe611 | 70 | { |
noutram | 0:3506f73fe611 | 71 | switchONFlag=1; |
noutram | 0:3506f73fe611 | 72 | } |
noutram | 0:3506f73fe611 | 73 | |
noutram | 0:3506f73fe611 | 74 | void doSwitchOFF() |
noutram | 0:3506f73fe611 | 75 | { |
noutram | 0:3506f73fe611 | 76 | switchOFFFlag=1; |
noutram | 0:3506f73fe611 | 77 | } |
noutram | 0:3506f73fe611 | 78 | |
noutram | 0:3506f73fe611 | 79 | //ISR for Timer |
noutram | 0:3506f73fe611 | 80 | void doTwinkle() |
noutram | 0:3506f73fe611 | 81 | { |
noutram | 0:3506f73fe611 | 82 | //Add on delta |
noutram | 0:3506f73fe611 | 83 | Tmark += delta; |
noutram | 0:3506f73fe611 | 84 | |
noutram | 0:3506f73fe611 | 85 | //Cap at extremes |
noutram | 0:3506f73fe611 | 86 | if (Tmark > 100) { |
noutram | 0:3506f73fe611 | 87 | Tmark = 100; |
noutram | 0:3506f73fe611 | 88 | } |
noutram | 0:3506f73fe611 | 89 | else if (Tmark < 0) { |
noutram | 0:3506f73fe611 | 90 | Tmark = 0; |
noutram | 0:3506f73fe611 | 91 | } |
noutram | 0:3506f73fe611 | 92 | |
noutram | 0:3506f73fe611 | 93 | //Check bounds |
noutram | 0:3506f73fe611 | 94 | if ((Tmark == 100) || (Tmark == 0)) { |
noutram | 0:3506f73fe611 | 95 | tickerFlag = 1; |
noutram | 0:3506f73fe611 | 96 | } |
noutram | 0:3506f73fe611 | 97 | } |