-
Notifications
You must be signed in to change notification settings - Fork 0
/
TPixy2.cs
392 lines (342 loc) · 12.4 KB
/
TPixy2.cs
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
using System;
using Microsoft.SPOT;
namespace CTRE.Phoenix
{
public class TPixy2
{
public const int PIXY_BUFFERSIZE = 0x104;
public const int PIXY_CHECKSUM_SYNC = 0xc1af;
public const int PIXY_NO_CHECKSUM_SYNC = 0xc1ae;
public const int PIXY_SEND_HEADER_SIZE = 4;
public const int PIXY_MAX_PROGNAME = 33;
public const int PIXY_TYPE_REQUEST_CHANGE_PROG = 0x02;
public const int PIXY_TYPE_REQUEST_RESOLUTION = 0x0c;
public const int PIXY_TYPE_RESPONSE_RESOLUTION = 0x0d;
public const int PIXY_TYPE_REQUEST_VERSION = 0x0e;
public const int PIXY_TYPE_RESPONSE_VERSION = 0x0f;
public const int PIXY_TYPE_RESPONSE_RESULT = 0x01;
public const int PIXY_TYPE_RESPONSE_ERROR = 0x03;
public const int PIXY_TYPE_REQUEST_BRIGHTNESS = 0x10;
public const int PIXY_TYPE_REQUEST_SERVO = 0x12;
public const int PIXY_TYPE_REQUEST_LED = 0x14;
public const int PIXY_TYPE_REQUEST_LAMP = 0x16;
public const int PIXY_RESULT_OK = 0;
public const int PIXY_RESULT_ERROR = -1;
public const int PIXY_RESULT_BUSY = -2;
public const int PIXY_RESULT_CHECKSUM_ERROR = -3;
public const int PIXY_RESULT_TIMEOUT = -4;
public const int PIXY_RESULT_BUTTON_OVERRIDE = -5;
public struct Version
{
public override string ToString()
{
return "hardware ver: " + hardware + " firmware ver: " + firmwareMajor + "." + firmwareMinor + "." + firmwareBuild + " " + firmwareType;
}
public uint hardware;
public byte firmwareMajor;
public byte firmwareMinor;
public uint firmwareBuild;
public string firmwareType;
};
public Pixy2CCC ccc;
public uint frameWidth;
public uint frameHeight;
public Version version;
//public Pixy2Line line;
public LinkType m_link;
public byte[] m_buf;
public byte[] m_bufPayload;
public byte m_type;
public byte m_length;
public bool m_cs;
public TPixy2()
{
ccc = new Pixy2CCC(this);
//line = new Pixy2Line(this);
// allocate buffer space for send/receive
m_buf = new byte[PIXY_BUFFERSIZE];
m_bufPayload = new byte[PIXY_BUFFERSIZE - PIXY_SEND_HEADER_SIZE];
// shifted buffer is used for sending, so we have space to write header information
Array.Copy(m_buf, PIXY_SEND_HEADER_SIZE, m_bufPayload, 0, m_bufPayload.Length);
frameWidth = frameHeight = 0;
version = new Version();
}
~TPixy2()
{
m_link.Close();
}
private long millis()
{
return DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
}
public int Init(CTRE.Gadgeteer.IPortUart portDef, int arg)
{
long t0;
byte res;
res = m_link.Open(portDef, arg);
if (res < 0)
return res;
// wait for pixy to be ready -- that is, Pixy takes a second or 2 boot up
// getVersion is an effective "ping". We timeout after 5s.
for (t0 = millis(); millis() - t0 < 5000;)
{
if (GetVersion() >= 0) // successful version get -> pixy is ready
{
GetResolution(); // get resolution so we have it
return PIXY_RESULT_OK;
}
}
// timeout
return PIXY_RESULT_TIMEOUT;
}
public int GetSync()
{
byte i, j, cprev;
byte[] c = new byte[1];
int res;
uint start;
// parse bytes until we find sync
for (i = j = 0, cprev = 0; true; i++)
{
res = m_link.Recv(c, 1);
if (res >= PIXY_RESULT_OK)
{
// since we're using little endian, previous byte is least significant byte
start = cprev;
// current byte is most significant byte
start |= ((uint)c[0] << 8);
cprev = c[0];
if (start == PIXY_CHECKSUM_SYNC)
{
m_cs = true;
return PIXY_RESULT_OK;
}
if (start == PIXY_NO_CHECKSUM_SYNC)
{
m_cs = false;
return PIXY_RESULT_OK;
}
}
// If we've read some bytes and no sync, then wait and try again.
// And do that several more times before we give up.
// Pixy guarantees to respond within 100us.
if (i >= 4)
{
if (j >= 4)
{
#if PIXY_DEBUG
Debug.Print("error: no response");
#endif
return PIXY_RESULT_ERROR;
}
j++;
i = 0;
}
}
}
public int RecvPacket()
{
UInt16 csCalc, csSerial;
int res;
csCalc = csSerial = 0;
res = GetSync();
if (res < 0)
return res;
if (m_cs)
{
res = m_link.Recv(m_buf, 4);
if (res < 0)
return res;
m_type = m_buf[0];
m_length = m_buf[1];
csSerial = m_buf[2];
csSerial |= (UInt16)(m_buf[3] << 8);
res = m_link.Recv(m_buf, m_length, ref csCalc);
if (res < 0)
return res;
if (csSerial != csCalc)
{
#if PIXY_DEBUG
Serial.println("error: checksum");
#endif
return PIXY_RESULT_CHECKSUM_ERROR;
}
}
else
{
res = m_link.Recv(m_buf, 2);
if (res < 0)
return res;
m_type = m_buf[0];
m_length = m_buf[1];
res = m_link.Recv(m_buf, m_length);
if (res < 0)
return res;
}
return PIXY_RESULT_OK;
}
public int SendPacket()
{
// write header info at beginnig of buffer
m_buf[0] = PIXY_NO_CHECKSUM_SYNC & 0xff;
m_buf[1] = PIXY_NO_CHECKSUM_SYNC >> 8;
m_buf[2] = m_type;
m_buf[3] = m_length;
Array.Copy(m_bufPayload, 0, m_buf, 4, m_length);
// send whole thing -- header and data in one call
return m_link.Send(m_buf, (byte)(m_length + PIXY_SEND_HEADER_SIZE));
}
public int ChangeProg(char[] prog)
{
int res;
// poll for program to change
while (true)
{
Array.Copy(m_bufPayload, prog, PIXY_MAX_PROGNAME);
m_length = PIXY_MAX_PROGNAME;
m_type = PIXY_TYPE_REQUEST_CHANGE_PROG;
SendPacket();
if (RecvPacket() == 0)
{
res = m_buf[0];
res |= m_buf[1] << 8;
res |= m_buf[2] << 16;
res |= m_buf[3] << 24;
if (res > 0)
{
GetResolution(); // get resolution so we have it
return PIXY_RESULT_OK; // success
}
}
else
return PIXY_RESULT_ERROR; // some kind of bitstream error
System.Threading.Thread.Sleep(1);
}
}
public int GetVersion()
{
m_length = 0;
m_type = PIXY_TYPE_REQUEST_VERSION;
SendPacket();
if (RecvPacket() == 0)
{
if (m_type == PIXY_TYPE_RESPONSE_VERSION)
{
version.hardware = m_buf[0];
version.hardware = (uint)m_buf[1] << 8;
version.firmwareMajor = m_buf[2];
version.firmwareMinor = m_buf[3];
version.firmwareBuild = m_buf[4];
string firmwareTypeString = "";
for (int i = 0; i < 10; i++)
firmwareTypeString += (char)m_buf[i + 5];
version.firmwareType = firmwareTypeString;
return m_length;
}
else if (m_type == PIXY_TYPE_RESPONSE_ERROR)
return PIXY_RESULT_BUSY;
}
return PIXY_RESULT_ERROR; // some kind of bitstream error
}
public int GetResolution()
{
m_length = 1;
m_bufPayload[0] = 0; // for future types of queries
m_type = PIXY_TYPE_REQUEST_RESOLUTION;
SendPacket();
if (RecvPacket() == 0)
{
if (m_type == PIXY_TYPE_RESPONSE_RESOLUTION)
{
frameWidth = m_buf[0];
frameWidth |= (uint)m_buf[1] << 8;
frameHeight = m_buf[2];
frameHeight |= (uint)m_buf[3] << 8;
return PIXY_RESULT_OK; // success
}
else
return PIXY_RESULT_ERROR;
}
else
return PIXY_RESULT_ERROR; // some kind of bitstream error
}
public int SetCameraBrightness(byte brightness)
{
ulong res;
m_bufPayload[0] = brightness;
m_length = 1;
m_type = PIXY_TYPE_REQUEST_BRIGHTNESS;
SendPacket();
if (RecvPacket() == 0) // && m_type==PIXY_TYPE_RESPONSE_RESULT && m_length==4)
{
res = m_buf[0];
res |= (ulong)m_buf[1] << 8;
res |= (ulong)m_buf[2] << 16;
res |= (ulong)m_buf[3] << 24;
return (int)res;
}
else
return PIXY_RESULT_ERROR; // some kind of bitstream error
}
public int SetServos(uint s0, uint s1)
{
ulong res;
m_bufPayload[0] = (byte)(s0 & 0xFF);
m_bufPayload[1] = (byte)((s0 >> 8) & 0xFF);
m_bufPayload[2] = (byte)(s1 & 0xFF);
m_bufPayload[3] = (byte)((s1 >> 8) & 0xFF);
m_length = 4;
m_type = PIXY_TYPE_REQUEST_SERVO;
SendPacket();
if (RecvPacket() == 0 && m_type == PIXY_TYPE_RESPONSE_RESULT && m_length == 4)
{
res = m_buf[0];
res |= (ulong)m_buf[1] << 8;
res |= (ulong)m_buf[2] << 16;
res |= (ulong)m_buf[3] << 24;
return (int)res;
}
else
return PIXY_RESULT_ERROR; // some kind of bitstream error
}
public int SetLED(byte r, byte g, byte b)
{
ulong res;
m_bufPayload[0] = r;
m_bufPayload[1] = g;
m_bufPayload[2] = b;
m_length = 3;
m_type = PIXY_TYPE_REQUEST_LED;
SendPacket();
if (RecvPacket() == 0 && m_type == PIXY_TYPE_RESPONSE_RESULT && m_length == 4)
{
res = m_buf[0];
res |= (ulong)m_buf[1] << 8;
res |= (ulong)m_buf[2] << 16;
res |= (ulong)m_buf[3] << 24;
return (int)res;
}
else
return PIXY_RESULT_ERROR; // some kind of bitstream error
}
public int SetLamp(byte upper, byte lower)
{
ulong res;
m_bufPayload[0] = upper;
m_bufPayload[1] = lower;
m_length = 2;
m_type = PIXY_TYPE_REQUEST_LAMP;
SendPacket();
if (RecvPacket() == 0 && m_type == PIXY_TYPE_RESPONSE_RESULT && m_length == 4)
{
res = m_buf[0];
res |= (ulong)m_buf[1] << 8;
res |= (ulong)m_buf[2] << 16;
res |= (ulong)m_buf[3] << 24;
return (int)res;
}
else
return PIXY_RESULT_ERROR; // some kind of bitstream error
}
}
}