-
Notifications
You must be signed in to change notification settings - Fork 9
/
com.serial.terminal.spin
323 lines (240 loc) · 7.46 KB
/
com.serial.terminal.spin
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
' Original Authors: Jeff Martin, Andy Lindsay, Chip Gracey
{{
This object adds extra features specific to Parallax serial terminals.
This object is heavily based on FullDuplexSerialPlus (by Andy Lindsay), which is itself
heavily based on FullDuplexSerial (by Chip Gracey).
# Usage
- Call Start, or StartRxTx, first.
- Be sure to set the Parallax Serial Terminal software to the baudrate specified in Start, and the proper COM port.
- At 80 MHz, this object properly receives/transmits at up to 250 Kbaud, or performs transmit-only at up to 1 Mbaud.
}}
CON
'' Control Character Constants
CS = 16 ' Clear Screen
CE = 11 ' Clear to End of line
CB = 12 ' Clear lines Below
HM = 1 ' HoMe cursor
PC = 2 ' Position Cursor in x,y
PX = 14 ' Position cursor in X
PY = 15 ' Position cursor in Y
NL = 13 ' New Line
LF = 10 ' Line Feed
ML = 3 ' Move cursor Left
MR = 4 ' Move cursor Right
MU = 5 ' Move cursor Up
MD = 6 ' Move cursor Down
TB = 9 ' TaB
BS = 8 ' BackSpace
CON
MAXSTR_LENGTH = 16 ' Maximum length of received numerical string (not including zero terminator).
OBJ
ser : "com.serial"
num : "string.integer"
VAR
byte str_buffer[MAXSTR_LENGTH+1] ' String buffer for numerical strings
PUB Start(baudrate) : okay
{{
Start communication with the Parallax Serial Terminal using the Propeller's programming connection.
Waits 1 second for connection, then clears screen.
Parameters:
baudrate - bits per second. Make sure it matches the Parallax Serial Terminal's
Baud Rate field.
Returns True (non-zero) if cog started, or False (0) if no cog is available.
}}
okay := ser.Start(baudrate)
Clear
return okay
PUB StartRxTx(rxpin, txpin, mode, baudrate)
{{
Start serial communication with designated pins, mode, and baud.
Parameters:
rxpin - input pin; receives signals from external device's TX pin.
txpin - output pin; sends signals to external device's RX pin.
mode - signaling mode (4-bit pattern).
bit 0 - inverts rx.
bit 1 - inverts tx.
bit 2 - open drain/source tx.
bit 3 - ignore tx echo on rx.
baudrate - bits per second.
Returns : True (non-zero) if cog started, or False (0) if no cog is available.
}}
return ser.StartRxTx(rxpin, txpin, mode, baudrate)
PUB Stop
{{
Stop serial communication; frees a cog.
}}
ser.Stop
PUB Count
{{
Get count of characters in receive buffer.
}}
return ser.Count
PUB Flush
{{
Flush receive buffer.
}}
ser.Flush
PUB Char(ch)
{{
Send single-byte character. Waits for room in transmit buffer if necessary.
}}
ser.Char(ch)
PUB Chars(ch, size)
{{
Send string of size `size` filled with `bytechr`.
}}
repeat size
ser.Char(ch)
PUB CharIn
{{
Receive single-byte character. Waits until character received.
}}
return ser.CharIn
PUB RxCheck
{
Check if character received; return immediately.
Returns: -1 if no byte received, $00..$FF if character received.
}
return ser.RxCheck
PUB Str(stringptr)
{{
Send zero-terminated string.
Parameter:
stringptr - pointer to zero terminated string to send.
}}
repeat strsize(stringptr)
ser.Char(byte[stringptr++])
PUB StrIn(stringptr)
{{
Receive a string (carriage return terminated) and stores it (zero terminated) starting at stringptr.
Waits until full string received.
Parameter:
stringptr - pointer to memory in which to store received string characters.
Memory reserved must be large enough for all string characters plus a zero terminator.
}}
StrInMax(stringptr, -1)
PUB StrInMax(stringptr, maxcount)
{{
Receive a string of characters (either carriage return terminated or maxcount in
length) and stores it (zero terminated) starting at stringptr. Waits until either
full string received or maxcount characters received.
Parameters:
stringptr - pointer to memory in which to store received string characters.
Memory reserved must be large enough for all string characters plus a zero terminator (maxcount + 1).
maxcount - maximum length of string to receive, or -1 for unlimited.
}}
repeat while (maxcount--) 'While maxcount not reached
if (byte[stringptr++] := ser.CharIn) == NL 'Get chars until NL
quit
byte[stringptr+(byte[stringptr-1] == NL)]~ 'Zero terminate string; overwrite NL or append 0 char
PUB Dec(value)
{{
Send value as decimal characters.
Parameter:
value - byte, word, or long value to send as decimal characters.
}}
Str(num.Dec(value))
PUB DecIn
{{
Receive carriage return terminated string of characters representing a decimal value.
Returns: the corresponding decimal value.
}}
StrInMax(@str_buffer, MAXSTR_LENGTH)
return num.StrToBase(@str_buffer, 10)
PUB Bin(value, digits)
{{
Send value as binary characters up to digits in length.
Parameters:
value - byte, word, or long value to send as binary characters.
digits - number of binary digits to send. Will be zero padded if necessary.
}}
Str(num.Bin(value,digits))
PUB BinIn
{{
Receive carriage return terminated string of characters representing a binary value.
Returns: the corresponding binary value.
}}
StrInMax(@str_buffer, MAXSTR_LENGTH)
return num.StrToBase(@str_buffer, 2)
PUB Hex(value, digits)
{{
Send value as hexadecimal characters up to digits in length.
Parameters:
value - byte, word, or long value to send as hexadecimal characters.
digits - number of hexadecimal digits to send. Will be zero padded if necessary.
}}
Str(num.Hex(value, digits))
PUB HexIn
{{
Receive carriage return terminated string of characters representing a hexadecimal value.
Returns: the corresponding hexadecimal value.
}}
StrInMax(@str_buffer, MAXSTR_LENGTH)
return num.StrToBase(@str_buffer, 16)
PUB Clear
{{
Clear screen and place cursor at top-left.
}}
ser.Char(CS)
PUB NewLine
{{
Clear screen and place cursor at top-left.
}}
ser.Char(NL)
PUB Position(x, y)
{{
Position cursor at column x, row y (from top-left).
}}
ser.Char(PC)
ser.Char(x)
ser.Char(y)
PUB PositionX(x)
{{
Position cursor at column x of current row.
}}
ser.Char(PX)
ser.Char(x)
PUB PositionY(y)
{{
Position cursor at row y of current column.
}}
ser.Char(PY)
ser.Char(y)
PUB MoveLeft(x)
{{
Move cursor left x characters.
}}
repeat x
ser.Char(ML)
PUB MoveRight(x)
{{
Move cursor right x characters.
}}
repeat x
ser.Char(MR)
PUB MoveUp(y)
{{
Move cursor up y lines.
}}
repeat y
ser.Char(MU)
PUB MoveDown(y)
{{
Move cursor down y lines.
}}
repeat y
ser.Char(MD)
PUB ReadLine(line, maxline) : size | c
repeat
case c := CharIn
BS: if size
size--
Char(c)
NL, LF: byte[line][size] := 0
Char(c)
quit
other: if size < maxline
byte[line][size++] := c
Char(c)
PUB GetMailbox
return ser.GetMailbox