Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RTno.cpp Source File

RTno.cpp

00001 /*******************************************
00002  * RTno.cpp
00003  * @author Yuki Suga
00004  * @copyright Yuki Suga (ysuga.net) Nov, 10th, 2010.
00005  * @license LGPLv3
00006  *****************************************/
00007 /********************************************
00008 It changed a little for mbed. 
00009 The serial buffer is not used. 
00010 2011/7/27 @nucho
00011 ********************************************/
00012 
00013 #include "RTno.h"
00014 //#include "WConstants.h"
00015 //#include "HardwareSerial.h"
00016 #include "ReceivePacket.h"
00017 #include "SendPacket.h"
00018 #include "Serial.h"
00019 #include "mbed.h"
00020 
00021 using namespace RTno;
00022 
00023 config_str conf;
00024 exec_cxt_str exec_cxt;
00025 
00026 enum {
00027     CREATED='C',
00028     INACTIVE='I',
00029     ACTIVE='A',
00030     ERROR='E',
00031 };
00032 
00033 char condition = CREATED;
00034 static char packet_buffer[64];
00035 
00036 void RTno::setup() {
00037     rtcconf();
00038     pc.baud(conf._default.baudrate);
00039 }
00040 
00041 
00042 static const char rtc_ok = RTNO_OK;
00043 static const char rtc_error = RTNO_ERROR;
00044 #define LP_RTC_OK (&rtc_ok)
00045 #define LP_RTC_ERROR (&rtc_error)
00046 
00047 
00048 int main() {
00049     RTno::setup();
00050 
00051     while(1){
00052       RTno::loop();
00053     }
00054 }
00055 
00056 void RTno::loop() {
00057     ReceivePacket(packet_buffer);
00058 
00059     switch (condition) {
00060         case CREATED:
00061             if (packet_buffer[INTERFACE] == INITIALIZE) {
00062                 if (onInitialize() == RTC_OK) {
00063                     condition = INACTIVE;
00064                     SendPacket(INITIALIZE, 1, LP_RTC_OK);
00065                 } else {
00066                     condition = ERROR;
00067                     SendPacket(INITIALIZE, 1, LP_RTC_ERROR);
00068                 }
00069             }
00070             break;
00071         case ERROR:
00072             if (packet_buffer[INTERFACE] == ONERROR) {
00073                 onError();
00074                 SendPacket(ONERROR, 1, LP_RTC_OK);
00075             } else if (packet_buffer[INTERFACE] == RESET) {
00076                 if (onReset() == RTC_OK) {
00077                     condition = INACTIVE;
00078                     SendPacket(RESET, 1, LP_RTC_OK);
00079                 } else {
00080                     condition = ERROR;
00081                     SendPacket(RESET, 1, LP_RTC_ERROR);
00082                 }
00083             }
00084             break;
00085         case INACTIVE:
00086             if (packet_buffer[INTERFACE] == ACTIVATE) {
00087                 if (onActivated() == RTC_OK) {
00088                     condition = ACTIVE;
00089                     SendPacket(ACTIVATE, 1, LP_RTC_OK);
00090                 } else {
00091                     condition = ERROR;
00092                     SendPacket(ACTIVATE, 1, LP_RTC_ERROR);
00093                 }
00094             }
00095             break;
00096         case ACTIVE:
00097             if (packet_buffer[INTERFACE] == DEACTIVATE) {
00098                 onDeactivated();
00099                 condition = INACTIVE;
00100                 SendPacket(DEACTIVATE, 1, LP_RTC_OK);
00101             } else if (packet_buffer[INTERFACE] == EXECUTE) {
00102                 if (onExecute() == RTC_OK) {
00103                     SendPacket(EXECUTE, 1, LP_RTC_OK);
00104                 } else {
00105                     condition = ERROR;
00106                     SendPacket(EXECUTE, 1, LP_RTC_ERROR);
00107                 }
00108             }
00109             break;
00110         default:
00111             condition = ERROR;
00112             break;
00113     }
00114 
00115     if (packet_buffer[INTERFACE] == GET_STATUS) {
00116         SendPacket(GET_STATUS, 1, &condition);
00117     }
00118 }
00119 
00120 
00121 /*NNN
00122 l:TimedLong
00123 d:TimedDouble
00124 f:TimedFloat
00125 L:TimedLongSeq
00126 D:TimedDoubleSeq
00127 F:TimedFloatSeq
00128 */
00129 
00130 void addInPort(InPortBase& Port) {
00131     int len = strlen(Port.GetName());
00132     char *data = (char*)malloc(len+1);
00133     data[0] = Port.GetTypeCode();
00134     memcpy(&(data[1]), Port.GetName(), len);
00135     SendPacket(ADD_INPORT, len+1, data);
00136     ReceivePacket(packet_buffer);
00137     free(data);
00138 }
00139 
00140 void addOutPort(OutPortBase& Port) {
00141     int len = strlen(Port.GetName());
00142     char *data = (char*)malloc(len+1);
00143     data[0] = Port.GetTypeCode();
00144     memcpy(&(data[1]), Port.GetName(), len);
00145     SendPacket(ADD_OUTPORT, len+1, data);
00146     ReceivePacket(packet_buffer);
00147     free(data);
00148 }