• Main Page
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

mComm_custom.c

Go to the documentation of this file.
00001 
00002 #include "mTouch.h"
00003 #include "mComm.h"
00004 
00005 #if defined(MCOMM_ENABLE_CUSTOM_OPCODE) && defined(MCOMM_TWO_WAY_ENABLED)
00006 
00007     // 
00008     // Custom Output Buffer
00009     //
00010     uint8_t mComm_myCustomBuffer[MTOUCH_STATEMASK_BYTES + 1];
00011     uint8_t mComm_GUICustomVariable;
00012 
00013     //
00014     // Custom Callback Function
00015     //
00016     // This function is an example of a custom callback used in UART
00017     // two-way communications when the application wants to send a
00018     // special packet based on custom conditions.
00019     //
00020     // The callback function should return either a 1 or 0 to tell
00021     // the mComm_Service() function whether or not there is custom
00022     // data to be sent at this time.
00023     //
00024     // This function is set as the callback function in mComm_config.h
00025     // by setting the MCOMM_CUSTOM_CALLBACK value.
00026     //
00027     uint8_t mComm_CustomCallback(void)
00028     {
00029         #define MCOMM_GUICUSTOM_STATE_BIT   0x01    // Use bit 0 to turn on/off sensor state 'onChange' packets
00030         
00031         return mTouch_state.buttonStateChange && (mComm_GUICustomVariable & MCOMM_GUICUSTOM_STATE_BIT);      
00032         // This mTouch flag is updated after each decode. If any sensors have changed 
00033         // state, we will return a 1 - signalling that we wish to send out a custom packet.
00034         // Also note - our custom variable must be equal to '1' as well. This allows
00035         // the master to turn off the custom output packet if desired.
00036     }
00037 
00038     //
00039     // Custom Process Function
00040     //
00041     // This function's is an example of a custom process function used
00042     // for any supported two-way protocol.
00043     //
00044     // The process function should process the input buffer and
00045     // prepare to either process a custom write request or custom read 
00046     // request using an iterator function. Which iterator function is
00047     // used will determine what variables should be initialized.
00048     //
00049     // In this example, the mComm_ramReadIterator() is used. This 
00050     // mComm function will output whatever variable is pointed to by
00051     // the output vector - so we need to initialize the output vector 
00052     // to tell the iterator what to output.
00053     //
00054     // Note: Which iterator function to use is determined by the
00055     //       MCOMM_CUSTOM_READ_ITERATOR and MCOMM_CUSTOM_WRITE_ITERATOR
00056     //       values in mComm_config.h. By using the already-implemented
00057     //       mComm_ramReadIterator() function, we are saving on program
00058     //       memory.
00059     //
00060     // In this example, the output vector is pointed to my custom buffer
00061     //       which is loaded with a custom opcode of my choosing and
00062     //       the data I want to output.
00063     //
00064     void mComm_CustomProcess(void)
00065     {
00066         mComm_myCustomBuffer[0]     = 0x00;     // My custom opcode value telling the master
00067                                                 // that I am sending an 'onChange' mTouch_stateMask
00068                                                 // update.
00069         
00070         #if MTOUCH_STATEMASK_BYTES > 0
00071         mComm_myCustomBuffer[1] = (uint8_t) mTouch_stateMask;
00072         #endif
00073         #if MTOUCH_STATEMASK_BYTES > 1
00074         mComm_myCustomBuffer[2] = (uint8_t) (mTouch_stateMask >> 8);
00075         #endif
00076         #if MTOUCH_STATEMASK_BYTES > 2
00077         mComm_myCustomBuffer[3] = (uint8_t) (mTouch_stateMask >> 16);
00078         #endif
00079         #if MTOUCH_STATEMASK_BYTES > 3
00080         mComm_myCustomBuffer[4] = (uint8_t) (mTouch_stateMask >> 24);
00081         #endif
00082     
00083         mComm_output.vector.pointer = (uint8_t*) (&mComm_myCustomBuffer);
00084         mComm_output.vector.length  = MTOUCH_STATEMASK_BYTES + 1;
00085         
00086         mTouch_state.buttonStateChange = 0;
00087     }
00088     
00089     uint8_t mComm_CustomWriteIterator(void)
00090     {
00091         RESET();
00092         return 0;
00093     }
00094     
00095     //
00096     // CUSTOM RESULT:
00097     //
00098     // This is the packet the PIC will output with the above example, assuming less than 8 sensors are enabled.
00099     //
00100     //  Byte0       Byte1       Byte2       Byte3       Byte4       Byte5
00101     //  ------------------------------------------------------------------
00102     //  [BREAK]     [0x04]      [0x07]      [0x00]      [0x01]      [0x00]
00103     //  ------------------------------------------------------------------
00104     //  |           |           |           |           |           |
00105     //  |           |           |           |           |           Checksum of the packet minus the byte length
00106     //  |           |           |           |           mTouch_stateMask value showing Sensor0 is pressed
00107     //  |           |           |           Custom opcode value stored at location 0 of mComm_myCustomBuffer
00108     //  |           |           mComm's opcode value for a 'custom read' command
00109     //  |           Byte length of the packet (not including the byte-length byte)
00110     //  Start of a packet using UART two way communications
00111     //
00112     
00113 #endif

mTouch Framework v2.1 documentation by  Click here to visit our website at www.microchip.com