-
Notifications
You must be signed in to change notification settings - Fork 2
/
OpenServo.cpp
118 lines (92 loc) · 2.53 KB
/
OpenServo.cpp
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
/**
* OpenServo Arduino/Wire Communication Library
*
* This library communicates with an OpenServo on the i2c bus using the
* OpenServo i2c API (http://www.openservo.com/APIGuide). You must create
* an instance of Wire to pass to the library. The library will grab and
* release the i2c bus during its communication cycles, but will not lock
* up the bus outside of the library, so you can use the Wire instance to
* communicate with other i2c devices on the bus, or send special commands
* to an OpenServo, should you find this library lacking in some way.
*
* Copyright 2012 Sterling Lewis Peet <[email protected]>
*
* This work is licensed under the Creative Commons
* Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of
* this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or
* send a letter to:
*
* Creative Commons
* 444 Castro Street, Suite 900
* Mountain View, California, 94041, USA.
*
* Other Licensing is available upon request.
*
*/
#include "OpenServo.h"
OpenServo::OpenServo(int address)
{
//Wire = wire;
_address = (uint8_t)address;
}
/* ================ Public methods ================ */
void OpenServo::setPosition(int pos)
{
_OpenServoWrite16(OPENSERVO_SEEK, pos);
}
unsigned int OpenServo::getPosition()
{
return _OpenServoRead16(OPENSERVO_POSITION);
}
void OpenServo::enable()
{
_OpenServoCommand8(OPENSERVO_PWM_ENABLE);
}
void OpenServo::disable()
{
_OpenServoCommand8(OPENSERVO_PWM_DISABLE);
}
/*
OpenServo::reverseSeekEnable()
{
}
OpenServo::reverseSeekDisable()
{
}*/
/* ================ Private methods ================ */
/*OpenServo::_setReverseSeek()
{
}*/
void OpenServo::_OpenServoWrite16(uint8_t reg, int data)
{
// write a 16 bit register
_OpenServoTransactionInit(reg);
OS_WRITE(data >> 8); // high uint8_t
OS_WRITE(data & 0xff); // low uint8_t
Wire.endTransmission();
}
unsigned int OpenServo::_OpenServoRead16(uint8_t reg)
{
// read a 16 bit register
_OpenServoTransactionInit(reg);
unsigned int data;
Wire.requestFrom(_address, 2);
if (Wire.available())
data = OS_READ() << 8; // high uint8_t
if (Wire.available())
data |= OS_READ(); // low uint8_t
Wire.endTransmission();
return data;
}
void OpenServo::_OpenServoCommand8(uint8_t reg)
{
// Send an 8 bit servo command
_OpenServoTransactionInit(reg);
Wire.endTransmission();
}
void OpenServo::_OpenServoTransactionInit(uint8_t reg)
{
// init an i2c transmission
Wire.beginTransmission(_address);
OS_WRITE(reg);
}