55 *
66 * @section Written by Boris Hilkens 05/06/2022
77 *
8- * @section license License
9- *
108 * MIT license, all text above must be included in any redistribution
119 */
1210
13-
14-
1511#include < SoftwareSerial.h>
1612
1713#include " Arduino.h"
3026 *
3127 */
3228
33- PelcoBus::PelcoBus (uint8_t txPin , uint8_t rxPin , uint8_t readEnPin) {
29+ PelcoBus::PelcoBus (uint8_t rxPin , uint8_t txPin , uint8_t readEnPin) {
3430 txPin_ = txPin;
3531 rxPin_ = rxPin;
3632 rePin_ = readEnPin;
@@ -41,7 +37,7 @@ PelcoBus::PelcoBus(uint8_t txPin, uint8_t rxPin, uint8_t readEnPin) {
4137 *
4238 */
4339
44- void PelcoBus::begin (uint32_t config, bool log_messages = false ) {
40+ void PelcoBus::begin (uint32_t config, bool log_messages) {
4541 uint16_t baud;
4642
4743 switch (config) {
@@ -56,33 +52,36 @@ void PelcoBus::begin(uint32_t config, bool log_messages = false) {
5652 break ;
5753 }
5854
55+ log_messages_ = false ;
56+
5957 if (log_messages) {
60- /* if (!Serial) {
58+ if (!Serial) {
6159 Serial.begin (9600 );
62- } */
60+ }
61+
6362 Serial.print (F (" Message log has been activated for the Pelco Bus!\n " ));
6463
65- sprintf_P (log_buffer, (const char * ) F (" PelcoBus config: baud=%u and protocol D \n " ), baud);
64+ sprintf_P (log_buffer, (const char *) F (" PelcoBus config: baud=%u and protocol D\n " ), baud);
6665 Serial.print (log_buffer);
6766
6867 log_messages_ = true ;
69- } else {
70- log_messages_ = false ;
7168 }
7269
73- if (rePin_ == NOT_A_PIN) {
74- autoModule_ = true ; // Is the module an auto switching between tx and RX ?
75- } else {
70+ // Declare pins
71+ pinMode (rxPin_, INPUT);
72+ pinMode (txPin_, OUTPUT);
73+
74+ // Is the module an auto switching between tx and RX ?
75+ autoModule_ = true ;
76+
77+ if (rePin_ != -1 ) {
7678 pinMode (rePin_, OUTPUT);
7779 autoModule_ = false ;
78- }
7980
80- pinMode (txPin_, OUTPUT);
81- pinMode (rxPin_, INPUT);
82-
83- if (!autoModule_)
8481 digitalWrite (rePin_, HIGH); // Set the module in tx mode
82+ }
8583
84+ // Initialize the serial bus
8685 SerialCamBus = new SoftwareSerial (rxPin_, txPin_);
8786 (*SerialCamBus).begin (baud);
8887}
@@ -91,14 +90,15 @@ void PelcoBus::begin(uint32_t config, bool log_messages = false) {
9190 * @brief Send message to the camera
9291 *
9392 * @param address Address of the camera
93+ * @param disableACK Disable ACK
9494 * @param command the wanted command (see header)
9595 * @param data1 Main parameter
9696 * @param data2 Second parameter for command that requires 2 parameters
9797 * @return true if succeed, false if not succeed
9898 *
9999 */
100100
101- bool PelcoBus::command (uint8_t address, bool disableACK , uint8_t command, uint16_t data1 = 0x00 , uint8_t data2 = 0x00 ) {
101+ bool PelcoBus::command (uint8_t address, bool disableACK, uint8_t command, uint16_t data1, uint8_t data2) {
102102 messToCamera[0 ] = 0xFF ; // The first byte is always FF (sync)
103103 messToCamera[1 ] = address; // the second is the adress
104104
@@ -129,7 +129,8 @@ bool PelcoBus::command(uint8_t address, bool disableACK , uint8_t command, uint1
129129
130130 } else if ((searchIndexPROGMEM (QUERY_CMND, command) != -1 && !disableACK)) {
131131 if (log_messages_) {
132- sprintf_P (log_buffer, (const char * ) F (" Cam %i: You are doing an query into send command ??????????" ), address);
132+ sprintf_P (log_buffer, (const char *)F (" Cam %i: You are doing an query into send command ??????????" ),
133+ address);
133134 Serial.print (log_buffer);
134135 }
135136 return false ;
@@ -145,8 +146,9 @@ bool PelcoBus::command(uint8_t address, bool disableACK , uint8_t command, uint1
145146 0x100 ; // Checksum modulo 0x100
146147
147148 if (log_messages_) { // log the message
148- sprintf_P (log_buffer, (const char * ) F (" Cam %i: Sending message: " ), address);
149+ sprintf_P (log_buffer, (const char *) F (" Cam %i: Sending message: " ), address);
149150 Serial.print (log_buffer);
151+
150152 for (int i = 0 ; i < 7 ; i++) {
151153 sprintf (log_buffer, " %02X" , messToCamera[i]);
152154 Serial.print (log_buffer);
@@ -157,11 +159,10 @@ bool PelcoBus::command(uint8_t address, bool disableACK , uint8_t command, uint1
157159
158160 (*SerialCamBus).write (messToCamera, sizeof (messToCamera)); // Write to the camera
159161
160-
161162 // /////////////////////////////////
162163
163-
164- if ((!disableACK) && (!disableACK)) { // Check the response of the camera only if it isn't a request or the camera does not support return
164+ if (!disableACK) { // Check the response of the camera only if it isn't a request or the camera does not support
165+ // return
165166 int timeout = 10000 ; // 10 millissecond wait
166167
167168 if (!autoModule_)
@@ -171,8 +172,9 @@ bool PelcoBus::command(uint8_t address, bool disableACK , uint8_t command, uint1
171172 if (timeout == 0 ) { // If timeout is reached
172173 if (log_messages_) {
173174 sprintf_P (log_buffer,
174- (const char * ) F (" Cam %i: ERROR Could not verify camera ACK: timeout reached (is camera well plugged in?)\n " ),
175- address);
175+ (const char *)F (" Cam %i: ERROR Could not verify camera ACK: timeout reached (is camera "
176+ " well plugged in?)\n " ),
177+ address);
176178 Serial.print (log_buffer);
177179 }
178180 if (!autoModule_)
@@ -182,8 +184,9 @@ bool PelcoBus::command(uint8_t address, bool disableACK , uint8_t command, uint1
182184 timeout--;
183185 delayMicroseconds (10 );
184186 }
187+
185188 if (log_messages_) {
186- sprintf_P (log_buffer, (const char * ) F (" Cam %i: Reading Acknoledge from camera\n " ), address);
189+ sprintf_P (log_buffer, (const char *) F (" Cam %i: Reading Acknoledge from camera\n " ), address);
187190 Serial.print (log_buffer);
188191 }
189192
@@ -193,7 +196,7 @@ bool PelcoBus::command(uint8_t address, bool disableACK , uint8_t command, uint1
193196 digitalWrite (rePin_, HIGH); // set back at TX mode
194197
195198 if (log_messages_) {
196- sprintf_P (log_buffer, (const char * ) F (" Cam %i: Received ACK data (may be wrong): " ), address);
199+ sprintf_P (log_buffer, (const char *) F (" Cam %i: Received ACK data (may be wrong): " ), address);
197200 Serial.print (log_buffer);
198201
199202 for (int i = 0 ; i < 7 ; i++) {
@@ -205,9 +208,11 @@ bool PelcoBus::command(uint8_t address, bool disableACK , uint8_t command, uint1
205208
206209 if (ACKmessFromCamera[0 ] != 0xFF ) { // Check sync byte and checksum of the previous comand
207210 if (log_messages_) {
208- sprintf_P (log_buffer,
209- (const char * ) F (" Cam %i: ERROR Could not verify camera ACK: bad sync byte (is camera well plugged in?)\n " ),
210- address);
211+ sprintf_P (
212+ log_buffer,
213+ (const char *)F (
214+ " Cam %i: ERROR Could not verify camera ACK: bad sync byte (is camera well plugged in?)\n " ),
215+ address);
211216 Serial.print (log_buffer);
212217 }
213218 return false ;
@@ -216,18 +221,21 @@ bool PelcoBus::command(uint8_t address, bool disableACK , uint8_t command, uint1
216221 if (ACKmessFromCamera[1 ] != address) { // Check adress byte
217222 if (log_messages_) {
218223 sprintf_P (log_buffer,
219- (const char * ) F (" Cam %i: ERROR Could not verify camera ACK: bad address (is camera well plugged in?)\n " ),
220- address);
224+ (const char *)F (
225+ " Cam %i: ERROR Could not verify camera ACK: bad address (is camera well plugged in?)\n " ),
226+ address);
221227 Serial.print (log_buffer);
222228 }
223229 return false ;
224230 }
225231
226232 if (ACKmessFromCamera[2 ] != 0x00 ) { // check the always 0 byte (alarm byte)
227233 if (log_messages_) {
228- sprintf_P (log_buffer,
229- (const char * ) F (" Cam %i: ERROR Could not verify camera ACK: bad null ???? (is camera well plugged in?)\n " ),
230- address);
234+ sprintf_P (
235+ log_buffer,
236+ (const char *)F (
237+ " Cam %i: ERROR Could not verify camera ACK: bad null ???? (is camera well plugged in?)\n " ),
238+ address);
231239 Serial.print (log_buffer);
232240 }
233241 return false ;
@@ -236,15 +244,16 @@ bool PelcoBus::command(uint8_t address, bool disableACK , uint8_t command, uint1
236244 if (ACKmessFromCamera[3 ] != messToCamera[6 ]) { // Check the checksum
237245 if (log_messages_) {
238246 sprintf_P (log_buffer,
239- (const char * ) F (" Cam %i: ERROR Could not verify camera ACK: bad checksum (is camera well plugged in?)\n " ),
240- address);
247+ (const char *)F (
248+ " Cam %i: ERROR Could not verify camera ACK: bad checksum (is camera well plugged in?)\n " ),
249+ address);
241250 Serial.print (log_buffer);
242251 }
243252 return false ;
244253 }
245254
246255 if (log_messages_) {
247- sprintf_P (log_buffer, (const char * ) F (" Cam %i: Message sent and ACK received!\n " ), address);
256+ sprintf_P (log_buffer, (const char *) F (" Cam %i: Message sent and ACK received!\n " ), address);
248257 Serial.print (log_buffer);
249258 }
250259 return true ;
@@ -261,14 +270,14 @@ bool PelcoBus::command(uint8_t address, bool disableACK , uint8_t command, uint1
261270 * @return true if succeded, false if an error occured
262271 */
263272
264- uint16_t PelcoBus::request (uint8_t address, uint8_t request, int timeout = 1000 ) {
273+ uint16_t PelcoBus::request (uint8_t address, uint8_t request, int timeout) {
265274 byte response_command;
266275
267276 if (searchIndexPROGMEM (QUERY_CMND, request) != -1 ) {
268277 response_command = pgm_read_byte (&RESP_CMND[searchIndexPROGMEM (QUERY_CMND, request)]); // Magic!
269278 } else {
270279 if (log_messages_) {
271- sprintf_P (log_buffer, (const char * ) F (" Cam %i: No valid request provided\n " ), address);
280+ sprintf_P (log_buffer, (const char *) F (" Cam %i: No valid request provided\n " ), address);
272281 Serial.print (log_buffer);
273282 Serial.println (request);
274283 }
@@ -286,7 +295,7 @@ uint16_t PelcoBus::request(uint8_t address, uint8_t request, int timeout = 1000)
286295 while (!(*SerialCamBus).available ()) { // Wait for the first bit
287296 if (timeout == 0 ) { // If timeout is reached
288297 if (log_messages_) {
289- sprintf_P (log_buffer, (const char * ) F (" Cam %i: ERROR timout reached\n " ), address);
298+ sprintf_P (log_buffer, (const char *) F (" Cam %i: ERROR timout reached\n " ), address);
290299 Serial.print (log_buffer);
291300 }
292301 if (!autoModule_) {
@@ -314,7 +323,7 @@ uint16_t PelcoBus::request(uint8_t address, uint8_t request, int timeout = 1000)
314323
315324 if (command_index == -1 ) { // Checks if found
316325 if (log_messages_) {
317- sprintf_P (log_buffer, (const char * ) F (" Cam %i: Warn: no reponse from camera (bad index)\n " ), address);
326+ sprintf_P (log_buffer, (const char *) F (" Cam %i: Warn: no reponse from camera (bad index)\n " ), address);
318327 Serial.print (log_buffer);
319328 }
320329 return -1 ;
@@ -334,7 +343,7 @@ uint16_t PelcoBus::request(uint8_t address, uint8_t request, int timeout = 1000)
334343 }
335344
336345 if (log_messages_) {
337- sprintf_P (log_buffer, (const char * ) F (" Cam %i: Message from camera: " ), address);
346+ sprintf_P (log_buffer, (const char *) F (" Cam %i: Message from camera: " ), address);
338347 Serial.print (log_buffer);
339348 for (int i = 0 ; i < 7 ; i++) {
340349 sprintf (log_buffer, " %02X " , messFromcamera[i]);
@@ -346,8 +355,9 @@ uint16_t PelcoBus::request(uint8_t address, uint8_t request, int timeout = 1000)
346355 return (uint16_t )((((uint16_t )messFromcamera[4 ]) << 8 ) | ((uint16_t )messFromcamera[5 ])); // Return LSB data
347356}
348357
349- // ////// todo get the response if it is a query or extended one!!
358+ // todo get the response if it is a query or extended one!!
350359bool PelcoBus::send_raw (String hex_string) {
360+
351361 hex_string.replace (" " , " " ); // Replace spaces
352362
353363 int size = hex_string.length () / 2 ; // Size of the string without space
@@ -365,25 +375,26 @@ bool PelcoBus::send_raw(String hex_string) {
365375
366376 if (raw_command[0 ] != 0xFF ) {
367377 if (log_messages_) {
368- sprintf_P (log_buffer, (const char * ) F (" Cam %i: Wrong sync byte, updating to right sync byte\n " ), address);
378+ sprintf_P (log_buffer, (const char *) F (" Cam %i: Wrong sync byte, updating to right sync byte\n " ), address);
369379 Serial.print (log_buffer);
370380 }
371381 raw_command[0 ] = 0xFF ;
372382 }
373383
374- // ///Check checksum
384+ // ///Check checksum if wrong, replace it
375385 uint8_t checksum = (raw_command[1 ] + raw_command[2 ] + raw_command[3 ] + raw_command[4 ] + raw_command[5 ]) % 0x100 ;
386+
376387 if (checksum != raw_command[6 ]) {
377388 if (log_messages_) {
378- sprintf_P (log_buffer, (const char * ) F (" Cam %i: Wrong checksum, updating to right checksum\n " ), address);
389+ sprintf_P (log_buffer, (const char *) F (" Cam %i: Wrong checksum, updating to right checksum\n " ), address);
379390 Serial.print (log_buffer);
380391 }
381392
382393 raw_command[6 ] = checksum;
383394 }
384395
385396 if (log_messages_) {
386- sprintf_P (log_buffer, (const char * ) F (" Cam %i: sending message " ), address);
397+ sprintf_P (log_buffer, (const char *) F (" Cam %i: sending message " ), address);
387398 Serial.print (log_buffer);
388399 for (int i = 0 ; i < 7 ; i++) {
389400 sprintf (log_buffer, " %02X " , messFromcamera[i]);
@@ -397,7 +408,6 @@ bool PelcoBus::send_raw(String hex_string) {
397408 return true ;
398409}
399410
400-
401411/* !
402412 * @brief Search a value trough a array of bytes one function is for progmem the other one isn't for progmem
403413 *
@@ -417,8 +427,8 @@ int PelcoBus::searchIndexPROGMEM(const byte look_array[], byte value) {
417427 return -1 ;
418428}
419429
420- int PelcoBus::searchIndex (byte look_array[], byte value,
421- size_t size) { // For an x or y reason sizeof don't work properly
430+ // For an x or y reason sizeof don't work properly
431+ int PelcoBus::searchIndex (byte look_array[], byte value, size_t size) {
422432
423433 int i = 0 ;
424434 for (i = 0 ; i <= (size); i++) {
0 commit comments