UART Driver to receive asynchronous Serial Comms from a Raspberry Pi and parse the results.
SerialComms.cpp@1:bf3fb80028d8, 2016-02-15 (annotated)
- Committer:
- sk398
- Date:
- Mon Feb 15 19:59:59 2016 +0000
- Revision:
- 1:bf3fb80028d8
- Parent:
- 0:8ac1280934b4
- Child:
- 2:cb74b330b285
changed to PinName constructer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 0:8ac1280934b4 | 1 | #include "mbed.h" |
sk398 | 0:8ac1280934b4 | 2 | #include "SerialComms.h" |
sk398 | 0:8ac1280934b4 | 3 | |
sk398 | 1:bf3fb80028d8 | 4 | SerialComms::SerialComms(PinName tx,PinName rx) |
sk398 | 0:8ac1280934b4 | 5 | { |
sk398 | 1:bf3fb80028d8 | 6 | _HLC_Conn = new Serial(tx,rx); |
sk398 | 0:8ac1280934b4 | 7 | _HLC_Conn-> baud(SERIAL_BAUD_RATE); |
sk398 | 0:8ac1280934b4 | 8 | _HLC_Conn -> format(8,SerialBase::None,1); |
sk398 | 0:8ac1280934b4 | 9 | |
sk398 | 0:8ac1280934b4 | 10 | _HLC_Conn -> attach(this,&SerialComms::incoming,Serial::RxIrq); |
sk398 | 0:8ac1280934b4 | 11 | |
sk398 | 0:8ac1280934b4 | 12 | dataUpdate = FALSE; |
sk398 | 0:8ac1280934b4 | 13 | } |
sk398 | 0:8ac1280934b4 | 14 | |
sk398 | 0:8ac1280934b4 | 15 | |
sk398 | 0:8ac1280934b4 | 16 | void SerialComms::incoming() |
sk398 | 0:8ac1280934b4 | 17 | { |
sk398 | 0:8ac1280934b4 | 18 | int a = 0; |
sk398 | 0:8ac1280934b4 | 19 | for(uint8_t charCount=0; charCount<(2*NUM_BYTES_RECEIVING); charCount++) |
sk398 | 0:8ac1280934b4 | 20 | { |
sk398 | 0:8ac1280934b4 | 21 | if((charCount%2) == 1) |
sk398 | 0:8ac1280934b4 | 22 | { |
sk398 | 0:8ac1280934b4 | 23 | receiverBuffer[a] = _HLC_Conn -> getc(); |
sk398 | 0:8ac1280934b4 | 24 | a++; |
sk398 | 0:8ac1280934b4 | 25 | } |
sk398 | 0:8ac1280934b4 | 26 | else |
sk398 | 0:8ac1280934b4 | 27 | { |
sk398 | 0:8ac1280934b4 | 28 | _HLC_Conn -> getc(); |
sk398 | 0:8ac1280934b4 | 29 | } |
sk398 | 0:8ac1280934b4 | 30 | } |
sk398 | 0:8ac1280934b4 | 31 | dataUpdate = TRUE; |
sk398 | 0:8ac1280934b4 | 32 | |
sk398 | 0:8ac1280934b4 | 33 | // for(int c = 0; c < 4; c++) |
sk398 | 0:8ac1280934b4 | 34 | // { |
sk398 | 0:8ac1280934b4 | 35 | // printf("recieved: %d\r\n",receiverBuffer[c]); |
sk398 | 0:8ac1280934b4 | 36 | // } |
sk398 | 0:8ac1280934b4 | 37 | } |
sk398 | 0:8ac1280934b4 | 38 | |
sk398 | 0:8ac1280934b4 | 39 | int *SerialComms::returnMotorSpeeds() |
sk398 | 0:8ac1280934b4 | 40 | { |
sk398 | 0:8ac1280934b4 | 41 | if(SerialComms::dataUpdate == TRUE) |
sk398 | 0:8ac1280934b4 | 42 | { |
sk398 | 0:8ac1280934b4 | 43 | SerialComms::dataUpdate = FALSE; |
sk398 | 0:8ac1280934b4 | 44 | return receiverBuffer; |
sk398 | 0:8ac1280934b4 | 45 | } |
sk398 | 0:8ac1280934b4 | 46 | else |
sk398 | 0:8ac1280934b4 | 47 | { |
sk398 | 0:8ac1280934b4 | 48 | int noNewData = -1; |
sk398 | 0:8ac1280934b4 | 49 | int *noNewDataPtr = &noNewData; |
sk398 | 0:8ac1280934b4 | 50 | return noNewDataPtr; |
sk398 | 0:8ac1280934b4 | 51 | } |
sk398 | 0:8ac1280934b4 | 52 | } |