![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
06.03.2018
Fork of Alarmsystem_med_matrix_log_funker_ikke by
main.cpp@16:3d2e29aa5df5, 2018-02-21 (annotated)
- Committer:
- andreped
- Date:
- Wed Feb 21 16:00:43 2018 +0000
- Revision:
- 16:3d2e29aa5df5
- Parent:
- 15:272fb4e610dc
- Child:
- 17:f046c0ca9edd
alt utenom log;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andreped | 0:33bf28fd1ac6 | 1 | #include "mbed.h" |
andreped | 12:0193024eab13 | 2 | #include "rtos.h" |
andreped | 15:272fb4e610dc | 3 | #include <string> |
andreped | 16:3d2e29aa5df5 | 4 | #include <stdio.h> |
andreped | 12:0193024eab13 | 5 | |
Banyrud | 13:0622c228b67d | 6 | /////DEFINISJONER///// |
andreped | 6:a3b97bf7902b | 7 | #define Inn 4 |
Banyrud | 13:0622c228b67d | 8 | Serial pc(USBTX, USBRX); |
andreped | 16:3d2e29aa5df5 | 9 | LocalFileSystem local("local"); |
andreped | 16:3d2e29aa5df5 | 10 | FILE *fp = fopen("/local/log.txt", "w" ) ; |
andreped | 10:f0a6d74095d4 | 11 | |
Banyrud | 13:0622c228b67d | 12 | /////INNGANGER///// |
andreped | 0:33bf28fd1ac6 | 13 | DigitalIn Servicebryter(p20); |
andreped | 12:0193024eab13 | 14 | AnalogIn _Sone1(p15); |
andreped | 12:0193024eab13 | 15 | AnalogIn _Sone2(p16); |
andreped | 12:0193024eab13 | 16 | AnalogIn _Sone3(p17); |
andreped | 12:0193024eab13 | 17 | AnalogIn _Sone4(p19); |
Banyrud | 13:0622c228b67d | 18 | |
Banyrud | 13:0622c228b67d | 19 | /////UTGANGER///// |
Banyrud | 13:0622c228b67d | 20 | DigitalOut Driftslys(LED1); |
Banyrud | 13:0622c228b67d | 21 | BusOut AlarmUtganger1(p21, p22, p23, p24); //1 - Dør 1 Sone 1, 2 - Dør 1 Sone 2, 4 - Dør 1 Sone 3, 8 - Dør 1 Sone 4 |
Banyrud | 13:0622c228b67d | 22 | BusOut AlarmUtganger2(p25, p26, p27, p28); //1 - Dør 2 Sone 1, 2 - Dør 2 Sone 2, 4 - Dør 2 Sone 3, 8 - Dør 2 Sone 4 |
Banyrud | 13:0622c228b67d | 23 | |
Banyrud | 13:0622c228b67d | 24 | /////VARIABLER///// |
andreped | 12:0193024eab13 | 25 | float Soner[Inn]; |
andreped | 12:0193024eab13 | 26 | int tilstand = 0; |
andreped | 12:0193024eab13 | 27 | volatile bool running = true; |
andreped | 15:272fb4e610dc | 28 | volatile bool admin = false; |
Banyrud | 14:8e7aaf0cf5d7 | 29 | string password = ("admin"); |
andreped | 16:3d2e29aa5df5 | 30 | Timer t; |
andreped | 1:6917ec4f5e6d | 31 | |
Banyrud | 13:0622c228b67d | 32 | /////FUNKSJON DEKLARASJONER///// |
Banyrud | 13:0622c228b67d | 33 | void Alarmf(); |
andreped | 15:272fb4e610dc | 34 | Thread Alarm; |
andreped | 16:3d2e29aa5df5 | 35 | int TilstandSjekk(int, int); |
andreped | 15:272fb4e610dc | 36 | void admin_access_granted(); |
andreped | 15:272fb4e610dc | 37 | void sone_monitor(); |
andreped | 15:272fb4e610dc | 38 | void password_check(); |
andreped | 16:3d2e29aa5df5 | 39 | void read_keyinput(); |
andreped | 16:3d2e29aa5df5 | 40 | void request_status(); |
andreped | 16:3d2e29aa5df5 | 41 | void request_status_sone_dor(int sone, int dor); |
andreped | 16:3d2e29aa5df5 | 42 | extern "C" void mbed_reset(); |
andreped | 16:3d2e29aa5df5 | 43 | int log_to_file(int, int); |
andreped | 12:0193024eab13 | 44 | |
Banyrud | 13:0622c228b67d | 45 | /////MAIN///// |
andreped | 1:6917ec4f5e6d | 46 | int main() |
andreped | 1:6917ec4f5e6d | 47 | { |
andreped | 16:3d2e29aa5df5 | 48 | t.start(); |
andreped | 15:272fb4e610dc | 49 | while(1) { |
andreped | 15:272fb4e610dc | 50 | if(admin == false) { |
andreped | 15:272fb4e610dc | 51 | password_check(); |
andreped | 15:272fb4e610dc | 52 | } |
andreped | 16:3d2e29aa5df5 | 53 | if(Servicebryter == 1) { |
andreped | 16:3d2e29aa5df5 | 54 | AlarmUtganger1 = 0; |
andreped | 16:3d2e29aa5df5 | 55 | AlarmUtganger2 = 0; |
andreped | 16:3d2e29aa5df5 | 56 | running = false; |
andreped | 16:3d2e29aa5df5 | 57 | pc.printf("NB! Alle alarmer er i innelast modus i 15 sek!! \n"); |
andreped | 16:3d2e29aa5df5 | 58 | wait(10); |
andreped | 16:3d2e29aa5df5 | 59 | pc.printf("NB! Alarmer gar pa om 5 sek! \n"); |
andreped | 16:3d2e29aa5df5 | 60 | wait(5); |
andreped | 16:3d2e29aa5df5 | 61 | } |
andreped | 16:3d2e29aa5df5 | 62 | while((admin) && (Servicebryter == 0)) { |
andreped | 15:272fb4e610dc | 63 | Alarm.start(Alarmf); |
andreped | 15:272fb4e610dc | 64 | sone_monitor(); |
Banyrud | 14:8e7aaf0cf5d7 | 65 | read_keyinput(); |
andreped | 16:3d2e29aa5df5 | 66 | } |
andreped | 16:3d2e29aa5df5 | 67 | } |
andreped | 15:272fb4e610dc | 68 | } |
Banyrud | 14:8e7aaf0cf5d7 | 69 | |
Banyrud | 13:0622c228b67d | 70 | /////FUNKSJONER///// |
Banyrud | 14:8e7aaf0cf5d7 | 71 | void Alarmf(void) |
Banyrud | 14:8e7aaf0cf5d7 | 72 | { |
andreped | 15:272fb4e610dc | 73 | while(1) { |
andreped | 15:272fb4e610dc | 74 | if(running == true) { |
andreped | 15:272fb4e610dc | 75 | Driftslys = !Driftslys; |
andreped | 15:272fb4e610dc | 76 | Thread::wait(50); |
andreped | 15:272fb4e610dc | 77 | } else { |
andreped | 15:272fb4e610dc | 78 | Driftslys = 1; |
andreped | 15:272fb4e610dc | 79 | } |
andreped | 15:272fb4e610dc | 80 | } |
Banyrud | 13:0622c228b67d | 81 | } |
Banyrud | 13:0622c228b67d | 82 | |
andreped | 11:dad1798ed88f | 83 | int TilstandSjekk(int i, int tilstand) |
andreped | 11:dad1798ed88f | 84 | { |
andreped | 16:3d2e29aa5df5 | 85 | log_to_file(i, tilstand); |
andreped | 11:dad1798ed88f | 86 | if(tilstand == 0) { |
andreped | 15:272fb4e610dc | 87 | running = false; |
andreped | 16:3d2e29aa5df5 | 88 | double n = 2; |
andreped | 11:dad1798ed88f | 89 | double Y = 1; |
andreped | 11:dad1798ed88f | 90 | for(int k=1; k<=i; k++) Y = Y*n; |
andreped | 12:0193024eab13 | 91 | if ( Y == AlarmUtganger1) AlarmUtganger1 = AlarmUtganger1 - Y; |
andreped | 12:0193024eab13 | 92 | if ( Y == AlarmUtganger2) AlarmUtganger2 = AlarmUtganger2 - Y; |
andreped | 11:dad1798ed88f | 93 | } |
andreped | 11:dad1798ed88f | 94 | if(tilstand == 1) { //Hvis dør 1 er åpen... |
andreped | 15:272fb4e610dc | 95 | running = true; |
andreped | 16:3d2e29aa5df5 | 96 | double n = 2; |
andreped | 11:dad1798ed88f | 97 | double Y = 1; |
andreped | 11:dad1798ed88f | 98 | for(int k=1; k<=i; k++) Y = Y*n; |
andreped | 11:dad1798ed88f | 99 | if( Y == AlarmUtganger2 ) AlarmUtganger2 = AlarmUtganger2 - Y; |
andreped | 11:dad1798ed88f | 100 | if( Y!=AlarmUtganger1) AlarmUtganger1 = Y + AlarmUtganger1; //Sjekk om denne verdien har blitt sjekket før, hvis ikke starter alarm på neste dør. |
andreped | 11:dad1798ed88f | 101 | } |
andreped | 11:dad1798ed88f | 102 | if(tilstand == 2) { //Hvis dør 2 er åpen... |
andreped | 15:272fb4e610dc | 103 | running = true; |
andreped | 16:3d2e29aa5df5 | 104 | double n = 2; |
andreped | 11:dad1798ed88f | 105 | double Y = 1; |
andreped | 11:dad1798ed88f | 106 | for(int k=1; k<=i; k++) Y = Y*n; |
andreped | 11:dad1798ed88f | 107 | if( Y == AlarmUtganger1 ) AlarmUtganger1 = AlarmUtganger1 - Y; |
andreped | 11:dad1798ed88f | 108 | if( Y!=AlarmUtganger2) AlarmUtganger2 = Y + AlarmUtganger2; //Sjekk om denne verdien har blitt sjekket før, hvis ikke starter alarm på neste dør. |
andreped | 11:dad1798ed88f | 109 | } |
andreped | 11:dad1798ed88f | 110 | if(tilstand == 3) { //Hvis begge dører er åpne |
andreped | 15:272fb4e610dc | 111 | running = true; |
andreped | 11:dad1798ed88f | 112 | double n = 2; // We want 2^i |
andreped | 11:dad1798ed88f | 113 | double Y = 1; |
andreped | 11:dad1798ed88f | 114 | for(int k=1; k<=i; k++) Y = Y*n; |
andreped | 11:dad1798ed88f | 115 | if( Y!=AlarmUtganger1 ) AlarmUtganger1 = Y + AlarmUtganger1; |
andreped | 11:dad1798ed88f | 116 | if( Y!=AlarmUtganger2 ) AlarmUtganger2 = Y + AlarmUtganger2; |
andreped | 11:dad1798ed88f | 117 | } |
andreped | 11:dad1798ed88f | 118 | return 0; |
andreped | 10:f0a6d74095d4 | 119 | } |
andreped | 16:3d2e29aa5df5 | 120 | |
andreped | 16:3d2e29aa5df5 | 121 | int log_to_file(int i, int tilstand) |
andreped | 16:3d2e29aa5df5 | 122 | { |
andreped | 16:3d2e29aa5df5 | 123 | int p; |
andreped | 16:3d2e29aa5df5 | 124 | FILE *fp = fopen("/local/log.txt", "w" ) ; |
andreped | 16:3d2e29aa5df5 | 125 | if(fp != NULL) { |
andreped | 16:3d2e29aa5df5 | 126 | if ((tilstand == 1) && (p != 1)) { |
andreped | 16:3d2e29aa5df5 | 127 | fprintf(fp,"Sone %d, dor 1 er aapen \n",i); |
andreped | 16:3d2e29aa5df5 | 128 | fprintf(fp,"Tid:%f siden start \n",t.read()); |
andreped | 16:3d2e29aa5df5 | 129 | p = 1; |
andreped | 16:3d2e29aa5df5 | 130 | } |
andreped | 16:3d2e29aa5df5 | 131 | |
andreped | 16:3d2e29aa5df5 | 132 | if ((tilstand == 2) && (p != 2)) { |
andreped | 16:3d2e29aa5df5 | 133 | fprintf(fp,"Sone %d, dor 2 er lukket \n",i); |
andreped | 16:3d2e29aa5df5 | 134 | fprintf(fp,"Tid:%f siden start \n",t.read()); |
andreped | 16:3d2e29aa5df5 | 135 | p = 2; |
andreped | 16:3d2e29aa5df5 | 136 | } |
andreped | 16:3d2e29aa5df5 | 137 | if ((tilstand == 3) && (p != 3)) { |
andreped | 16:3d2e29aa5df5 | 138 | fprintf(fp,"Sone %d dor 1 og 2 er lukket \n",i); |
andreped | 16:3d2e29aa5df5 | 139 | fprintf(fp,"Tid:%f siden start \n",t.read()); |
andreped | 16:3d2e29aa5df5 | 140 | p = 3; |
andreped | 16:3d2e29aa5df5 | 141 | } |
andreped | 16:3d2e29aa5df5 | 142 | } |
andreped | 16:3d2e29aa5df5 | 143 | return 0; |
andreped | 16:3d2e29aa5df5 | 144 | } |
andreped | 16:3d2e29aa5df5 | 145 | |
andreped | 15:272fb4e610dc | 146 | void sone_monitor() |
Banyrud | 14:8e7aaf0cf5d7 | 147 | { |
Banyrud | 14:8e7aaf0cf5d7 | 148 | Soner[0] = _Sone1; |
Banyrud | 14:8e7aaf0cf5d7 | 149 | //Soner[1] = _Sone2; |
Banyrud | 14:8e7aaf0cf5d7 | 150 | //Soner[2] = _Sone3; |
Banyrud | 14:8e7aaf0cf5d7 | 151 | //Soner[3] = _Sone4; |
Banyrud | 14:8e7aaf0cf5d7 | 152 | for(int i=0; i < Inn; i++) { |
Banyrud | 14:8e7aaf0cf5d7 | 153 | if((Soner[i] > 0.75 )&&( Soner[i] < 0.83)) { //Tilstand1, Dør 1 åpen |
Banyrud | 14:8e7aaf0cf5d7 | 154 | //pc.printf("Vi er i Tilstand1\n"); |
Banyrud | 14:8e7aaf0cf5d7 | 155 | tilstand = 1; |
Banyrud | 14:8e7aaf0cf5d7 | 156 | TilstandSjekk(i, tilstand); |
Banyrud | 14:8e7aaf0cf5d7 | 157 | } |
Banyrud | 14:8e7aaf0cf5d7 | 158 | if((Soner[i] > 0.55 )&&( Soner[i] < 0.59)) { //Normaldrift |
Banyrud | 14:8e7aaf0cf5d7 | 159 | tilstand = 0; |
Banyrud | 14:8e7aaf0cf5d7 | 160 | TilstandSjekk(i, tilstand); |
Banyrud | 14:8e7aaf0cf5d7 | 161 | } |
andreped | 16:3d2e29aa5df5 | 162 | if((Soner[i] > 0.65 )&&( Soner[i] < 0.69)) { //Tilstand2, Dør 2 åpen |
Banyrud | 14:8e7aaf0cf5d7 | 163 | tilstand = 2; |
Banyrud | 14:8e7aaf0cf5d7 | 164 | TilstandSjekk(i, tilstand); |
Banyrud | 14:8e7aaf0cf5d7 | 165 | } |
andreped | 16:3d2e29aa5df5 | 166 | if(Soner[i] >= 1) { //Tilstand 3, Begge dører er åpen |
Banyrud | 14:8e7aaf0cf5d7 | 167 | tilstand = 3; |
Banyrud | 14:8e7aaf0cf5d7 | 168 | TilstandSjekk(i, tilstand); |
Banyrud | 14:8e7aaf0cf5d7 | 169 | } |
Banyrud | 14:8e7aaf0cf5d7 | 170 | } |
Banyrud | 14:8e7aaf0cf5d7 | 171 | } |
andreped | 15:272fb4e610dc | 172 | void admin_access_granted() |
andreped | 15:272fb4e610dc | 173 | { |
andreped | 15:272fb4e610dc | 174 | admin = true; |
andreped | 15:272fb4e610dc | 175 | pc.printf("Admin access granted\n");//info |
andreped | 15:272fb4e610dc | 176 | pc.printf("Folgende kommandoer kan brukes: \n");// liste over funksjoner tilgjenglig. |
andreped | 15:272fb4e610dc | 177 | pc.printf("Status sone og dor(sone forst, dor etter) Kommando: 1,2 \n");// funksjon 1 - request spesific status |
andreped | 15:272fb4e610dc | 178 | pc.printf("Status alle soner og dorer. Kommando: Status \n");// funksjon 1 - request status all |
andreped | 15:272fb4e610dc | 179 | pc.printf("Reset hele systemet. Kommando: 'Reset' \n");// funksjon 4 - request log all |
andreped | 15:272fb4e610dc | 180 | } |
andreped | 16:3d2e29aa5df5 | 181 | |
andreped | 15:272fb4e610dc | 182 | void password_check() |
Banyrud | 14:8e7aaf0cf5d7 | 183 | { |
andreped | 15:272fb4e610dc | 184 | if(pc.readable() == 1) { |
andreped | 15:272fb4e610dc | 185 | pc.printf("Write password: "); |
andreped | 15:272fb4e610dc | 186 | string b; |
andreped | 15:272fb4e610dc | 187 | char s; |
andreped | 15:272fb4e610dc | 188 | for(int i = 0; i < 5; i++) { |
andreped | 15:272fb4e610dc | 189 | s = pc.getc(); |
andreped | 15:272fb4e610dc | 190 | pc.printf("%c", s); |
andreped | 15:272fb4e610dc | 191 | b += s; |
andreped | 15:272fb4e610dc | 192 | if(b == password) { |
andreped | 15:272fb4e610dc | 193 | admin_access_granted(); |
andreped | 15:272fb4e610dc | 194 | } |
andreped | 15:272fb4e610dc | 195 | } |
andreped | 15:272fb4e610dc | 196 | } |
Banyrud | 14:8e7aaf0cf5d7 | 197 | } |
Banyrud | 14:8e7aaf0cf5d7 | 198 | |
andreped | 16:3d2e29aa5df5 | 199 | void read_keyinput() |
andreped | 1:6917ec4f5e6d | 200 | { |
andreped | 15:272fb4e610dc | 201 | if(pc.readable() == 1) { |
andreped | 16:3d2e29aa5df5 | 202 | pc.printf("\nDu skriver: "); |
andreped | 15:272fb4e610dc | 203 | string read; |
andreped | 15:272fb4e610dc | 204 | char c; |
andreped | 16:3d2e29aa5df5 | 205 | for(int i = 0; i < 6; i++) { |
andreped | 15:272fb4e610dc | 206 | c = pc.getc(); |
andreped | 16:3d2e29aa5df5 | 207 | pc.printf("%c ", c); |
andreped | 15:272fb4e610dc | 208 | read += c; |
andreped | 16:3d2e29aa5df5 | 209 | if( read == ("1, 1")) request_status_sone_dor(1, 1); |
andreped | 16:3d2e29aa5df5 | 210 | if( read == ("1, 2")) request_status_sone_dor(1, 2); |
andreped | 16:3d2e29aa5df5 | 211 | if( read == ("2, 1")) request_status_sone_dor(2, 1); |
andreped | 16:3d2e29aa5df5 | 212 | if( read == ("2, 2")) request_status_sone_dor(2, 2); |
andreped | 16:3d2e29aa5df5 | 213 | if( read == ("3, 1")) request_status_sone_dor(3, 1); |
andreped | 16:3d2e29aa5df5 | 214 | if( read == ("3, 2")) request_status_sone_dor(3, 2); |
andreped | 16:3d2e29aa5df5 | 215 | if( read == ("4, 1")) request_status_sone_dor(4, 1); |
andreped | 16:3d2e29aa5df5 | 216 | if( read == ("4, 2")) request_status_sone_dor(4, 2); |
andreped | 16:3d2e29aa5df5 | 217 | if( read == ("Status")) request_status(); |
andreped | 16:3d2e29aa5df5 | 218 | if( read == ("Stop")) while(1){ fclose(fp); } |
andreped | 16:3d2e29aa5df5 | 219 | if( read == ("Reset")) { |
andreped | 16:3d2e29aa5df5 | 220 | fclose(fp); |
andreped | 16:3d2e29aa5df5 | 221 | mbed_reset(); |
andreped | 15:272fb4e610dc | 222 | } |
andreped | 15:272fb4e610dc | 223 | } |
andreped | 15:272fb4e610dc | 224 | } |
andreped | 15:272fb4e610dc | 225 | } |
andreped | 15:272fb4e610dc | 226 | |
andreped | 16:3d2e29aa5df5 | 227 | |
andreped | 15:272fb4e610dc | 228 | void request_status_sone_dor(int sone, int dor) |
andreped | 15:272fb4e610dc | 229 | { |
andreped | 16:3d2e29aa5df5 | 230 | int n = 2; |
andreped | 16:3d2e29aa5df5 | 231 | for(int i = 0; i <= 16;) { |
andreped | 16:3d2e29aa5df5 | 232 | if(i == 0) i = 1; |
andreped | 16:3d2e29aa5df5 | 233 | else i = i * n; |
andreped | 16:3d2e29aa5df5 | 234 | if(sone == i) { |
andreped | 16:3d2e29aa5df5 | 235 | if(dor == 1) { |
andreped | 16:3d2e29aa5df5 | 236 | if(AlarmUtganger1 == sone) pc.printf("Dor: %d i sone: %d er apnet \n", dor, sone); |
andreped | 16:3d2e29aa5df5 | 237 | else pc.printf("Dor: %d i sone: %d er lukket \n", dor, sone); |
andreped | 16:3d2e29aa5df5 | 238 | } |
andreped | 16:3d2e29aa5df5 | 239 | if(dor == 2) { |
andreped | 16:3d2e29aa5df5 | 240 | if(AlarmUtganger2 == sone) pc.printf("Dor: %d i sone: %d er apnet \n", dor, sone); |
andreped | 16:3d2e29aa5df5 | 241 | else pc.printf("Dor: %d i sone: %d er lukket \n", dor, sone); |
andreped | 16:3d2e29aa5df5 | 242 | } |
andreped | 16:3d2e29aa5df5 | 243 | } |
andreped | 15:272fb4e610dc | 244 | } |
andreped | 15:272fb4e610dc | 245 | } |
andreped | 15:272fb4e610dc | 246 | |
andreped | 16:3d2e29aa5df5 | 247 | void request_status() |
andreped | 16:3d2e29aa5df5 | 248 | { |
andreped | 16:3d2e29aa5df5 | 249 | int sone = 0; |
andreped | 16:3d2e29aa5df5 | 250 | int n = 2; |
andreped | 16:3d2e29aa5df5 | 251 | for(int i = 0; i <= 8;) { |
andreped | 16:3d2e29aa5df5 | 252 | sone = sone + sone; |
andreped | 16:3d2e29aa5df5 | 253 | if(i == 0) i = 1; |
andreped | 16:3d2e29aa5df5 | 254 | else i = i*n; |
andreped | 16:3d2e29aa5df5 | 255 | if(AlarmUtganger1 == i) pc.printf("Dor: 1 i Sone: %d er apent \n", sone); |
andreped | 16:3d2e29aa5df5 | 256 | else pc.printf("Dor: 1 i Sone: %d er lukket \n", i); |
andreped | 16:3d2e29aa5df5 | 257 | if(AlarmUtganger2 == i) pc.printf("Dor: 2 i Sone: %d er apent \n", sone); |
andreped | 16:3d2e29aa5df5 | 258 | else pc.printf("Dor: 2 i Sone: %d er lukket \n", i); |
andreped | 16:3d2e29aa5df5 | 259 | } |
andreped | 16:3d2e29aa5df5 | 260 | } |