Bug fix release

Dependents:   AntiTheftGPS XbeeReceive XbeeSend Superball_Ball2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example3b.cpp Source File

example3b.cpp

00001 /*
00002     Copyright (c) 2011 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021     
00022     @file          example3b.cpp 
00023     @purpose       Demos a simple filter.
00024     @version       see ChangeLog.c
00025     @author        Andy Kirkham
00026 */
00027 
00028 /*
00029     This example shows how to use the new callback system. In the old system
00030     Mbed's FunctionPointer[1] type was used to store abd make calls to callbacks.
00031     However, that limits the callback function prototype to void func(void);
00032     which means we cannot pass parameters.
00033     
00034     This latest version of MODSERIAL now uses its own callback object. This allows
00035     the passing of a pointer to a class that holds information about the MODSERIAL
00036     object making the callback. As of version 1.18 one critcal piece of information
00037     is passed, a pointer to the MODSERIAL object. This allows callbacks to use the
00038     MODSERIAL functions and data.
00039         
00040     Additionally, since MODSERIAL and the callback parameter class MODSERIAL_IRQ_INFO
00041     are friends, MODSERIAL_IRQ_INFO can access the protected functions of MODSERIAL.
00042     This is used to ensure functions that can only be called during a callback
00043     can be invoked from a callback. 
00044     
00045     [1] http://mbed.org/projects/libraries/svn/mbed/trunk/FunctionPointer.h    
00046 */
00047 
00048 
00049 #ifdef COMPILE_EXAMPLE3_CODE_MODSERIAL
00050 
00051 #include "mbed.h"
00052 #include "MODSERIAL.h"
00053 
00054 void rxCallback(MODSERIAL_IRQ_INFO *info) {
00055 
00056     // Get the pointer to our MODSERIAL object that invoked this callback.
00057     MODSERIAL *pc = info->serial;
00058     
00059     // info->serial points at the MODSERIAL instance so we can use it to call
00060     // any of the public MODSERIAL functions that are normally available. So
00061     // there's now no need to use the global version (pc in our case) inside
00062     // callback functions.    
00063     char c = pc->rxGetLastChar(); // Where local pc variable is a pointer to the global MODSERIAL pc object.
00064     
00065     // The following is rather daft but demos the point.
00066     // Don't allow the letter "A" go into the RX buffer.
00067     // Basically acts as a filter to remove the letter "A" 
00068     // if it goes into the RX buffer.
00069     if (c == 'A') {
00070         // Note, we call the MODSERIAL_IRQ_INFO::rxDiscardLastChar() public function which
00071         // is permitted access to the protected version of MODSERIAL::rxDiscardLastChar()
00072         // within MODSERIAL (because they are friends). This ensures rxDiscardLastChar()
00073         // can only be called within an rxCallback function. 
00074         info->rxDiscardLastChar(); 
00075     }
00076 }
00077 
00078 #endif