Library set up as dummy module on mbed to mimic Nordic.

Dependencies:   mbed-rtos mbed

Revision:
2:9ab591cf81b8
Parent:
1:d6b18299a715
--- a/rs485.cpp	Mon Dec 12 23:19:30 2016 +0000
+++ b/rs485.cpp	Tue Dec 13 00:20:29 2016 +0000
@@ -3,14 +3,10 @@
 
 #include "rs485.h"
 
-
-#define PRINTPACKETDEBUG
-#define DEVADDRESS 0xF0
 #define DEBUG
 
 static list_t functionList = { .index = 0 };
 
-
 Packet buildPacket;
 static unsigned int buffPtr;
 static unsigned char buffer[128+1];
@@ -20,21 +16,35 @@
 unsigned int plength;
 unsigned char command;
 int message;
-//  extern int message;
-
-//  extern unsigned char packetBuffer[128];
-//  extern unsigned int packetBufferSize;
 static unsigned char packetBuffer[128];
 static unsigned int packetBufferSize;
 
+/************************** External Functions********************************
+*   These functions need to be externall defined to be able to process packets
+*    and to send out chars over the bus.
+/************************** External Functions********************************/
 extern int Bluetooth_ReceivePacket( Packet *_packet );
 extern int Bluetooth_SendChar( unsigned char );
+extern const unsigned char DEVADDRESS;
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
+/******************************************************************************
+ * Function void RegisterCommand( command , function )
+ *
+ * When a command is registered with this function it will be added to the function
+ *  list. Once on the list, when a packet is received on the bus containing that
+ *  command the associated function will get called and it will be passed the data
+ *  within the packet as a parameter.
+ *
+ * PreCondition:    None
+ *
+ * Input:           'command'  - command byte
+ *                  'function' - pointer to function to be called
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/
 unsigned char RegisterCommand( unsigned char _command , void (*_function)( unsigned char *) ){
     functionList.list[ functionList.index ].function = _function;
     functionList.list[ functionList.index ].command = _command;
@@ -42,6 +52,23 @@
     return 0;
 }
 
+/******************************************************************************
+ * Function void CheckFunction( Packet * )
+ *
+ * This passes in a packet and compares the command byte with a list of registered
+ *  commands. If it finds a match it will call that function along with the data
+ *  in the packet.
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - packet containing command byte and data
+ *                  
+ * Output:          '1' - command matched and function called
+ *                  '2' - no command found
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/
 int CheckFunction( Packet *_packet ){
     int i = 0;
     void (*Caller)(unsigned char *);
@@ -59,6 +86,21 @@
        
 }
 
+/******************************************************************************
+ * Function void SendAck( void )
+ *
+ * Sends an acknowledge byte over the bus. It calls an external function to
+ *  handle the actual hardware communication.
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - prebuilt packet to send
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/ 
 static void SendAck( void ){
     #ifdef DEBUG
         pc.printf( "\n\rAck Sent.\n\r" );
@@ -67,6 +109,21 @@
     Bluetooth_SendChar( ack );
 }
 
+/******************************************************************************
+ * Function void SendNack( void )
+ *
+ * Sends a non acknowledge byte over the bus. It calls an external function to
+ *  handle the actual hardware communication.
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - prebuilt packet to send
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/ 
 static void SendNack( void ){
     #ifdef DEBUG
         pc.printf( "\n\rNack Sent.\n\r" );
@@ -75,10 +132,40 @@
     Bluetooth_SendChar( nack );
 }
 
+/******************************************************************************
+ * Function void ProcessPacket( Packet *)
+ *
+ * Gets automatically called once an entire packet is successfully received.
+ *  May modify what is done, but it should just pass the packet over to 
+ *  an external function where it can be correctly handled.
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - packet received on bus
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/ 
 static int ProcessPacket( Packet *_packet ){
     return Bluetooth_ReceivePacket( _packet );
 }
-  
+
+/******************************************************************************
+ * Function void setResponse(int, int, int)
+ *
+ * This function sets the feedback information to be send in the master device.
+ *
+ * PreCondition:    None
+ *
+ * Input:           '_packet' - prebuilt packet to send
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/  
 void SendMessage( void ){    
     int j = 0;
     Bluetooth_SendChar( packetBuffer[ j++ ] );
@@ -105,7 +192,7 @@
 }
 
 /******************************************************************************
- * Function void setResponse(int, int, int)
+ * Function void SetResponse( Packet *_packet )
  *
  * This function sets the feedback information to be send in the master device.
  *
@@ -118,12 +205,54 @@
  * Side Effects:    None
  *
  *****************************************************************************/ 
- void SetResponse( Packet *_packet ){
+ void SetResponsePacket( Packet *_packet ){
     packetBufferSize = getFormattedPacket(_packet, packetBuffer);    
     message = 1;
 }
 
 /******************************************************************************
+ * Function void SetResponseWithData( unsigned char deviceID, unsigned char 
+    command, unsigned char * data, unsigned short dataLength)
+ *
+ * This function sets the feedback information to be send in the master device.
+ *
+ * PreCondition:    None
+ *
+ * Input:           'deviceID'   - address where the packet is going
+                    'command'    - command to be sent
+                    '*data'      - point to unsiged char array packet with data
+                    'dataLength' - length of 'data'
+ *                  
+ * Output:          None
+ *
+ * Side Effects:    None
+ *
+ *****************************************************************************/ 
+void SetResponseWithData( unsigned char deviceID, unsigned char command, unsigned char * data, unsigned short dataLength){
+        
+    //  build packet to be sent    
+    Packet packet;    
+    packet.deviceID = deviceID;
+    packet.sourceID = DEVADDRESS;
+    packet.command = command;
+    packet.packetLength = dataLength;
+    memcpy( packet.packetData , data , dataLength );
+    packetBufferSize = getFormattedPacket(&packet, packetBuffer);
+    
+    //  print message to be sent
+    #ifdef DEBUG
+        pc.printf( "Message to send: ");    
+        unsigned short length = 0;
+        for( length = 1 ; length < ( dataLength + sizeof( packet.deviceID ) + sizeof( packet.sourceID ) + sizeof( packet.command ) + sizeof( packet.packetLength ) + 1 ) ; length++ ){
+            pc.printf( "%x " , packetBuffer[ length ] );   
+        }
+        pc.printf( "\n\r" );
+    #endif
+    
+    message = 1;
+}
+
+/******************************************************************************
  * Function void SerialHandler(unsigned char )
  *
  * This function handles the received data from the Serial Communication.
@@ -135,7 +264,8 @@
  *
  * Output:          None
  *
- * Side Effects:    None
+ * Side Effects:    Will call functions to send ACK, NACK and process a packet
+ *                   once an entire packet is received.
  *
  *****************************************************************************/
 void SerialHandler(unsigned char RXByte){
@@ -370,7 +500,7 @@
                
                 buffer[buffPtr++] = RXByte;             //  add RXByte to buffer
                 
-                #ifdef PRINTPACKETDEBUG
+                #ifdef DEBUG
                    pc.printf( "Receive Buffer: " );       //  debug print received packet
                     unsigned int i = 0;
                     for( i = 0 ; i < buffPtr ; i++ ){
@@ -398,7 +528,3 @@
 }   //  end serial2Handler function
 
 
-
-#ifdef __cplusplus
-}
-#endif