nucho
/
RTno_digitalInOut
main.cpp@1:b96a6ff9bb6f, 2011-08-01 (annotated)
- Committer:
- nucho
- Date:
- Mon Aug 01 14:45:58 2011 +0000
- Revision:
- 1:b96a6ff9bb6f
- Parent:
- 0:b14546a3cfab
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 | 1:b96a6ff9bb6f | 26 | #include "Serial.h" |
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 | 1:b96a6ff9bb6f | 57 | |
nucho | 0:b14546a3cfab | 58 | TimedLongSeq led; |
nucho | 0:b14546a3cfab | 59 | InPort ledIn("led", led); |
nucho | 0:b14546a3cfab | 60 | |
nucho | 0:b14546a3cfab | 61 | TimedLongSeq in0; |
nucho | 0:b14546a3cfab | 62 | InPort in0In("in0", in0); |
nucho | 0:b14546a3cfab | 63 | |
nucho | 0:b14546a3cfab | 64 | TimedLongSeq out0; |
nucho | 0:b14546a3cfab | 65 | OutPort out0Out("out0", out0); |
nucho | 0:b14546a3cfab | 66 | |
nucho | 1:b96a6ff9bb6f | 67 | |
nucho | 0:b14546a3cfab | 68 | ////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 69 | // on_initialize |
nucho | 0:b14546a3cfab | 70 | // |
nucho | 0:b14546a3cfab | 71 | // This function is called in the initialization |
nucho | 0:b14546a3cfab | 72 | // sequence. The sequence is triggered by the |
nucho | 0:b14546a3cfab | 73 | // PC. When the RTnoRTC is launched in the PC, |
nucho | 0:b14546a3cfab | 74 | // then, this function is remotely called |
nucho | 0:b14546a3cfab | 75 | // through the USB cable. |
nucho | 0:b14546a3cfab | 76 | // In on_initialize, usually DataPorts are added. |
nucho | 0:b14546a3cfab | 77 | // |
nucho | 0:b14546a3cfab | 78 | ////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 79 | int RTno::onInitialize() { |
nucho | 0:b14546a3cfab | 80 | /* Data Ports are added in this section. |
nucho | 0:b14546a3cfab | 81 | */ |
nucho | 0:b14546a3cfab | 82 | addInPort(ledIn); |
nucho | 0:b14546a3cfab | 83 | addInPort(in0In); |
nucho | 0:b14546a3cfab | 84 | addOutPort(out0Out); |
nucho | 0:b14546a3cfab | 85 | |
nucho | 0:b14546a3cfab | 86 | // Some initialization (like port direction setting) |
nucho | 0:b14546a3cfab | 87 | |
nucho | 0:b14546a3cfab | 88 | return RTC_OK; |
nucho | 0:b14546a3cfab | 89 | } |
nucho | 0:b14546a3cfab | 90 | |
nucho | 0:b14546a3cfab | 91 | //////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 92 | // on_activated |
nucho | 0:b14546a3cfab | 93 | // This function is called when the RTnoRTC |
nucho | 0:b14546a3cfab | 94 | // is activated. When the activation, the RTnoRTC |
nucho | 0:b14546a3cfab | 95 | // sends message to call this function remotely. |
nucho | 0:b14546a3cfab | 96 | // If this function is failed (return value |
nucho | 0:b14546a3cfab | 97 | // is RTC_ERROR), RTno will enter ERROR condition. |
nucho | 0:b14546a3cfab | 98 | //////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 99 | int RTno::onActivated() { |
nucho | 0:b14546a3cfab | 100 | // Write here initialization code. |
nucho | 0:b14546a3cfab | 101 | |
nucho | 0:b14546a3cfab | 102 | return RTC_OK; |
nucho | 0:b14546a3cfab | 103 | } |
nucho | 0:b14546a3cfab | 104 | |
nucho | 0:b14546a3cfab | 105 | ///////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 106 | // on_deactivated |
nucho | 0:b14546a3cfab | 107 | // This function is called when the RTnoRTC |
nucho | 0:b14546a3cfab | 108 | // is deactivated. |
nucho | 0:b14546a3cfab | 109 | ///////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 110 | int RTno::onDeactivated() { |
nucho | 0:b14546a3cfab | 111 | // Write here finalization code. |
nucho | 0:b14546a3cfab | 112 | |
nucho | 0:b14546a3cfab | 113 | return RTC_OK; |
nucho | 0:b14546a3cfab | 114 | } |
nucho | 0:b14546a3cfab | 115 | |
nucho | 0:b14546a3cfab | 116 | ////////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 117 | // This function is repeatedly called when the |
nucho | 0:b14546a3cfab | 118 | // RTno is in the ACTIVE condition. |
nucho | 0:b14546a3cfab | 119 | // If this function is failed (return value is |
nucho | 0:b14546a3cfab | 120 | // RTC_ERROR), RTno immediately enter into the |
nucho | 0:b14546a3cfab | 121 | // ERROR condition.r |
nucho | 0:b14546a3cfab | 122 | ////////////////////////////////////////////// |
nucho | 0:b14546a3cfab | 123 | int RTno::onExecute() { |
nucho | 0:b14546a3cfab | 124 | |
nucho | 0:b14546a3cfab | 125 | /* |
nucho | 0:b14546a3cfab | 126 | * Input digital data |
nucho | 0:b14546a3cfab | 127 | */ |
nucho | 1:b96a6ff9bb6f | 128 | |
nucho | 0:b14546a3cfab | 129 | if (ledIn.isNew()) { |
nucho | 0:b14546a3cfab | 130 | ledIn.read(); |
nucho | 0:b14546a3cfab | 131 | for (int i = 0; i < led.data.length() && i < 4; i++) { |
nucho | 0:b14546a3cfab | 132 | leds[i] =led.data[i]; |
nucho | 0:b14546a3cfab | 133 | } |
nucho | 0:b14546a3cfab | 134 | } |
nucho | 0:b14546a3cfab | 135 | |
nucho | 1:b96a6ff9bb6f | 136 | |
nucho | 1:b96a6ff9bb6f | 137 | if (in0In.isNew()) { |
nucho | 1:b96a6ff9bb6f | 138 | in0In.read(); |
nucho | 1:b96a6ff9bb6f | 139 | for (int i = 0; i < in0.data.length() && i < 6; i++) { |
nucho | 1:b96a6ff9bb6f | 140 | outPorts[i] =in0.data[i]; |
nucho | 1:b96a6ff9bb6f | 141 | } |
nucho | 0:b14546a3cfab | 142 | } |
nucho | 1:b96a6ff9bb6f | 143 | |
nucho | 0:b14546a3cfab | 144 | |
nucho | 0:b14546a3cfab | 145 | /* |
nucho | 0:b14546a3cfab | 146 | * Output digital data in Voltage unit. |
nucho | 0:b14546a3cfab | 147 | */ |
nucho | 0:b14546a3cfab | 148 | out0.data.length(6); |
nucho | 0:b14546a3cfab | 149 | for (int i = 0; i < 6; i++) { |
nucho | 1:b96a6ff9bb6f | 150 | out0.data[i] = inPorts[i]; |
nucho | 0:b14546a3cfab | 151 | } |
nucho | 0:b14546a3cfab | 152 | out0Out.write(); |
nucho | 0:b14546a3cfab | 153 | |
nucho | 0:b14546a3cfab | 154 | return RTC_OK; |
nucho | 0:b14546a3cfab | 155 | } |
nucho | 0:b14546a3cfab | 156 | |
nucho | 0:b14546a3cfab | 157 | |
nucho | 0:b14546a3cfab | 158 | ////////////////////////////////////// |
nucho | 0:b14546a3cfab | 159 | // on_error |
nucho | 0:b14546a3cfab | 160 | // This function is repeatedly called when |
nucho | 0:b14546a3cfab | 161 | // the RTno is in the ERROR condition. |
nucho | 0:b14546a3cfab | 162 | // The ERROR condition can be recovered, |
nucho | 0:b14546a3cfab | 163 | // when the RTno is reset. |
nucho | 0:b14546a3cfab | 164 | /////////////////////////////////////// |
nucho | 0:b14546a3cfab | 165 | int RTno::onError() { |
nucho | 0:b14546a3cfab | 166 | return RTC_OK; |
nucho | 0:b14546a3cfab | 167 | } |
nucho | 0:b14546a3cfab | 168 | |
nucho | 0:b14546a3cfab | 169 | //////////////////////////////////////// |
nucho | 0:b14546a3cfab | 170 | // This function is called when |
nucho | 0:b14546a3cfab | 171 | // the RTno is reset. If on_reset is |
nucho | 0:b14546a3cfab | 172 | // succeeded, the RTno will enter into |
nucho | 0:b14546a3cfab | 173 | // the INACTIVE condition. If failed |
nucho | 0:b14546a3cfab | 174 | // (return value is RTC_ERROR), RTno |
nucho | 0:b14546a3cfab | 175 | // will stay in ERROR condition.ec |
nucho | 0:b14546a3cfab | 176 | /////////////////////////////////////// |
nucho | 0:b14546a3cfab | 177 | int RTno::onReset() { |
nucho | 0:b14546a3cfab | 178 | return RTC_OK; |
nucho | 0:b14546a3cfab | 179 | } |