AjK@18: /* AjK@18: Copyright (c) 2011 Andy Kirkham AjK@18: AjK@18: Permission is hereby granted, free of charge, to any person obtaining a copy AjK@18: of this software and associated documentation files (the "Software"), to deal AjK@18: in the Software without restriction, including without limitation the rights AjK@18: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell AjK@18: copies of the Software, and to permit persons to whom the Software is AjK@18: furnished to do so, subject to the following conditions: AjK@18: AjK@18: The above copyright notice and this permission notice shall be included in AjK@18: all copies or substantial portions of the Software. AjK@18: AjK@18: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR AjK@18: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, AjK@18: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AjK@18: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AjK@18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, AjK@18: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN AjK@18: THE SOFTWARE. AjK@18: AjK@18: @file example3.cpp AjK@18: @purpose Demos a simple filter. AjK@18: @version see ChangeLog.c AjK@18: @author Andy Kirkham AjK@18: */ AjK@18: AjK@18: /* AjK@18: This example shows how to use the new callback system. In the old system AjK@18: Mbed's FunctionPointer[1] type was used to store abd make calls to callbacks. AjK@18: However, that limits the callback function prototype to void func(void); AjK@18: which means we cannot pass parameters. AjK@18: AjK@18: This latest version of MODSERIAL now uses its own callback object. This allows AjK@18: the passing of a pointer to a class that holds information about the MODSERIAL AjK@18: object making the callback. As of version 1.18 one critcal piece of information AjK@18: is passed, a pointer to the MODSERIAL object. This allows callbacks to use the AjK@18: MODSERIAL functions and data. AjK@18: AjK@18: Additionally, since MODSERIAL and the callback parameter class MODSERIAL_IRQ_INFO AjK@18: are friends, MODSERIAL_IRQ_INFO can access the protected functions of MODSERIAL. AjK@18: This is used to ensure functions that can only be called during a callback AjK@18: can be invoked from a callback. AjK@18: AjK@18: [1] http://mbed.org/projects/libraries/svn/mbed/trunk/FunctionPointer.h AjK@18: */ AjK@18: AjK@18: #ifdef COMPILE_EXAMPLE3_CODE_MODSERIAL AjK@18: AjK@18: #include "mbed.h" AjK@18: #include "MODSERIAL.h" AjK@18: AjK@18: DigitalOut led1(LED1); AjK@18: AjK@18: MODSERIAL pc(USBTX, USBRX); AjK@18: AjK@18: // The following callback is defined in example3b.cpp AjK@18: //! @see example3b.cpp AjK@18: void rxCallback(MODSERIAL_IRQ_INFO *info); AjK@18: AjK@18: int main() { AjK@18: AjK@18: int life_counter = 0; AjK@18: AjK@18: pc.baud(115200); AjK@18: AjK@18: pc.attach(&rxCallback, MODSERIAL::RxIrq); AjK@18: AjK@18: while(1) { AjK@18: // Echo back any chars we get except 'A' which is filtered by the rxCallback. AjK@18: if (pc.readable()) { AjK@18: pc.putc(pc.getc()); AjK@18: } AjK@18: AjK@18: // Toggle LED1 every so often to show we are alive. AjK@18: if (life_counter++ == 1000000) { AjK@18: life_counter = 0; AjK@18: led1 = !led1; AjK@18: } AjK@18: } AjK@18: } AjK@18: AjK@18: #endif