Alex Millane / Mbed 2 deprecated IFARanging

Dependencies:   mbed

Committer:
millanea
Date:
Tue Jul 07 09:36:12 2015 +0000
Revision:
0:99928431bb44
First commit. Committing the entire project such that it can be published.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
millanea 0:99928431bb44 1 #include "BufferedOutput.h"
millanea 0:99928431bb44 2
millanea 0:99928431bb44 3 BufferedOutput::BufferedOutput( Serial& serial ) : framesOut( serial )
millanea 0:99928431bb44 4 {
millanea 0:99928431bb44 5 // Debug startup message
millanea 0:99928431bb44 6 debugprintf("Initializing Buffered Output object\r\n") ;
millanea 0:99928431bb44 7
millanea 0:99928431bb44 8 // Attaching the serial interrupt callbacks
millanea 0:99928431bb44 9 framesOut.attach(this, &BufferedOutput::framesOutCallback, Serial::TxIrq ) ;
millanea 0:99928431bb44 10
millanea 0:99928431bb44 11 // Initializing the buffer pointers
millanea 0:99928431bb44 12 framesBufferIn = 0 ;
millanea 0:99928431bb44 13 framesBufferOut = 0 ;
millanea 0:99928431bb44 14
millanea 0:99928431bb44 15 // Initializing frame transfer state
millanea 0:99928431bb44 16 frameTransferInProgress = false ;
millanea 0:99928431bb44 17
millanea 0:99928431bb44 18 }
millanea 0:99928431bb44 19
millanea 0:99928431bb44 20 void BufferedOutput::executeBackgroundTask()
millanea 0:99928431bb44 21 {
millanea 0:99928431bb44 22 // Checking if the Frame buffer is non empty
millanea 0:99928431bb44 23 if( !(framesBufferOut == framesBufferIn) && !frameTransferInProgress )
millanea 0:99928431bb44 24 {
millanea 0:99928431bb44 25 //debugprintf("Starting frames Tx\r\n") ;
millanea 0:99928431bb44 26 // Moving characters from the mavlink buffer to the USART device buffer
millanea 0:99928431bb44 27 // while there are still chars in the MAVLink Buffer
millanea 0:99928431bb44 28 if( framesOut.writeable() )
millanea 0:99928431bb44 29 {
millanea 0:99928431bb44 30 // Disabling interrupts while interacting with UART device
millanea 0:99928431bb44 31 __disable_irq();
millanea 0:99928431bb44 32 // Putting character to the USART device buffer
millanea 0:99928431bb44 33 framesOut.putc( framesBuffer[ framesBufferOut ] ) ;
millanea 0:99928431bb44 34 // Disabling interrupts while modifying buffers
millanea 0:99928431bb44 35 __enable_irq();
millanea 0:99928431bb44 36 // Incrementing the output pointer
millanea 0:99928431bb44 37 framesBufferOut = ( framesBufferOut + 1 ) % framesBufferSize ;
millanea 0:99928431bb44 38 // Indicating that the frame transfer has started
millanea 0:99928431bb44 39 frameTransferInProgress = true ;
millanea 0:99928431bb44 40 }
millanea 0:99928431bb44 41 }
millanea 0:99928431bb44 42 }
millanea 0:99928431bb44 43
millanea 0:99928431bb44 44 /*
millanea 0:99928431bb44 45 void BufferedOutput::addFrame( char* frame, char length )
millanea 0:99928431bb44 46 {
millanea 0:99928431bb44 47 // Disabling interrupt while modifying global buffers
millanea 0:99928431bb44 48 __disable_irq() ;
millanea 0:99928431bb44 49 // Looping over all characters in the frame
millanea 0:99928431bb44 50 for( char index = 0 ; index < length ; index++ )
millanea 0:99928431bb44 51 {
millanea 0:99928431bb44 52 // Placing char output buffer
millanea 0:99928431bb44 53 framesBuffer[ framesBufferIn ] = frame[index] ;
millanea 0:99928431bb44 54 // Incrementing the input pointer
millanea 0:99928431bb44 55 framesBufferIn = ( framesBufferIn + 1 ) % framesBufferSize ;
millanea 0:99928431bb44 56 }
millanea 0:99928431bb44 57 // Re-enabling interrupts
millanea 0:99928431bb44 58 __enable_irq() ;
millanea 0:99928431bb44 59 }
millanea 0:99928431bb44 60 */
millanea 0:99928431bb44 61
millanea 0:99928431bb44 62 void BufferedOutput::addChar( char c )
millanea 0:99928431bb44 63 {
millanea 0:99928431bb44 64 // Placing char output buffer
millanea 0:99928431bb44 65 framesBuffer[ framesBufferIn ] = c ;
millanea 0:99928431bb44 66 // Incrementing the input pointer
millanea 0:99928431bb44 67 framesBufferIn = ( framesBufferIn + 1 ) % framesBufferSize ;
millanea 0:99928431bb44 68 }
millanea 0:99928431bb44 69
millanea 0:99928431bb44 70 void BufferedOutput::framesOutCallback()
millanea 0:99928431bb44 71 {
millanea 0:99928431bb44 72 // Moving characters from the mavlink buffer to the USART device buffer
millanea 0:99928431bb44 73 // while there are still chars in the MAVLink Buffer
millanea 0:99928431bb44 74 while( framesOut.writeable() && !( framesBufferOut == framesBufferIn ) )
millanea 0:99928431bb44 75 {
millanea 0:99928431bb44 76 // Putting character to the USART device buffer
millanea 0:99928431bb44 77 framesOut.putc( framesBuffer[ framesBufferOut ] ) ;
millanea 0:99928431bb44 78 // Incrementing the output pointer
millanea 0:99928431bb44 79 framesBufferOut = ( framesBufferOut + 1 ) % framesBufferSize ;
millanea 0:99928431bb44 80 }
millanea 0:99928431bb44 81 // If buffer emptied indicate that tx interrupts are over
millanea 0:99928431bb44 82 if( ( framesBufferOut == framesBufferIn ) )
millanea 0:99928431bb44 83 {
millanea 0:99928431bb44 84 frameTransferInProgress = false ;
millanea 0:99928431bb44 85 }
millanea 0:99928431bb44 86 }