RTnoV3_LED is example program which uses mbed. To know about RTno, visit: http://ysuga.net/robot_e/rtm_e/rtc_e/1065?lang=en
Dependencies: EthernetNetIf mbed RTnoV3
main.cpp@0:6cfb2284ea53, 2012-02-09 (annotated)
- Committer:
- ysuga
- Date:
- Thu Feb 09 05:56:37 2012 +0000
- Revision:
- 0:6cfb2284ea53
RTnoV3_LED first revision
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ysuga | 0:6cfb2284ea53 | 1 | #include "mbed.h" |
ysuga | 0:6cfb2284ea53 | 2 | /** |
ysuga | 0:6cfb2284ea53 | 3 | * RTno_Template.pde |
ysuga | 0:6cfb2284ea53 | 4 | * RTno is RT-middleware and arduino. |
ysuga | 0:6cfb2284ea53 | 5 | * |
ysuga | 0:6cfb2284ea53 | 6 | * Using RTno, arduino device can communicate any RT-components |
ysuga | 0:6cfb2284ea53 | 7 | * through the RTno-proxy component which is launched in PC. |
ysuga | 0:6cfb2284ea53 | 8 | * Connect arduino with USB, and program with RTno library. |
ysuga | 0:6cfb2284ea53 | 9 | * You do not have to define any protocols to establish communication |
ysuga | 0:6cfb2284ea53 | 10 | * between arduino and PC. |
ysuga | 0:6cfb2284ea53 | 11 | * |
ysuga | 0:6cfb2284ea53 | 12 | * Using RTno, you must not define the function "setup" and "loop". |
ysuga | 0:6cfb2284ea53 | 13 | * Those functions are automatically defined in the RTno libarary. |
ysuga | 0:6cfb2284ea53 | 14 | * You, developers, must define following functions: |
ysuga | 0:6cfb2284ea53 | 15 | * int onInitialize(void); |
ysuga | 0:6cfb2284ea53 | 16 | * int onActivated(void); |
ysuga | 0:6cfb2284ea53 | 17 | * int onDeactivated(void); |
ysuga | 0:6cfb2284ea53 | 18 | * int onExecute(void); |
ysuga | 0:6cfb2284ea53 | 19 | * int onError(void); |
ysuga | 0:6cfb2284ea53 | 20 | * int onReset(void); |
ysuga | 0:6cfb2284ea53 | 21 | * These functions are spontaneously called by the RTno-proxy |
ysuga | 0:6cfb2284ea53 | 22 | * RT-component which is launched in the PC. |
ysuga | 0:6cfb2284ea53 | 23 | * @author Yuki Suga |
ysuga | 0:6cfb2284ea53 | 24 | * This code is written/distributed for public-domain. |
ysuga | 0:6cfb2284ea53 | 25 | */ |
ysuga | 0:6cfb2284ea53 | 26 | |
ysuga | 0:6cfb2284ea53 | 27 | #include <RTno.h> |
ysuga | 0:6cfb2284ea53 | 28 | |
ysuga | 0:6cfb2284ea53 | 29 | /** |
ysuga | 0:6cfb2284ea53 | 30 | * This function is called at first. |
ysuga | 0:6cfb2284ea53 | 31 | * conf._default.baudrate: baudrate of serial communication |
ysuga | 0:6cfb2284ea53 | 32 | * exec_cxt.periodic.type: reserved but not used. |
ysuga | 0:6cfb2284ea53 | 33 | */ |
ysuga | 0:6cfb2284ea53 | 34 | void rtcconf(config_str& conf, exec_cxt_str& exec_cxt) { |
ysuga | 0:6cfb2284ea53 | 35 | // If you want to use Serial Connection, configure below: |
ysuga | 0:6cfb2284ea53 | 36 | conf._default.connection_type = ConnectionTypeSerialUSB; // USBTX & USBRX (In Windows, Driver must be updated.) |
ysuga | 0:6cfb2284ea53 | 37 | conf._default.baudrate = 57600; // This value is required when you select ConnectionTypeSerial* |
ysuga | 0:6cfb2284ea53 | 38 | exec_cxt.periodic.type = ProxySynchronousExecutionContext; |
ysuga | 0:6cfb2284ea53 | 39 | } |
ysuga | 0:6cfb2284ea53 | 40 | |
ysuga | 0:6cfb2284ea53 | 41 | |
ysuga | 0:6cfb2284ea53 | 42 | /** |
ysuga | 0:6cfb2284ea53 | 43 | * Declaration Division: |
ysuga | 0:6cfb2284ea53 | 44 | * |
ysuga | 0:6cfb2284ea53 | 45 | * DataPort and Data Buffer should be placed here. |
ysuga | 0:6cfb2284ea53 | 46 | * |
ysuga | 0:6cfb2284ea53 | 47 | * available data types are as follows: |
ysuga | 0:6cfb2284ea53 | 48 | * TimedLong |
ysuga | 0:6cfb2284ea53 | 49 | * TimedDouble |
ysuga | 0:6cfb2284ea53 | 50 | * TimedFloat |
ysuga | 0:6cfb2284ea53 | 51 | * TimedLongSeq |
ysuga | 0:6cfb2284ea53 | 52 | * TimedDoubleSeq |
ysuga | 0:6cfb2284ea53 | 53 | * TimedFloatSeq |
ysuga | 0:6cfb2284ea53 | 54 | * |
ysuga | 0:6cfb2284ea53 | 55 | * Please refer following comments. If you need to use some ports, |
ysuga | 0:6cfb2284ea53 | 56 | * uncomment the line you want to declare. |
ysuga | 0:6cfb2284ea53 | 57 | **/ |
ysuga | 0:6cfb2284ea53 | 58 | TimedLong in0; |
ysuga | 0:6cfb2284ea53 | 59 | InPort<TimedLong> in0In("in0", in0); |
ysuga | 0:6cfb2284ea53 | 60 | |
ysuga | 0:6cfb2284ea53 | 61 | DigitalOut led1(LED1); |
ysuga | 0:6cfb2284ea53 | 62 | DigitalOut led2(LED2); |
ysuga | 0:6cfb2284ea53 | 63 | DigitalOut led3(LED3); |
ysuga | 0:6cfb2284ea53 | 64 | DigitalOut led4(LED4); |
ysuga | 0:6cfb2284ea53 | 65 | ////////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 66 | // on_initialize |
ysuga | 0:6cfb2284ea53 | 67 | // |
ysuga | 0:6cfb2284ea53 | 68 | // This function is called in the initialization |
ysuga | 0:6cfb2284ea53 | 69 | // sequence. The sequence is triggered by the |
ysuga | 0:6cfb2284ea53 | 70 | // PC. When the RTnoRTC is launched in the PC, |
ysuga | 0:6cfb2284ea53 | 71 | // then, this function is remotely called |
ysuga | 0:6cfb2284ea53 | 72 | // through the USB cable. |
ysuga | 0:6cfb2284ea53 | 73 | // In on_initialize, usually DataPorts are added. |
ysuga | 0:6cfb2284ea53 | 74 | // |
ysuga | 0:6cfb2284ea53 | 75 | ////////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 76 | int RTno::onInitialize() { |
ysuga | 0:6cfb2284ea53 | 77 | /* Data Ports are added in this section. |
ysuga | 0:6cfb2284ea53 | 78 | */ |
ysuga | 0:6cfb2284ea53 | 79 | |
ysuga | 0:6cfb2284ea53 | 80 | addInPort(in0In); |
ysuga | 0:6cfb2284ea53 | 81 | |
ysuga | 0:6cfb2284ea53 | 82 | // Some initialization (like port direction setting) |
ysuga | 0:6cfb2284ea53 | 83 | led1 = 1; |
ysuga | 0:6cfb2284ea53 | 84 | led2 = 0; |
ysuga | 0:6cfb2284ea53 | 85 | led3 = 0; |
ysuga | 0:6cfb2284ea53 | 86 | led4 = 0; |
ysuga | 0:6cfb2284ea53 | 87 | return RTC_OK; |
ysuga | 0:6cfb2284ea53 | 88 | } |
ysuga | 0:6cfb2284ea53 | 89 | |
ysuga | 0:6cfb2284ea53 | 90 | //////////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 91 | // on_activated |
ysuga | 0:6cfb2284ea53 | 92 | // This function is called when the RTnoRTC |
ysuga | 0:6cfb2284ea53 | 93 | // is activated. When the activation, the RTnoRTC |
ysuga | 0:6cfb2284ea53 | 94 | // sends message to call this function remotely. |
ysuga | 0:6cfb2284ea53 | 95 | // If this function is failed (return value |
ysuga | 0:6cfb2284ea53 | 96 | // is RTC_ERROR), RTno will enter ERROR condition. |
ysuga | 0:6cfb2284ea53 | 97 | //////////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 98 | int RTno::onActivated() { |
ysuga | 0:6cfb2284ea53 | 99 | // Write here initialization code. |
ysuga | 0:6cfb2284ea53 | 100 | led2 = 1; |
ysuga | 0:6cfb2284ea53 | 101 | led3 = 0; |
ysuga | 0:6cfb2284ea53 | 102 | led4 = 0; |
ysuga | 0:6cfb2284ea53 | 103 | return RTC_OK; |
ysuga | 0:6cfb2284ea53 | 104 | } |
ysuga | 0:6cfb2284ea53 | 105 | |
ysuga | 0:6cfb2284ea53 | 106 | ///////////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 107 | // on_deactivated |
ysuga | 0:6cfb2284ea53 | 108 | // This function is called when the RTnoRTC |
ysuga | 0:6cfb2284ea53 | 109 | // is deactivated. |
ysuga | 0:6cfb2284ea53 | 110 | ///////////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 111 | int RTno::onDeactivated() |
ysuga | 0:6cfb2284ea53 | 112 | { |
ysuga | 0:6cfb2284ea53 | 113 | // Write here finalization code. |
ysuga | 0:6cfb2284ea53 | 114 | led2 = 0; |
ysuga | 0:6cfb2284ea53 | 115 | led3 = 0; |
ysuga | 0:6cfb2284ea53 | 116 | led4 = 0; |
ysuga | 0:6cfb2284ea53 | 117 | return RTC_OK; |
ysuga | 0:6cfb2284ea53 | 118 | } |
ysuga | 0:6cfb2284ea53 | 119 | |
ysuga | 0:6cfb2284ea53 | 120 | ////////////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 121 | // This function is repeatedly called when the |
ysuga | 0:6cfb2284ea53 | 122 | // RTno is in the ACTIVE condition. |
ysuga | 0:6cfb2284ea53 | 123 | // If this function is failed (return value is |
ysuga | 0:6cfb2284ea53 | 124 | // RTC_ERROR), RTno immediately enter into the |
ysuga | 0:6cfb2284ea53 | 125 | // ERROR condition.r |
ysuga | 0:6cfb2284ea53 | 126 | ////////////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 127 | int RTno::onExecute() { |
ysuga | 0:6cfb2284ea53 | 128 | static int flag; |
ysuga | 0:6cfb2284ea53 | 129 | if(flag) { |
ysuga | 0:6cfb2284ea53 | 130 | led3 = 1; |
ysuga | 0:6cfb2284ea53 | 131 | flag = 0; |
ysuga | 0:6cfb2284ea53 | 132 | } else { |
ysuga | 0:6cfb2284ea53 | 133 | led3 = 0; |
ysuga | 0:6cfb2284ea53 | 134 | flag++; |
ysuga | 0:6cfb2284ea53 | 135 | } |
ysuga | 0:6cfb2284ea53 | 136 | /** |
ysuga | 0:6cfb2284ea53 | 137 | * Usage of InPort with premitive type. |
ysuga | 0:6cfb2284ea53 | 138 | */ |
ysuga | 0:6cfb2284ea53 | 139 | if(in0In.isNew()) { |
ysuga | 0:6cfb2284ea53 | 140 | in0In.read(); |
ysuga | 0:6cfb2284ea53 | 141 | led4 = in0.data; |
ysuga | 0:6cfb2284ea53 | 142 | } |
ysuga | 0:6cfb2284ea53 | 143 | |
ysuga | 0:6cfb2284ea53 | 144 | return RTC_OK; |
ysuga | 0:6cfb2284ea53 | 145 | } |
ysuga | 0:6cfb2284ea53 | 146 | |
ysuga | 0:6cfb2284ea53 | 147 | |
ysuga | 0:6cfb2284ea53 | 148 | ////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 149 | // on_error |
ysuga | 0:6cfb2284ea53 | 150 | // This function is repeatedly called when |
ysuga | 0:6cfb2284ea53 | 151 | // the RTno is in the ERROR condition. |
ysuga | 0:6cfb2284ea53 | 152 | // The ERROR condition can be recovered, |
ysuga | 0:6cfb2284ea53 | 153 | // when the RTno is reset. |
ysuga | 0:6cfb2284ea53 | 154 | /////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 155 | int RTno::onError() |
ysuga | 0:6cfb2284ea53 | 156 | { |
ysuga | 0:6cfb2284ea53 | 157 | return RTC_OK; |
ysuga | 0:6cfb2284ea53 | 158 | } |
ysuga | 0:6cfb2284ea53 | 159 | |
ysuga | 0:6cfb2284ea53 | 160 | //////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 161 | // This function is called when |
ysuga | 0:6cfb2284ea53 | 162 | // the RTno is reset. If on_reset is |
ysuga | 0:6cfb2284ea53 | 163 | // succeeded, the RTno will enter into |
ysuga | 0:6cfb2284ea53 | 164 | // the INACTIVE condition. If failed |
ysuga | 0:6cfb2284ea53 | 165 | // (return value is RTC_ERROR), RTno |
ysuga | 0:6cfb2284ea53 | 166 | // will stay in ERROR condition.ec |
ysuga | 0:6cfb2284ea53 | 167 | /////////////////////////////////////// |
ysuga | 0:6cfb2284ea53 | 168 | int RTno::onReset() |
ysuga | 0:6cfb2284ea53 | 169 | { |
ysuga | 0:6cfb2284ea53 | 170 | return RTC_OK; |
ysuga | 0:6cfb2284ea53 | 171 | } |