1010 * MIT license, all text above must be included in any redistribution
1111 */
1212
13- // Function comment
14-
1513#include " Arduino.h"
1614#include < SoftwareSerial.h>
1715
2119 * @brief Constructor
2220 *
2321 * @param Address The address of the camera
24- * @param baud baud of the camera
22+ * @param baud baud of the cameras
23+ * @param parameter-name description
2524 *
2625 */
2726
@@ -57,11 +56,11 @@ void PelcoCam::begin(){
5756 *
5857 */
5958
60- void PelcoCam::send_message (uint8_t command, uint8_t params, uint8_t params2){
59+ void PelcoCam::send_command (uint8_t command, uint8_t params, uint8_t params2){
6160 messToCamera[0 ] = 0xFF ;
6261 messToCamera[1 ] = Address_;
6362
64- if (command == ON || command == OFF){ // If the command is on or off, set the command to uint8_t 3
63+ if (command == ON || command == OFF || command == FOCUS_N ){ // If the command is on or off, set the command to uint8_t 3
6564 messToCamera[2 ] = command;
6665 messToCamera[3 ] = 0x00 ;
6766 }else {
@@ -92,4 +91,105 @@ void PelcoCam::send_message(uint8_t command, uint8_t params, uint8_t params2){
9291 }
9392
9493 SerialCam.write (messToCamera, sizeof (messToCamera));
94+ }
95+
96+ /* !
97+ * @brief Send a query to the camera and reads the response
98+ *
99+ * @param request the wanted reponse (see header)
100+ * @param timeout default 1000; timout for waiting a byte
101+ * @param maxbuffer Maximum size of the buffer; defaut 20
102+ *
103+ * @return true if succeded, false if an error occured
104+ */
105+
106+ bool PelcoCam::send_request (uint8_t request, uint timeout, uint maxbuffer){
107+ byte response_command;
108+
109+ if (request == QUERY_PAN){response_command == RESP_PAN;}
110+ else if (request == QUERY_TILT){response_command = RESP_TILT;}
111+ else if (request == QUERY_ZOOM){response_command = RESP_ZOOM;}
112+ else if (request == QUERY_FOCUS){response_command = RESP_FOCUS;}
113+ else {
114+ if (log_messages_) Serial.println (" No valid request provided" );
115+ return false ;
116+ }
117+
118+ send_command (request); // Send the query
119+
120+ while (!SerialCam.available ()){// Wait for the first bit
121+ if (timeout==0 ){ // If timeout is reached
122+ if (log_messages_) Serial.print (" ERROR timout reached" );
123+ return false ;
124+ }
125+ timeout--;
126+ delayMicroseconds (10 );
127+ }
128+
129+ byte buffer[maxbuffer]; // Buffer for the reception from the camera
130+ uint index = 0 ;
131+
132+ while (SerialCam.available ()){// Do it until there is no more things to read
133+ buffer[index]=SerialCam.read ();
134+ index++;
135+ }
136+
137+ /* A theorical response:
138+ FF 00 59 4A 13 B7
139+ sync null command msb lsb checksum
140+
141+ A practical response (what does the arduino reads):
142+ FF 00 FF 00 59 4A 13 B7 FF 01 48
143+ */
144+
145+ int command_index = searchIndex (buffer, 0x59 );// Looks up where is the index of the response command
146+
147+ if (command_index == -1 // Checks if found
148+ || command_index < 3 // Checks if the reponse byte is in the right place
149+ ){
150+ if (log_messages_)Serial.println (" Warn: no reponse from camera" );
151+ return false ;
152+ }
153+
154+ // Checksum is sum of everything except sync modulo 0x100
155+ bool checksum = ((buffer[command_index] + buffer[command_index+1 ] + buffer[command_index+2 ])%0x100 == buffer[command_index+3 ]);
156+
157+ // Checking the sync byte, the null byte and the checksum
158+ if (buffer[command_index-3 ] != 0xFF &&
159+ buffer[command_index-2 != 00 ] &&
160+ !checksum){
161+ return false ;
162+ }
163+
164+ for (int i=0 ; i<7 ; i++){
165+ messFromcamera[i] = buffer[(command_index-3 )+i];
166+ }
167+
168+ if (log_messages_){
169+ Serial.print (" Message from camera: " );
170+ for (int i=0 ; i<7 ; i++){
171+ Serial.printf (" %02X " , messFromcamera[i]);
172+ }
173+ Serial.println ();
174+ }
175+
176+ return true ;
177+ }
178+
179+ /* !
180+ * @brief Dearch a value trough a array of bytes
181+ *
182+ * @param look_array the array
183+ * @param value value to lookup
184+ *
185+ * @return the index of the element found
186+ */
187+
188+ int PelcoCam::searchIndex (byte look_array[], byte value){
189+ for (int i=0 ; i<sizeof (look_array)/sizeof (look_array[0 ]); i++){
190+ if (look_array[i] == value){
191+ return i;
192+ }
193+ }
194+ return -1 ;
95195}
0 commit comments