![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
oppgave 10
Dependencies: mbed
main.cpp@0:6f57f70cb6af, 2017-11-07 (annotated)
- Committer:
- Smashftw
- Date:
- Tue Nov 07 12:42:37 2017 +0000
- Revision:
- 0:6f57f70cb6af
oppgave10
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Smashftw | 0:6f57f70cb6af | 1 | #include "mbed.h" |
Smashftw | 0:6f57f70cb6af | 2 | |
Smashftw | 0:6f57f70cb6af | 3 | #define RED 4 |
Smashftw | 0:6f57f70cb6af | 4 | #define YELLOW 2 |
Smashftw | 0:6f57f70cb6af | 5 | #define GREEN 1 |
Smashftw | 0:6f57f70cb6af | 6 | #define ON 1 |
Smashftw | 0:6f57f70cb6af | 7 | #define OFF 0 |
Smashftw | 0:6f57f70cb6af | 8 | |
Smashftw | 0:6f57f70cb6af | 9 | #define _10SEK 100 |
Smashftw | 0:6f57f70cb6af | 10 | #define _5SEK 50 |
Smashftw | 0:6f57f70cb6af | 11 | #define _4SEK 40 |
Smashftw | 0:6f57f70cb6af | 12 | #define _2SEK 20 |
Smashftw | 0:6f57f70cb6af | 13 | #define _1SEK 10 |
Smashftw | 0:6f57f70cb6af | 14 | |
Smashftw | 0:6f57f70cb6af | 15 | BusOut ew_lights(p26, p27, p28); //Lyskryss retning OV |
Smashftw | 0:6f57f70cb6af | 16 | BusOut ns_lights(p21, p22, p23); //Lyskryss retnng NS |
Smashftw | 0:6f57f70cb6af | 17 | DigitalIn PED(p12); //Trykkbryter for ganglys |
Smashftw | 0:6f57f70cb6af | 18 | DigitalOut FK(LED1); //Ganglys klart |
Smashftw | 0:6f57f70cb6af | 19 | DigitalOut FV(LED4); //Ganglys vent |
Smashftw | 0:6f57f70cb6af | 20 | Ticker tc; // 10 Hz Klokke |
Smashftw | 0:6f57f70cb6af | 21 | |
Smashftw | 0:6f57f70cb6af | 22 | int time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 23 | volatile int knapp = 0; |
Smashftw | 0:6f57f70cb6af | 24 | |
Smashftw | 0:6f57f70cb6af | 25 | enum {OV0, OV1, OV2, OV3, NS0, NS1, NS2, NS3, PED1, PED2, ILLEGAL} state; |
Smashftw | 0:6f57f70cb6af | 26 | |
Smashftw | 0:6f57f70cb6af | 27 | void day_night_control(void); |
Smashftw | 0:6f57f70cb6af | 28 | void tick_control(void); |
Smashftw | 0:6f57f70cb6af | 29 | void fsm(void); |
Smashftw | 0:6f57f70cb6af | 30 | |
Smashftw | 0:6f57f70cb6af | 31 | volatile int sysclock = 0; |
Smashftw | 0:6f57f70cb6af | 32 | |
Smashftw | 0:6f57f70cb6af | 33 | int main(void) { |
Smashftw | 0:6f57f70cb6af | 34 | tc.attach(&tick_control, 0.05); |
Smashftw | 0:6f57f70cb6af | 35 | state = OV0; |
Smashftw | 0:6f57f70cb6af | 36 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 37 | |
Smashftw | 0:6f57f70cb6af | 38 | while(1) { |
Smashftw | 0:6f57f70cb6af | 39 | fsm(); |
Smashftw | 0:6f57f70cb6af | 40 | while (sysclock == 1); |
Smashftw | 0:6f57f70cb6af | 41 | while (sysclock == 0); |
Smashftw | 0:6f57f70cb6af | 42 | time_tick++; |
Smashftw | 0:6f57f70cb6af | 43 | } |
Smashftw | 0:6f57f70cb6af | 44 | } |
Smashftw | 0:6f57f70cb6af | 45 | |
Smashftw | 0:6f57f70cb6af | 46 | void tick_control() |
Smashftw | 0:6f57f70cb6af | 47 | { |
Smashftw | 0:6f57f70cb6af | 48 | sysclock = !sysclock; |
Smashftw | 0:6f57f70cb6af | 49 | } |
Smashftw | 0:6f57f70cb6af | 50 | |
Smashftw | 0:6f57f70cb6af | 51 | void fsm(void) |
Smashftw | 0:6f57f70cb6af | 52 | { |
Smashftw | 0:6f57f70cb6af | 53 | static int knapp = 0; |
Smashftw | 0:6f57f70cb6af | 54 | switch(state) { |
Smashftw | 0:6f57f70cb6af | 55 | case OV0: |
Smashftw | 0:6f57f70cb6af | 56 | ns_lights = RED; |
Smashftw | 0:6f57f70cb6af | 57 | ew_lights = GREEN; |
Smashftw | 0:6f57f70cb6af | 58 | FK = OFF; |
Smashftw | 0:6f57f70cb6af | 59 | FV = ON; |
Smashftw | 0:6f57f70cb6af | 60 | if (PED == ON) { |
Smashftw | 0:6f57f70cb6af | 61 | knapp = 1; |
Smashftw | 0:6f57f70cb6af | 62 | } |
Smashftw | 0:6f57f70cb6af | 63 | if (time_tick >= _10SEK) { |
Smashftw | 0:6f57f70cb6af | 64 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 65 | state = OV1; |
Smashftw | 0:6f57f70cb6af | 66 | } |
Smashftw | 0:6f57f70cb6af | 67 | break; |
Smashftw | 0:6f57f70cb6af | 68 | case OV1: |
Smashftw | 0:6f57f70cb6af | 69 | ns_lights = RED; |
Smashftw | 0:6f57f70cb6af | 70 | ew_lights = YELLOW; |
Smashftw | 0:6f57f70cb6af | 71 | FK = OFF; |
Smashftw | 0:6f57f70cb6af | 72 | FV = ON; |
Smashftw | 0:6f57f70cb6af | 73 | if (PED == ON) { |
Smashftw | 0:6f57f70cb6af | 74 | knapp = 1; |
Smashftw | 0:6f57f70cb6af | 75 | } |
Smashftw | 0:6f57f70cb6af | 76 | if (time_tick >= _2SEK) { |
Smashftw | 0:6f57f70cb6af | 77 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 78 | state = OV2; |
Smashftw | 0:6f57f70cb6af | 79 | } |
Smashftw | 0:6f57f70cb6af | 80 | break; |
Smashftw | 0:6f57f70cb6af | 81 | case OV2: |
Smashftw | 0:6f57f70cb6af | 82 | ns_lights = RED; |
Smashftw | 0:6f57f70cb6af | 83 | ew_lights = RED; |
Smashftw | 0:6f57f70cb6af | 84 | FK = OFF; |
Smashftw | 0:6f57f70cb6af | 85 | FV = ON; |
Smashftw | 0:6f57f70cb6af | 86 | if (PED == ON || knapp == 1) { |
Smashftw | 0:6f57f70cb6af | 87 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 88 | state = PED1; |
Smashftw | 0:6f57f70cb6af | 89 | knapp = 0; |
Smashftw | 0:6f57f70cb6af | 90 | } |
Smashftw | 0:6f57f70cb6af | 91 | if (time_tick >= _1SEK) { |
Smashftw | 0:6f57f70cb6af | 92 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 93 | state = OV3; |
Smashftw | 0:6f57f70cb6af | 94 | } |
Smashftw | 0:6f57f70cb6af | 95 | break; |
Smashftw | 0:6f57f70cb6af | 96 | case OV3: |
Smashftw | 0:6f57f70cb6af | 97 | ns_lights = RED+YELLOW; |
Smashftw | 0:6f57f70cb6af | 98 | ew_lights = RED; |
Smashftw | 0:6f57f70cb6af | 99 | FK = OFF; |
Smashftw | 0:6f57f70cb6af | 100 | FV = ON; |
Smashftw | 0:6f57f70cb6af | 101 | if (PED == ON) { |
Smashftw | 0:6f57f70cb6af | 102 | knapp = 1; |
Smashftw | 0:6f57f70cb6af | 103 | } |
Smashftw | 0:6f57f70cb6af | 104 | if(time_tick >= _1SEK) { |
Smashftw | 0:6f57f70cb6af | 105 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 106 | state = NS0; |
Smashftw | 0:6f57f70cb6af | 107 | } |
Smashftw | 0:6f57f70cb6af | 108 | break; |
Smashftw | 0:6f57f70cb6af | 109 | case NS0: |
Smashftw | 0:6f57f70cb6af | 110 | ns_lights = GREEN; |
Smashftw | 0:6f57f70cb6af | 111 | ew_lights = RED; |
Smashftw | 0:6f57f70cb6af | 112 | FK = OFF; |
Smashftw | 0:6f57f70cb6af | 113 | FV = ON; |
Smashftw | 0:6f57f70cb6af | 114 | if (PED == ON) { |
Smashftw | 0:6f57f70cb6af | 115 | knapp = 1; |
Smashftw | 0:6f57f70cb6af | 116 | } |
Smashftw | 0:6f57f70cb6af | 117 | if(time_tick >= _5SEK) { |
Smashftw | 0:6f57f70cb6af | 118 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 119 | state = NS1; |
Smashftw | 0:6f57f70cb6af | 120 | } |
Smashftw | 0:6f57f70cb6af | 121 | break; |
Smashftw | 0:6f57f70cb6af | 122 | case NS1: |
Smashftw | 0:6f57f70cb6af | 123 | ns_lights = YELLOW; |
Smashftw | 0:6f57f70cb6af | 124 | ew_lights = RED; |
Smashftw | 0:6f57f70cb6af | 125 | FK = OFF; |
Smashftw | 0:6f57f70cb6af | 126 | FV = ON; |
Smashftw | 0:6f57f70cb6af | 127 | if (PED == ON) { |
Smashftw | 0:6f57f70cb6af | 128 | knapp = 1; |
Smashftw | 0:6f57f70cb6af | 129 | } |
Smashftw | 0:6f57f70cb6af | 130 | if(time_tick >= _2SEK) { |
Smashftw | 0:6f57f70cb6af | 131 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 132 | state = NS2; |
Smashftw | 0:6f57f70cb6af | 133 | } |
Smashftw | 0:6f57f70cb6af | 134 | break; |
Smashftw | 0:6f57f70cb6af | 135 | case NS2: |
Smashftw | 0:6f57f70cb6af | 136 | ns_lights = RED; |
Smashftw | 0:6f57f70cb6af | 137 | ew_lights = RED; |
Smashftw | 0:6f57f70cb6af | 138 | FK = OFF; |
Smashftw | 0:6f57f70cb6af | 139 | FV = ON; |
Smashftw | 0:6f57f70cb6af | 140 | if (PED == ON || knapp == 1) { |
Smashftw | 0:6f57f70cb6af | 141 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 142 | state = PED2; |
Smashftw | 0:6f57f70cb6af | 143 | knapp = 0; |
Smashftw | 0:6f57f70cb6af | 144 | } |
Smashftw | 0:6f57f70cb6af | 145 | if(time_tick >= _1SEK) { |
Smashftw | 0:6f57f70cb6af | 146 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 147 | state = NS3; |
Smashftw | 0:6f57f70cb6af | 148 | } |
Smashftw | 0:6f57f70cb6af | 149 | break; |
Smashftw | 0:6f57f70cb6af | 150 | case NS3: |
Smashftw | 0:6f57f70cb6af | 151 | ns_lights = RED; |
Smashftw | 0:6f57f70cb6af | 152 | ew_lights = RED+YELLOW; |
Smashftw | 0:6f57f70cb6af | 153 | FK = OFF; |
Smashftw | 0:6f57f70cb6af | 154 | FV = ON; |
Smashftw | 0:6f57f70cb6af | 155 | if (PED == ON) { |
Smashftw | 0:6f57f70cb6af | 156 | knapp = 1; |
Smashftw | 0:6f57f70cb6af | 157 | } |
Smashftw | 0:6f57f70cb6af | 158 | if(time_tick >= _1SEK) { |
Smashftw | 0:6f57f70cb6af | 159 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 160 | state = OV0; |
Smashftw | 0:6f57f70cb6af | 161 | } |
Smashftw | 0:6f57f70cb6af | 162 | break; |
Smashftw | 0:6f57f70cb6af | 163 | case PED1: |
Smashftw | 0:6f57f70cb6af | 164 | ns_lights = RED; |
Smashftw | 0:6f57f70cb6af | 165 | ew_lights = RED; |
Smashftw | 0:6f57f70cb6af | 166 | FK = ON; |
Smashftw | 0:6f57f70cb6af | 167 | FV = OFF; |
Smashftw | 0:6f57f70cb6af | 168 | if(time_tick >= _10SEK) { |
Smashftw | 0:6f57f70cb6af | 169 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 170 | state = OV3; |
Smashftw | 0:6f57f70cb6af | 171 | } |
Smashftw | 0:6f57f70cb6af | 172 | break; |
Smashftw | 0:6f57f70cb6af | 173 | case PED2: |
Smashftw | 0:6f57f70cb6af | 174 | ns_lights = RED; |
Smashftw | 0:6f57f70cb6af | 175 | ew_lights = RED; |
Smashftw | 0:6f57f70cb6af | 176 | FV = OFF; |
Smashftw | 0:6f57f70cb6af | 177 | FK = ON; |
Smashftw | 0:6f57f70cb6af | 178 | if(time_tick >= _10SEK) { |
Smashftw | 0:6f57f70cb6af | 179 | time_tick = 0; |
Smashftw | 0:6f57f70cb6af | 180 | state = NS3; |
Smashftw | 0:6f57f70cb6af | 181 | } |
Smashftw | 0:6f57f70cb6af | 182 | break; |
Smashftw | 0:6f57f70cb6af | 183 | } |
Smashftw | 0:6f57f70cb6af | 184 | } |