Completo
Dependencies: mbed Servo KeypadLib TextLCD
Cassaforte.cpp@2:ca52e00afb99, 2018-12-10 (annotated)
- Committer:
- Virgi97
- Date:
- Mon Dec 10 19:50:47 2018 +0000
- Revision:
- 2:ca52e00afb99
- Parent:
- 1:0661b733846e
Da limare
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Virgi97 | 1:0661b733846e | 1 | #include "Cassaforte.h" |
Virgi97 | 1:0661b733846e | 2 | #include "TextLCD.h" |
Virgi97 | 1:0661b733846e | 3 | #include "Servo.h" |
Virgi97 | 1:0661b733846e | 4 | #include "keypad.h" |
Virgi97 | 1:0661b733846e | 5 | I2C i2c(D14, D15); |
Virgi97 | 1:0661b733846e | 6 | TextLCD_I2C lcd(&i2c, 0x4E, TextLCD_I2C::LCD16x2, TextLCD_I2C::HD44780); //comunicazione Nucleo-display |
Virgi97 | 1:0661b733846e | 7 | Keypad keypad(D6,D5,D4,D3,D10,D9,D8,D7); //porte del tastierino |
Virgi97 | 1:0661b733846e | 8 | Servo servo(D11); //porta servo |
Virgi97 | 1:0661b733846e | 9 | //PwmOut buzzer(D2); |
Virgi97 | 1:0661b733846e | 10 | //Ticker t; |
Virgi97 | 1:0661b733846e | 11 | Timeout t1; |
Virgi97 | 1:0661b733846e | 12 | DigitalOut led_verde(D13); //led apertura |
Virgi97 | 1:0661b733846e | 13 | DigitalOut led_rosso(D12); //led chiusura |
Virgi97 | 1:0661b733846e | 14 | DigitalOut buz(D2); //porta buz |
Virgi97 | 2:ca52e00afb99 | 15 | DigitalIn mag(PC_10); //porta del magnete |
Virgi97 | 1:0661b733846e | 16 | int dim=4; |
Virgi97 | 1:0661b733846e | 17 | //int buz=0; |
Virgi97 | 1:0661b733846e | 18 | char pass[4]={'1','2','3','4'};//password di default (è possibile cambiarla dal menu) |
Virgi97 | 1:0661b733846e | 19 | bool alonoff=true; //variabile booleana per abilitare o disabilitare l'allarme in caso di apertura e chiusura (?) |
Virgi97 | 1:0661b733846e | 20 | bool jump = false; |
Virgi97 | 1:0661b733846e | 21 | |
Virgi97 | 1:0661b733846e | 22 | void inizializza(){ |
Virgi97 | 1:0661b733846e | 23 | lcd.setMode(TextLCD_I2C::DispOn); |
Virgi97 | 1:0661b733846e | 24 | lcd.setBacklight(TextLCD_I2C::LightOff); |
Virgi97 | 1:0661b733846e | 25 | buz=0; |
Virgi97 | 1:0661b733846e | 26 | |
Virgi97 | 2:ca52e00afb99 | 27 | if(mag == 0){ |
Virgi97 | 1:0661b733846e | 28 | chiuso(); |
Virgi97 | 1:0661b733846e | 29 | //wait(0.3); |
Virgi97 | 2:ca52e00afb99 | 30 | } else { |
Virgi97 | 2:ca52e00afb99 | 31 | aperto(); |
Virgi97 | 2:ca52e00afb99 | 32 | alonoff = false; |
Virgi97 | 2:ca52e00afb99 | 33 | } |
Virgi97 | 1:0661b733846e | 34 | //lcd.setCursor(TextLCD_I2C::CurOn_BlkOn); |
Virgi97 | 1:0661b733846e | 35 | } |
Virgi97 | 1:0661b733846e | 36 | |
Virgi97 | 1:0661b733846e | 37 | void scrivi(char parola[]){ |
Virgi97 | 1:0661b733846e | 38 | lcd.cls(); |
Virgi97 | 1:0661b733846e | 39 | lcd.printf("%s\n", parola); |
Virgi97 | 1:0661b733846e | 40 | } |
Virgi97 | 1:0661b733846e | 41 | |
Virgi97 | 1:0661b733846e | 42 | void chiuso(){ |
Virgi97 | 1:0661b733846e | 43 | servo.position(90); |
Virgi97 | 1:0661b733846e | 44 | wait(0.3); |
Virgi97 | 1:0661b733846e | 45 | led_rosso = 1; |
Virgi97 | 1:0661b733846e | 46 | led_verde = 0; |
Virgi97 | 1:0661b733846e | 47 | } |
Virgi97 | 1:0661b733846e | 48 | |
Virgi97 | 1:0661b733846e | 49 | void aperto(){ |
Virgi97 | 1:0661b733846e | 50 | servo.position(-90); |
Virgi97 | 1:0661b733846e | 51 | wait(0.3); |
Virgi97 | 1:0661b733846e | 52 | led_rosso = 0; |
Virgi97 | 1:0661b733846e | 53 | led_verde = 1; |
Virgi97 | 1:0661b733846e | 54 | } |
Virgi97 | 1:0661b733846e | 55 | void inserimento(char a[],int n,bool vis){ |
Virgi97 | 1:0661b733846e | 56 | int i=0; |
Virgi97 | 1:0661b733846e | 57 | jump = false; |
Virgi97 | 1:0661b733846e | 58 | /*Timer t; |
Virgi97 | 1:0661b733846e | 59 | t.reset(); |
Virgi97 | 1:0661b733846e | 60 | t.start();*/ |
Virgi97 | 1:0661b733846e | 61 | t1.attach(&salta, 20); |
Virgi97 | 1:0661b733846e | 62 | do{ |
Virgi97 | 1:0661b733846e | 63 | keypad.enablePullUp(); |
Virgi97 | 1:0661b733846e | 64 | char val = keypad.getKey(); |
Virgi97 | 1:0661b733846e | 65 | if(/*t.read()>20*/jump == true && buz==0){ //CHIUSURA dopo 20 secondi dal mancato inserimento |
Virgi97 | 1:0661b733846e | 66 | main(); |
Virgi97 | 1:0661b733846e | 67 | } |
Virgi97 | 1:0661b733846e | 68 | if(val != KEY_RELEASED){ |
Virgi97 | 1:0661b733846e | 69 | if(vis==true){ |
Virgi97 | 1:0661b733846e | 70 | lcd.putc(val); |
Virgi97 | 1:0661b733846e | 71 | lcd.setAddress(i+1,1); |
Virgi97 | 1:0661b733846e | 72 | } |
Virgi97 | 1:0661b733846e | 73 | a[i]=val; |
Virgi97 | 1:0661b733846e | 74 | i++; |
Virgi97 | 1:0661b733846e | 75 | wait(0.4); |
Virgi97 | 1:0661b733846e | 76 | |
Virgi97 | 1:0661b733846e | 77 | } |
Virgi97 | 1:0661b733846e | 78 | }while(i<n); |
Virgi97 | 1:0661b733846e | 79 | |
Virgi97 | 1:0661b733846e | 80 | } |
Virgi97 | 1:0661b733846e | 81 | |
Virgi97 | 1:0661b733846e | 82 | bool confronto(char v[],int dim){ |
Virgi97 | 1:0661b733846e | 83 | int cont=0; |
Virgi97 | 1:0661b733846e | 84 | for(int i=0;i<dim;i++){ |
Virgi97 | 1:0661b733846e | 85 | if (v[i]==pass[i]){ |
Virgi97 | 1:0661b733846e | 86 | cont++; |
Virgi97 | 1:0661b733846e | 87 | } |
Virgi97 | 1:0661b733846e | 88 | } |
Virgi97 | 1:0661b733846e | 89 | if (cont==4){ |
Virgi97 | 1:0661b733846e | 90 | return true; |
Virgi97 | 1:0661b733846e | 91 | } |
Virgi97 | 1:0661b733846e | 92 | else{ |
Virgi97 | 1:0661b733846e | 93 | return false; |
Virgi97 | 1:0661b733846e | 94 | } |
Virgi97 | 1:0661b733846e | 95 | } |
Virgi97 | 1:0661b733846e | 96 | |
Virgi97 | 1:0661b733846e | 97 | void menu(){ |
Virgi97 | 1:0661b733846e | 98 | scrivi("A Apri B BloccaC Cambia codice"); |
Virgi97 | 1:0661b733846e | 99 | char val; |
Virgi97 | 1:0661b733846e | 100 | do{ |
Virgi97 | 1:0661b733846e | 101 | inserimento(&val,1,false); |
Virgi97 | 1:0661b733846e | 102 | |
Virgi97 | 1:0661b733846e | 103 | }while(val!='A' && val!='B' && val!='C'); |
Virgi97 | 1:0661b733846e | 104 | |
Virgi97 | 1:0661b733846e | 105 | if(val=='A'){ |
Virgi97 | 1:0661b733846e | 106 | //if(mag==1){ |
Virgi97 | 1:0661b733846e | 107 | scrivi("Apertura..."); |
Virgi97 | 1:0661b733846e | 108 | alonoff=false; |
Virgi97 | 1:0661b733846e | 109 | aperto(); |
Virgi97 | 1:0661b733846e | 110 | wait(0.3); //2 |
Virgi97 | 1:0661b733846e | 111 | menu(); |
Virgi97 | 1:0661b733846e | 112 | // } |
Virgi97 | 1:0661b733846e | 113 | } |
Virgi97 | 1:0661b733846e | 114 | if(val=='B'){ |
Virgi97 | 1:0661b733846e | 115 | if(mag==0){ |
Virgi97 | 1:0661b733846e | 116 | scrivi("Chiusura..."); |
Virgi97 | 1:0661b733846e | 117 | alonoff=true; |
Virgi97 | 1:0661b733846e | 118 | chiuso(); |
Virgi97 | 1:0661b733846e | 119 | wait(0.3); //2 |
Virgi97 | 1:0661b733846e | 120 | main(); |
Virgi97 | 2:ca52e00afb99 | 121 | }else{ |
Virgi97 | 2:ca52e00afb99 | 122 | scrivi("Chiudere la porta"); |
Virgi97 | 2:ca52e00afb99 | 123 | wait(1); |
Virgi97 | 2:ca52e00afb99 | 124 | menu(); |
Virgi97 | 2:ca52e00afb99 | 125 | } |
Virgi97 | 1:0661b733846e | 126 | } |
Virgi97 | 1:0661b733846e | 127 | if(val=='C'){ |
Virgi97 | 1:0661b733846e | 128 | char v[dim]; |
Virgi97 | 1:0661b733846e | 129 | scrivi("Nuovo codice:"); |
Virgi97 | 1:0661b733846e | 130 | inserimento(v,dim,true); |
Virgi97 | 1:0661b733846e | 131 | scrivi("Codice: "); |
Virgi97 | 1:0661b733846e | 132 | for(int i=0;i<4;i++){ |
Virgi97 | 1:0661b733846e | 133 | lcd.setAddress(i+12,0); |
Virgi97 | 1:0661b733846e | 134 | lcd.putc(v[i]); |
Virgi97 | 1:0661b733846e | 135 | } |
Virgi97 | 1:0661b733846e | 136 | lcd.printf("A:ok C:annulla"); |
Virgi97 | 1:0661b733846e | 137 | do{ |
Virgi97 | 1:0661b733846e | 138 | inserimento(&val,1,false); |
Virgi97 | 1:0661b733846e | 139 | if(val=='A'){ |
Virgi97 | 1:0661b733846e | 140 | scrivi("Codice cambiato."); |
Virgi97 | 1:0661b733846e | 141 | wait(1); |
Virgi97 | 1:0661b733846e | 142 | for(int k=0;k<dim;k++){ |
Virgi97 | 1:0661b733846e | 143 | pass[k]=v[k]; |
Virgi97 | 1:0661b733846e | 144 | } |
Virgi97 | 1:0661b733846e | 145 | main(); |
Virgi97 | 1:0661b733846e | 146 | } |
Virgi97 | 1:0661b733846e | 147 | if(val=='C'){ |
Virgi97 | 1:0661b733846e | 148 | menu(); |
Virgi97 | 1:0661b733846e | 149 | } |
Virgi97 | 1:0661b733846e | 150 | |
Virgi97 | 1:0661b733846e | 151 | }while(1); |
Virgi97 | 1:0661b733846e | 152 | } |
Virgi97 | 1:0661b733846e | 153 | } |
Virgi97 | 1:0661b733846e | 154 | |
Virgi97 | 1:0661b733846e | 155 | void allarme(){ |
Virgi97 | 1:0661b733846e | 156 | bool esci = true; |
Virgi97 | 1:0661b733846e | 157 | char insa[4]; |
Virgi97 | 1:0661b733846e | 158 | //t.attach(&suona, 1); |
Virgi97 | 1:0661b733846e | 159 | do{ |
Virgi97 | 1:0661b733846e | 160 | buz=1; |
Virgi97 | 1:0661b733846e | 161 | scrivi(" Allarme!"); |
Virgi97 | 1:0661b733846e | 162 | inserimento(insa,4,true); |
Virgi97 | 1:0661b733846e | 163 | |
Virgi97 | 1:0661b733846e | 164 | |
Virgi97 | 1:0661b733846e | 165 | if(confronto(insa,4)==true){ |
Virgi97 | 1:0661b733846e | 166 | buz=0; |
Virgi97 | 1:0661b733846e | 167 | esci = false; |
Virgi97 | 1:0661b733846e | 168 | //t.detach; |
Virgi97 | 1:0661b733846e | 169 | } |
Virgi97 | 1:0661b733846e | 170 | }while(esci); |
Virgi97 | 1:0661b733846e | 171 | //scrivi("Attendere..."); |
Virgi97 | 1:0661b733846e | 172 | t1.attach(&salta, 0.5); |
Virgi97 | 1:0661b733846e | 173 | } |
Virgi97 | 1:0661b733846e | 174 | |
Virgi97 | 1:0661b733846e | 175 | void controllo(){ |
Virgi97 | 1:0661b733846e | 176 | if (alonoff == true){ |
Virgi97 | 1:0661b733846e | 177 | allarme(); |
Virgi97 | 1:0661b733846e | 178 | } |
Virgi97 | 1:0661b733846e | 179 | } |
Virgi97 | 1:0661b733846e | 180 | |
Virgi97 | 1:0661b733846e | 181 | /*void suona (float freq1, float freq2, float t1, float t2 float v[]){ |
Virgi97 | 1:0661b733846e | 182 | buzzer.period_ms(3); |
Virgi97 | 1:0661b733846e | 183 | buzzer.write(0.1); |
Virgi97 | 1:0661b733846e | 184 | wait(0.5); |
Virgi97 | 1:0661b733846e | 185 | buzzer.write(0.5); |
Virgi97 | 1:0661b733846e | 186 | wait(0.5); |
Virgi97 | 1:0661b733846e | 187 | }*/ |
Virgi97 | 1:0661b733846e | 188 | |
Virgi97 | 1:0661b733846e | 189 | void salta(){ |
Virgi97 | 1:0661b733846e | 190 | jump = true; |
Virgi97 | 1:0661b733846e | 191 | } |