Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
ElectronicBell.cpp@21:7e0f5b8f6c77, 2020-01-11 (annotated)
- Committer:
- pinofal
- Date:
- Sat Jan 11 17:54:23 2020 +0000
- Revision:
- 21:7e0f5b8f6c77
bell bell
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pinofal | 21:7e0f5b8f6c77 | 1 | // mbed specific header files. |
pinofal | 21:7e0f5b8f6c77 | 2 | #include "mbed.h" |
pinofal | 21:7e0f5b8f6c77 | 3 | |
pinofal | 21:7e0f5b8f6c77 | 4 | #include "Arrivederci.h" |
pinofal | 21:7e0f5b8f6c77 | 5 | #include "CambioOra.h" |
pinofal | 21:7e0f5b8f6c77 | 6 | #include "PrestoInClasse.h" |
pinofal | 21:7e0f5b8f6c77 | 7 | #include "Welcome.h" |
pinofal | 21:7e0f5b8f6c77 | 8 | #include "TooDark.h" |
pinofal | 21:7e0f5b8f6c77 | 9 | |
pinofal | 21:7e0f5b8f6c77 | 10 | |
pinofal | 21:7e0f5b8f6c77 | 11 | // dimensione massima del pacchetto ricevuto su seriale |
pinofal | 21:7e0f5b8f6c77 | 12 | #define PACKETDIM 8 |
pinofal | 21:7e0f5b8f6c77 | 13 | |
pinofal | 21:7e0f5b8f6c77 | 14 | |
pinofal | 21:7e0f5b8f6c77 | 15 | // Input/Output utilizzati da funzioni default su scheda NUCLEO |
pinofal | 21:7e0f5b8f6c77 | 16 | DigitalOut myOnBoardLed(LED2);// LED verde sulla scheda. Associato a PA_5 |
pinofal | 21:7e0f5b8f6c77 | 17 | Serial pc(SERIAL_TX, SERIAL_RX,9600); // seriale di comunicazione con il PC. Associati a PA_11 e PA_12 |
pinofal | 21:7e0f5b8f6c77 | 18 | DigitalIn myOnBoardButton(USER_BUTTON); // pulsante Blu sulla scheda. Associato a PC_13 |
pinofal | 21:7e0f5b8f6c77 | 19 | |
pinofal | 21:7e0f5b8f6c77 | 20 | // Definizione periferica seriale del Modulo BLE HC05 |
pinofal | 21:7e0f5b8f6c77 | 21 | Serial myBLE(PA_9, PA_10, 9600); //Tx, Rx, bps |
pinofal | 21:7e0f5b8f6c77 | 22 | |
pinofal | 21:7e0f5b8f6c77 | 23 | |
pinofal | 21:7e0f5b8f6c77 | 24 | // inizializza variabili contenente comando da BLE |
pinofal | 21:7e0f5b8f6c77 | 25 | volatile char cCommandBLE = 0; // ultimo comando ricevuto tramite BLE |
pinofal | 21:7e0f5b8f6c77 | 26 | volatile char cOldCommandBLE = 0; // precedente comando ricevuto tramite BLE |
pinofal | 21:7e0f5b8f6c77 | 27 | |
pinofal | 21:7e0f5b8f6c77 | 28 | // pacchetto di dati e relativa dimensione, ricevuto tramite BLE |
pinofal | 21:7e0f5b8f6c77 | 29 | char volatile caRxPacket[PACKETDIM]; // variabile che viene modificata e aggiornata nella IRQ della BLE |
pinofal | 21:7e0f5b8f6c77 | 30 | |
pinofal | 21:7e0f5b8f6c77 | 31 | //indice e contatore di caratteri ricevuti da BLE |
pinofal | 21:7e0f5b8f6c77 | 32 | volatile int nCharCount; // variabile che viene modificata e aggiornata nella IRQ della BLE |
pinofal | 21:7e0f5b8f6c77 | 33 | |
pinofal | 21:7e0f5b8f6c77 | 34 | // flag che diventa true quando si accende la luce |
pinofal | 21:7e0f5b8f6c77 | 35 | bool bLuce = false; |
pinofal | 21:7e0f5b8f6c77 | 36 | |
pinofal | 21:7e0f5b8f6c77 | 37 | DigitalOut myRelay (PC_7); // pin di output verso il secondo relay |
pinofal | 21:7e0f5b8f6c77 | 38 | AnalogOut myWaveOut (PA_4); // uscita analogica Audio |
pinofal | 21:7e0f5b8f6c77 | 39 | AnalogIn myPhotoTrans (PA_1); // input analogico per phototransistor |
pinofal | 21:7e0f5b8f6c77 | 40 | |
pinofal | 21:7e0f5b8f6c77 | 41 | |
pinofal | 21:7e0f5b8f6c77 | 42 | // funzione per generare messaggi vocali |
pinofal | 21:7e0f5b8f6c77 | 43 | //void SoundMessage(array in in put, numero di campioni nell'array in input, ritardo nella generazione tra un campione e l'altro) |
pinofal | 21:7e0f5b8f6c77 | 44 | void SoundMessage(const int naInputSoundWave[], int nSampleNum, long fSampleDeltaT); |
pinofal | 21:7e0f5b8f6c77 | 45 | |
pinofal | 21:7e0f5b8f6c77 | 46 | /*******************************************/ |
pinofal | 21:7e0f5b8f6c77 | 47 | /* Funzione di generazione Messaggio Audio */ |
pinofal | 21:7e0f5b8f6c77 | 48 | /*******************************************/ |
pinofal | 21:7e0f5b8f6c77 | 49 | void SoundMessage(const int naInputSoundWave[],int nSampleNum, long fSampleDeltaT) |
pinofal | 21:7e0f5b8f6c77 | 50 | { |
pinofal | 21:7e0f5b8f6c77 | 51 | // indice per i cicli interni alla funzione |
pinofal | 21:7e0f5b8f6c77 | 52 | int nIndex; |
pinofal | 21:7e0f5b8f6c77 | 53 | |
pinofal | 21:7e0f5b8f6c77 | 54 | |
pinofal | 21:7e0f5b8f6c77 | 55 | //++++++++++++ INIZIO generazione messaggio +++++++++++++++++ |
pinofal | 21:7e0f5b8f6c77 | 56 | for(nIndex=0; nIndex < nSampleNum; nIndex++) |
pinofal | 21:7e0f5b8f6c77 | 57 | { |
pinofal | 21:7e0f5b8f6c77 | 58 | // mette in output un campione della forma d'onda del welcome message moltiplicato per l'amplificazione fAmp |
pinofal | 21:7e0f5b8f6c77 | 59 | myWaveOut.write_u16(naInputSoundWave[nIndex]); |
pinofal | 21:7e0f5b8f6c77 | 60 | |
pinofal | 21:7e0f5b8f6c77 | 61 | // tra un campione e l'altro attendi un periodo pari al periodo di campionamento |
pinofal | 21:7e0f5b8f6c77 | 62 | wait_us(fSampleDeltaT); // 57; 37 quando non sottocampionato |
pinofal | 21:7e0f5b8f6c77 | 63 | } |
pinofal | 21:7e0f5b8f6c77 | 64 | //++++++++++++ FINE generazione messaggio +++++++++++++++++ |
pinofal | 21:7e0f5b8f6c77 | 65 | } |
pinofal | 21:7e0f5b8f6c77 | 66 | |
pinofal | 21:7e0f5b8f6c77 | 67 | |
pinofal | 21:7e0f5b8f6c77 | 68 | //**********************************************/ |
pinofal | 21:7e0f5b8f6c77 | 69 | // IRQ associata a Rx da BLE |
pinofal | 21:7e0f5b8f6c77 | 70 | //**********************************************/ |
pinofal | 21:7e0f5b8f6c77 | 71 | void BLERxInterrupt(void) |
pinofal | 21:7e0f5b8f6c77 | 72 | { |
pinofal | 21:7e0f5b8f6c77 | 73 | // carattere ricevuto da BLE |
pinofal | 21:7e0f5b8f6c77 | 74 | char cReadChar; |
pinofal | 21:7e0f5b8f6c77 | 75 | |
pinofal | 21:7e0f5b8f6c77 | 76 | //myOnBoardLed = !myOnBoardLed; |
pinofal | 21:7e0f5b8f6c77 | 77 | |
pinofal | 21:7e0f5b8f6c77 | 78 | |
pinofal | 21:7e0f5b8f6c77 | 79 | while((myBLE.readable())) |
pinofal | 21:7e0f5b8f6c77 | 80 | { |
pinofal | 21:7e0f5b8f6c77 | 81 | // acquisice stringa in input e memorizza in array |
pinofal | 21:7e0f5b8f6c77 | 82 | cReadChar = myBLE.getc(); // Read character |
pinofal | 21:7e0f5b8f6c77 | 83 | caRxPacket[nCharCount]=cReadChar; |
pinofal | 21:7e0f5b8f6c77 | 84 | nCharCount++; |
pinofal | 21:7e0f5b8f6c77 | 85 | pc.printf("%c\r\n", cReadChar); // diagnostica |
pinofal | 21:7e0f5b8f6c77 | 86 | |
pinofal | 21:7e0f5b8f6c77 | 87 | if(cReadChar != NULL) |
pinofal | 21:7e0f5b8f6c77 | 88 | { |
pinofal | 21:7e0f5b8f6c77 | 89 | // assegna il carattere ricevuto, al comando da BLE |
pinofal | 21:7e0f5b8f6c77 | 90 | cCommandBLE = cReadChar; |
pinofal | 21:7e0f5b8f6c77 | 91 | } |
pinofal | 21:7e0f5b8f6c77 | 92 | } |
pinofal | 21:7e0f5b8f6c77 | 93 | } |
pinofal | 21:7e0f5b8f6c77 | 94 | |
pinofal | 21:7e0f5b8f6c77 | 95 | /********/ |
pinofal | 21:7e0f5b8f6c77 | 96 | /* Main */ |
pinofal | 21:7e0f5b8f6c77 | 97 | /********/ |
pinofal | 21:7e0f5b8f6c77 | 98 | int main() |
pinofal | 21:7e0f5b8f6c77 | 99 | { |
pinofal | 21:7e0f5b8f6c77 | 100 | // messaggio di benvenuto |
pinofal | 21:7e0f5b8f6c77 | 101 | pc.printf("\r\n***** Hallo Amaldi Students - Exercise 40 *****\r\n"); |
pinofal | 21:7e0f5b8f6c77 | 102 | pc.printf("***** Electronic Bell *****\r\n"); |
pinofal | 21:7e0f5b8f6c77 | 103 | |
pinofal | 21:7e0f5b8f6c77 | 104 | // inizializza variabili |
pinofal | 21:7e0f5b8f6c77 | 105 | myOnBoardLed = 0; |
pinofal | 21:7e0f5b8f6c77 | 106 | myRelay = 0; |
pinofal | 21:7e0f5b8f6c77 | 107 | bLuce = false; |
pinofal | 21:7e0f5b8f6c77 | 108 | |
pinofal | 21:7e0f5b8f6c77 | 109 | |
pinofal | 21:7e0f5b8f6c77 | 110 | // Attiva la IRQ per la RX su seriale BLE |
pinofal | 21:7e0f5b8f6c77 | 111 | myBLE.attach(&BLERxInterrupt,Serial::RxIrq); // // entra in questa routine quando riceve un carattere dalla seriale del BLE |
pinofal | 21:7e0f5b8f6c77 | 112 | |
pinofal | 21:7e0f5b8f6c77 | 113 | |
pinofal | 21:7e0f5b8f6c77 | 114 | //+++++++++++++++++++ |
pinofal | 21:7e0f5b8f6c77 | 115 | //+ Ciclo Principale |
pinofal | 21:7e0f5b8f6c77 | 116 | //+++++++++++++++++++ |
pinofal | 21:7e0f5b8f6c77 | 117 | while(true) |
pinofal | 21:7e0f5b8f6c77 | 118 | { |
pinofal | 21:7e0f5b8f6c77 | 119 | pc.printf("%.3f\r\n",myPhotoTrans.read()); |
pinofal | 21:7e0f5b8f6c77 | 120 | if((myPhotoTrans.read() < 0.1) && (bLuce==false)) |
pinofal | 21:7e0f5b8f6c77 | 121 | { |
pinofal | 21:7e0f5b8f6c77 | 122 | myRelay = 1; |
pinofal | 21:7e0f5b8f6c77 | 123 | SoundMessage(naInputSoundWaveTooDark,nSampleNumTooDark, 70.0); // genera messaggio audio di Ingresso |
pinofal | 21:7e0f5b8f6c77 | 124 | bLuce = true; |
pinofal | 21:7e0f5b8f6c77 | 125 | } |
pinofal | 21:7e0f5b8f6c77 | 126 | if(myPhotoTrans.read() > 0.5) |
pinofal | 21:7e0f5b8f6c77 | 127 | { |
pinofal | 21:7e0f5b8f6c77 | 128 | myRelay = 0; |
pinofal | 21:7e0f5b8f6c77 | 129 | bLuce = false; |
pinofal | 21:7e0f5b8f6c77 | 130 | } |
pinofal | 21:7e0f5b8f6c77 | 131 | // se è arrivato un comando diverso dal precedente, applica le azioni richieste |
pinofal | 21:7e0f5b8f6c77 | 132 | if(cCommandBLE != cOldCommandBLE) |
pinofal | 21:7e0f5b8f6c77 | 133 | { |
pinofal | 21:7e0f5b8f6c77 | 134 | |
pinofal | 21:7e0f5b8f6c77 | 135 | switch (cCommandBLE) |
pinofal | 21:7e0f5b8f6c77 | 136 | { |
pinofal | 21:7e0f5b8f6c77 | 137 | // accendi/spegni la luce |
pinofal | 21:7e0f5b8f6c77 | 138 | case 'L': // 'A' = 0x41; 'a' = 0x61 |
pinofal | 21:7e0f5b8f6c77 | 139 | { |
pinofal | 21:7e0f5b8f6c77 | 140 | myOnBoardLed = 1; |
pinofal | 21:7e0f5b8f6c77 | 141 | myRelay = 1; |
pinofal | 21:7e0f5b8f6c77 | 142 | } break; |
pinofal | 21:7e0f5b8f6c77 | 143 | case 'l': |
pinofal | 21:7e0f5b8f6c77 | 144 | { |
pinofal | 21:7e0f5b8f6c77 | 145 | myOnBoardLed = 0; |
pinofal | 21:7e0f5b8f6c77 | 146 | myRelay = 0; |
pinofal | 21:7e0f5b8f6c77 | 147 | bLuce = false; // riattiva il flag per accendere la luce in caso di superamento soglia di bassa luminosità |
pinofal | 21:7e0f5b8f6c77 | 148 | } break; |
pinofal | 21:7e0f5b8f6c77 | 149 | case 'I': // 'A' = 0x41; 'a' = 0x61 |
pinofal | 21:7e0f5b8f6c77 | 150 | { |
pinofal | 21:7e0f5b8f6c77 | 151 | myOnBoardLed = 1; |
pinofal | 21:7e0f5b8f6c77 | 152 | SoundMessage(naInputSoundWavePrestoInClasse,nSampleNumPrestoInClasse, 62.0); // genera messaggio audio di Ingresso |
pinofal | 21:7e0f5b8f6c77 | 153 | } break; |
pinofal | 21:7e0f5b8f6c77 | 154 | case 'i': |
pinofal | 21:7e0f5b8f6c77 | 155 | { |
pinofal | 21:7e0f5b8f6c77 | 156 | myOnBoardLed = 0; |
pinofal | 21:7e0f5b8f6c77 | 157 | SoundMessage(naInputSoundWavePrestoInClasse,0, 0.0); // spegni il messaggio audio |
pinofal | 21:7e0f5b8f6c77 | 158 | } break; |
pinofal | 21:7e0f5b8f6c77 | 159 | case 'R': // 'A' = 0x41; 'a' = 0x61 |
pinofal | 21:7e0f5b8f6c77 | 160 | { |
pinofal | 21:7e0f5b8f6c77 | 161 | myOnBoardLed = 1; |
pinofal | 21:7e0f5b8f6c77 | 162 | SoundMessage(naInputSoundWaveCambioOra,nSampleNumCambioOra, 65.0); // genera messaggio audio di Ricreazione |
pinofal | 21:7e0f5b8f6c77 | 163 | } break; |
pinofal | 21:7e0f5b8f6c77 | 164 | case 'r': |
pinofal | 21:7e0f5b8f6c77 | 165 | { |
pinofal | 21:7e0f5b8f6c77 | 166 | myOnBoardLed = 0; |
pinofal | 21:7e0f5b8f6c77 | 167 | SoundMessage(naInputSoundWaveCambioOra,0, 0.0); // spegni il messaggio audio |
pinofal | 21:7e0f5b8f6c77 | 168 | } break; |
pinofal | 21:7e0f5b8f6c77 | 169 | case 'U': // 'A' = 0x41; 'a' = 0x61 |
pinofal | 21:7e0f5b8f6c77 | 170 | { |
pinofal | 21:7e0f5b8f6c77 | 171 | myOnBoardLed = 1; |
pinofal | 21:7e0f5b8f6c77 | 172 | SoundMessage(naInputSoundWaveArrivederci,nSampleNumArrivederci, 67.0); // genera messaggio audio di Uscita |
pinofal | 21:7e0f5b8f6c77 | 173 | } break; |
pinofal | 21:7e0f5b8f6c77 | 174 | case 'u': |
pinofal | 21:7e0f5b8f6c77 | 175 | { |
pinofal | 21:7e0f5b8f6c77 | 176 | myOnBoardLed = 0; |
pinofal | 21:7e0f5b8f6c77 | 177 | SoundMessage(naInputSoundWaveArrivederci,0, 0.0); // spegni il messaggio audio |
pinofal | 21:7e0f5b8f6c77 | 178 | } break; |
pinofal | 21:7e0f5b8f6c77 | 179 | case 'B': // 'A' = 0x41; 'a' = 0x61 |
pinofal | 21:7e0f5b8f6c77 | 180 | { |
pinofal | 21:7e0f5b8f6c77 | 181 | myOnBoardLed = 1; |
pinofal | 21:7e0f5b8f6c77 | 182 | SoundMessage(naInputSoundWaveWelcome,nSampleNumWelcome, 40.0); // genera messaggio audio di benvenuto |
pinofal | 21:7e0f5b8f6c77 | 183 | } break; |
pinofal | 21:7e0f5b8f6c77 | 184 | case 'b': |
pinofal | 21:7e0f5b8f6c77 | 185 | { |
pinofal | 21:7e0f5b8f6c77 | 186 | myOnBoardLed = 0; |
pinofal | 21:7e0f5b8f6c77 | 187 | SoundMessage(naInputSoundWaveWelcome,0, 0.0); // spegni il messaggio audio |
pinofal | 21:7e0f5b8f6c77 | 188 | } break; |
pinofal | 21:7e0f5b8f6c77 | 189 | default: {} break; |
pinofal | 21:7e0f5b8f6c77 | 190 | |
pinofal | 21:7e0f5b8f6c77 | 191 | } |
pinofal | 21:7e0f5b8f6c77 | 192 | // visualizza il comando ricevuto |
pinofal | 21:7e0f5b8f6c77 | 193 | pc.printf("Comando = %c \r\n", cCommandBLE); // diagnostica |
pinofal | 21:7e0f5b8f6c77 | 194 | cOldCommandBLE = cCommandBLE; // memorizza il comando ricevuto |
pinofal | 21:7e0f5b8f6c77 | 195 | } |
pinofal | 21:7e0f5b8f6c77 | 196 | } |
pinofal | 21:7e0f5b8f6c77 | 197 | } |
pinofal | 21:7e0f5b8f6c77 | 198 | |
pinofal | 21:7e0f5b8f6c77 | 199 |