Skip to content

Commit 573e678

Browse files
committed
Request is working!
1 parent 99fe24e commit 573e678

2 files changed

Lines changed: 132 additions & 21 deletions

File tree

src/Pelco_And_Arduino.cpp

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
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

@@ -21,7 +19,8 @@
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
}

src/Pelco_And_Arduino.h

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "Arduino.h"
1818
#include <SoftwareSerial.h>
1919

20-
//////////////////////////////////////////Some constants
20+
//////////////////////////////////////////Some constants TODO:maybe json; maybe an other thing
2121
const uint8_t PROGMEM PAN_L = 0x04;
2222
const uint8_t PROGMEM PAN_R = 0x02;
2323
const uint8_t PROGMEM TILT_U = 0x08;
@@ -30,7 +30,12 @@ const uint8_t PROGMEM PAN_R_TILT_D = 0x12;
3030

3131
const uint8_t PROGMEM ZOOM_T = 0x20;
3232
const uint8_t PROGMEM ZOOM_W = 0x40;
33-
const uint8_t PROGMEM SET_ZOOM_SPEED = 0x25;
33+
const uint8_t PROGMEM SET_ZOOM_SPEED = 0x25; //Param from 00 to 03
34+
35+
const uint8_t PROGMEM FOCUS_F = 0x80;
36+
const uint8_t PROGMEM FOCUS_N = 0x01;//!!!!not byte 4 but 3
37+
const uint8_t PROGMEM SET_FOCUS_SPEED = 0x27; //Param from 00 to 03
38+
const uint8_t PROGMEM AUTO_FOCUS = 0x2B; //Param from 00 to 02
3439

3540
const uint8_t PROGMEM STOP = 0x00;
3641

@@ -39,16 +44,19 @@ const uint8_t PROGMEM OFF = 0x08;
3944

4045
const uint8_t PROGMEM RESET = 0x29;
4146

42-
const uint8_t PROGMEM SET_PRESET = 0x03; //data2 preset id
43-
const uint8_t PROGMEM GOTO_PRESET = 0x05; //data2 preset id
44-
const uint8_t PROGMEM CLR_PRESET = 0x07; //data2 preset id
47+
const uint8_t PROGMEM SET_PRESET = 0x03; //data preset id
48+
const uint8_t PROGMEM GOTO_PRESET = 0x05; //data preset id
49+
const uint8_t PROGMEM CLR_PRESET = 0x07; //data preset id
4550

4651
const uint8_t PROGMEM QUERY_PAN = 0x51;
4752
const uint8_t PROGMEM QUERY_TILT = 0x53;
4853
const uint8_t PROGMEM QUERY_ZOOM = 0x55;
54+
const uint8_t PROGMEM QUERY_FOCUS = 0x61;
55+
4956
const uint8_t PROGMEM RESP_PAN = 0x59;
5057
const uint8_t PROGMEM RESP_TILT = 0x5B;
5158
const uint8_t PROGMEM RESP_ZOOM = 0x5D;
59+
const uint8_t PROGMEM RESP_FOCUS = 0x63;
5260

5361
/*
5462
@@ -77,22 +85,25 @@ class PelcoCam {
7785
0x00 //Checksum: add all byte except sync and then modulo 0x100
7886
};
7987

80-
uint8_t messFromcamera[7]={
81-
0x00, //sync byte
82-
0x00, //Address
83-
0x00, //Always 0 (in most of the cases)
84-
0x00, //Command
85-
0x00, //Data1 in MSB
86-
0x00, //Data2 in LSB
87-
0x00 //Checksum: add all byte except sync and then modulo 0x100
88-
};
88+
uint8_t messFromcamera[7] ={
89+
0x00, //sync byte
90+
0x00, //Address
91+
0x00, //Always 0 (in most of the cases)
92+
0x00, //Command
93+
0x00, //Data1 (Used for pan speed and response from camera)
94+
0x00, //Data2 (Used everywhere)
95+
0x00 //Checksum: add all byte except sync and then modulo 0x100
96+
};
97+
98+
99+
int searchIndex(byte look_array[], byte value);
89100

90101
public:
91102
PelcoCam(uint8_t Address, int baud, int txPin, int rxPin, bool log_messages);
92103
void begin();
93-
void send_message(uint8_t command, uint8_t params=0x00, uint8_t params2=0x00);
104+
void send_command(uint8_t command, uint8_t params=0x00, uint8_t params2=0x00);
105+
bool send_request(uint8_t request, uint timeout = 1000, uint max_buffer=20);
94106

95-
96107
};
97108

98109
#endif

0 commit comments

Comments
 (0)