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