Stephen Davis / Mbed 2 deprecated BT_Hub

Dependencies:   mbed-rtos mbed

main.cpp

Committer:
Stephen_NewVistas
Date:
2016-12-12
Revision:
0:226550611f0d
Child:
1:d6b18299a715

File content as of revision 0:226550611f0d:

//#define TEST
#define DEBUG

#include "mbed.h"
#include "rtos.h"
#include "comms.h"
#include "rs485.h"
#include "uart1.h"

#define FX 0x02

/*===========================================    
*   These functions need to be completed
*
*/
int Bluetooth_ReceivePacket( Packet * );
int Bluetooth_SendChar( unsigned char );
/*==========================================*/

void Fx( unsigned char * );


/*===========================================    
*
*
*/
void receivedSerialData( void ){
  while( uart1_is_char() ){
    SerialHandler( uart1_get_char() );
  }
}

void bus_thread(void const *argument){
    while (true){
        receivedSerialData(); 
        wait_ms( 100 );
    }
}

void Test( void );
/*==========================================*/

int main() {  
    
    RegisterCommand( FX , &Fx );  
    
    initComms();
    pc.printf( "\n\rStarted...\n\r" );
    init_uart1();
    
    Thread bus( bus_thread , NULL , osPriorityRealtime , DEFAULT_STACK_SIZE ); 

    while( true ){ };

        #ifdef TEST
            Test();
        #endif // DEBUG


}


void Fx( unsigned char *_receivedData ){
    
    pc.printf( "In FX!\n\r" );
    
    Packet packet;
    packet.sourceID = 0xF0;
    packet.deviceID = 0xFE;
    packet.command = 0x90;
    packet.packetData[0] = 0x22;
    packet.packetLength = 0x01;
    
    SetResponse( &packet );
}

int Bluetooth_SerialGetChar( unsigned char *_c ){
    if( bus.readable() ){
        char c = bus.getc();
        pc.printf( "%x" , c );
        _c = (unsigned char*)c;
        return 1;
    }   
    return 0;
}

int Bluetooth_ReceivePacket( Packet *_packet ){

        #ifdef DEBUG
            pc.printf( "Process Packet\n\r" );
                    
            pc.printf( "\t%x - Device ID\n\r" , _packet->deviceID );
            pc.printf( "\t%x - Source ID\n\r" , _packet->sourceID );
            pc.printf( "\t%x - Command\n\r" , _packet->command );
            pc.printf( "\t%x - Length\n\r" , _packet->packetLength );
            unsigned char *ptr = (unsigned char *)&_packet->packetData;
            for( int i = 0 ; i < _packet->packetLength ; i++ ){
                pc.printf( "\t%x - Data[%d]\n\r" , *ptr++ , i );   
            }
            
        #endif // DEBUG    
    
    CheckFunction( _packet );
    
    return 0;
}

int Bluetooth_SendChar( unsigned char _c ){
    bus.putc( _c );
    return 0;
}

void Test( void ){
    
            Packet commandPacket;
            commandPacket.sourceID = 0xFE;
            commandPacket.deviceID = 0xF0;
            commandPacket.command = 0x10;
            commandPacket.packetLength = 0x04;
            commandPacket.packetData[0] = 0x8E;
            commandPacket.packetData[1] = 0x7F;
            commandPacket.packetData[2] = 0x8F;
            commandPacket.packetData[3] = 0xB0;
        
            unsigned char buf1[1024];
            unsigned char buf2[1024];
            unsigned char *ptr = buf1;
            
            //  buf for commandPacket
            int size1 = getFormattedPacket( &commandPacket , buf1  );
            int size2;

            // buf for POLL                        
            buf1[0] = 0x8F;
            buf1[1] = 0xF0;
            size1 = 2;          
            
            buf2[0] = 0x8D; 
            size2 = 1; 
            
            pc.printf( "size: %i\t" , size1 );
            
            pc.printf( "buf: " );
            for( int i = 0 ; i < size1 ; i++ ){
                pc.printf( "%x " , *(ptr++) );
            }
            pc.printf( " \n\r" );
            
            while(1) {
                //  poll with no message
                ptr = buf1;
                for( int i = 0 ; i < size1 ; i++ ){
                    SerialHandler( *(ptr++) );
                    wait( 1 );
                }
                
                //  create message
                SetResponse( 0xF0 , 0x10 , 0x01 );
                
                //  poll with message
                ptr = buf1;
                for( int i = 0 ; i < size1 ; i++ ){
                    SerialHandler( *(ptr++) );
                    wait( 1 );
                }
                
                //  send ACK                
                ptr = buf2;
                for( int i = 0 ; i < size2 ; i++ ){
                    SerialHandler( *(ptr++) );
                    wait( 1 );
                }
            }
}