This is test program for RTno for mbed!

Dependencies:   mbed

Committer:
ysuga
Date:
Thu Dec 22 14:54:29 2011 +0000
Revision:
0:f5d842e99c96
RTno Test v2.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ysuga 0:f5d842e99c96 1 #include "mbed.h"
ysuga 0:f5d842e99c96 2
ysuga 0:f5d842e99c96 3 DigitalOut myled(LED1);
ysuga 0:f5d842e99c96 4
ysuga 0:f5d842e99c96 5
ysuga 0:f5d842e99c96 6 /**
ysuga 0:f5d842e99c96 7 * RTno_Template.pde
ysuga 0:f5d842e99c96 8 * RTno is RT-middleware and arduino.
ysuga 0:f5d842e99c96 9 *
ysuga 0:f5d842e99c96 10 * Using RTno, arduino device can communicate any RT-components
ysuga 0:f5d842e99c96 11 * through the RTno-proxy component which is launched in PC.
ysuga 0:f5d842e99c96 12 * Connect arduino with USB, and program with RTno library.
ysuga 0:f5d842e99c96 13 * You do not have to define any protocols to establish communication
ysuga 0:f5d842e99c96 14 * between arduino and PC.
ysuga 0:f5d842e99c96 15 *
ysuga 0:f5d842e99c96 16 * Using RTno, you must not define the function "setup" and "loop".
ysuga 0:f5d842e99c96 17 * Those functions are automatically defined in the RTno libarary.
ysuga 0:f5d842e99c96 18 * You, developers, must define following functions:
ysuga 0:f5d842e99c96 19 * int onInitialize(void);
ysuga 0:f5d842e99c96 20 * int onActivated(void);
ysuga 0:f5d842e99c96 21 * int onDeactivated(void);
ysuga 0:f5d842e99c96 22 * int onExecute(void);
ysuga 0:f5d842e99c96 23 * int onError(void);
ysuga 0:f5d842e99c96 24 * int onReset(void);
ysuga 0:f5d842e99c96 25 * These functions are spontaneously called by the RTno-proxy
ysuga 0:f5d842e99c96 26 * RT-component which is launched in the PC.
ysuga 0:f5d842e99c96 27 * @author Yuki Suga
ysuga 0:f5d842e99c96 28 * This code is written/distributed for public-domain.
ysuga 0:f5d842e99c96 29 */
ysuga 0:f5d842e99c96 30
ysuga 0:f5d842e99c96 31 #include <RTno.h>
ysuga 0:f5d842e99c96 32
ysuga 0:f5d842e99c96 33 /**
ysuga 0:f5d842e99c96 34 * This function is called at first.
ysuga 0:f5d842e99c96 35 * conf._default.baudrate: baudrate of serial communication
ysuga 0:f5d842e99c96 36 * exec_cxt.periodic.type: reserved but not used.
ysuga 0:f5d842e99c96 37 */
ysuga 0:f5d842e99c96 38 void rtcconf(void) {
ysuga 0:f5d842e99c96 39 conf._default.connection_type = ConnectionTypeSerial1;
ysuga 0:f5d842e99c96 40 // conf._default.connection_type = ConnectionTypeSerial2; // This configuration is avaiable in Arduino-Mega
ysuga 0:f5d842e99c96 41 // conf._default.connection_type = ConnectionTypeSerial3; // This configuration is avaiable in Arduino-Mega
ysuga 0:f5d842e99c96 42 conf._default.baudrate = 57600;
ysuga 0:f5d842e99c96 43 exec_cxt.periodic.type = ProxySynchronousExecutionContext;
ysuga 0:f5d842e99c96 44 // exec_cxt.periodic.type = Timer1ExecutionContext; // onExecute is called by Timer1. Period must be specified by 'rate' option.
ysuga 0:f5d842e99c96 45 // *caution: TimerOne can not be used with PWM 9, 10.
ysuga 0:f5d842e99c96 46 // exec_cxt.periodic.rate = 1000; // [Hz] This option is indispensable when type is Timer*ExecutionContext.
ysuga 0:f5d842e99c96 47 }
ysuga 0:f5d842e99c96 48
ysuga 0:f5d842e99c96 49
ysuga 0:f5d842e99c96 50 /**
ysuga 0:f5d842e99c96 51 * Declaration Division:
ysuga 0:f5d842e99c96 52 *
ysuga 0:f5d842e99c96 53 * DataPort and Data Buffer should be placed here.
ysuga 0:f5d842e99c96 54 *
ysuga 0:f5d842e99c96 55 * available data types are as follows:
ysuga 0:f5d842e99c96 56 * TimedLong
ysuga 0:f5d842e99c96 57 * TimedDouble
ysuga 0:f5d842e99c96 58 * TimedFloat
ysuga 0:f5d842e99c96 59 * TimedLongSeq
ysuga 0:f5d842e99c96 60 * TimedDoubleSeq
ysuga 0:f5d842e99c96 61 * TimedFloatSeq
ysuga 0:f5d842e99c96 62 *
ysuga 0:f5d842e99c96 63 * Please refer following comments. If you need to use some ports,
ysuga 0:f5d842e99c96 64 * uncomment the line you want to declare.
ysuga 0:f5d842e99c96 65 **/
ysuga 0:f5d842e99c96 66 //TimedLong in0;
ysuga 0:f5d842e99c96 67 //InPort in0In("in0", in0);
ysuga 0:f5d842e99c96 68 //TimedLongSeq in0;
ysuga 0:f5d842e99c96 69 //InPort in0In("in0", in0);
ysuga 0:f5d842e99c96 70
ysuga 0:f5d842e99c96 71 //TimedLong out0;
ysuga 0:f5d842e99c96 72 //OutPort out0Out("out0", out0);
ysuga 0:f5d842e99c96 73 //TimedLongSeq out0;
ysuga 0:f5d842e99c96 74 //OutPort out0Out("out0", out0);
ysuga 0:f5d842e99c96 75 DigitalOut led1(LED1);
ysuga 0:f5d842e99c96 76 DigitalOut led2(LED2);
ysuga 0:f5d842e99c96 77 DigitalOut led3(LED3);
ysuga 0:f5d842e99c96 78 DigitalOut led4(LED4);
ysuga 0:f5d842e99c96 79 //////////////////////////////////////////
ysuga 0:f5d842e99c96 80 // on_initialize
ysuga 0:f5d842e99c96 81 //
ysuga 0:f5d842e99c96 82 // This function is called in the initialization
ysuga 0:f5d842e99c96 83 // sequence. The sequence is triggered by the
ysuga 0:f5d842e99c96 84 // PC. When the RTnoRTC is launched in the PC,
ysuga 0:f5d842e99c96 85 // then, this function is remotely called
ysuga 0:f5d842e99c96 86 // through the USB cable.
ysuga 0:f5d842e99c96 87 // In on_initialize, usually DataPorts are added.
ysuga 0:f5d842e99c96 88 //
ysuga 0:f5d842e99c96 89 //////////////////////////////////////////
ysuga 0:f5d842e99c96 90 int RTno::onInitialize() {
ysuga 0:f5d842e99c96 91 /* Data Ports are added in this section.
ysuga 0:f5d842e99c96 92 addInPort(in0In);
ysuga 0:f5d842e99c96 93 addInPort(in1In);
ysuga 0:f5d842e99c96 94 addOutPort(out0Out);
ysuga 0:f5d842e99c96 95 addOutPort(out1Out);
ysuga 0:f5d842e99c96 96 */
ysuga 0:f5d842e99c96 97 led1 = 1;
ysuga 0:f5d842e99c96 98 led2 = 0;
ysuga 0:f5d842e99c96 99 led3 = 0;
ysuga 0:f5d842e99c96 100 led4 = 0;
ysuga 0:f5d842e99c96 101
ysuga 0:f5d842e99c96 102 // Some initialization (like port direction setting)
ysuga 0:f5d842e99c96 103 // int LED = 13;
ysuga 0:f5d842e99c96 104 // pinMode(LED, OUTPUT);
ysuga 0:f5d842e99c96 105 return RTC_OK;
ysuga 0:f5d842e99c96 106 }
ysuga 0:f5d842e99c96 107
ysuga 0:f5d842e99c96 108 ////////////////////////////////////////////
ysuga 0:f5d842e99c96 109 // on_activated
ysuga 0:f5d842e99c96 110 // This function is called when the RTnoRTC
ysuga 0:f5d842e99c96 111 // is activated. When the activation, the RTnoRTC
ysuga 0:f5d842e99c96 112 // sends message to call this function remotely.
ysuga 0:f5d842e99c96 113 // If this function is failed (return value
ysuga 0:f5d842e99c96 114 // is RTC_ERROR), RTno will enter ERROR condition.
ysuga 0:f5d842e99c96 115 ////////////////////////////////////////////
ysuga 0:f5d842e99c96 116 int RTno::onActivated() {
ysuga 0:f5d842e99c96 117 // Write here initialization code.
ysuga 0:f5d842e99c96 118 led2 = 1;
ysuga 0:f5d842e99c96 119 led3 = 0;
ysuga 0:f5d842e99c96 120 led4 = 0;
ysuga 0:f5d842e99c96 121 return RTC_OK;
ysuga 0:f5d842e99c96 122 }
ysuga 0:f5d842e99c96 123
ysuga 0:f5d842e99c96 124 /////////////////////////////////////////////
ysuga 0:f5d842e99c96 125 // on_deactivated
ysuga 0:f5d842e99c96 126 // This function is called when the RTnoRTC
ysuga 0:f5d842e99c96 127 // is deactivated.
ysuga 0:f5d842e99c96 128 /////////////////////////////////////////////
ysuga 0:f5d842e99c96 129 int RTno::onDeactivated()
ysuga 0:f5d842e99c96 130 {
ysuga 0:f5d842e99c96 131 // Write here finalization code.
ysuga 0:f5d842e99c96 132 led2 = 0;
ysuga 0:f5d842e99c96 133 led3 = 0;
ysuga 0:f5d842e99c96 134 led4 = 0;
ysuga 0:f5d842e99c96 135 return RTC_OK;
ysuga 0:f5d842e99c96 136 }
ysuga 0:f5d842e99c96 137
ysuga 0:f5d842e99c96 138 //////////////////////////////////////////////
ysuga 0:f5d842e99c96 139 // This function is repeatedly called when the
ysuga 0:f5d842e99c96 140 // RTno is in the ACTIVE condition.
ysuga 0:f5d842e99c96 141 // If this function is failed (return value is
ysuga 0:f5d842e99c96 142 // RTC_ERROR), RTno immediately enter into the
ysuga 0:f5d842e99c96 143 // ERROR condition.r
ysuga 0:f5d842e99c96 144 //////////////////////////////////////////////
ysuga 0:f5d842e99c96 145 int RTno::onExecute() {
ysuga 0:f5d842e99c96 146 static int flag;
ysuga 0:f5d842e99c96 147 if(flag) {
ysuga 0:f5d842e99c96 148 led2 = 1;
ysuga 0:f5d842e99c96 149 flag = 0;
ysuga 0:f5d842e99c96 150 } else {
ysuga 0:f5d842e99c96 151 led2 = 0;
ysuga 0:f5d842e99c96 152 flag++;
ysuga 0:f5d842e99c96 153 }
ysuga 0:f5d842e99c96 154 /**
ysuga 0:f5d842e99c96 155 * Usage of InPort with premitive type.
ysuga 0:f5d842e99c96 156 if(in0In.isNew()) {
ysuga 0:f5d842e99c96 157 in0In.read();
ysuga 0:f5d842e99c96 158 long data = in0.data;
ysuga 0:f5d842e99c96 159 }
ysuga 0:f5d842e99c96 160 */
ysuga 0:f5d842e99c96 161
ysuga 0:f5d842e99c96 162 /**
ysuga 0:f5d842e99c96 163 * Usage of InPort with sequence type
ysuga 0:f5d842e99c96 164 if(in0In.isNew(&in1In)) {
ysuga 0:f5d842e99c96 165 in0In.read();
ysuga 0:f5d842e99c96 166 for(int i = 0;i < in0.data.length;i++) {
ysuga 0:f5d842e99c96 167 long data_buffer = in0.data[i];
ysuga 0:f5d842e99c96 168 }
ysuga 0:f5d842e99c96 169 }
ysuga 0:f5d842e99c96 170 */
ysuga 0:f5d842e99c96 171
ysuga 0:f5d842e99c96 172 /**
ysuga 0:f5d842e99c96 173 * Usage of OutPort with primitive type.
ysuga 0:f5d842e99c96 174 out0.data = 3.14159;
ysuga 0:f5d842e99c96 175 out0Out.write();
ysuga 0:f5d842e99c96 176 */
ysuga 0:f5d842e99c96 177
ysuga 0:f5d842e99c96 178 /**
ysuga 0:f5d842e99c96 179 * Usage of OutPort with sequence type.
ysuga 0:f5d842e99c96 180 out0.data.length(3);
ysuga 0:f5d842e99c96 181 out0.data[0] = 1.1;
ysuga 0:f5d842e99c96 182 out0.data[1] = 2.2;
ysuga 0:f5d842e99c96 183 out0.data[2] = 3.3;
ysuga 0:f5d842e99c96 184 out0Out.write();
ysuga 0:f5d842e99c96 185 */
ysuga 0:f5d842e99c96 186
ysuga 0:f5d842e99c96 187 return RTC_OK;
ysuga 0:f5d842e99c96 188 }
ysuga 0:f5d842e99c96 189
ysuga 0:f5d842e99c96 190
ysuga 0:f5d842e99c96 191 //////////////////////////////////////
ysuga 0:f5d842e99c96 192 // on_error
ysuga 0:f5d842e99c96 193 // This function is repeatedly called when
ysuga 0:f5d842e99c96 194 // the RTno is in the ERROR condition.
ysuga 0:f5d842e99c96 195 // The ERROR condition can be recovered,
ysuga 0:f5d842e99c96 196 // when the RTno is reset.
ysuga 0:f5d842e99c96 197 ///////////////////////////////////////
ysuga 0:f5d842e99c96 198 int RTno::onError()
ysuga 0:f5d842e99c96 199 {
ysuga 0:f5d842e99c96 200 return RTC_OK;
ysuga 0:f5d842e99c96 201 }
ysuga 0:f5d842e99c96 202
ysuga 0:f5d842e99c96 203 ////////////////////////////////////////
ysuga 0:f5d842e99c96 204 // This function is called when
ysuga 0:f5d842e99c96 205 // the RTno is reset. If on_reset is
ysuga 0:f5d842e99c96 206 // succeeded, the RTno will enter into
ysuga 0:f5d842e99c96 207 // the INACTIVE condition. If failed
ysuga 0:f5d842e99c96 208 // (return value is RTC_ERROR), RTno
ysuga 0:f5d842e99c96 209 // will stay in ERROR condition.ec
ysuga 0:f5d842e99c96 210 ///////////////////////////////////////
ysuga 0:f5d842e99c96 211 int RTno::onReset()
ysuga 0:f5d842e99c96 212 {
ysuga 0:f5d842e99c96 213 return RTC_OK;
ysuga 0:f5d842e99c96 214 }