prova bt
Dependencies: mbed
Prova_bl.cpp@0:245b8c83ca71, 2020-07-02 (annotated)
- Committer:
- maristella
- Date:
- Thu Jul 02 09:27:25 2020 +0000
- Revision:
- 0:245b8c83ca71
prova bt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maristella | 0:245b8c83ca71 | 1 | |
maristella | 0:245b8c83ca71 | 2 | #include "mbed.h" |
maristella | 0:245b8c83ca71 | 3 | #include<stdlib.h> |
maristella | 0:245b8c83ca71 | 4 | #define PACKETDIM 8 |
maristella | 0:245b8c83ca71 | 5 | // Definizione periferica USB seriale del PC |
maristella | 0:245b8c83ca71 | 6 | Serial pc(USBTX, USBRX, 921600); // seriale di comunicazione con il PC. Associati a PA_11 e PA_12 |
maristella | 0:245b8c83ca71 | 7 | |
maristella | 0:245b8c83ca71 | 8 | // Definizione periferica seriale del Modulo BLE ELETT114A |
maristella | 0:245b8c83ca71 | 9 | Serial myBLE(PA_9, PA_10, 9600); //Tx, Rx, bps // F401 |
maristella | 0:245b8c83ca71 | 10 | DigitalOut myLed(LED2); // LED verde sulla scheda. Associato a PA_5 |
maristella | 0:245b8c83ca71 | 11 | |
maristella | 0:245b8c83ca71 | 12 | //carattere di comando ricevuto dal BLE e relativo parametro |
maristella | 0:245b8c83ca71 | 13 | volatile char cCommandBLE; // cambia nella routine di interrupt |
maristella | 0:245b8c83ca71 | 14 | volatile char cParamBLE; // cambia nella routine di interrupt |
maristella | 0:245b8c83ca71 | 15 | volatile int nParamBLE; // corrispondente valore numerico di cParamBLE |
maristella | 0:245b8c83ca71 | 16 | |
maristella | 0:245b8c83ca71 | 17 | // memorizza l'ultimo comando ricevuto e relativo parametro. Ci saranno delle azioni solo se il comando ricevuto o il parametro è cambiato rispetto al precedente |
maristella | 0:245b8c83ca71 | 18 | char cOldCommandBLE; |
maristella | 0:245b8c83ca71 | 19 | int nOldParamBLE; |
maristella | 0:245b8c83ca71 | 20 | |
maristella | 0:245b8c83ca71 | 21 | // array per la ricezione dei messaggi da BLE |
maristella | 0:245b8c83ca71 | 22 | volatile char caRxPacket[PACKETDIM]; |
maristella | 0:245b8c83ca71 | 23 | // contatore di caratteri ricevuti daBLE |
maristella | 0:245b8c83ca71 | 24 | volatile int nCharCount; |
maristella | 0:245b8c83ca71 | 25 | |
maristella | 0:245b8c83ca71 | 26 | |
maristella | 0:245b8c83ca71 | 27 | /**********************************************/ |
maristella | 0:245b8c83ca71 | 28 | // IRQ associata a Rx da PC |
maristella | 0:245b8c83ca71 | 29 | //**********************************************/ |
maristella | 0:245b8c83ca71 | 30 | void pcRxInterrupt(void) |
maristella | 0:245b8c83ca71 | 31 | { |
maristella | 0:245b8c83ca71 | 32 | // array per la ricezione dei messaggi da seriale |
maristella | 0:245b8c83ca71 | 33 | char cReadChar; |
maristella | 0:245b8c83ca71 | 34 | |
maristella | 0:245b8c83ca71 | 35 | // ricevi caratteri su seriale, se disponibili |
maristella | 0:245b8c83ca71 | 36 | while((pc.readable())) |
maristella | 0:245b8c83ca71 | 37 | { |
maristella | 0:245b8c83ca71 | 38 | // acquisice stringa in input e relativa dimensione |
maristella | 0:245b8c83ca71 | 39 | cReadChar = pc.getc(); // read character from PC |
maristella | 0:245b8c83ca71 | 40 | //myBLE.putc(cReadChar); // Diagnostica: write char to BLE |
maristella | 0:245b8c83ca71 | 41 | //pc.putc(cReadChar); // Diagnostica: write char to PC |
maristella | 0:245b8c83ca71 | 42 | |
maristella | 0:245b8c83ca71 | 43 | //pc.printf("W>: 0x%02x\n\r",cReadChar); // diagnostica |
maristella | 0:245b8c83ca71 | 44 | if(cReadChar == '0') // se scrivo '0', invia questa stringa |
maristella | 0:245b8c83ca71 | 45 | { |
maristella | 0:245b8c83ca71 | 46 | // DIAGNOSTICA: |
maristella | 0:245b8c83ca71 | 47 | // Invia Stringa di comando al Robot |
maristella | 0:245b8c83ca71 | 48 | myBLE.printf("\r\n> Prova di Trasmissione \r\n"); |
maristella | 0:245b8c83ca71 | 49 | } |
maristella | 0:245b8c83ca71 | 50 | } |
maristella | 0:245b8c83ca71 | 51 | } |
maristella | 0:245b8c83ca71 | 52 | |
maristella | 0:245b8c83ca71 | 53 | //**********************************************/ |
maristella | 0:245b8c83ca71 | 54 | // IRQ associata a Rx da BLE |
maristella | 0:245b8c83ca71 | 55 | //**********************************************/ |
maristella | 0:245b8c83ca71 | 56 | void BLERxInterrupt(void) |
maristella | 0:245b8c83ca71 | 57 | { |
maristella | 0:245b8c83ca71 | 58 | |
maristella | 0:245b8c83ca71 | 59 | // carattere ricevuto da BLE |
maristella | 0:245b8c83ca71 | 60 | char cReadChar; |
maristella | 0:245b8c83ca71 | 61 | |
maristella | 0:245b8c83ca71 | 62 | // indice per l'array di caratteri ricevuti |
maristella | 0:245b8c83ca71 | 63 | int nCharIndex; |
maristella | 0:245b8c83ca71 | 64 | |
maristella | 0:245b8c83ca71 | 65 | // variabile ausiliaria |
maristella | 0:245b8c83ca71 | 66 | int nAux; |
maristella | 0:245b8c83ca71 | 67 | |
maristella | 0:245b8c83ca71 | 68 | // flag che diventa true quando viene rilevata una incongruenza dei valori di Ro e Teta, presumibilmente dovuta a errori di comunicazione |
maristella | 0:245b8c83ca71 | 69 | bool bIncongruenza; |
maristella | 0:245b8c83ca71 | 70 | |
maristella | 0:245b8c83ca71 | 71 | while((myBLE.readable())) |
maristella | 0:245b8c83ca71 | 72 | { |
maristella | 0:245b8c83ca71 | 73 | // acquisice stringa in input e memorizza in array |
maristella | 0:245b8c83ca71 | 74 | cReadChar = myBLE.getc(); // Read character |
maristella | 0:245b8c83ca71 | 75 | caRxPacket[nCharCount]=cReadChar; |
maristella | 0:245b8c83ca71 | 76 | nCharCount++; |
maristella | 0:245b8c83ca71 | 77 | //pc.printf("%c", cReadChar); // diagnostica |
maristella | 0:245b8c83ca71 | 78 | |
maristella | 0:245b8c83ca71 | 79 | // inizializza flag di incongruenza |
maristella | 0:245b8c83ca71 | 80 | bIncongruenza = false; |
maristella | 0:245b8c83ca71 | 81 | |
maristella | 0:245b8c83ca71 | 82 | if(cReadChar==')') |
maristella | 0:245b8c83ca71 | 83 | { |
maristella | 0:245b8c83ca71 | 84 | //pc.printf("\r\n"); // diagnostica |
maristella | 0:245b8c83ca71 | 85 | |
maristella | 0:245b8c83ca71 | 86 | // +++++++++++++++++ INIZIO gestione Comando da Button +++++++++++++++++ |
maristella | 0:245b8c83ca71 | 87 | // Ho ricevuto il comando da un Button se il carattere numero 1, è una lettera maiuscola |
maristella | 0:245b8c83ca71 | 88 | if((caRxPacket[1] > 0x40) && (caRxPacket[1] < 0x5B)) // caratteri alfabetici |
maristella | 0:245b8c83ca71 | 89 | { |
maristella | 0:245b8c83ca71 | 90 | cCommandBLE = caRxPacket[1]; // legge e memorizza il primo carattere |
maristella | 0:245b8c83ca71 | 91 | nParamBLE = caRxPacket[2]-0x30; |
maristella | 0:245b8c83ca71 | 92 | myLed=1; |
maristella | 0:245b8c83ca71 | 93 | // visualizza comando e parametro inviato da BLE |
maristella | 0:245b8c83ca71 | 94 | //pc.printf("> %c%d \r\n\r",cCommandBLE, nParamBLE); // diagnostica |
maristella | 0:245b8c83ca71 | 95 | } |
maristella | 0:245b8c83ca71 | 96 | // +++++++++++++++++ FINE gestione Comando da Button +++++++++++++++++ |
maristella | 0:245b8c83ca71 | 97 | |
maristella | 0:245b8c83ca71 | 98 | |
maristella | 0:245b8c83ca71 | 99 | } // if(cReadChar == ')') |
maristella | 0:245b8c83ca71 | 100 | } |
maristella | 0:245b8c83ca71 | 101 | } |
maristella | 0:245b8c83ca71 | 102 | |
maristella | 0:245b8c83ca71 | 103 | |
maristella | 0:245b8c83ca71 | 104 | /**********/ |
maristella | 0:245b8c83ca71 | 105 | /* MAIN */ |
maristella | 0:245b8c83ca71 | 106 | /**********/ |
maristella | 0:245b8c83ca71 | 107 | int main() |
maristella | 0:245b8c83ca71 | 108 | { |
maristella | 0:245b8c83ca71 | 109 | myLed = 0; |
maristella | 0:245b8c83ca71 | 110 | while(true) |
maristella | 0:245b8c83ca71 | 111 | { |
maristella | 0:245b8c83ca71 | 112 | if((cCommandBLE != cOldCommandBLE) || (nParamBLE != nOldParamBLE)) |
maristella | 0:245b8c83ca71 | 113 | { |
maristella | 0:245b8c83ca71 | 114 | switch (cCommandBLE) |
maristella | 0:245b8c83ca71 | 115 | { |
maristella | 0:245b8c83ca71 | 116 | case 'T': // accendi/spegni LED su scheda |
maristella | 0:245b8c83ca71 | 117 | { |
maristella | 0:245b8c83ca71 | 118 | myLed = 1; |
maristella | 0:245b8c83ca71 | 119 | } break; |
maristella | 0:245b8c83ca71 | 120 | default: break; |
maristella | 0:245b8c83ca71 | 121 | } |
maristella | 0:245b8c83ca71 | 122 | //pc.printf("Comando = %c, Parametro = %d \r\n", cCommandBLE, nParamBLE); // diagnostica |
maristella | 0:245b8c83ca71 | 123 | cOldCommandBLE = cCommandBLE; |
maristella | 0:245b8c83ca71 | 124 | nOldParamBLE = nParamBLE; |
maristella | 0:245b8c83ca71 | 125 | } |
maristella | 0:245b8c83ca71 | 126 | } |
maristella | 0:245b8c83ca71 | 127 | } |