-
Notifications
You must be signed in to change notification settings - Fork 3
/
AP_PIXY_I2C.h
101 lines (86 loc) · 1.88 KB
/
AP_PIXY_I2C.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
94
95
96
97
98
99
100
101
/*
* AP_PIXY_I2C.h - Library for the CMUcam5 PIXY
*
* code by Antoine LECESTRE for DIYdrones.com
* based on the arduino's TPixy.h and PixyI2C.h library form CMUCAM PIXY project
*
* sensor should be connected to the I2C port
*
*
*/
#ifndef _PIXY_I2C_H
#define _PIXY_I2C_H
#include <AP_HAL.h>
extern const AP_HAL::HAL& hal;
#include <AP_PIXY.h>
#include <AP_PIXY.cpp>
class LinkI2C
{
public:
// init - simply sets the i2c address
void init(uint8_t address)
{
_addr = address;
}
// get a Word from PIXY
uint16_t getWord()
{
uint16_t word_;
uint8_t buff[2];
uint8_t c;
AP_HAL::Semaphore* _i2c_sem = hal.i2c->get_semaphore();
if (!_i2c_sem->take(1)) {
// the bus is busy - try again later
return 0;
}
if ( hal.i2c->read(_addr, 2, buff) != 0) {
{
//PIXY_I2C_is_healthy = false;
_i2c_sem->give();
return 0;
}
}else{
c = buff[0];
word_ = buff[1];
word_ <<= 8;
word_ |= c;
PIXY_I2C_is_healthy = true;
_i2c_sem->give();
return word_;
}
}
// get a byte from PIXY
uint8_t getByte()
{
uint8_t buff[1];
AP_HAL::Semaphore* _i2c_sem = hal.i2c->get_semaphore();
if (!_i2c_sem->take(1)) {
// the bus is busy - try again later
return 0;
}
if ( hal.i2c->read( _addr, 1, buff) != 0)
{
//PIXY_I2C_is_healthy = false;
_i2c_sem->give();
return 0;
}
else
{
PIXY_I2C_is_healthy = true;
_i2c_sem->give();
return buff[0];
}
}
// send health status
bool health()
{
return PIXY_I2C_is_healthy;
}
private:
// health
bool PIXY_I2C_is_healthy;
// address variable
uint8_t _addr;
};
typedef AP_PIXY<LinkI2C> AP_PIXY_I2C;
#endif // _PIXY_I2C_H