@@ -47,7 +47,7 @@ PelcoCam::PelcoCam(uint8_t address, uint32_t config, uint8_t txPin, uint8_t rxPi
4747
4848void PelcoCam::begin () {
4949 if (log_messages_) {
50- Serial.begin (115200 );
50+ if (Serial) Serial.begin (115200 );
5151 Serial.printf (" Message log has been activated for the camera %02X !\n " , address_);
5252 }
5353
@@ -98,49 +98,50 @@ void PelcoCam::begin() {
9898 *
9999 */
100100
101- bool PelcoCam::send_command (uint8_t command, uint8_t data1, uint8_t data2, bool request) {
102- messToCamera[0 ] = 0xFF ; // The first byte is always FF (sync)
103- messToCamera[1 ] = address_; // the second is the adress
101+ bool PelcoCam::send_command (uint8_t command, uint16_t data1, uint8_t data2, bool request) {
102+ messToCamera[0 ] = 0xFF ; // The first byte is always FF (sync)
103+ messToCamera[1 ] = address_; // the second is the adress
104104
105- // Special commands handling:
106-
107- if (searchIndex (CMND1, command)!=-1 ){
108- messToCamera[2 ] = command - 0b01100000 ; // Minus the thing that differentiate from others
105+ // Special commands handling:
106+ if (searchIndex (CMND1, command) != -1 ) {
107+ messToCamera[2 ] = command - 0b01100000 ; // Minus the thing that differentiate from others
109108 messToCamera[3 ] = 0x00 ;
110109 messToCamera[4 ] = 0x00 ;
111110 messToCamera[5 ] = 0x00 ;
112- }else {// that means command is the byte 4
111+ } else { // that means command is the byte 4
112+
113113 messToCamera[2 ] = 0x00 ;
114114 messToCamera[3 ] = command;
115-
116- if (searchIndex (DATA1, command)!=-1 ){
115+ if (searchIndex (DATA1, command) != -1 ) {
117116 messToCamera[4 ] = data1;
118117 messToCamera[5 ] = 0x00 ;
119- }else if (searchIndex (DATA_BOTH, command)!= -1 ){
118+ } else if (searchIndex (DATA_BOTH, command) != -1 ) {
120119 messToCamera[4 ] = data1;
121120 messToCamera[5 ] = data2;
122- }else if (searchIndex (SETPOS, command)!=-1 ){
123- messToCamera[4 ] = (uint8_t ) (data1 >> 8 ) & 0x00FF ; // Get MSB
124- messToCamera[5 ] = (uint8_t ) data1 & 0x00FF ; // Get LSB
125- }else if (searchIndex (QUERY_CMND, command)!=-1 ){
126- if (log_messages_) Serial.printf (" Cam %i: You are doing an query into send command ??????????" , address_);
127- return false ;
128- }else {
129- if (log_messages_) Serial.printf (" Cam %i: No vlaid command? ??????????" , address_);
121+ } else if (searchIndex (SETPOS, command) != -1 ) {
122+ uint angle = (uint16_t )(data1 % 35999 ); // Centième de degrès! avec max = 35999 (359.99 deg)
123+
124+ messToCamera[4 ] = (uint8_t )((angle >> 0x08 ) & 0x00FF ); // Get MSB
125+ messToCamera[5 ] = (uint8_t )(angle & 0x00FF ); // Get LSB
126+ Serial.printf (" %X" , angle);
127+ Serial.printf (" %X" , messToCamera[4 ]);
128+ Serial.printf (" %X\n " , messToCamera[5 ]);
129+ } else if (searchIndex (QUERY_CMND, command) != -1 ) {
130+ if (log_messages_)
131+ Serial.printf (" Cam %i: You are doing an query into send command ??????????" , address_);
130132 return false ;
133+ } else {
134+ messToCamera[4 ] = 0x00 ;
135+ messToCamera[5 ] = data1;
131136 }
132137 }
133138
134139 // ////
135140
136- messToCamera[6 ] = (messToCamera[1 ]
137- + messToCamera[2 ]
138- + messToCamera[3 ]
139- + messToCamera[4 ]
140- + messToCamera[5 ]) % 0x100 ; // Checksum modulo 0x100
141+ messToCamera[6 ] = (messToCamera[1 ] + messToCamera[2 ] + messToCamera[3 ] + messToCamera[4 ] + messToCamera[5 ]) %
142+ 0x100 ; // Checksum modulo 0x100
141143
142-
143- if (log_messages_) { // log the message
144+ if (log_messages_) { // log the message
144145 Serial.printf (" Cam %i: Sending message: " , address_);
145146 for (int i = 0 ; i < 7 ; i++) {
146147 Serial.printf (" %02X" , messToCamera[i]);
@@ -149,9 +150,9 @@ bool PelcoCam::send_command(uint8_t command, uint8_t data1, uint8_t data2, bool
149150 Serial.println ();
150151 }
151152
152- SerialCam.write (messToCamera, sizeof (messToCamera)); // Write to the camera
153+ SerialCam.write (messToCamera, sizeof (messToCamera)); // Write to the camera
153154
154- if (!request) {// Check the response of the camera only if it isn't a request
155+ if (!request) { // Check the response of the camera only if it isn't a request
155156 int timeout = 10000 ; // 10 millissecond wait
156157
157158 if (!autoModule_)
@@ -171,45 +172,44 @@ bool PelcoCam::send_command(uint8_t command, uint8_t data1, uint8_t data2, bool
171172 delayMicroseconds (10 );
172173 }
173174
174- SerialCam.readBytes (ACKmessFromCamera, 4 ); // General response is 4 byte
175+ SerialCam.readBytes (ACKmessFromCamera, 4 ); // General response is 4 byte
175176
176177 if (!autoModule_)
177178 digitalWrite (rePin_, HIGH); // set back at TX mode
178179
179-
180180 if (ACKmessFromCamera[0 ] != 0xFF ) { // Check sync byte and checksum of the previous comand
181- if (log_messages_)
182- Serial.printf (
183- " Cam %i: ERROR Could not verify camera reponse: bad sync byte (is camera well plugged in?)\n " ,
184- address_);
185- return false ;
181+ if (log_messages_)
182+ Serial.printf (
183+ " Cam %i: ERROR Could not verify camera reponse: bad sync byte (is camera well plugged in?)\n " ,
184+ address_);
185+ return false ;
186186 }
187187
188- if (ACKmessFromCamera[1 ] != address_) {// Check adress byte
188+ if (ACKmessFromCamera[1 ] != address_) { // Check adress byte
189189 if (log_messages_)
190190 Serial.printf (
191191 " Cam %i: ERROR Could not verify camera reponse: bad address (is camera well plugged in?)\n " ,
192192 address_);
193193 return false ;
194194 }
195195
196- if (ACKmessFromCamera[2 ] != 0x00 ) {// check the always 0 byte (alarm byte)
196+ if (ACKmessFromCamera[2 ] != 0x00 ) { // check the always 0 byte (alarm byte)
197197 if (log_messages_)
198198 Serial.printf (
199199 " Cam %i: ERROR Could not verify camera reponse: bad null ???? (is camera well plugged in?)\n " ,
200200 address_);
201201 return false ;
202202 }
203203
204- if (ACKmessFromCamera[3 ] != messToCamera[6 ]) {// Check the checksum
204+ if (ACKmessFromCamera[3 ] != messToCamera[6 ]) { // Check the checksum
205205 if (log_messages_)
206206 Serial.printf (
207207 " Cam %i: ERROR Could not verify camera reponse: bad checksum (is camera well plugged in?)\n " ,
208208 address_);
209209 return false ;
210210 }
211211
212- if (log_messages_) // log
212+ if (log_messages_) // log
213213 Serial.printf (" Cam %i: Message sent and ACK received!\n " , address_);
214214 return true ;
215215 }
@@ -228,8 +228,8 @@ bool PelcoCam::send_command(uint8_t command, uint8_t data1, uint8_t data2, bool
228228uint16_t PelcoCam::send_request (uint8_t request, uint timeout, uint maxbuffer) {
229229 byte response_command;
230230
231- if (searchIndex (QUERY_CMND, request)!= -1 ) {
232- response_command == RESP_CMND[searchIndex (QUERY_CMND, request)]; // Magic!
231+ if (searchIndex (QUERY_CMND, request) != -1 ) {
232+ response_command == RESP_CMND[searchIndex (QUERY_CMND, request)]; // Magic!
233233 } else {
234234 if (log_messages_)
235235 Serial.printf (" Cam %i: No valid request provided\n " , address_);
@@ -296,10 +296,9 @@ uint16_t PelcoCam::send_request(uint8_t request, uint timeout, uint maxbuffer) {
296296 Serial.println ();
297297 }
298298
299- return (uint16_t ) ( messFromcamera[4 ] << 8 | messFromcamera[5 ]); // Return LSB data
299+ return (uint16_t )(((( uint16_t ) messFromcamera[4 ]) << 8 ) | (( uint16_t ) messFromcamera[5 ]) ); // Return LSB data
300300}
301301
302-
303302// //////todo get the response if it is a query or extended one!!
304303bool PelcoCam::send_raw (String hex_string) {
305304 hex_string.replace (" " , " " ); // Replace spaces
@@ -315,14 +314,17 @@ bool PelcoCam::send_raw(String hex_string) {
315314 raw_command[i] = (byte)strtol (buffer, NULL , 16 ); // conversion into byte
316315 }
317316
318- if (raw_command[0 ] != 0xFF )
319- return ; // Check sync byte
317+ if (raw_command[0 ] != 0xFF ) {
318+ if (log_messages_)
319+ Serial.printf (" Cam %i: Wrong sync byte, updating to right sync byte\n " , address_);
320+ raw_command[0 ] = 0xFF ;
321+ }
320322
321323 // ///Check checksum
322324 uint8_t checksum = (raw_command[1 ] + raw_command[2 ] + raw_command[3 ] + raw_command[4 ] + raw_command[5 ]) % 0x100 ;
323325 if (checksum != raw_command[6 ]) {
324326 if (log_messages_)
325- Serial.printf (" Cam %i: Wrong chacksum , updating to right checksum\n " , address_);
327+ Serial.printf (" Cam %i: Wrong checksum , updating to right checksum\n " , address_);
326328
327329 raw_command[6 ] = checksum;
328330 }
@@ -351,7 +353,8 @@ bool PelcoCam::send_raw(String hex_string) {
351353 */
352354
353355int PelcoCam::searchIndex (const byte look_array[], byte value) {
354- for (int i = 0 ; i < sizeof (look_array) / sizeof (look_array[0 ]); i++) {
356+ int i = 0 ;
357+ for (i = 0 ; i <= (sizeof (look_array) / sizeof (look_array[0 ])); i++) {
355358 if (look_array[i] == value) {
356359 return i;
357360 }
0 commit comments