Skip to content

Commit 061555d

Browse files
committed
A lot of changes
1 parent 09be484 commit 061555d

8 files changed

Lines changed: 161 additions & 97 deletions

File tree

README.md

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,4 @@ About the modules for RS485, I'm working with a standart MAX485 chip but I'll te
3636

3737
## Exemple
3838

39-
```arduino
40-
#include "Arduino.h"
41-
#include <Pelco_And_Arduino.h>
42-
43-
int Pan_position = 0;
44-
45-
PelcoCam PelcoCam(0x01, //Address of the camera
46-
PELCO_D9600,//Config
47-
32, //TX pin
48-
33, //RX pin
49-
true, //Enable logging?
50-
25); //RE pin for manual switching modules
51-
52-
void setup() {
53-
54-
//Begin the serial communication If you enabled logging, no need to begin the normal serial
55-
PelcoCam.begin();
56-
57-
//Send stop, send_command will return a bool that indicates if the cameras sended its ACK
58-
while (!PelcoCam.send_command(STOP)) {
59-
Serial.println("Camera not plugged?");
60-
delay(1000);
61-
}
62-
Serial.println("Init Finished!");
63-
}
64-
void loop() {
65-
66-
//Pan to the left with max speed before turbo
67-
PelcoCam.send_command(PAN_L, 0x3F);
68-
69-
//send a request about the pan position
70-
Pan_position = PelcoCam.send_request(QUERY_PAN);
71-
Serial.println(Pan_position);
72-
73-
delay(1000);
74-
75-
PelcoCam.send_command(STOP); //Stop the camera
76-
delay(1000);
77-
}
78-
```
39+
go to the examples folder

examples/send_raw/send_raw.ino

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "Arduino.h"
2+
#include <Pelco_And_Arduino.h>
3+
4+
PelcoCam PelcoCam(0x01, //Address of the camera
5+
PELCO_D9600, //Config
6+
32, //TX pin
7+
33, //RX pin
8+
true, //Enable logging?
9+
25); //RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
10+
11+
void setup() {
12+
//Begin the serial communication If you enabled logging, no need to begin the normal serial
13+
PelcoCam.begin();
14+
15+
//Send stop, send_command will return a bool that indicates if the cameras sended its ACK
16+
//It's a good way to see if the camera is plugged in
17+
while (!PelcoCam.send_command(STOP)) {
18+
Serial.println("Camera not plugged?");
19+
delay(1000);
20+
}
21+
Serial.println("Init Finished!");
22+
}
23+
24+
void loop() {
25+
while(Serial.available()) {
26+
String in = Serial.readString(); //Read from the serial
27+
PelcoCam.send_raw(in); //Send the raw command to the cameras (adress can be another than the one declared in the object because it's raw transmission)
28+
delay(500);
29+
PelcoCam.send_command(STOP); //Stop it after a while
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "Arduino.h"
2+
#include <Pelco_And_Arduino.h>
3+
4+
PelcoCam PelcoCam(0x01, //Address of the camera
5+
PELCO_D9600, //Config
6+
32, //TX pin
7+
33, //RX pin
8+
true, //Enable logging?
9+
25); //RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
10+
11+
void setup() {
12+
//Begin the serial communication If you enabled logging, no need to begin the normal serial
13+
PelcoCam.begin();
14+
15+
//Send stop, send_command will return a bool that indicates if the cameras sended its ACK
16+
//It's a good way to see if the camera is plugged in
17+
while (!PelcoCam.send_command(STOP)) {
18+
Serial.println("Camera not plugged?");
19+
delay(1000);
20+
}
21+
Serial.println("Init Finished!");
22+
}
23+
24+
void loop() {
25+
//loops the camera increment angle of 1°; very fun because it shakes like hell
26+
int i=0;
27+
for(i=0; i<35999; i=i+100){
28+
PelcoCam.send_command(SET_PAN, i); //Hundered of degrees, max is 35999 == 359,99°
29+
delay(500);
30+
}
31+
32+
}

examples/simple/simple.ino

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "Arduino.h"
2+
#include <Pelco_And_Arduino.h>
3+
4+
PelcoCam PelcoCam(0x01, //Address of the camera
5+
PELCO_D9600, //Config
6+
32, //TX pin
7+
33, //RX pin
8+
true, //Enable logging?
9+
25); //RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
10+
11+
void setup() {
12+
//Begin the serial communication If you enabled logging, no need to begin the normal serial
13+
PelcoCam.begin();
14+
15+
//Send stop, send_command will return a bool that indicates if the cameras sended its ACK
16+
//It's a good way to see if the camera is plugged in
17+
while (!PelcoCam.send_command(STOP)) {
18+
Serial.println("Camera not plugged?");
19+
delay(1000);
20+
}
21+
Serial.println("Init Finished!");
22+
}
23+
24+
void loop() {
25+
26+
//Pan to the left with max speed before turbo
27+
PelcoCam.send_command(PAN_L, 0x3F);
28+
delay(1000);
29+
PelcoCam.send_command(STOP); //Stop the camera
30+
delay(1000);
31+
//Pan to the left with less speed than the tilt speed that goes up
32+
PelcoCam.send_comand(PAN_L_TILT_U, 0x10, 0xFF);)
33+
delay(1000);
34+
PelcoCam.send_command(STOP); //Stop the camera
35+
delay(1000);
36+
}

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=Pelco_And_Arduino
2-
version=1.0.0
2+
version=2.0.0
33
author=Boris Hilkens <pixelbo21@gmail.com>
44
maintainer=Boris Hilkens <pixelbo21@gmail.com>
55
sentence=This library makes Pelco cameras moves!
6-
paragraph=WARN: not entirely tested: will have to wait a couple of weeks
6+
paragraph=WARN: not entirely tested
77
category=Communication
88
url=https://github.com/Pixelbo/Pelco_And_Arduino
99
architectures=avr, esp32

src/Pelco_And_Arduino.cpp

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ PelcoCam::PelcoCam(uint8_t address, uint32_t config, uint8_t txPin, uint8_t rxPi
4747

4848
void PelcoCam::begin() {
4949
if (log_messages_) {
50-
Serial.begin(115200);
50+
if(Serial) Serial.begin(115200);
5151
Serial.printf("Message log has been activated for the camera %02X !\n", address_);
5252
}
5353

@@ -98,49 +98,50 @@ void PelcoCam::begin() {
9898
*
9999
*/
100100

101-
bool PelcoCam::send_command(uint8_t command, uint8_t data1, uint8_t data2, bool request) {
102-
messToCamera[0] = 0xFF; //The first byte is always FF (sync)
103-
messToCamera[1] = address_; // the second is the adress
101+
bool PelcoCam::send_command(uint8_t command, uint16_t data1, uint8_t data2, bool request) {
102+
messToCamera[0] = 0xFF; // The first byte is always FF (sync)
103+
messToCamera[1] = address_; // the second is the adress
104104

105-
//Special commands handling:
106-
107-
if(searchIndex(CMND1, command)!=-1){
108-
messToCamera[2] = command - 0b01100000; //Minus the thing that differentiate from others
105+
// Special commands handling:
106+
if (searchIndex(CMND1, command) != -1) {
107+
messToCamera[2] = command - 0b01100000; // Minus the thing that differentiate from others
109108
messToCamera[3] = 0x00;
110109
messToCamera[4] = 0x00;
111110
messToCamera[5] = 0x00;
112-
}else{//that means command is the byte 4
111+
} else { // that means command is the byte 4
112+
113113
messToCamera[2] = 0x00;
114114
messToCamera[3] = command;
115-
116-
if(searchIndex(DATA1, command)!=-1){
115+
if (searchIndex(DATA1, command) != -1) {
117116
messToCamera[4] = data1;
118117
messToCamera[5] = 0x00;
119-
}else if(searchIndex(DATA_BOTH, command)!=-1){
118+
} else if (searchIndex(DATA_BOTH, command) != -1) {
120119
messToCamera[4] = data1;
121120
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_);
121+
} else if (searchIndex(SETPOS, command) != -1) {
122+
uint angle = (uint16_t)(data1 % 35999); // Centième de degrès! avec max = 35999 (359.99 deg)
123+
124+
messToCamera[4] = (uint8_t)((angle >> 0x08) & 0x00FF); // Get MSB
125+
messToCamera[5] = (uint8_t)(angle & 0x00FF); // Get LSB
126+
Serial.printf(" %X", angle);
127+
Serial.printf(" %X", messToCamera[4]);
128+
Serial.printf(" %X\n", messToCamera[5]);
129+
} else if (searchIndex(QUERY_CMND, command) != -1) {
130+
if (log_messages_)
131+
Serial.printf("Cam %i: You are doing an query into send command ??????????", address_);
130132
return false;
133+
} else {
134+
messToCamera[4] = 0x00;
135+
messToCamera[5] = data1;
131136
}
132137
}
133138

134139
//////
135140

136-
messToCamera[6] = (messToCamera[1]
137-
+ messToCamera[2]
138-
+ messToCamera[3]
139-
+ messToCamera[4]
140-
+ messToCamera[5]) % 0x100; // Checksum modulo 0x100
141+
messToCamera[6] = (messToCamera[1] + messToCamera[2] + messToCamera[3] + messToCamera[4] + messToCamera[5]) %
142+
0x100; // Checksum modulo 0x100
141143

142-
143-
if (log_messages_) { //log the message
144+
if (log_messages_) { // log the message
144145
Serial.printf("Cam %i: Sending message: ", address_);
145146
for (int i = 0; i < 7; i++) {
146147
Serial.printf("%02X", messToCamera[i]);
@@ -149,9 +150,9 @@ bool PelcoCam::send_command(uint8_t command, uint8_t data1, uint8_t data2, bool
149150
Serial.println();
150151
}
151152

152-
SerialCam.write(messToCamera, sizeof(messToCamera)); //Write to the camera
153+
SerialCam.write(messToCamera, sizeof(messToCamera)); // Write to the camera
153154

154-
if (!request) {// Check the response of the camera only if it isn't a request
155+
if (!request) { // Check the response of the camera only if it isn't a request
155156
int timeout = 10000; // 10 millissecond wait
156157

157158
if (!autoModule_)
@@ -171,45 +172,44 @@ bool PelcoCam::send_command(uint8_t command, uint8_t data1, uint8_t data2, bool
171172
delayMicroseconds(10);
172173
}
173174

174-
SerialCam.readBytes(ACKmessFromCamera, 4); //General response is 4 byte
175+
SerialCam.readBytes(ACKmessFromCamera, 4); // General response is 4 byte
175176

176177
if (!autoModule_)
177178
digitalWrite(rePin_, HIGH); // set back at TX mode
178179

179-
180180
if (ACKmessFromCamera[0] != 0xFF) { // Check sync byte and checksum of the previous comand
181-
if (log_messages_)
182-
Serial.printf(
183-
"Cam %i: ERROR Could not verify camera reponse: bad sync byte (is camera well plugged in?)\n",
184-
address_);
185-
return false;
181+
if (log_messages_)
182+
Serial.printf(
183+
"Cam %i: ERROR Could not verify camera reponse: bad sync byte (is camera well plugged in?)\n",
184+
address_);
185+
return false;
186186
}
187187

188-
if (ACKmessFromCamera[1] != address_) {//Check adress byte
188+
if (ACKmessFromCamera[1] != address_) { // Check adress byte
189189
if (log_messages_)
190190
Serial.printf(
191191
"Cam %i: ERROR Could not verify camera reponse: bad address (is camera well plugged in?)\n",
192192
address_);
193193
return false;
194194
}
195195

196-
if (ACKmessFromCamera[2] != 0x00) {//check the always 0 byte (alarm byte)
196+
if (ACKmessFromCamera[2] != 0x00) { // check the always 0 byte (alarm byte)
197197
if (log_messages_)
198198
Serial.printf(
199199
"Cam %i: ERROR Could not verify camera reponse: bad null ???? (is camera well plugged in?)\n",
200200
address_);
201201
return false;
202202
}
203203

204-
if (ACKmessFromCamera[3] != messToCamera[6]) {//Check the checksum
204+
if (ACKmessFromCamera[3] != messToCamera[6]) { // Check the checksum
205205
if (log_messages_)
206206
Serial.printf(
207207
"Cam %i: ERROR Could not verify camera reponse: bad checksum (is camera well plugged in?)\n",
208208
address_);
209209
return false;
210210
}
211211

212-
if (log_messages_) //log
212+
if (log_messages_) // log
213213
Serial.printf("Cam %i: Message sent and ACK received!\n", address_);
214214
return true;
215215
}
@@ -228,8 +228,8 @@ bool PelcoCam::send_command(uint8_t command, uint8_t data1, uint8_t data2, bool
228228
uint16_t PelcoCam::send_request(uint8_t request, uint timeout, uint maxbuffer) {
229229
byte response_command;
230230

231-
if (searchIndex(QUERY_CMND, request)!=-1) {
232-
response_command == RESP_CMND[searchIndex(QUERY_CMND, request)]; //Magic!
231+
if (searchIndex(QUERY_CMND, request) != -1) {
232+
response_command == RESP_CMND[searchIndex(QUERY_CMND, request)]; // Magic!
233233
} else {
234234
if (log_messages_)
235235
Serial.printf("Cam %i: No valid request provided\n", address_);
@@ -296,10 +296,9 @@ uint16_t PelcoCam::send_request(uint8_t request, uint timeout, uint maxbuffer) {
296296
Serial.println();
297297
}
298298

299-
return (uint16_t) (messFromcamera[4] << 8 | messFromcamera[5]); // Return LSB data
299+
return (uint16_t)((((uint16_t)messFromcamera[4]) << 8) | ((uint16_t)messFromcamera[5])); // Return LSB data
300300
}
301301

302-
303302
////////todo get the response if it is a query or extended one!!
304303
bool PelcoCam::send_raw(String hex_string) {
305304
hex_string.replace(" ", ""); // Replace spaces
@@ -315,14 +314,17 @@ bool PelcoCam::send_raw(String hex_string) {
315314
raw_command[i] = (byte)strtol(buffer, NULL, 16); // conversion into byte
316315
}
317316

318-
if (raw_command[0] != 0xFF)
319-
return; // Check sync byte
317+
if (raw_command[0] != 0xFF) {
318+
if (log_messages_)
319+
Serial.printf("Cam %i: Wrong sync byte, updating to right sync byte\n", address_);
320+
raw_command[0] = 0xFF;
321+
}
320322

321323
/////Check checksum
322324
uint8_t checksum = (raw_command[1] + raw_command[2] + raw_command[3] + raw_command[4] + raw_command[5]) % 0x100;
323325
if (checksum != raw_command[6]) {
324326
if (log_messages_)
325-
Serial.printf("Cam %i: Wrong chacksum, updating to right checksum\n", address_);
327+
Serial.printf("Cam %i: Wrong checksum, updating to right checksum\n", address_);
326328

327329
raw_command[6] = checksum;
328330
}
@@ -351,7 +353,8 @@ bool PelcoCam::send_raw(String hex_string) {
351353
*/
352354

353355
int PelcoCam::searchIndex(const byte look_array[], byte value) {
354-
for (int i = 0; i < sizeof(look_array) / sizeof(look_array[0]); i++) {
356+
int i = 0;
357+
for (i = 0; i <= (sizeof(look_array) / sizeof(look_array[0])); i++) {
355358
if (look_array[i] == value) {
356359
return i;
357360
}

src/Pelco_And_Arduino.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class PelcoCam
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 data1 = 0x00, uint8_t data2 = 0x00, bool request = false);
72+
bool send_command(uint8_t command, uint16_t data1 = 0x00, uint8_t data2 = 0x00, bool request = false);
7373
uint16_t send_request(uint8_t request, uint timeout = 1000, uint max_buffer = 20);
7474
bool send_raw(String hex_string); // TODO: get ACK
7575
};

0 commit comments

Comments
 (0)