RTOS Support.

Dependents:   SerialRPC_rtos_example

Fork of RPCInterface by Michael Walker

Revision:
9:2aa76667c340
Parent:
8:682c65afe534
--- a/SerialRPCInterface.cpp	Sat Jan 28 19:12:00 2012 +0000
+++ b/SerialRPCInterface.cpp	Thu Apr 09 18:02:10 2015 +0000
@@ -30,29 +30,32 @@
 using namespace mbed;
 
 //Requires multiple contstructors for each type, serial to set different pin numbers, TCP for port.
-SerialRPCInterface::SerialRPCInterface(PinName tx, PinName rx, int baud):pc(tx, rx) {
+SerialRPCInterface::SerialRPCInterface(PinName tx, PinName rx, int baud):pc(tx, rx), _thread(SerialRPCInterface::_MsgProcessStarter, this, osPriorityNormal, 1024) {
     _RegClasses();
     _enabled  = true;
+    _command_ptr = _command;
      pc.attach(this, &SerialRPCInterface::_RPCSerial, Serial::RxIrq);
      if(baud != 9600)pc.baud(baud);
 }
 
 void SerialRPCInterface::_RegClasses(void){
     //Register classes with base 
-    Base::add_rpc_class<AnalogIn>();
-    Base::add_rpc_class<DigitalIn>();
-    Base::add_rpc_class<DigitalOut>();
-    Base::add_rpc_class<DigitalInOut>();
-    Base::add_rpc_class<PwmOut>();
-    Base::add_rpc_class<Timer>();
-    Base::add_rpc_class<BusOut>();
-    Base::add_rpc_class<BusIn>();
-    Base::add_rpc_class<BusInOut>();
-    Base::add_rpc_class<Serial>();
+    //RPC::add_rpc_class<RpcAnalogIn>();
+    RPC::add_rpc_class<RpcDigitalIn>();
+    RPC::add_rpc_class<RpcDigitalOut>();
+    RPC::add_rpc_class<RpcDigitalInOut>();
+    RPC::add_rpc_class<RpcPwmOut>();
+    RPC::add_rpc_class<RpcTimer>();
+    //RPC::add_rpc_class<RpcBusOut>();
+    //RPC::add_rpc_class<RpcBusIn>();
+    //RPC::add_rpc_class<RpcBusInOut>();
+    RPC::add_rpc_class<RpcSerial>();
+    RPC::add_rpc_class<RpcSPI>();
+    //RPC::add_rpc_class<RpcI2C>();
     
     //AnalogOut not avaliable on mbed LPC11U24 so only compile for other devices
     #if !defined(TARGET_LPC11U24) 
-    Base::add_rpc_class<AnalogOut>();
+    //RPC::add_rpc_class<RpcAnalogOut>();
     #endif
 }
 
@@ -62,18 +65,38 @@
 void SerialRPCInterface::Enable(void){
     _enabled = true;
 }
+void SerialRPCInterface::_MsgProcessStarter(const void *args){
+    SerialRPCInterface *instance = (SerialRPCInterface*)args;
+    while(1){
+        instance->_MsgProcess();
+    }
+}
 void SerialRPCInterface::_MsgProcess(void) {
-    if(_enabled == true){
-        rpc(_command, _response);
+    osEvent evt = _commandMail.get();
+    if(evt.status == osEventMail){
+        mail_t *mail = (mail_t*)evt.value.p;
+        if(_enabled == true){
+            RPC::call(_command, _response);
+            pc.printf("%s\n", _response);
+        }
+        _commandMail.free(mail);
     }
 }
 
 void SerialRPCInterface::_RPCSerial() {
     _RPCflag = true;
     if(_enabled == true){
-        pc.gets(_command, 256);
-        _MsgProcess();
-        pc.printf("%s\n", _response);
+        char c = pc.getc();
+        if(c == 13){
+            *_command_ptr = '\0';
+            mail_t *mail = _commandMail.alloc();
+            strcpy(mail->command, _command);
+            _commandMail.put(mail);
+            _command_ptr = _command;
+        }else if(_command_ptr - _command < 256){
+            *_command_ptr = c;
+            _command_ptr++;
+        }
     }
     _RPCflag = false;
 }