Skip to content

Commit 7d6a02e

Browse files
committed
temp changes
1 parent 03dfc94 commit 7d6a02e

2 files changed

Lines changed: 97 additions & 64 deletions

File tree

src/Pelco_And_Arduino.cpp

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
*
2525
*/
2626

27-
PelcoCam::PelcoCam(uint8_t Address, int baud, int txPin, int rxPin, bool log_messages=false){
27+
PelcoCam::PelcoCam(uint8_t Address, int baud, int txPin, int rxPin, int readEnPin=NOT_A_PIN, bool log_messages=false){
2828
Address_ = Address;
2929
baud_ = baud;
3030
txPin_ = txPin;
3131
rxPin_ = rxPin;
32+
rePin_ = readEnPin;
3233
log_messages_ = log_messages;
33-
3434
}
3535

3636
/*!
@@ -44,6 +44,22 @@ void PelcoCam::begin(){
4444
Serial.println("Message log has been activated!");
4545
}
4646

47+
48+
if(rePin_ == NOT_A_PIN){
49+
autoModule_ = false; //Is the module an auto switching between tx and RX ?
50+
}else{
51+
pinMode(rePin_, OUTPUT);
52+
autoModule_ = true;
53+
}
54+
55+
pinMode(txPin_, OUTPUT);
56+
pinMode(rxPin_, INPUT);
57+
58+
59+
if(autoModule_) digitalWrite(rePin_, HIGH); //Set the module in tx mode
60+
61+
62+
4763
SerialCam.begin(baud_, SWSERIAL_8N1, rxPin_, txPin_);
4864
}
4965

@@ -53,10 +69,11 @@ void PelcoCam::begin(){
5369
* @param command the wanted command (see header)
5470
* @param params Main parameter
5571
* @param params2 Second parameter for command that requires 2 parameters
72+
* @param params2 Second parameter for command that requires 2 parameters
5673
*
5774
*/
5875

59-
void PelcoCam::send_command(uint8_t command, uint8_t params, uint8_t params2){
76+
void PelcoCam::send_command(uint8_t command, uint8_t params, uint8_t params2, bool request){
6077
messToCamera[0] = 0xFF;
6178
messToCamera[1] = Address_;
6279

@@ -91,13 +108,14 @@ void PelcoCam::send_command(uint8_t command, uint8_t params, uint8_t params2){
91108
}
92109

93110
SerialCam.write(messToCamera, sizeof(messToCamera));
111+
if(!request) delay(10); //delay because the camera respond back TODO: check reponse and delete the relay
94112
}
95113

96114
/*!
97115
* @brief Send a query to the camera and reads the response
98116
*
99117
* @param request the wanted reponse (see header)
100-
* @param timeout default 1000; timout for waiting a byte
118+
* @param timeout default 1000; timeout for waiting a byte
101119
* @param maxbuffer Maximum size of the buffer; defaut 20
102120
*
103121
* @return true if succeded, false if an error occured
@@ -115,24 +133,36 @@ int PelcoCam::send_request(uint8_t request, uint timeout, uint maxbuffer){
115133
return -1;
116134
}
117135

118-
send_command(request); //Send the query
136+
send_command(request, 0x00, 0x00, true); //Send the query
137+
138+
if(autoModule_) digitalWrite(rePin_, LOW); //Set the module at RX mode
139+
140+
byte buffer[maxbuffer]; // Buffer for the reception from the camera
141+
uint index = 0;
142+
119143

120-
while (!SerialCam.available()){//Wait for the first bit
144+
while (!SerialCam.available()){//Wait for the first bit
121145
if (timeout==0){ //If timeout is reached
122-
if(log_messages_) Serial.print("ERROR timout reached");
146+
if(log_messages_) Serial.println("ERROR timout reached");
147+
if(autoModule_) digitalWrite(rePin_, HIGH); //set back at TX mode
123148
return -1;
124149
}
125150
timeout--;
126151
delayMicroseconds(10);
127152
}
128153

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
154+
//////Old solution
155+
/* while(SerialCam.available()){//Do it until there is no more things to read
133156
buffer[index]=SerialCam.read();
157+
Serial.printf("%02X ",buffer[index]);
134158
index++;
135-
}
159+
} */
160+
161+
SerialCam.readBytes(buffer, 7);//Apparently this works
162+
163+
if(autoModule_) digitalWrite(rePin_, HIGH); //set back at TX mode
164+
165+
int command_index = searchIndex(buffer, 0x59);//Looks up where is the index of the response command
136166

137167
/* A theorical response:
138168
FF 00 59 4A 13 B7
@@ -142,8 +172,6 @@ int PelcoCam::send_request(uint8_t request, uint timeout, uint maxbuffer){
142172
FF 00 FF 00 59 4A 13 B7 FF 01 48
143173
*/
144174

145-
int command_index = searchIndex(buffer, 0x59);//Looks up where is the index of the response command
146-
147175
if(command_index == -1 //Checks if found
148176
|| command_index < 3 //Checks if the reponse byte is in the right place
149177
){
@@ -173,9 +201,14 @@ int PelcoCam::send_request(uint8_t request, uint timeout, uint maxbuffer){
173201
Serial.println();
174202
}
175203

176-
return messFromcamera[4]; //Returns MSB
204+
205+
206+
return messFromcamera[4]; //Return MSB data
177207
}
178208

209+
210+
211+
179212
void PelcoCam::send_raw(String hex_string){
180213
hex_string.replace(" ", ""); //Replace spaces
181214

src/Pelco_And_Arduino.h

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
* @mainpage The root of this libray!
55
*
66
* @section author Written by Boris Hilkens 15/02/2022.
7-
*
7+
*
88
* @section license License
99
*
1010
* MIT license, all text above must be included in any redistribution
1111
*/
1212

13-
1413
#ifndef Pelco_And_Arduino_H
1514
#define Pelco_And_Arduino_H
1615

@@ -30,12 +29,12 @@ const uint8_t PROGMEM PAN_R_TILT_D = 0x12;
3029

3130
const uint8_t PROGMEM ZOOM_T = 0x20;
3231
const uint8_t PROGMEM ZOOM_W = 0x40;
33-
const uint8_t PROGMEM SET_ZOOM_SPEED = 0x25; //Param from 00 to 03
32+
const uint8_t PROGMEM SET_ZOOM_SPEED = 0x25; // Param from 00 to 03
3433

3534
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 (auto/on/off)
35+
const uint8_t PROGMEM FOCUS_N = 0x01; //!!!!not byte 4 but 3
36+
const uint8_t PROGMEM SET_FOCUS_SPEED = 0x27; // Param from 00 to 03
37+
const uint8_t PROGMEM AUTO_FOCUS = 0x2B; // Param from 00 to 02 (auto/on/off)
3938

4039
const uint8_t PROGMEM STOP = 0x00;
4140

@@ -44,9 +43,9 @@ const uint8_t PROGMEM OFF = 0x08;
4443

4544
const uint8_t PROGMEM RESET = 0x29;
4645

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
46+
const uint8_t PROGMEM SET_PRESET = 0x03; // data preset id
47+
const uint8_t PROGMEM GOTO_PRESET = 0x05; // data preset id
48+
const uint8_t PROGMEM CLR_PRESET = 0x07; // data preset id
5049

5150
const uint8_t PROGMEM QUERY_PAN = 0x51;
5251
const uint8_t PROGMEM QUERY_TILT = 0x53;
@@ -65,46 +64,47 @@ A lot of commands is not implemented yet, I'll do it in the future!
6564

6665
/////////////////////////////////////////////////
6766

68-
class PelcoCam {
69-
private:
70-
SoftwareSerial SerialCam;
71-
72-
uint8_t Address_;
73-
uint32_t baud_;
74-
uint16_t txPin_;
75-
uint16_t rxPin_;
76-
bool log_messages_;
77-
78-
uint8_t messToCamera[7] ={
79-
0x00, //sync byte
80-
0x00, //Address
81-
0x00, //Always 0 (in most of the cases)
82-
0x00, //Command
83-
0x00, //Data1 (Used for pan speed and response from camera)
84-
0x00, //Data2 (Used everywhere)
85-
0x00 //Checksum: add all byte except sync and then modulo 0x100
86-
};
87-
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);
100-
101-
public:
102-
PelcoCam(uint8_t Address, int baud, int txPin, int rxPin, bool log_messages);
103-
void begin();
104-
void send_command(uint8_t command, uint8_t params=0x00, uint8_t params2=0x00);
105-
int send_request(uint8_t request, uint timeout = 1000, uint max_buffer=20);
106-
void send_raw(String hex_string);
107-
67+
class PelcoCam
68+
{
69+
private:
70+
SoftwareSerial SerialCam;
71+
72+
uint8_t Address_;
73+
uint32_t baud_;
74+
uint16_t txPin_;
75+
uint16_t rxPin_;
76+
uint16_t rePin_;
77+
bool log_messages_;
78+
bool autoModule_;
79+
80+
uint8_t messToCamera[7] = {
81+
0x00, // sync byte
82+
0x00, // Address
83+
0x00, // Always 0 (in most of the cases)
84+
0x00, // Command
85+
0x00, // Data1 (Used for pan speed and response from camera)
86+
0x00, // Data2 (Used everywhere)
87+
0x00 // Checksum: add all byte except sync and then modulo 0x100
88+
};
89+
90+
uint8_t messFromcamera[7] = {
91+
0x00, // sync byte
92+
0x00, // Address
93+
0x00, // Always 0 (in most of the cases)
94+
0x00, // Command
95+
0x00, // Data1 (Used for pan speed and response from camera)
96+
0x00, // Data2 (Used everywhere)
97+
0x00 // Checksum: add all byte except sync and then modulo 0x100
98+
};
99+
100+
int searchIndex(byte look_array[], byte value);
101+
102+
public:
103+
PelcoCam(uint8_t Address, int baud, int txPin, int rxPin, int readEnPin, bool log_messages);
104+
void begin();
105+
void send_command(uint8_t command, uint8_t params = 0x00, uint8_t params2 = 0x00, bool request=false);
106+
int send_request(uint8_t request, uint timeout=1000, uint max_buffer = 20);
107+
void send_raw(String hex_string);
108108
};
109109

110110
#endif

0 commit comments

Comments
 (0)