21 #include "AnimatSerial.h"
24 AnimatSerial::AnimatSerial(){
42 AnimatSerial::AnimatSerial(HardwareSerial *ss,
unsigned int inTotal,
unsigned int outTotal){
47 inDataTotal = inTotal;
48 outDataTotal = outTotal;
58 changed =
new bool[inDataTotal];
69 AnimatSerial::~AnimatSerial()
84 bool AnimatSerial::isChanged()
89 void AnimatSerial::clearInData()
93 for(
int i=0; i<inDataTotal; i++)
95 inData[i].value.fval = 0;
101 void AnimatSerial::clearOutData()
105 for(
int i=0; i<outDataTotal; i++)
106 outData[i].value.fval = 0;
110 void AnimatSerial::clearChanged()
115 for(
int i=0; i<inDataTotal; i++)
120 bool AnimatSerial::isChanged(
unsigned int index)
122 if(changed && index < inDataTotal)
123 return changed[index];
128 bool AnimatSerial::getData(
unsigned int index,
AnimatData &data)
130 if(index < inDataTotal)
132 data = inData[index];
139 bool AnimatSerial::addData(
unsigned int id,
float val)
141 if(outDataCount < outDataTotal)
143 outData[0].id.ival = id;
144 outData[0].value.fval = val;
163 void AnimatSerial::setInDataValue(
int id,
float val)
165 if(inData && changed &&
id < inDataTotal)
167 inData[id].value.fval = val;
173 void AnimatSerial::begin(
unsigned long baud){
185 int AnimatSerial::readMsgs()
189 while(stream->available() > 0)
195 if(stream->read() == 0xff)
198 checksum = (int) vals[0];
207 vals[index] = (
unsigned char) stream->read();
208 if(vals[index] == 0xff)
210 checksum += (int) vals[index];
219 vals[index] = (
unsigned char) stream->read();
220 messageID = vals[index];
222 checksum += (int) vals[index];
228 vals[index] = (
unsigned char) stream->read();
229 packetSize = vals[index];
231 checksum += (int) vals[index];
237 vals[index] = (
unsigned char) stream->read();
238 packetSize += (vals[index] << 8);
245 checksum += (int) vals[index];
249 else if(index > 4 && index <= packetSize)
251 vals[index] = (
unsigned char) stream->read();
253 if(index == (packetSize-1))
256 int iChecksum = vals[index];
258 if(checksum%256 != iChecksum)
271 else if(messageID == 1)
273 int iStop = packetSize - 1;
275 for(
int iIdx=START_MESSAGE_INFO_BYTE; iIdx<iStop; iIdx+=DATA_SIZE)
277 id.bval[0] = vals[iIdx];
278 id.bval[1] = vals[iIdx+1];
279 value.bval[0] = vals[iIdx+2];
280 value.bval[1] = vals[iIdx+3];
281 value.bval[2] = vals[iIdx+4];
282 value.bval[3] = vals[iIdx+5];
296 setInDataValue(
id.ival, value.fval);
307 checksum += (int) vals[index];
311 if(index >= MAX_ANIMAT_BUFFER)
321 void AnimatSerial::writeMsgs(){
322 if(stream != NULL && outDataCount > 0) {
323 int checksum = 0xFF + 0xFF + 0x01;
326 stream->write((byte) 0xFF);
327 stream->write((byte) 0xFF);
328 stream->write((byte) 0x01);
331 size.ival = HEADER_SIZE + (DATA_SIZE * outDataCount) + FOOTER_SIZE;
332 stream->write((byte) size.bval[0]);
333 checksum += size.bval[0];
334 stream->write((byte) size.bval[1]);
335 checksum += size.bval[1];
342 for(
int i=0; i<outDataCount; i++) {
343 stream->write((byte) outData[i].
id.bval[0]);
344 stream->write((byte) outData[i].
id.bval[1]);
345 stream->write((byte) outData[i].value.bval[0]);
346 stream->write((byte) outData[i].value.bval[1]);
347 stream->write((byte) outData[i].value.bval[2]);
348 stream->write((byte) outData[i].value.bval[3]);
350 checksum += outData[i].id.bval[0];
351 checksum += outData[i].id.bval[1];
352 checksum += outData[i].value.bval[0];
353 checksum += outData[i].value.bval[1];
354 checksum += outData[i].value.bval[2];
355 checksum += outData[i].value.bval[3];
373 unsigned char bchecksum = (
unsigned char) (checksum%256);
374 stream->write(bchecksum);
387 void AnimatSerial::writeResendMsg(){
389 int checksum = 0xFF + 0xFF + 0x02;
392 stream->write((byte) 0xFF);
393 stream->write((byte) 0xFF);
394 stream->write((byte) 0x02);
397 size.ival = HEADER_SIZE + FOOTER_SIZE;
398 stream->write((byte) size.bval[0]);
399 checksum += size.bval[0];
400 stream->write((byte) size.bval[1]);
401 checksum += size.bval[1];
408 unsigned char bchecksum = (
unsigned char) (checksum%256);
409 stream->write(bchecksum);
420 void AnimatSerial::writeAllMsgs(){
422 if(stream != NULL && outDataTotal > 0) {
423 for(
int i=0; i<outDataTotal; i++)
424 addData(i, outData[i].value.fval);