Bartek Wereszczyński
/
control_led_action
My first big writted program ;)
main.cpp@1:d3366745d5ec, 2016-12-30 (annotated)
- Committer:
- foreste
- Date:
- Fri Dec 30 07:30:09 2016 +0000
- Revision:
- 1:d3366745d5ec
- Parent:
- 0:f86463132427
- Child:
- 2:83e427afeea0
Add comment and fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
foreste | 1:d3366745d5ec | 1 | /* |
foreste | 1:d3366745d5ec | 2 | Author: foreste |
foreste | 1:d3366745d5ec | 3 | Program name: control led action |
foreste | 1:d3366745d5ec | 4 | Platform test hardware: Stm32f0RB and Atnel.pl ATB 1,04A |
foreste | 1:d3366745d5ec | 5 | */ |
foreste | 1:d3366745d5ec | 6 | |
foreste | 0:f86463132427 | 7 | #include "mbed.h" |
foreste | 0:f86463132427 | 8 | |
foreste | 1:d3366745d5ec | 9 | //Settings Uart |
foreste | 0:f86463132427 | 10 | Serial rs232(USBTX, USBRX); |
foreste | 0:f86463132427 | 11 | |
foreste | 1:d3366745d5ec | 12 | //Setting pins |
foreste | 0:f86463132427 | 13 | DigitalOut led[8] {PC_3, PC_2, PA_1, PA_0, PA_15, PC_12, PC_13, PB_7}; |
foreste | 1:d3366745d5ec | 14 | |
foreste | 1:d3366745d5ec | 15 | //Delay no blocking program |
foreste | 1:d3366745d5ec | 16 | Timer delay; |
foreste | 1:d3366745d5ec | 17 | |
foreste | 1:d3366745d5ec | 18 | char user_input = 0; //user variable |
foreste | 1:d3366745d5ec | 19 | |
foreste | 1:d3366745d5ec | 20 | //fuction replace state pin in High |
foreste | 0:f86463132427 | 21 | void ledintilize() |
foreste | 0:f86463132427 | 22 | { |
foreste | 0:f86463132427 | 23 | for(int x=0; x<8; x++) { |
foreste | 0:f86463132427 | 24 | led[x] = 1; |
foreste | 0:f86463132427 | 25 | } |
foreste | 0:f86463132427 | 26 | } |
foreste | 1:d3366745d5ec | 27 | |
foreste | 1:d3366745d5ec | 28 | //fuction replace state pin in High special method |
foreste | 0:f86463132427 | 29 | void pin_clear_state() |
foreste | 0:f86463132427 | 30 | { |
foreste | 0:f86463132427 | 31 | led[0] = led[1] = led[2] = led[3] = led[4] = led[5] = led[6] = led[7] = 1; |
foreste | 0:f86463132427 | 32 | } |
foreste | 1:d3366745d5ec | 33 | //first method flashing knight |
foreste | 0:f86463132427 | 34 | void led_knight() |
foreste | 0:f86463132427 | 35 | { |
foreste | 1:d3366745d5ec | 36 | switch(delay.read_ms()) { |
foreste | 0:f86463132427 | 37 | case 500: |
foreste | 0:f86463132427 | 38 | pin_clear_state(); |
foreste | 0:f86463132427 | 39 | led[0] = 0; |
foreste | 0:f86463132427 | 40 | break; |
foreste | 0:f86463132427 | 41 | case 1000: |
foreste | 0:f86463132427 | 42 | led[0] = 1; |
foreste | 0:f86463132427 | 43 | led[1] = 0; |
foreste | 0:f86463132427 | 44 | break; |
foreste | 0:f86463132427 | 45 | case 1500: |
foreste | 0:f86463132427 | 46 | led[1] = 1; |
foreste | 0:f86463132427 | 47 | led[2] = 0; |
foreste | 0:f86463132427 | 48 | break; |
foreste | 0:f86463132427 | 49 | case 2000: |
foreste | 0:f86463132427 | 50 | led[2] = 1; |
foreste | 0:f86463132427 | 51 | led[3] = 0; |
foreste | 0:f86463132427 | 52 | break; |
foreste | 0:f86463132427 | 53 | case 2500: |
foreste | 0:f86463132427 | 54 | led[3] = 1; |
foreste | 0:f86463132427 | 55 | led[4] = 0; |
foreste | 0:f86463132427 | 56 | break; |
foreste | 0:f86463132427 | 57 | case 3000: |
foreste | 0:f86463132427 | 58 | led[4] = 1; |
foreste | 0:f86463132427 | 59 | led[5] = 0; |
foreste | 0:f86463132427 | 60 | break; |
foreste | 0:f86463132427 | 61 | case 3500: |
foreste | 0:f86463132427 | 62 | led[5] = 1; |
foreste | 0:f86463132427 | 63 | led[6] = 0; |
foreste | 0:f86463132427 | 64 | break; |
foreste | 0:f86463132427 | 65 | case 4000: |
foreste | 0:f86463132427 | 66 | led[6] = 1; |
foreste | 0:f86463132427 | 67 | led[7] = 0; |
foreste | 0:f86463132427 | 68 | break; |
foreste | 0:f86463132427 | 69 | case 4500: |
foreste | 0:f86463132427 | 70 | led[7] = 1; |
foreste | 1:d3366745d5ec | 71 | delay.reset(); |
foreste | 0:f86463132427 | 72 | break; |
foreste | 0:f86463132427 | 73 | } |
foreste | 0:f86463132427 | 74 | } |
foreste | 1:d3366745d5ec | 75 | // second method flashing knight |
foreste | 0:f86463132427 | 76 | void led_knight2() |
foreste | 0:f86463132427 | 77 | { |
foreste | 1:d3366745d5ec | 78 | switch(delay.read_ms()) { |
foreste | 0:f86463132427 | 79 | case 500: |
foreste | 0:f86463132427 | 80 | pin_clear_state(); |
foreste | 0:f86463132427 | 81 | led[0] = 0; |
foreste | 0:f86463132427 | 82 | break; |
foreste | 0:f86463132427 | 83 | case 1000: |
foreste | 0:f86463132427 | 84 | led[0] = 1; |
foreste | 0:f86463132427 | 85 | led[1] = 0; |
foreste | 0:f86463132427 | 86 | break; |
foreste | 0:f86463132427 | 87 | case 1500: |
foreste | 0:f86463132427 | 88 | led[1] = 1; |
foreste | 0:f86463132427 | 89 | led[2] = 0; |
foreste | 0:f86463132427 | 90 | break; |
foreste | 0:f86463132427 | 91 | case 2000: |
foreste | 0:f86463132427 | 92 | led[2] = 1; |
foreste | 0:f86463132427 | 93 | led[3] = 0; |
foreste | 0:f86463132427 | 94 | break; |
foreste | 0:f86463132427 | 95 | case 2500: |
foreste | 0:f86463132427 | 96 | led[3] = 1; |
foreste | 0:f86463132427 | 97 | led[4] = 0; |
foreste | 0:f86463132427 | 98 | break; |
foreste | 0:f86463132427 | 99 | case 3000: |
foreste | 0:f86463132427 | 100 | led[4] = 1; |
foreste | 0:f86463132427 | 101 | led[5] = 0; |
foreste | 0:f86463132427 | 102 | break; |
foreste | 0:f86463132427 | 103 | case 3500: |
foreste | 0:f86463132427 | 104 | led[5] = 1; |
foreste | 0:f86463132427 | 105 | led[6] = 0; |
foreste | 0:f86463132427 | 106 | break; |
foreste | 0:f86463132427 | 107 | case 4000: |
foreste | 0:f86463132427 | 108 | led[6] = 1; |
foreste | 0:f86463132427 | 109 | led[7] = 0; |
foreste | 0:f86463132427 | 110 | break; |
foreste | 0:f86463132427 | 111 | case 4500: |
foreste | 0:f86463132427 | 112 | led[7] = 1; |
foreste | 0:f86463132427 | 113 | break; |
foreste | 0:f86463132427 | 114 | |
foreste | 0:f86463132427 | 115 | case 5000: |
foreste | 0:f86463132427 | 116 | led[7] = 1; |
foreste | 0:f86463132427 | 117 | led[6] = 0; |
foreste | 0:f86463132427 | 118 | break; |
foreste | 0:f86463132427 | 119 | case 5500: |
foreste | 0:f86463132427 | 120 | led[6] = 1; |
foreste | 0:f86463132427 | 121 | led[5] = 0; |
foreste | 0:f86463132427 | 122 | break; |
foreste | 0:f86463132427 | 123 | case 6000: |
foreste | 0:f86463132427 | 124 | led[5] = 1; |
foreste | 0:f86463132427 | 125 | led[4] = 0; |
foreste | 0:f86463132427 | 126 | break; |
foreste | 0:f86463132427 | 127 | case 6500: |
foreste | 0:f86463132427 | 128 | led[4] = 1; |
foreste | 0:f86463132427 | 129 | led[3] = 0; |
foreste | 0:f86463132427 | 130 | break; |
foreste | 0:f86463132427 | 131 | case 7000: |
foreste | 0:f86463132427 | 132 | led[3] = 1; |
foreste | 0:f86463132427 | 133 | led[2] = 0; |
foreste | 0:f86463132427 | 134 | break; |
foreste | 0:f86463132427 | 135 | case 7500: |
foreste | 0:f86463132427 | 136 | led[2] = 1; |
foreste | 0:f86463132427 | 137 | led[1] = 0; |
foreste | 0:f86463132427 | 138 | break; |
foreste | 0:f86463132427 | 139 | case 8000: |
foreste | 0:f86463132427 | 140 | led[1] = 1; |
foreste | 0:f86463132427 | 141 | led[0] = 0; |
foreste | 0:f86463132427 | 142 | break; |
foreste | 0:f86463132427 | 143 | case 8500: |
foreste | 0:f86463132427 | 144 | led[0] = 1; |
foreste | 1:d3366745d5ec | 145 | delay.reset(); |
foreste | 0:f86463132427 | 146 | break; |
foreste | 0:f86463132427 | 147 | } |
foreste | 0:f86463132427 | 148 | |
foreste | 0:f86463132427 | 149 | } |
foreste | 1:d3366745d5ec | 150 | |
foreste | 1:d3366745d5ec | 151 | //Third method flashing knight |
foreste | 0:f86463132427 | 152 | void knight3() |
foreste | 0:f86463132427 | 153 | { |
foreste | 1:d3366745d5ec | 154 | switch(delay.read_ms()) { |
foreste | 0:f86463132427 | 155 | case 500: |
foreste | 0:f86463132427 | 156 | pin_clear_state(); |
foreste | 0:f86463132427 | 157 | // led[7] = 1; |
foreste | 0:f86463132427 | 158 | led[6] = 0; |
foreste | 0:f86463132427 | 159 | break; |
foreste | 0:f86463132427 | 160 | case 1000: |
foreste | 0:f86463132427 | 161 | led[6] = 1; |
foreste | 0:f86463132427 | 162 | led[5] = 0; |
foreste | 0:f86463132427 | 163 | break; |
foreste | 0:f86463132427 | 164 | case 1500: |
foreste | 0:f86463132427 | 165 | led[5] = 1; |
foreste | 0:f86463132427 | 166 | led[4] = 0; |
foreste | 0:f86463132427 | 167 | break; |
foreste | 0:f86463132427 | 168 | case 2000: |
foreste | 0:f86463132427 | 169 | led[4] = 1; |
foreste | 0:f86463132427 | 170 | led[3] = 0; |
foreste | 0:f86463132427 | 171 | break; |
foreste | 0:f86463132427 | 172 | case 25000: |
foreste | 0:f86463132427 | 173 | led[3] = 1; |
foreste | 0:f86463132427 | 174 | led[2] = 0; |
foreste | 0:f86463132427 | 175 | break; |
foreste | 0:f86463132427 | 176 | case 3000: |
foreste | 0:f86463132427 | 177 | led[2] = 1; |
foreste | 0:f86463132427 | 178 | led[1] = 0; |
foreste | 0:f86463132427 | 179 | break; |
foreste | 0:f86463132427 | 180 | case 35000: |
foreste | 0:f86463132427 | 181 | led[1] = 1; |
foreste | 0:f86463132427 | 182 | led[0] = 0; |
foreste | 0:f86463132427 | 183 | break; |
foreste | 0:f86463132427 | 184 | case 4000: |
foreste | 0:f86463132427 | 185 | led[0] = 1; |
foreste | 1:d3366745d5ec | 186 | delay.reset(); |
foreste | 0:f86463132427 | 187 | break; |
foreste | 0:f86463132427 | 188 | } |
foreste | 0:f86463132427 | 189 | } |
foreste | 1:d3366745d5ec | 190 | //first method flashing Police sirens |
foreste | 0:f86463132427 | 191 | void police_light() |
foreste | 0:f86463132427 | 192 | { |
foreste | 1:d3366745d5ec | 193 | if(delay.read_ms() >= 200) { |
foreste | 0:f86463132427 | 194 | pin_clear_state(); |
foreste | 0:f86463132427 | 195 | led[0] = led[1] = led[2] = led[3] = 0; |
foreste | 1:d3366745d5ec | 196 | if(delay.read_ms() >= 400) { |
foreste | 0:f86463132427 | 197 | led[0] = led[1] = led[2] = led[3] = 1; |
foreste | 0:f86463132427 | 198 | led[4] = led[5] = led[6] = led[7] = 0; |
foreste | 1:d3366745d5ec | 199 | delay.reset(); |
foreste | 0:f86463132427 | 200 | } |
foreste | 0:f86463132427 | 201 | } |
foreste | 0:f86463132427 | 202 | } |
foreste | 0:f86463132427 | 203 | |
foreste | 1:d3366745d5ec | 204 | //Second method flashing Police sirens |
foreste | 0:f86463132427 | 205 | void police_light2() |
foreste | 0:f86463132427 | 206 | { |
foreste | 1:d3366745d5ec | 207 | if(delay.read_ms() >= 200) { |
foreste | 0:f86463132427 | 208 | pin_clear_state(); |
foreste | 0:f86463132427 | 209 | led[0] = led[2] = led[4] = led[6] = 0; |
foreste | 1:d3366745d5ec | 210 | if(delay.read_ms() >= 400) { |
foreste | 0:f86463132427 | 211 | led[0] = led[2] = led[4] = led[6] = 1; |
foreste | 0:f86463132427 | 212 | led[1] = led[3] = led[5] = led[7] = 0; |
foreste | 1:d3366745d5ec | 213 | delay.reset(); |
foreste | 0:f86463132427 | 214 | } |
foreste | 0:f86463132427 | 215 | } |
foreste | 0:f86463132427 | 216 | } |
foreste | 0:f86463132427 | 217 | |
foreste | 0:f86463132427 | 218 | |
foreste | 1:d3366745d5ec | 219 | //Fuction interruption uart |
foreste | 0:f86463132427 | 220 | void rs232in() |
foreste | 0:f86463132427 | 221 | { |
foreste | 0:f86463132427 | 222 | if(rs232.readable()) { |
foreste | 0:f86463132427 | 223 | user_input = rs232.getc(); |
foreste | 0:f86463132427 | 224 | } |
foreste | 0:f86463132427 | 225 | } |
foreste | 0:f86463132427 | 226 | int main() |
foreste | 0:f86463132427 | 227 | { |
foreste | 1:d3366745d5ec | 228 | ledintilize(); //fuction replace state pin in High |
foreste | 1:d3366745d5ec | 229 | rs232.attach(&rs232in); //Fuction interruption uart |
foreste | 0:f86463132427 | 230 | |
foreste | 0:f86463132427 | 231 | while(1) { |
foreste | 0:f86463132427 | 232 | |
foreste | 0:f86463132427 | 233 | |
foreste | 1:d3366745d5ec | 234 | //Select mode flash light for user |
foreste | 0:f86463132427 | 235 | switch(user_input) { |
foreste | 0:f86463132427 | 236 | |
foreste | 0:f86463132427 | 237 | |
foreste | 0:f86463132427 | 238 | case '1': |
foreste | 0:f86463132427 | 239 | |
foreste | 1:d3366745d5ec | 240 | delay.start(); |
foreste | 0:f86463132427 | 241 | led_knight(); |
foreste | 0:f86463132427 | 242 | break; |
foreste | 0:f86463132427 | 243 | case '2': |
foreste | 0:f86463132427 | 244 | |
foreste | 1:d3366745d5ec | 245 | delay.start(); |
foreste | 0:f86463132427 | 246 | led_knight2(); |
foreste | 0:f86463132427 | 247 | break; |
foreste | 0:f86463132427 | 248 | |
foreste | 0:f86463132427 | 249 | case '3': |
foreste | 1:d3366745d5ec | 250 | delay.start(); |
foreste | 0:f86463132427 | 251 | knight3(); |
foreste | 0:f86463132427 | 252 | break; |
foreste | 0:f86463132427 | 253 | |
foreste | 0:f86463132427 | 254 | case '4': |
foreste | 1:d3366745d5ec | 255 | delay.start(); |
foreste | 0:f86463132427 | 256 | police_light(); |
foreste | 0:f86463132427 | 257 | break; |
foreste | 0:f86463132427 | 258 | |
foreste | 0:f86463132427 | 259 | case '5': |
foreste | 1:d3366745d5ec | 260 | delay.start(); |
foreste | 0:f86463132427 | 261 | police_light2(); |
foreste | 0:f86463132427 | 262 | break; |
foreste | 0:f86463132427 | 263 | |
foreste | 0:f86463132427 | 264 | } |
foreste | 0:f86463132427 | 265 | } |
foreste | 0:f86463132427 | 266 | } |