summaryrefslogtreecommitdiff
path: root/trunk/software/firmware/FiveD_GCode/FiveD_GCode_Interpreter/hostcom.h
blob: 70009b73b88a43bbfd23275a954336d601ef81be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#ifndef HOSTCOM_H
#define HOSTCOM_H
/*
  Class to handle sending messages from and back to the host.
  NOWHERE ELSE in this program should anything send to Serial.print()
  or get anything from Serial.read().
  
  All communication is in printable ASCII characters.  Messages sent back
  to the host computer are terminated by a newline and look like this:
  
  xx [line number to resend] [T:93.2 B:22.9] [C: X:9.2 Y:125.4 Z:3.7 E:1902.5] [Some debugging or other information may be here]
  
  where xx can be one of:
  
  ok
  rs
  !!
  
  ok means that no error has been detected.
  rs means resend, and must be followed by the line number to resend.
  !! means that a hardware fault has been detected.  The RepRap machine will
       shut down immediately after it has sent this message.
       
  The T: and B: values are the temperature of the currently-selected extruder 
  and the bed respectively, and are only sent in response to a request using the
  appropriate M code.
  
  C: means that coordinates follow.  Those are the X: Y: etc values.  These are only 
  sent in response to a request using the appropriate M code.

  The most common response is simply:

  ok  
       
  When the machine boots up it sends the string
  
  start
  
  once to the host before sending anything else.  This should not be replaced or augmented
  by version numbers and the like.  We should implement an M code to request those.
       
 */

// Can't get lower than absolute zero...

#define NO_TEMP -300

extern void shutdown();

class hostcom
{
public:
  hostcom();
  char* string();
  void setETemp(int et);
  void setBTemp(int bt);
  void setCoords(const FloatPoint& where);
  void setResend(long ln);
  void setFatal();
  void sendMessage(bool doMessage);
  void start();
  
// Wrappers for the comms interface

  void putInit();
  void put(char* s);
  void put(const float& f);
  void put(const long& l);
  void put(int i);
  void putEnd();
  byte gotData();
  char get();
  
private:
  void reset();
  void sendtext(bool noText);
  char message[RESPONSE_SIZE];
  int etemp;
  int btemp;
  float x;
  float y;
  float z;
  float e;
  long resend;
  bool fatal;
  bool sendCoordinates;  
};

inline hostcom::hostcom()
{
  fatal = false;
  reset();
}

// Wrappers for the comms interface

inline void hostcom::putInit() {  Serial.begin(HOST_BAUD); }
inline void hostcom::put(char* s) { Serial.print(s); }
inline void hostcom::put(const float& f) { Serial.print(f); }
inline void hostcom::put(const long& l) { Serial.print(l); }
inline void hostcom::put(int i) { Serial.print(i); }
inline void hostcom::putEnd() { Serial.println(); }
inline byte hostcom::gotData() { return Serial.available(); }
inline char hostcom::get() { return Serial.read(); }


// called after each message has been sent

inline void hostcom::reset()
{
  etemp = NO_TEMP;
  btemp = NO_TEMP;
  message[0] = 0;
  resend = -1;
  sendCoordinates = false;
  // Don't reset fatal.
}

// Called once when the machine boots

inline void hostcom::start()
{
  putInit();
  put("start");
  putEnd();  
}

// Return the place to write messages into.  Typically this is used in lines like:
// sprintf(talkToHost.string(), "Echo: %s", cmdbuffer);

inline char* hostcom::string()
{
  return message;
}

// Set the extruder temperature to be returned.

inline void hostcom::setETemp(int et)
{
  etemp = et;
}

// Set the bed temperature to be returned

inline void hostcom::setBTemp(int bt)
{
  btemp = bt;
}

// Set the machine's coordinates to be returned

inline void hostcom::setCoords(const FloatPoint& where)
{
  x = where.x;
  y = where.y;
  z = where.z;
  e = where.e;
  sendCoordinates = true;
}

// Request a resend of line ln

inline void hostcom::setResend(long ln)
{
  resend = ln;
}

// Flag that a fatal error has occurred (such as a temperature sensor failure).

inline void hostcom::setFatal()
{
  fatal = true;
}

// Send the text stored (if any) to the host.

inline void hostcom::sendtext(bool doMessage)
{
  if(!doMessage)
    return;
  if(!message[0])
    return;
  put(" ");
  put(message);
}

// Master function to return messages to the host

inline void hostcom::sendMessage(bool doMessage)
{
  if(fatal)
  {
    put("!!");
    sendtext(true);
    putEnd();
    shutdown();
    return; // Technically redundant - shutdown never returns.
  }
  
  if(resend < 0)
    put("ok");
  else
  {
    put("rs ");
    put(resend);
  }
    
  if(etemp > NO_TEMP)
  {
    put(" T:");
    put(etemp);
  }
  
  if(btemp > NO_TEMP)
  {
    put(" B:");
    put(btemp);
  }
  
  if(sendCoordinates)
  {				
    put(" C: X:");
    put(x);
    put(" Y:");
    put(y);
    put(" Z:");
    put(z);
    put(" E:");
    put(e);
  }
  
  sendtext(doMessage);
  
  putEnd();
  
  reset(); 
}


#endif