User | Revision | Line number | New contents of line |
nucho |
0:b14546a3cfab
|
1
|
/*******************************************
|
nucho |
0:b14546a3cfab
|
2
|
* RTno.cpp
|
nucho |
0:b14546a3cfab
|
3
|
* @author Yuki Suga
|
nucho |
0:b14546a3cfab
|
4
|
* @copyright Yuki Suga (ysuga.net) Nov, 10th, 2010.
|
nucho |
0:b14546a3cfab
|
5
|
* @license LGPLv3
|
nucho |
0:b14546a3cfab
|
6
|
*****************************************/
|
nucho |
0:b14546a3cfab
|
7
|
/********************************************
|
nucho |
0:b14546a3cfab
|
8
|
It changed a little for mbed.
|
nucho |
0:b14546a3cfab
|
9
|
The serial buffer is not used.
|
nucho |
0:b14546a3cfab
|
10
|
2011/7/27 @nucho
|
nucho |
0:b14546a3cfab
|
11
|
********************************************/
|
nucho |
0:b14546a3cfab
|
12
|
|
nucho |
0:b14546a3cfab
|
13
|
#include "RTno.h"
|
nucho |
0:b14546a3cfab
|
14
|
//#include "WConstants.h"
|
nucho |
0:b14546a3cfab
|
15
|
//#include "HardwareSerial.h"
|
nucho |
0:b14546a3cfab
|
16
|
#include "ReceivePacket.h"
|
nucho |
0:b14546a3cfab
|
17
|
#include "SendPacket.h"
|
nucho |
0:b14546a3cfab
|
18
|
#include "Serial.h"
|
nucho |
0:b14546a3cfab
|
19
|
#include "mbed.h"
|
nucho |
0:b14546a3cfab
|
20
|
|
nucho |
0:b14546a3cfab
|
21
|
using namespace RTno;
|
nucho |
0:b14546a3cfab
|
22
|
|
nucho |
0:b14546a3cfab
|
23
|
config_str conf;
|
nucho |
0:b14546a3cfab
|
24
|
exec_cxt_str exec_cxt;
|
nucho |
0:b14546a3cfab
|
25
|
|
nucho |
0:b14546a3cfab
|
26
|
enum {
|
nucho |
0:b14546a3cfab
|
27
|
CREATED='C',
|
nucho |
0:b14546a3cfab
|
28
|
INACTIVE='I',
|
nucho |
0:b14546a3cfab
|
29
|
ACTIVE='A',
|
nucho |
0:b14546a3cfab
|
30
|
ERROR='E',
|
nucho |
0:b14546a3cfab
|
31
|
};
|
nucho |
0:b14546a3cfab
|
32
|
|
nucho |
0:b14546a3cfab
|
33
|
char condition = CREATED;
|
nucho |
0:b14546a3cfab
|
34
|
static char packet_buffer[64];
|
nucho |
0:b14546a3cfab
|
35
|
|
nucho |
0:b14546a3cfab
|
36
|
void RTno::setup() {
|
nucho |
0:b14546a3cfab
|
37
|
rtcconf();
|
nucho |
0:b14546a3cfab
|
38
|
pc.baud(conf._default.baudrate);
|
nucho |
0:b14546a3cfab
|
39
|
}
|
nucho |
0:b14546a3cfab
|
40
|
|
nucho |
0:b14546a3cfab
|
41
|
|
nucho |
0:b14546a3cfab
|
42
|
static const char rtc_ok = RTNO_OK;
|
nucho |
0:b14546a3cfab
|
43
|
static const char rtc_error = RTNO_ERROR;
|
nucho |
0:b14546a3cfab
|
44
|
#define LP_RTC_OK (&rtc_ok)
|
nucho |
0:b14546a3cfab
|
45
|
#define LP_RTC_ERROR (&rtc_error)
|
nucho |
0:b14546a3cfab
|
46
|
|
nucho |
0:b14546a3cfab
|
47
|
|
nucho |
0:b14546a3cfab
|
48
|
int main() {
|
nucho |
0:b14546a3cfab
|
49
|
RTno::setup();
|
nucho |
0:b14546a3cfab
|
50
|
|
nucho |
0:b14546a3cfab
|
51
|
while(1){
|
nucho |
0:b14546a3cfab
|
52
|
RTno::loop();
|
nucho |
0:b14546a3cfab
|
53
|
}
|
nucho |
0:b14546a3cfab
|
54
|
}
|
nucho |
0:b14546a3cfab
|
55
|
|
nucho |
0:b14546a3cfab
|
56
|
void RTno::loop() {
|
nucho |
0:b14546a3cfab
|
57
|
ReceivePacket(packet_buffer);
|
nucho |
0:b14546a3cfab
|
58
|
|
nucho |
0:b14546a3cfab
|
59
|
switch (condition) {
|
nucho |
0:b14546a3cfab
|
60
|
case CREATED:
|
nucho |
0:b14546a3cfab
|
61
|
if (packet_buffer[INTERFACE] == INITIALIZE) {
|
nucho |
0:b14546a3cfab
|
62
|
if (onInitialize() == RTC_OK) {
|
nucho |
0:b14546a3cfab
|
63
|
condition = INACTIVE;
|
nucho |
0:b14546a3cfab
|
64
|
SendPacket(INITIALIZE, 1, LP_RTC_OK);
|
nucho |
0:b14546a3cfab
|
65
|
} else {
|
nucho |
0:b14546a3cfab
|
66
|
condition = ERROR;
|
nucho |
0:b14546a3cfab
|
67
|
SendPacket(INITIALIZE, 1, LP_RTC_ERROR);
|
nucho |
0:b14546a3cfab
|
68
|
}
|
nucho |
0:b14546a3cfab
|
69
|
}
|
nucho |
0:b14546a3cfab
|
70
|
break;
|
nucho |
0:b14546a3cfab
|
71
|
case ERROR:
|
nucho |
0:b14546a3cfab
|
72
|
if (packet_buffer[INTERFACE] == ONERROR) {
|
nucho |
0:b14546a3cfab
|
73
|
onError();
|
nucho |
0:b14546a3cfab
|
74
|
SendPacket(ONERROR, 1, LP_RTC_OK);
|
nucho |
0:b14546a3cfab
|
75
|
} else if (packet_buffer[INTERFACE] == RESET) {
|
nucho |
0:b14546a3cfab
|
76
|
if (onReset() == RTC_OK) {
|
nucho |
0:b14546a3cfab
|
77
|
condition = INACTIVE;
|
nucho |
0:b14546a3cfab
|
78
|
SendPacket(RESET, 1, LP_RTC_OK);
|
nucho |
0:b14546a3cfab
|
79
|
} else {
|
nucho |
0:b14546a3cfab
|
80
|
condition = ERROR;
|
nucho |
0:b14546a3cfab
|
81
|
SendPacket(RESET, 1, LP_RTC_ERROR);
|
nucho |
0:b14546a3cfab
|
82
|
}
|
nucho |
0:b14546a3cfab
|
83
|
}
|
nucho |
0:b14546a3cfab
|
84
|
break;
|
nucho |
0:b14546a3cfab
|
85
|
case INACTIVE:
|
nucho |
0:b14546a3cfab
|
86
|
if (packet_buffer[INTERFACE] == ACTIVATE) {
|
nucho |
0:b14546a3cfab
|
87
|
if (onActivated() == RTC_OK) {
|
nucho |
0:b14546a3cfab
|
88
|
condition = ACTIVE;
|
nucho |
0:b14546a3cfab
|
89
|
SendPacket(ACTIVATE, 1, LP_RTC_OK);
|
nucho |
0:b14546a3cfab
|
90
|
} else {
|
nucho |
0:b14546a3cfab
|
91
|
condition = ERROR;
|
nucho |
0:b14546a3cfab
|
92
|
SendPacket(ACTIVATE, 1, LP_RTC_ERROR);
|
nucho |
0:b14546a3cfab
|
93
|
}
|
nucho |
0:b14546a3cfab
|
94
|
}
|
nucho |
0:b14546a3cfab
|
95
|
break;
|
nucho |
0:b14546a3cfab
|
96
|
case ACTIVE:
|
nucho |
0:b14546a3cfab
|
97
|
if (packet_buffer[INTERFACE] == DEACTIVATE) {
|
nucho |
0:b14546a3cfab
|
98
|
onDeactivated();
|
nucho |
0:b14546a3cfab
|
99
|
condition = INACTIVE;
|
nucho |
0:b14546a3cfab
|
100
|
SendPacket(DEACTIVATE, 1, LP_RTC_OK);
|
nucho |
0:b14546a3cfab
|
101
|
} else if (packet_buffer[INTERFACE] == EXECUTE) {
|
nucho |
0:b14546a3cfab
|
102
|
if (onExecute() == RTC_OK) {
|
nucho |
0:b14546a3cfab
|
103
|
SendPacket(EXECUTE, 1, LP_RTC_OK);
|
nucho |
0:b14546a3cfab
|
104
|
} else {
|
nucho |
0:b14546a3cfab
|
105
|
condition = ERROR;
|
nucho |
0:b14546a3cfab
|
106
|
SendPacket(EXECUTE, 1, LP_RTC_ERROR);
|
nucho |
0:b14546a3cfab
|
107
|
}
|
nucho |
0:b14546a3cfab
|
108
|
}
|
nucho |
0:b14546a3cfab
|
109
|
break;
|
nucho |
0:b14546a3cfab
|
110
|
default:
|
nucho |
0:b14546a3cfab
|
111
|
condition = ERROR;
|
nucho |
0:b14546a3cfab
|
112
|
break;
|
nucho |
0:b14546a3cfab
|
113
|
}
|
nucho |
0:b14546a3cfab
|
114
|
|
nucho |
0:b14546a3cfab
|
115
|
if (packet_buffer[INTERFACE] == GET_STATUS) {
|
nucho |
0:b14546a3cfab
|
116
|
SendPacket(GET_STATUS, 1, &condition);
|
nucho |
0:b14546a3cfab
|
117
|
}
|
nucho |
0:b14546a3cfab
|
118
|
}
|
nucho |
0:b14546a3cfab
|
119
|
|
nucho |
0:b14546a3cfab
|
120
|
|
nucho |
0:b14546a3cfab
|
121
|
/*NNN
|
nucho |
0:b14546a3cfab
|
122
|
l:TimedLong
|
nucho |
0:b14546a3cfab
|
123
|
d:TimedDouble
|
nucho |
0:b14546a3cfab
|
124
|
f:TimedFloat
|
nucho |
0:b14546a3cfab
|
125
|
L:TimedLongSeq
|
nucho |
0:b14546a3cfab
|
126
|
D:TimedDoubleSeq
|
nucho |
0:b14546a3cfab
|
127
|
F:TimedFloatSeq
|
nucho |
0:b14546a3cfab
|
128
|
*/
|
nucho |
0:b14546a3cfab
|
129
|
|
nucho |
0:b14546a3cfab
|
130
|
void addInPort(InPortBase& Port) {
|
nucho |
0:b14546a3cfab
|
131
|
int len = strlen(Port.GetName());
|
nucho |
0:b14546a3cfab
|
132
|
char *data = (char*)malloc(len+1);
|
nucho |
0:b14546a3cfab
|
133
|
data[0] = Port.GetTypeCode();
|
nucho |
0:b14546a3cfab
|
134
|
memcpy(&(data[1]), Port.GetName(), len);
|
nucho |
0:b14546a3cfab
|
135
|
SendPacket(ADD_INPORT, len+1, data);
|
nucho |
0:b14546a3cfab
|
136
|
ReceivePacket(packet_buffer);
|
nucho |
0:b14546a3cfab
|
137
|
free(data);
|
nucho |
0:b14546a3cfab
|
138
|
}
|
nucho |
0:b14546a3cfab
|
139
|
|
nucho |
0:b14546a3cfab
|
140
|
void addOutPort(OutPortBase& Port) {
|
nucho |
0:b14546a3cfab
|
141
|
int len = strlen(Port.GetName());
|
nucho |
0:b14546a3cfab
|
142
|
char *data = (char*)malloc(len+1);
|
nucho |
0:b14546a3cfab
|
143
|
data[0] = Port.GetTypeCode();
|
nucho |
0:b14546a3cfab
|
144
|
memcpy(&(data[1]), Port.GetName(), len);
|
nucho |
0:b14546a3cfab
|
145
|
SendPacket(ADD_OUTPORT, len+1, data);
|
nucho |
0:b14546a3cfab
|
146
|
ReceivePacket(packet_buffer);
|
nucho |
0:b14546a3cfab
|
147
|
free(data);
|
nucho |
0:b14546a3cfab
|
148
|
}
|