MercurySDK
Software development kit for Mercury digital servos
group_sync_write.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2017 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Author: zerom, Ryu Woon Jung (Leon) */
18 
19 #include <algorithm>
20 
21 #if defined(__linux__)
22 #include "group_sync_write.h"
23 #elif defined(__APPLE__)
24 #include "group_sync_write.h"
25 #elif defined(_WIN32) || defined(_WIN64)
26 #define WINDLLEXPORT
27 #include "group_sync_write.h"
28 #elif defined(ARDUINO) || defined(__OPENCR__) || defined(__OPENCM904__) || defined(ARDUINO_OpenRB)
29 #include "../../include/mercury_sdk/group_sync_write.h"
30 #endif
31 
32 using namespace mercury;
33 
34 GroupSyncWrite::GroupSyncWrite(PortHandler *port, PacketHandler *ph, uint16_t start_address, uint16_t data_length)
35  : GroupHandler(port, ph),
36  start_address_(start_address),
37  data_length_(data_length)
38 {
39  clearParam();
40 }
41 
42 void GroupSyncWrite::makeParam()
43 {
44  if (id_list_.size() == 0) return;
45 
46  if (param_ != 0)
47  delete[] param_;
48  param_ = 0;
49 
50  param_ = new uint8_t[id_list_.size() * (1 + data_length_)]; // ID(1) + DATA(data_length)
51 
52  int idx = 0;
53  for (unsigned int i = 0; i < id_list_.size(); i++)
54  {
55  uint8_t id = id_list_[i];
56  if (data_list_[id] == 0)
57  return;
58 
59  param_[idx++] = id;
60  for (int c = 0; c < data_length_; c++)
61  param_[idx++] = (data_list_[id])[c];
62  }
63 }
64 
65 bool GroupSyncWrite::addParam(uint8_t id, uint8_t *data)
66 {
67  if (std::find(id_list_.begin(), id_list_.end(), id) != id_list_.end()) // id already exist
68  return false;
69 
70  id_list_.push_back(id);
71  data_list_[id] = new uint8_t[data_length_];
72  for (int c = 0; c < data_length_; c++)
73  data_list_[id][c] = data[c];
74 
75  is_param_changed_ = true;
76  return true;
77 }
78 
80 {
81  std::vector<uint8_t>::iterator it = std::find(id_list_.begin(), id_list_.end(), id);
82  if (it == id_list_.end()) // NOT exist
83  return;
84 
85  id_list_.erase(it);
86  delete[] data_list_[id];
87  data_list_.erase(id);
88 
89  is_param_changed_ = true;
90 }
91 
92 bool GroupSyncWrite::changeParam(uint8_t id, uint8_t *data)
93 {
94  std::vector<uint8_t>::iterator it = std::find(id_list_.begin(), id_list_.end(), id);
95  if (it == id_list_.end()) // NOT exist
96  return false;
97 
98  delete[] data_list_[id];
99  data_list_[id] = new uint8_t[data_length_];
100  for (int c = 0; c < data_length_; c++)
101  data_list_[id][c] = data[c];
102 
103  is_param_changed_ = true;
104  return true;
105 }
106 
108 {
109  if (id_list_.size() == 0)
110  return;
111 
112  for (unsigned int i = 0; i < id_list_.size(); i++)
113  delete[] data_list_[id_list_[i]];
114 
115  id_list_.clear();
116  data_list_.clear();
117  if (param_ != 0)
118  delete[] param_;
119  param_ = 0;
120 }
121 
123 {
124  if (id_list_.size() == 0)
125  return COMM_NOT_AVAILABLE;
126 
127  if (is_param_changed_ == true || param_ == 0)
128  makeParam();
129 
130  return ph_->syncWriteTxOnly(port_, start_address_, data_length_, param_, id_list_.size() * (1 + data_length_));
131 }
PacketHandler * ph_
Definition: group_handler.h:44
std::vector< uint8_t > id_list_
Definition: group_handler.h:46
std::map< uint8_t, uint8_t * > data_list_
Definition: group_handler.h:47
PortHandler * port_
Definition: group_handler.h:43
bool changeParam(uint8_t id, uint8_t *data)
The function that changes the data for write in id -> start_address -> data_length to the Sync Write ...
void clearParam()
The function that clears the Sync Write list.
void removeParam(uint8_t id)
The function that removes id from the Sync Write list.
GroupSyncWrite(PortHandler *port, PacketHandler *ph, uint16_t start_address, uint16_t data_length)
The function that Initializes instance for Sync Write.
int txPacket()
The function that transmits the Sync Write instruction packet which might be constructed by GroupSync...
bool addParam(uint8_t id, uint8_t *data)
The function that adds id, start_address, data_length to the Sync Write list.
The class that inherits Protocol1PacketHandler class or Protocol2PacketHandler class.
virtual int syncWriteTxOnly(PortHandler *port, uint16_t start_address, uint16_t data_length, uint8_t *param, uint16_t param_length)=0
The function that transmits INST_SYNC_WRITE instruction packet @description The function makes an ins...
The class for port control that inherits PortHandlerLinux, PortHandlerWindows, PortHandlerMac,...
Definition: port_handler.h:56
#define COMM_NOT_AVAILABLE