Skip to content

Commit 6ac8d52

Browse files
committed
changes: constantst , handling special commands
1 parent 43d7986 commit 6ac8d52

4 files changed

Lines changed: 152 additions & 100 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
Hello, this is an Arduino library designed to control Pelco Cameras!
1010

11-
For now it can support PELCO-D protocol but I havn't got tested everything yet.
11+
For now it can support **PELCO-D** protocol but I havn't got tested everything yet.
1212

1313
This library was made for the project: [Controlling a cctv camera on Arduino](https://hackaday.io/project/183986-controlling-a-cctv-camera-with-arduino) on hacakday.io
1414

15-
Before you go on, this is a school, project, I'm still in school and learnings things so be kind if the libray is ugly. I would like to thanks my school and teachers for allowing me to work on this project.
15+
*Before you go on, this is a school, project, I'm still in school and learnings things so be kind if the libray is ugly. I would like to thanks my school and teachers for allowing me to work on this project.*
1616

1717
The main goal of the project is to do a control table (It'll be on arduino.)
1818

src/Pelco_And_Arduino.cpp

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,6 @@ void PelcoCam::begin() {
6666
protocol = D;
6767
baud = 9600;
6868
break;
69-
70-
case PELCO_P2400:
71-
protocol = P;
72-
baud = 2400;
73-
break;
74-
case PELCO_P4800:
75-
protocol = P;
76-
baud = 4800;
77-
break;
78-
case PELCO_P9600:
79-
protocol = P;
80-
baud = 9600;
81-
break;
8269
}
8370

8471
if (log_messages_)
@@ -105,42 +92,47 @@ void PelcoCam::begin() {
10592
* @brief Send message to the camera
10693
*
10794
* @param command the wanted command (see header)
108-
* @param params Main parameter
109-
* @param params2 Second parameter for command that requires 2 parameters
110-
* @param params2 Second parameter for command that requires 2 parameters
111-
*
95+
* @param data1 Main parameter
96+
* @param data2 Second parameter for command that requires 2 parameters
11297
* @return true if succeed, false if not succeed
11398
*
11499
*/
115100

116-
bool PelcoCam::send_command(uint8_t command, uint8_t params, uint8_t params2, bool request) {
101+
bool PelcoCam::send_command(uint8_t command, uint8_t data1, uint8_t data2, bool request) {
117102
messToCamera[0] = 0xFF; //The first byte is always FF (sync)
118103
messToCamera[1] = address_; // the second is the adress
119104

105+
//Special commands handling:
120106

121-
//the thrid byte is determined by the command itself (see the command list)
122-
123-
if (command == ON || command == OFF || command == FOCUS_N) { // If the command is on or off, set the command to the third byte
124-
messToCamera[2] = command;
107+
if(searchIndex(CMND1, command)!=-1){
108+
messToCamera[2] = command - 0b01100000; //Minus the thing that differentiate from others
125109
messToCamera[3] = 0x00;
126-
} else {
110+
messToCamera[4] = 0x00;
111+
messToCamera[5] = 0x00;
112+
}else{//that means command is the byte 4
127113
messToCamera[2] = 0x00;
128114
messToCamera[3] = command;
129-
}
130-
131115

132-
if (command == PAN_L || command == PAN_R) {////Only for PAN Left and right you doing this
133-
messToCamera[4] = params;
134-
messToCamera[5] = 0x00;
135-
} else if (command == PAN_L_TILT_U || command == PAN_R_TILT_U || command == PAN_L_TILT_D || //Commands that takes two params
136-
command == PAN_R_TILT_D) {
137-
messToCamera[4] = params2;
138-
messToCamera[5] = params;
139-
} else { //"Normal" condition
140-
messToCamera[4] = 0x00;
141-
messToCamera[5] = params;
116+
if(searchIndex(DATA1, command)!=-1){
117+
messToCamera[4] = data1;
118+
messToCamera[5] = 0x00;
119+
}else if(searchIndex(DATA_BOTH, command)!=-1){
120+
messToCamera[4] = data1;
121+
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_);
130+
return false;
131+
}
142132
}
143133

134+
//////
135+
144136
messToCamera[6] = (messToCamera[1]
145137
+ messToCamera[2]
146138
+ messToCamera[3]
@@ -184,29 +176,37 @@ bool PelcoCam::send_command(uint8_t command, uint8_t params, uint8_t params2, bo
184176
if (!autoModule_)
185177
digitalWrite(rePin_, HIGH); // set back at TX mode
186178

187-
188-
/// TODO better
189-
int command_index = searchIndex(
190-
ACKmessFromCamera, messToCamera[6]); // Looks up where is the index of the response TODO: verify every command
191-
192-
if (command_index != 3) { // Checks if found
193-
if (messToCamera[6] != 0x01) { // Filter stop command cause checksum==1
179+
180+
if (ACKmessFromCamera[0] != 0xFF) { // Check sync byte and checksum of the previous comand
194181
if (log_messages_)
195182
Serial.printf(
196-
"Cam %i: ERROR Could not verify camera reponse: bad index (is camera well plugged in?)\n",
183+
"Cam %i: ERROR Could not verify camera reponse: bad sync byte (is camera well plugged in?)\n",
197184
address_);
198185
return false;
199-
}
200186
}
201187

202-
if (ACKmessFromCamera[command_index - 3] != 0xFF) { // Check sync byte and checksum of the previous comand
203-
if (messToCamera[6] != 0x01) { // Filter stop command cause checksum==1
204-
if (log_messages_)
205-
Serial.printf(
206-
"Cam %i: ERROR Could not verify camera reponse: bad sync byte (is camera well plugged in?)\n",
207-
address_);
208-
return false;
209-
}
188+
if (ACKmessFromCamera[1] != address_) {//Check adress byte
189+
if (log_messages_)
190+
Serial.printf(
191+
"Cam %i: ERROR Could not verify camera reponse: bad address (is camera well plugged in?)\n",
192+
address_);
193+
return false;
194+
}
195+
196+
if (ACKmessFromCamera[2] != 0x00) {//check the always 0 byte
197+
if (log_messages_)
198+
Serial.printf(
199+
"Cam %i: ERROR Could not verify camera reponse: bad null ???? (is camera well plugged in?)\n",
200+
address_);
201+
return false;
202+
}
203+
204+
if (ACKmessFromCamera[3] != messToCamera[6]) {//Check the checksum
205+
if (log_messages_)
206+
Serial.printf(
207+
"Cam %i: ERROR Could not verify camera reponse: bad checksum (is camera well plugged in?)\n",
208+
address_);
209+
return false;
210210
}
211211

212212
if (log_messages_) //log
@@ -228,14 +228,8 @@ bool PelcoCam::send_command(uint8_t command, uint8_t params, uint8_t params2, bo
228228
int PelcoCam::send_request(uint8_t request, uint timeout, uint maxbuffer) {
229229
byte response_command;
230230

231-
if (request == QUERY_PAN) {
232-
response_command == RESP_PAN;
233-
} else if (request == QUERY_TILT) {
234-
response_command = RESP_TILT;
235-
} else if (request == QUERY_ZOOM) {
236-
response_command = RESP_ZOOM;
237-
} else if (request == QUERY_FOCUS) {
238-
response_command = RESP_FOCUS;
231+
if (searchIndex(QUERY_CMND, request)!=-1) {
232+
response_command == RESP_CMND[searchIndex(QUERY_CMND, request)]; //Magic!
239233
} else {
240234
if (log_messages_)
241235
Serial.printf("Cam %i: No valid request provided\n", address_);
@@ -360,7 +354,7 @@ void PelcoCam::send_raw(String hex_string) {
360354
* @return the index of the element found
361355
*/
362356

363-
int PelcoCam::searchIndex(byte look_array[], byte value) {
357+
int PelcoCam::searchIndex(const byte look_array[], byte value) {
364358
for (int i = 0; i < sizeof(look_array) / sizeof(look_array[0]); i++) {
365359
if (look_array[i] == value) {
366360
return i;

src/Pelco_And_Arduino.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ class PelcoCam
6363
0x00 //Checksum taken for the previous message
6464
};
6565

66-
int searchIndex(byte look_array[], byte value);
66+
int searchIndex(const byte look_array[], byte value);
6767

6868
public:
6969
PelcoCam(uint8_t address, uint32_t config, uint8_t txPin, uint8_t rxPin, bool log_messages = false, uint8_t readEnPin = NOT_A_PIN);
7070
void begin();
7171

72-
bool send_command(uint8_t command, uint8_t params = 0x00, uint8_t params2 = 0x00, bool request = false);
72+
bool send_command(uint8_t command, uint8_t data1 = 0x00, uint8_t data2 = 0x00, bool request = false);
7373
int send_request(uint8_t request, uint timeout = 1000, uint max_buffer = 20);
7474
void send_raw(String hex_string); // TODO: get ACK
7575
};

src/constants.h

Lines changed: 93 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,111 @@
22
#define constants_H
33

44
//////////////////////////////////////////Some constants stored in Progmem
5-
const uint8_t PROGMEM PAN_L = 0x04;
6-
const uint8_t PROGMEM PAN_R = 0x02;
7-
const uint8_t PROGMEM TILT_U = 0x08;
8-
const uint8_t PROGMEM TILT_D = 0x10;
95

10-
const uint8_t PROGMEM PAN_L_TILT_U = 0x0C;
11-
const uint8_t PROGMEM PAN_R_TILT_U = 0x0A;
12-
const uint8_t PROGMEM PAN_L_TILT_D = 0x14;
13-
const uint8_t PROGMEM PAN_R_TILT_D = 0x12;
6+
//////////////////////////////////////CMND1
7+
/////Basic commands bit 5 &6 are always 0
8+
#define FOCUS_N 0x61 //0x01 + 0b0110 0000
149

15-
const uint8_t PROGMEM ZOOM_T = 0x20;
16-
const uint8_t PROGMEM ZOOM_W = 0x40;
17-
const uint8_t PROGMEM SET_ZOOM_SPEED = 0x25; // Param from 00 to 03
10+
#define IRIS_O 0x62 //0x02 + 0b0110 0000
11+
#define IRIS_C 0x64 //0x04 + 0b0110 0000
1812

19-
const uint8_t PROGMEM FOCUS_F = 0x80;
20-
const uint8_t PROGMEM FOCUS_N = 0x01; //!!!!not byte 4 but 3
21-
const uint8_t PROGMEM SET_FOCUS_SPEED = 0x27; // Param from 00 to 03
22-
const uint8_t PROGMEM AUTO_FOCUS = 0x2B; // Param from 00 to 02 (auto/on/off)
13+
#define OFF 0x68 //0x08 + 0b0110 0000
14+
#define ON 0xE8 //0x88 + 0b0110 0000
2315

24-
const uint8_t PROGMEM STOP = 0x00;
16+
//////////////////////////////////////CMND2
17+
/////Basic commands
18+
#define STOP 0x00
2519

26-
const uint8_t PROGMEM ON = 0x88; //!!!!not byte 4 but 3
27-
const uint8_t PROGMEM OFF = 0x08;
20+
#define PAN_R 0x02
21+
#define PAN_L 0x04
2822

29-
const uint8_t PROGMEM RESET = 0x29;
23+
#define TILT_U 0x08
24+
#define TILT_D 0x10
3025

31-
const uint8_t PROGMEM SET_PRESET = 0x03; // data preset id
32-
const uint8_t PROGMEM GOTO_PRESET = 0x05; // data preset id
33-
const uint8_t PROGMEM CLR_PRESET = 0x07; // data preset id
26+
#define ZOOM_T 0x20
27+
#define ZOOM_W 0x40
3428

35-
const uint8_t PROGMEM QUERY_PAN = 0x51;
36-
const uint8_t PROGMEM QUERY_TILT = 0x53;
37-
const uint8_t PROGMEM QUERY_ZOOM = 0x55;
38-
const uint8_t PROGMEM QUERY_FOCUS = 0x61;
29+
#define FOCUS_F 0x80
3930

40-
const uint8_t PROGMEM RESP_PAN = 0x59;
41-
const uint8_t PROGMEM RESP_TILT = 0x5B;
42-
const uint8_t PROGMEM RESP_ZOOM = 0x5D;
43-
const uint8_t PROGMEM RESP_FOCUS = 0x63;
31+
#define PAN_L_TILT_U 0x0C
32+
#define PAN_R_TILT_U 0x0A
33+
#define PAN_L_TILT_D 0x14
34+
#define PAN_R_TILT_D 0x12
35+
36+
/////Complex commands
37+
#define SET_PRESET 0x03 // byte6: preset id
38+
#define CLR_PRESET 0x05 // byte6: preset id
39+
#define GOTO_PRESET 0x07 // byte6: preset id
40+
41+
// Predifined preset (everything is call (goto) unless mentioned otherwise):
42+
#define PRESET_FLIP 0x21
43+
#define PRESET_ZERO_PAN 0x22
44+
#define PRESET_AUX_1 0x54 // set: on ; call:off
45+
#define PRESET_AUX_2 0x55 // set: on ; call:off
46+
#define PRESET_WIPER 0x56
47+
#define PRESET_WASHER 0x57
48+
#define PRESET_IR_IN 0x58
49+
#define PRESET_IR_OUT 0x59
50+
//#define PRESET_CLEAR 0x5E NOT CLEAR????
51+
#define PRESET_MAIN_MENU 0x5F // Brings up the main menu (set)
52+
//
53+
54+
#define SET_AUX 0x09 // byte6: aux id
55+
#define CLEAR_AUX 0x0B // byte6: aux id
56+
57+
#define DUMMY 0x0D //??
58+
59+
#define REMOTE_RESET 0x0F
60+
61+
#define WRITE_CHAR 0x15 // Byte5: column Byte6: ASCII code
62+
#define CLEAR_SCR 0x17
63+
64+
#define ALARM_ACK 0x19 // byte6: ALARM id; ????
65+
66+
#define SET_ZOOM_SPEED 0x25 // Param from 00 to 03
67+
#define SET_FOCUS_SPEED 0x27 // Param from 00 to 03
68+
69+
#define RESET 0x29
70+
71+
#define AUTO_FOCUS 0x2B // Param from 00 to 02 (auto/on/off)
72+
#define AUTO_IRIS 0x2D // Param from 00 to 02 (auto/on/off)
73+
#define AUTO_AGC 0x2F // Param from 00 to 02 (auto/on/off)
74+
#define BACKLIGHT_COMPENSATION 0x31 // Param from 00 to 01 (on/off)
75+
#define AUTO_WHITE_BALANCE 0x33 // Param from 00 to 01 (on/off)
76+
77+
#define QUERY 0x45 //???????????
78+
79+
#define ZERO_POS 0x49
80+
#define SET_PAN 0x4B // b5: MSB and b6: LSB
81+
#define SET_TILT 0x4D // b5: MSB and b6: LSB
82+
#define SET_ZOOM 0x4F // b5: MSB and b6: LSB
83+
84+
#define QUERY_PAN 0x51
85+
#define QUERY_TILT 0x53
86+
#define QUERY_ZOOM 0x55
87+
88+
#define RESP_PAN 0x59 // b5: MSB and b6: LSB
89+
#define RESP_TILT 0x5B // b5: MSB and b6: LSB
90+
#define RESP_ZOOM 0x5D // b5: MSB and b6: LSB
4491

4592
/*
4693
47-
A lot of commands is not implemented yet, I'll do it in the future!
94+
A lot of commands is not implemented yet, it is not "important" commands for now message me via github if you wanna get
95+
a command
4896
*/
97+
//Special commands:
98+
99+
const PROGMEM byte CMND1[5] = {FOCUS_N, IRIS_O, IRIS_C, OFF, ON}; //List of all commands that go on the byte3; a list with command 2 is not required, a bit of logic please
100+
101+
const PROGMEM byte SETPOS[3] = {SET_PAN, SET_TILT, SET_ZOOM}; //List of all command that require an MSB and LSB
102+
103+
const PROGMEM byte DATA1[2] = {PAN_L, PAN_R}; //List of all commands that use ONLY the DATA1 for data pass
104+
const PROGMEM byte DATA_BOTH[5]= {PAN_L_TILT_D, PAN_L_TILT_U, PAN_R_TILT_D, PAN_R_TILT_U, WRITE_CHAR};//List of all commands that use the DATA1 and DATA2 for data pass (excluding the SETPOS' commands)
105+
106+
const PROGMEM byte PRESET_CMND[3] = {SET_PRESET, CLR_PRESET, GOTO_PRESET}; //will I use it?
107+
108+
const PROGMEM byte QUERY_CMND[3] = {QUERY_PAN, QUERY_TILT, QUERY_ZOOM};
109+
const PROGMEM byte RESP_CMND[3] = {RESP_PAN, RESP_TILT, RESP_ZOOM};
49110

50111
///////////////////////////////////////////////// Constants for the config parameter
51112

@@ -56,10 +117,7 @@ A lot of commands is not implemented yet, I'll do it in the future!
56117
#define PELCO_D2400 0xD24
57118
#define PELCO_D4800 0xD48
58119
#define PELCO_D9600 0xD96
59-
// P-protocol baud self-explaintory
60-
#define PELCO_P2400 0xE24
61-
#define PELCO_P4800 0xE48
62-
#define PELCO_P9600 0xE96
120+
63121

64122
//////////////////////////////////////////::
65123

0 commit comments

Comments
 (0)