Accepts RPC commands over bluetooth (RN42)

Dependencies:   mbed m3pi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialRPCInterface.cpp Source File

SerialRPCInterface.cpp

00001  /**
00002 * @section LICENSE
00003 *Copyright (c) 2010 ARM Ltd.
00004 * 
00005 *Permission is hereby granted, free of charge, to any person obtaining a copy
00006 *of this software and associated documentation files (the "Software"), to deal
00007 *in the Software without restriction, including without limitation the rights
00008 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 *copies of the Software, and to permit persons to whom the Software is
00010 *furnished to do so, subject to the following conditions:
00011 * 
00012 *The above copyright notice and this permission notice shall be included in
00013 *all copies or substantial portions of the Software.
00014 * 
00015 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 *THE SOFTWARE.
00022 *
00023 * 
00024 * @section DESCRIPTION
00025 *
00026 *This class sets up RPC communication. This allows objects on mbed to be controlled. Objects can be created or existing objects can be used
00027 */
00028 #include "SerialRPCInterface.h"
00029 
00030 using namespace mbed;
00031 
00032 //Requires multiple contstructors for each type, serial to set different pin numbers, TCP for port.
00033 SerialRPCInterface::SerialRPCInterface(PinName tx, PinName rx, int baud):pc(tx, rx) {
00034     _RegClasses();
00035     _enabled  = true;
00036      pc.attach(this, &SerialRPCInterface::_RPCSerial, Serial::RxIrq);
00037      if(baud != 9600)pc.baud(baud);
00038 }
00039 
00040 void SerialRPCInterface::_RegClasses(void){
00041     //Register classes with base 
00042     Base::add_rpc_class<AnalogIn>();
00043     Base::add_rpc_class<AnalogOut>();
00044     Base::add_rpc_class<DigitalIn>();
00045     Base::add_rpc_class<DigitalOut>();
00046     Base::add_rpc_class<DigitalInOut>();
00047     Base::add_rpc_class<PwmOut>();
00048     Base::add_rpc_class<Timer>();
00049     Base::add_rpc_class<BusOut>();
00050     Base::add_rpc_class<BusIn>();
00051     Base::add_rpc_class<BusInOut>();
00052     Base::add_rpc_class<Serial>();
00053 }
00054 
00055 void SerialRPCInterface::Disable(void){
00056      _enabled = false;
00057  }
00058 void SerialRPCInterface::Enable(void){
00059     _enabled = true;
00060 }
00061 void SerialRPCInterface::_MsgProcess(void) {
00062     if(_enabled == true){
00063         rpc(_command, _response);
00064     }
00065 }
00066 
00067 void SerialRPCInterface::_RPCSerial() {
00068     _RPCflag = true;
00069     if(_enabled == true){
00070         pc.gets(_command, 256);
00071         _MsgProcess();
00072         pc.printf("%s\n", _response);
00073     }
00074     _RPCflag = false;
00075 }