nucho
/
RTno_digitalInOut
main.cpp@0:b14546a3cfab, 2011-07-29 (annotated)
- Committer:
- nucho
- Date:
- Fri Jul 29 11:23:06 2011 +0000
- Revision:
- 0:b14546a3cfab
- Child:
- 1:b96a6ff9bb6f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nucho | 0:b14546a3cfab | 1 | /** |
nucho | 0:b14546a3cfab | 2 | * digitalInOut.pde |
nucho | 0:b14546a3cfab | 3 | * RTno is RT-middleware and arduino. |
nucho | 0:b14546a3cfab | 4 | * |
nucho | 0:b14546a3cfab | 5 | * Using RTno, arduino device can communicate any RT-components |
nucho | 0:b14546a3cfab | 6 | * through the RTno-proxy component which is launched in PC. |
nucho | 0:b14546a3cfab | 7 | * Connect arduino with USB, and program with RTno library. |
nucho | 0:b14546a3cfab | 8 | * You do not have to define any protocols to establish communication |
nucho | 0:b14546a3cfab | 9 | * between arduino and PC. |
nucho | 0:b14546a3cfab | 10 | * |
nucho | 0:b14546a3cfab | 11 | * Using RTno, you must not define the function "setup" and "loop". |
nucho | 0:b14546a3cfab | 12 | * Those functions are automatically defined in the RTno libarary. |
nucho | 0:b14546a3cfab | 13 | * You, developers, must define following functions: |
nucho | 0:b14546a3cfab | 14 | * int onInitialize(void); |
nucho | 0:b14546a3cfab | 15 | * int onActivated(void); |
nucho | 0:b14546a3cfab | 16 | * int onDeactivated(void); |
nucho | 0:b14546a3cfab | 17 | * int onExecute(void); |
nucho | 0:b14546a3cfab | 18 | * int onError(void); |
nucho | 0:b14546a3cfab | 19 | * int onReset(void); |
nucho | 0:b14546a3cfab | 20 | * These functions are spontaneously called by the RTno-proxy |
nucho | 0:b14546a3cfab | 21 | * RT-component which is launched in the PC. |
nucho | 0:b14546a3cfab | 22 | */ |
nucho | 0:b14546a3cfab | 23 | |
nucho | 0:b14546a3cfab | 24 | #include "mbed.h" |
nucho | 0:b14546a3cfab | 25 | #include "RTno.h" |
nucho | 0:b14546a3cfab | 26 | |
nucho | 0:b14546a3cfab | 27 | DigitalIn inPorts[] = {(p5), (p6),(p7),(p8),(p9),(p10)}; |
nucho | 0:b14546a3cfab | 28 | DigitalOut outPorts[] = {(p15),(p16),(p17),(p18),(p19),(p20)}; |
nucho | 0:b14546a3cfab | 29 | DigitalOut leds[] = {(LED1),(LED2),(LED3),(LED4)}; |
nucho | 0:b14546a3cfab | 30 | |
nucho | 0:b14546a3cfab | 31 | /** |
nucho | 0:b14546a3cfab | 32 | * This function is called at first. |
nucho | 0:b14546a3cfab | 33 | * conf._default.baudrate: baudrate of serial communication |
nucho | 0:b14546a3cfab | 34 | * exec_cxt.periodic.type: reserved but not used. |
nucho | 0:b14546a3cfab | 35 | */ |
nucho | 0:b14546a3cfab | 36 | void rtcconf(void) { |
nucho | 0:b14546a3cfab | 37 | conf._default.baudrate = 115200; |
nucho | 0:b14546a3cfab | 38 | exec_cxt.periodic.type = ProxySynchronousExecutionContext; |
nucho | 0:b14546a3cfab | 39 | } |
nucho | 0:b14546a3cfab | 40 | |
nucho | 0:b14546a3cfab | 41 | /** |
nucho | 0:b14546a3cfab | 42 | * Declaration Division: |
nucho | 0:b14546a3cfab | 43 | * |
nucho | 0:b14546a3cfab | 44 | * DataPort and Data Buffer should be placed here. |
nucho | 0:b14546a3cfab | 45 | * |
nucho | 0:b14546a3cfab | 46 | * Currently, following 6 types are available. |
nucho | 0:b14546a3cfab | 47 | * TimedLong: |
nucho | 0:b14546a3cfab | 48 | * TimedDouble: |
nucho | 0:b14546a3cfab | 49 | * TimedFloat: |
nucho | 0:b14546a3cfab | 50 | * TimedLongSeq: |
nucho | 0:b14546a3cfab | 51 | * TimedDoubleSeq: |
nucho | 0:b14546a3cfab | 52 | * TimedFloatSeq: |
nucho | 0:b14546a3cfab | 53 | * |
nucho | 0:b14546a3cfab | 54 | * Please refer following comments. If you need to use some ports, |
nucho | 0:b14546a3cfab | 55 | * uncomment the line you want to declare. |
nucho | 0:b14546a3cfab | 56 | **/ |
nucho | 0:b14546a3cfab | 57 | TimedLongSeq led; |
nucho | 0:b14546a3cfab | 58 | InPort ledIn("led", led); |
nucho | 0:b14546a3cfab | 59 | |
nucho | 0:b14546a3cfab | 60 | TimedLongSeq in0; |
nucho | 0:b14546a3cfab | 61 | InPort in0In("in0", in0); |
nucho | 0:b14546a3cfab | 62 | |
nucho | 0:b14546a3cfab | 63 | TimedLongSeq out0; |
nucho | 0:b14546a3cfab | 64 | OutPort out0Out("out0", out0); |
nucho | 0:b14546a3cfab | 65 | |
nucho | 0:b14546a3cfab | 66 | ////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 67 | // on_initialize |
nucho | 0:b14546a3cfab | 68 | // |
nucho | 0:b14546a3cfab | 69 | // This function is called in the initialization |
nucho | 0:b14546a3cfab | 70 | // sequence. The sequence is triggered by the |
nucho | 0:b14546a3cfab | 71 | // PC. When the RTnoRTC is launched in the PC, |
nucho | 0:b14546a3cfab | 72 | // then, this function is remotely called |
nucho | 0:b14546a3cfab | 73 | // through the USB cable. |
nucho | 0:b14546a3cfab | 74 | // In on_initialize, usually DataPorts are added. |
nucho | 0:b14546a3cfab | 75 | // |
nucho | 0:b14546a3cfab | 76 | ////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 77 | int RTno::onInitialize() { |
nucho | 0:b14546a3cfab | 78 | /* Data Ports are added in this section. |
nucho | 0:b14546a3cfab | 79 | */ |
nucho | 0:b14546a3cfab | 80 | addInPort(ledIn); |
nucho | 0:b14546a3cfab | 81 | addInPort(in0In); |
nucho | 0:b14546a3cfab | 82 | addOutPort(out0Out); |
nucho | 0:b14546a3cfab | 83 | |
nucho | 0:b14546a3cfab | 84 | // Some initialization (like port direction setting) |
nucho | 0:b14546a3cfab | 85 | |
nucho | 0:b14546a3cfab | 86 | return RTC_OK; |
nucho | 0:b14546a3cfab | 87 | } |
nucho | 0:b14546a3cfab | 88 | |
nucho | 0:b14546a3cfab | 89 | //////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 90 | // on_activated |
nucho | 0:b14546a3cfab | 91 | // This function is called when the RTnoRTC |
nucho | 0:b14546a3cfab | 92 | // is activated. When the activation, the RTnoRTC |
nucho | 0:b14546a3cfab | 93 | // sends message to call this function remotely. |
nucho | 0:b14546a3cfab | 94 | // If this function is failed (return value |
nucho | 0:b14546a3cfab | 95 | // is RTC_ERROR), RTno will enter ERROR condition. |
nucho | 0:b14546a3cfab | 96 | //////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 97 | int RTno::onActivated() { |
nucho | 0:b14546a3cfab | 98 | // Write here initialization code. |
nucho | 0:b14546a3cfab | 99 | |
nucho | 0:b14546a3cfab | 100 | return RTC_OK; |
nucho | 0:b14546a3cfab | 101 | } |
nucho | 0:b14546a3cfab | 102 | |
nucho | 0:b14546a3cfab | 103 | ///////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 104 | // on_deactivated |
nucho | 0:b14546a3cfab | 105 | // This function is called when the RTnoRTC |
nucho | 0:b14546a3cfab | 106 | // is deactivated. |
nucho | 0:b14546a3cfab | 107 | ///////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 108 | int RTno::onDeactivated() { |
nucho | 0:b14546a3cfab | 109 | // Write here finalization code. |
nucho | 0:b14546a3cfab | 110 | |
nucho | 0:b14546a3cfab | 111 | return RTC_OK; |
nucho | 0:b14546a3cfab | 112 | } |
nucho | 0:b14546a3cfab | 113 | |
nucho | 0:b14546a3cfab | 114 | ////////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 115 | // This function is repeatedly called when the |
nucho | 0:b14546a3cfab | 116 | // RTno is in the ACTIVE condition. |
nucho | 0:b14546a3cfab | 117 | // If this function is failed (return value is |
nucho | 0:b14546a3cfab | 118 | // RTC_ERROR), RTno immediately enter into the |
nucho | 0:b14546a3cfab | 119 | // ERROR condition.r |
nucho | 0:b14546a3cfab | 120 | ////////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 121 | int RTno::onExecute() { |
nucho | 0:b14546a3cfab | 122 | |
nucho | 0:b14546a3cfab | 123 | /* |
nucho | 0:b14546a3cfab | 124 | * Input digital data |
nucho | 0:b14546a3cfab | 125 | */ |
nucho | 0:b14546a3cfab | 126 | if (ledIn.isNew()) { |
nucho | 0:b14546a3cfab | 127 | ledIn.read(); |
nucho | 0:b14546a3cfab | 128 | for (int i = 0; i < led.data.length() && i < 4; i++) { |
nucho | 0:b14546a3cfab | 129 | leds[i] =led.data[i]; |
nucho | 0:b14546a3cfab | 130 | } |
nucho | 0:b14546a3cfab | 131 | } |
nucho | 0:b14546a3cfab | 132 | |
nucho | 0:b14546a3cfab | 133 | if (in0In.isNew()) { |
nucho | 0:b14546a3cfab | 134 | in0In.read(); |
nucho | 0:b14546a3cfab | 135 | for (int i = 0; i < in0.data.length() && i < 6; i++) { |
nucho | 0:b14546a3cfab | 136 | outPorts[i] =in0.data[i]; |
nucho | 0:b14546a3cfab | 137 | } |
nucho | 0:b14546a3cfab | 138 | } |
nucho | 0:b14546a3cfab | 139 | |
nucho | 0:b14546a3cfab | 140 | |
nucho | 0:b14546a3cfab | 141 | /* |
nucho | 0:b14546a3cfab | 142 | * Output digital data in Voltage unit. |
nucho | 0:b14546a3cfab | 143 | */ |
nucho | 0:b14546a3cfab | 144 | out0.data.length(6); |
nucho | 0:b14546a3cfab | 145 | for (int i = 0; i < 6; i++) { |
nucho | 0:b14546a3cfab | 146 | out0.data[i] = inPorts[i]; |
nucho | 0:b14546a3cfab | 147 | } |
nucho | 0:b14546a3cfab | 148 | out0Out.write(); |
nucho | 0:b14546a3cfab | 149 | |
nucho | 0:b14546a3cfab | 150 | return RTC_OK; |
nucho | 0:b14546a3cfab | 151 | } |
nucho | 0:b14546a3cfab | 152 | |
nucho | 0:b14546a3cfab | 153 | |
nucho | 0:b14546a3cfab | 154 | ////////////////////////////////////// |
nucho | 0:b14546a3cfab | 155 | // on_error |
nucho | 0:b14546a3cfab | 156 | // This function is repeatedly called when |
nucho | 0:b14546a3cfab | 157 | // the RTno is in the ERROR condition. |
nucho | 0:b14546a3cfab | 158 | // The ERROR condition can be recovered, |
nucho | 0:b14546a3cfab | 159 | // when the RTno is reset. |
nucho | 0:b14546a3cfab | 160 | /////////////////////////////////////// |
nucho | 0:b14546a3cfab | 161 | int RTno::onError() { |
nucho | 0:b14546a3cfab | 162 | return RTC_OK; |
nucho | 0:b14546a3cfab | 163 | } |
nucho | 0:b14546a3cfab | 164 | |
nucho | 0:b14546a3cfab | 165 | //////////////////////////////////////// |
nucho | 0:b14546a3cfab | 166 | // This function is called when |
nucho | 0:b14546a3cfab | 167 | // the RTno is reset. If on_reset is |
nucho | 0:b14546a3cfab | 168 | // succeeded, the RTno will enter into |
nucho | 0:b14546a3cfab | 169 | // the INACTIVE condition. If failed |
nucho | 0:b14546a3cfab | 170 | // (return value is RTC_ERROR), RTno |
nucho | 0:b14546a3cfab | 171 | // will stay in ERROR condition.ec |
nucho | 0:b14546a3cfab | 172 | /////////////////////////////////////// |
nucho | 0:b14546a3cfab | 173 | int RTno::onReset() { |
nucho | 0:b14546a3cfab | 174 | return RTC_OK; |
nucho | 0:b14546a3cfab | 175 | } |