@@ -82,7 +82,8 @@ void PelcoCam::begin() {
8282 }
8383
8484 if (log_messages_)
85- Serial.printf (" Cam %i: Pelco config: baud=%u and protocol=%d (0 for D and 1 for P) \n " , address_, baud, protocol);
85+ Serial.printf (" Cam %i: Pelco config: baud=%u and protocol=%d (0 for D and 1 for P) \n " , address_, baud,
86+ protocol);
8687
8788 if (rePin_ == NOT_A_PIN) {
8889 autoModule_ = true ; // Is the module an auto switching between tx and RX ?
@@ -108,38 +109,46 @@ void PelcoCam::begin() {
108109 * @param params2 Second parameter for command that requires 2 parameters
109110 * @param params2 Second parameter for command that requires 2 parameters
110111 *
111- * @return true if succeed false if not succeed
112+ * @return true if succeed, false if not succeed
112113 *
113114 */
114115
115116bool PelcoCam::send_command (uint8_t command, uint8_t params, uint8_t params2, bool request) {
116- messToCamera[0 ] = 0xFF ;
117- messToCamera[1 ] = address_;
117+ messToCamera[0 ] = 0xFF ; // The first byte is always FF (sync)
118+ messToCamera[1 ] = address_; // the second is the adress
118119
119- if (command == ON || command == OFF || command == FOCUS_N) { // If the command is on or off, set the command to
120- // uint8_t 3
120+
121+ // the thrid byte is determined by the command itself (see the command list)
122+
123+ if (command == ON || command == OFF || command == FOCUS_N) { // If the command is on or off, set the command to the third byte
121124 messToCamera[2 ] = command;
122125 messToCamera[3 ] = 0x00 ;
123126 } else {
124127 messToCamera[2 ] = 0x00 ;
125128 messToCamera[3 ] = command;
126129 }
127- if (command == PAN_L || command == PAN_R) {
130+
131+
132+ if (command == PAN_L || command == PAN_R) {// //Only for PAN Left and right you doing this
128133 messToCamera[4 ] = params;
129134 messToCamera[5 ] = 0x00 ;
130- } else if (command == PAN_L_TILT_U || command == PAN_R_TILT_U || command == PAN_L_TILT_D ||
135+ } else if (command == PAN_L_TILT_U || command == PAN_R_TILT_U || command == PAN_L_TILT_D || // Commands that takes two params
131136 command == PAN_R_TILT_D) {
132137 messToCamera[4 ] = params2;
133138 messToCamera[5 ] = params;
134- } else {
139+ } else { // "Normal" condition
135140 messToCamera[4 ] = 0x00 ;
136141 messToCamera[5 ] = params;
137142 }
138143
139- messToCamera[6 ] =
140- (messToCamera[1 ] + messToCamera[2 ] + messToCamera[3 ] + messToCamera[4 ] + messToCamera[5 ]) % 0x100 ; // Checksum
144+ messToCamera[6 ] = (messToCamera[1 ]
145+ + messToCamera[2 ]
146+ + messToCamera[3 ]
147+ + messToCamera[4 ]
148+ + messToCamera[5 ]) % 0x100 ; // Checksum modulo 0x100
141149
142- if (log_messages_) {
150+
151+ if (log_messages_) { // log the message
143152 Serial.printf (" Cam %i: Sending message: " , address_);
144153 for (int i = 0 ; i < 7 ; i++) {
145154 Serial.printf (" %02X" , messToCamera[i]);
@@ -148,19 +157,20 @@ bool PelcoCam::send_command(uint8_t command, uint8_t params, uint8_t params2, bo
148157 Serial.println ();
149158 }
150159
151- SerialCam.write (messToCamera, sizeof (messToCamera));
160+ SerialCam.write (messToCamera, sizeof (messToCamera)); // Write to the camera
152161
153162 if (!request) { // Check the response of the camera
154163 int timeout = 10000 ; // 10 millissecond wait
155- byte buffer[4 ]; // Buffer for the reception from the camera
156164
157165 if (!autoModule_)
158166 digitalWrite (rePin_, LOW); // Set the module at RX mode
159167
160168 while (!SerialCam.available ()) { // Wait for the first bit
161169 if (timeout == 0 ) { // If timeout is reached
162170 if (log_messages_)
163- Serial.printf (" Cam %i: ERROR Could not verify camera reponse: timeout reached (is camera well plugged in?)\n " , address_);
171+ Serial.printf (
172+ " Cam %i: ERROR Could not verify camera reponse: timeout reached (is camera well plugged in?)\n " ,
173+ address_);
164174 if (!autoModule_)
165175 digitalWrite (rePin_, HIGH); // set back at TX mode
166176 return false ;
@@ -169,30 +179,37 @@ bool PelcoCam::send_command(uint8_t command, uint8_t params, uint8_t params2, bo
169179 delayMicroseconds (10 );
170180 }
171181
172- SerialCam.readBytes (buffer , 4 );
182+ SerialCam.readBytes (ACKmessFromCamera , 4 );
173183
174184 if (!autoModule_)
175185 digitalWrite (rePin_, HIGH); // set back at TX mode
176186
177- int command_index = searchIndex (buffer, messToCamera[6 ]); // Looks up where is the index of the response TODO: verify every command
178187
179- if (command_index != 3 ) { // Checks if found
180- if (messToCamera[6 ] != 0x01 ){// Filter stop command cause checksum==1
188+ // / TODO better
189+ int command_index = searchIndex (
190+ ACKmessFromCamera, messToCamera[6 ]); // Looks up where is the index of the response TODO: verify every command
191+
192+ if (command_index != 3 ) { // Checks if found
193+ if (messToCamera[6 ] != 0x01 ) { // Filter stop command cause checksum==1
181194 if (log_messages_)
182- Serial.printf (" Cam %i: ERROR Could not verify camera reponse: bad index (is camera well plugged in?)\n " , address_);
195+ Serial.printf (
196+ " Cam %i: ERROR Could not verify camera reponse: bad index (is camera well plugged in?)\n " ,
197+ address_);
183198 return false ;
184199 }
185200 }
186201
187- if (buffer[command_index - 3 ] != 0xFF ) { // Check sync byte and checksum of the previous comand
188- if (messToCamera[6 ] != 0x01 ){// Filter stop command cause checksum==1
189- if (log_messages_)
190- Serial.printf (" Cam %i: ERROR Could not verify camera reponse: bad sync byte (is camera well plugged in?)\n " , address_);
191- return false ;
192- }
202+ if (ACKmessFromCamera[command_index - 3 ] != 0xFF ) { // Check sync byte and checksum of the previous comand
203+ if (messToCamera[6 ] != 0x01 ) { // Filter stop command cause checksum==1
204+ if (log_messages_)
205+ Serial.printf (
206+ " Cam %i: ERROR Could not verify camera reponse: bad sync byte (is camera well plugged in?)\n " ,
207+ address_);
208+ return false ;
209+ }
193210 }
194211
195- if (log_messages_)
212+ if (log_messages_) // log
196213 Serial.printf (" Cam %i: Message sent and ACK received!\n " , address_);
197214 return true ;
198215 }
0 commit comments