AccelStepperI2C  v 0.1
I2C wrapper (and a bit more) for the AccelStepper Arduino library
ServoI2C.cpp
Go to the documentation of this file.
1 
12 #include <Wire.h>
13 #include <ServoI2C.h>
14 
15 
16 
17 // Constructor
19 {
20  wrapper = w;
21 }
22 
23 
24 uint8_t ServoI2C::attach(int pin)
25 {
27  wrapper->buf.write(pin);
28  if (wrapper->sendCommand() and wrapper->readResult(servoAttachResult)) {
29  wrapper->buf.read(myNum);
30  } // else leave myNum at -1 (= failed)
31  log("Servo attached with myNum="); log(myNum); log("\n");
32  return (uint8_t)myNum;
33 }
34 
35 uint8_t ServoI2C::attach(int pin, int min, int max)
36 {
38  wrapper->buf.write(pin);
39  wrapper->buf.write(min);
40  wrapper->buf.write(max);
41  if (wrapper->sendCommand() and wrapper->readResult(servoAttachResult)) {
42  wrapper->buf.read(myNum);
43  } // else leave myNum at -1 (= failed)
44  return (uint8_t)myNum;
45 }
46 
48 {
50  wrapper->sendCommand();
51 }
52 
53 void ServoI2C::write(int value)
54 {
56  wrapper->buf.write(value);
57  wrapper->sendCommand();
58 }
59 
61 {
63  wrapper->buf.write(value);
64  wrapper->sendCommand();
65 }
66 
68 {
70  int res = -1;
71  if (wrapper->sendCommand() and wrapper->readResult(servoReadResult)) {
72  wrapper->buf.read(res);
73  }
74  return res;
75 }
76 
78 {
80  int res = -1;
81  if (wrapper->sendCommand() and wrapper->readResult(servoReadMicrosecondsResult)) {
82  wrapper->buf.read(res);
83  }
84  return res;
85 }
86 
88 {
90  bool res = false;
91  if (wrapper->sendCommand() and wrapper->readResult(servoAttachedResult)) {
92  wrapper->buf.read(res);
93  }
94  return res;
95 }
96 
ServoI2C::read
int read()
Definition: ServoI2C.cpp:67
servoWriteMicrosecondsCmd
const uint8_t servoWriteMicrosecondsCmd
Definition: ServoI2C.h:42
I2Cwrapper
A helper class for the AccelStepperI2C (and ServoI2C) library.
Definition: I2Cwrapper.h:79
ServoI2C::attached
bool attached()
Definition: ServoI2C.cpp:87
ServoI2C::myNum
int8_t myNum
Servo number with myNum >= 0 for successfully added servo.
Definition: ServoI2C.h:96
servoDetachCmd
const uint8_t servoDetachCmd
Definition: ServoI2C.h:40
ServoI2C::writeMicroseconds
void writeMicroseconds(int value)
Definition: ServoI2C.cpp:60
I2Cwrapper::prepareCommand
void prepareCommand(uint8_t cmd, uint8_t unit=-1)
Definition: I2Cwrapper.cpp:34
ServoI2C::write
void write(int value)
Definition: ServoI2C.cpp:53
servoReadMicrosecondsResult
const uint8_t servoReadMicrosecondsResult
Definition: ServoI2C.h:44
servoReadMicrosecondsCmd
const uint8_t servoReadMicrosecondsCmd
Definition: ServoI2C.h:44
I2Cwrapper::buf
SimpleBuffer buf
Definition: I2Cwrapper.h:190
servoAttachResult
const uint8_t servoAttachResult
Definition: ServoI2C.h:38
SimpleBuffer::read
void read(T &value)
Read any basic data type from the buffer from the current position and increment the position pointer...
Definition: SimpleBuffer.h:112
servoReadCmd
const uint8_t servoReadCmd
Definition: ServoI2C.h:43
servoAttach2Cmd
const uint8_t servoAttach2Cmd
Definition: ServoI2C.h:39
ServoI2C::ServoI2C
ServoI2C(I2Cwrapper *w)
Constructor.
Definition: ServoI2C.cpp:18
ServoI2C::readMicroseconds
int readMicroseconds()
Definition: ServoI2C.cpp:77
I2Cwrapper::readResult
bool readResult(uint8_t numBytes)
Definition: I2Cwrapper.cpp:70
log
#define log(...)
Definition: firmware.ino:94
ServoI2C.h
Arduino library for I2C-control of servo motors connected to another Arduino which runs the associate...
ServoI2C::attach
uint8_t attach(int pin)
Attach and prepare servo for use. Similar to the original, but see below.
Definition: ServoI2C.cpp:24
servoReadResult
const uint8_t servoReadResult
Definition: ServoI2C.h:43
servoAttachedResult
const uint8_t servoAttachedResult
Definition: ServoI2C.h:45
servoAttachedCmd
const uint8_t servoAttachedCmd
Definition: ServoI2C.h:45
servoAttach1Cmd
const uint8_t servoAttach1Cmd
Definition: ServoI2C.h:38
ServoI2C::detach
void detach()
Definition: ServoI2C.cpp:47
SimpleBuffer::write
void write(const T &value)
Write any basic data type to the buffer at the current position and increment the position pointer ac...
Definition: SimpleBuffer.h:103
servoWriteCmd
const uint8_t servoWriteCmd
Definition: ServoI2C.h:41
I2Cwrapper::sendCommand
bool sendCommand()
Definition: I2Cwrapper.cpp:46