Jrk G2 library for Arduino
JrkG2.cpp
1 #include <JrkG2.h>
2 #include <Arduino.h>
3 
4 /**** JrkG2Serial ****/
5 
6 void JrkG2Serial::commandW7(uint8_t cmd, uint8_t val)
7 {
8  sendCommandHeader(cmd);
9  serialW7(val);
10 
11  _lastError = 0;
12 }
13 
14 void JrkG2Serial::commandWs14(uint8_t cmd, int16_t val)
15 {
16  uint16_t v = val;
17  sendCommandHeader(cmd);
18  serialW7(v); // lower 7 bits
19  serialW7(v >> 7); // upper 7 bits
20 
21  _lastError = 0;
22 }
23 
24 uint8_t JrkG2Serial::commandR8(uint8_t cmd)
25 {
26  uint8_t val;
27 
28  sendCommandHeader(cmd);
29 
30  uint8_t byteCount = _stream->readBytes(&val, 1);
31  if (byteCount != 1)
32  {
34  return 0;
35  }
36 
37  _lastError = 0;
38  return val;
39 }
40 
41 uint16_t JrkG2Serial::commandR16(uint8_t cmd)
42 {
43  uint8_t buffer[2];
44 
45  sendCommandHeader(cmd);
46 
47  uint8_t byteCount = _stream->readBytes(buffer, 2);
48  if (byteCount != 2)
49  {
51  return 0;
52  }
53 
54  _lastError = 0;
55  return ((uint16_t)buffer[0] << 0) | ((uint16_t)buffer[1] << 8);
56 }
57 
58 void JrkG2Serial::segmentRead(uint8_t cmd, uint8_t offset,
59  uint8_t length, uint8_t * buffer)
60 {
61  // The Jrk does not allow reads longer than 15 bytes.
62  if (length > 15) { length = 15; }
63 
64  sendCommandHeader(cmd);
65  serialW7(offset);
66  serialW7(length);
67 
68  uint8_t byteCount = _stream->readBytes(buffer, length);
69  if (byteCount != length)
70  {
72 
73  // Set the buffer bytes to 0 so the program will not use an uninitialized
74  // variable.
75  memset(buffer, 0, length);
76  return;
77  }
78 
79  _lastError = 0;
80 }
81 
82 void JrkG2Serial::segmentWrite(uint8_t cmd, uint8_t offset,
83  uint8_t length, uint8_t * buffer)
84 {
85  // The Jrk does not accept writes longer than 7 bytes over serial.
86  if (length > 7) { length = 7; }
87 
88  sendCommandHeader(cmd);
89  serialW7(offset);
90  serialW7(length);
91 
92  // bit i = most-significant bit of buffer[i]
93  uint8_t msbs = 0;
94  for (uint8_t i = 0; i < length; i++)
95  {
96  serialW7(buffer[i]);
97  msbs |= (buffer[i] >> 7 & 1) << i;
98  }
99  serialW7(msbs);
100 
101  _lastError = 0;
102 }
103 
104 void JrkG2Serial::sendCommandHeader(uint8_t cmd)
105 {
106  if (_deviceNumber == 255)
107  {
108  // Compact protocol
109  _stream->write((uint8_t)cmd);
110  }
111  else
112  {
113  // Pololu protocol
114  _stream->write(0xAA);
115  serialW7(_deviceNumber);
116  serialW7((uint8_t)cmd);
117  }
118  _lastError = 0;
119 }
120 
121 /**** JrkG2I2C ****/
122 
123 void JrkG2I2C::commandQuick(uint8_t cmd)
124 {
125  Wire.beginTransmission(_address);
126  Wire.write(cmd);
127  _lastError = Wire.endTransmission();
128 }
129 
130 void JrkG2I2C::commandW7(uint8_t cmd, uint8_t val)
131 {
132  Wire.beginTransmission(_address);
133  Wire.write(cmd);
134  Wire.write(val & 0x7F);
135  _lastError = Wire.endTransmission();
136 }
137 
138 void JrkG2I2C::commandWs14(uint8_t cmd, int16_t val)
139 {
140  uint16_t v = val;
141  Wire.beginTransmission(_address);
142  Wire.write(cmd);
143  Wire.write(v & 0xFF);
144  Wire.write(v >> 8 & 0xFF);
145  _lastError = Wire.endTransmission();
146 }
147 
148 uint8_t JrkG2I2C::commandR8(uint8_t cmd)
149 {
150  Wire.beginTransmission(_address);
151  Wire.write(cmd);
152  _lastError = Wire.endTransmission(false); // no stop (repeated start)
153  if (_lastError) { return 0; }
154 
155  uint8_t byteCount = Wire.requestFrom(_address, (uint8_t)1);
156  if (byteCount != 1)
157  {
159  return 0;
160  }
161 
162  _lastError = 0;
163  uint8_t val = Wire.read();
164  return val;
165 }
166 
167 uint16_t JrkG2I2C::commandR16(uint8_t cmd)
168 {
169  Wire.beginTransmission(_address);
170  Wire.write(cmd);
171  _lastError = Wire.endTransmission(false); // no stop (repeated start)
172  if (_lastError) { return 0; }
173 
174  uint8_t byteCount = Wire.requestFrom(_address, (uint8_t)2);
175  if (byteCount != 2)
176  {
178  return 0;
179  }
180 
181  _lastError = 0;
182  uint8_t valL = Wire.read();
183  uint8_t valH = Wire.read();
184  return (uint16_t)valL | ((uint16_t)valH << 8);
185 }
186 
187 void JrkG2I2C::segmentRead(uint8_t cmd, uint8_t offset,
188  uint8_t length, uint8_t * buffer)
189 {
190  // The Jrk does not allow reads longer than 15 bytes.
191  if (length > 15) { length = 15; }
192 
193  Wire.beginTransmission(_address);
194  Wire.write(cmd);
195  Wire.write(offset);
196  _lastError = Wire.endTransmission(false); // no stop (repeated start)
197  if (_lastError)
198  {
199  // Set the buffer bytes to 0 so the program will not use an uninitialized
200  // variable.
201  memset(buffer, 0, length);
202  return;
203  }
204 
205  uint8_t byteCount = Wire.requestFrom(_address, (uint8_t)length);
206  if (byteCount != length)
207  {
209  memset(buffer, 0, length);
210  return;
211  }
212 
213  _lastError = 0;
214  for (uint8_t i = 0; i < length; i++)
215  {
216  buffer[i] = Wire.read();
217  }
218 }
219 
220 void JrkG2I2C::segmentWrite(uint8_t cmd, uint8_t offset,
221  uint8_t length, uint8_t * buffer)
222 {
223  // The Jrk does not accept writes longer than 13 bytes over I2C.
224  if (length > 13) { length = 13; }
225 
226  Wire.beginTransmission(_address);
227  Wire.write((uint8_t)cmd);
228  Wire.write(offset);
229  Wire.write(length);
230  for (uint8_t i = 0; i < length; i++)
231  {
232  Wire.write(buffer[i]);
233  }
234  _lastError = Wire.endTransmission();
235 }
const uint8_t JrkG2CommReadError
Definition: JrkG2.h:23
uint8_t _lastError
Definition: JrkG2.h:1437