-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPixy.h
93 lines (75 loc) · 2.13 KB
/
Pixy.h
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
//
// begin license header
//
// This file is part of Pixy CMUcam5 or "Pixy" for short
//
// All Pixy source code is provided under the terms of the
// GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html).
// Those wishing to use Pixy source code, software and/or
// technologies under different licensing terms should contact us at
// [email protected]. Such licensing terms are available for
// all portions of the Pixy codebase presented here.
//
// end license header
//
/*
Pixy.h - Library for interfacing with Pixy.
Cobbled together by Omwah and Jasco May 2014
Based on code originally created by Scott Robinson, October 22, 2013.
Released into the public domain.
*/
#ifndef PIXY_H
#define PIXY_H
#include "TPixy.h"
#include <wiringPiSPI.h>
#define SPI_CHANNEL 0
#define SPI_SPEED 250000 // Arduino: SPI_CLOCK_DIV16
#define PIXY_SYNC_BYTE 0x5a
#define PIXY_SYNC_BYTE_DATA 0x5b
#define PIXY_OUTBUF_SIZE 6
class LinkSPI {
public:
void init(uint8_t addr) {
outLen = 0;
wiringPiSPISetup(SPI_CHANNEL, SPI_SPEED) ;
}
uint16_t getWord() {
// ordering is different because Pixy is sending 16 bits through SPI
// instead of 2 bytes in a 16-bit word as with I2C
uint16_t w;
uint8_t c, out = 0;
if (outLen) {
w = getByte(PIXY_SYNC_BYTE_DATA);
out = outBuf[outIndex++];
if (outIndex == outLen) {
outLen = 0;
}
} else {
w = getByte(PIXY_SYNC_BYTE);
}
w <<= 8;
c = getByte(out);
w |= c;
return w;
}
uint8_t getByte(uint8_t out=0x00) {
uint8_t c = out;
wiringPiSPIDataRW(SPI_CHANNEL, &c, 1);
return c;
}
int8_t send(uint8_t *data, uint8_t len) {
if (len > PIXY_OUTBUF_SIZE || outLen != 0) {
return -1;
}
memcpy(outBuf, data, len);
outLen = len;
outIndex = 0;
return len;
}
private:
uint8_t outBuf[PIXY_OUTBUF_SIZE];
uint8_t outLen;
uint8_t outIndex;
};
typedef TPixy<LinkSPI> Pixy;
#endif