Skip to content

Commit 0bfa197

Browse files
committed
added camera struct + better lisibillyty
1 parent d4165b7 commit 0bfa197

9 files changed

Lines changed: 89 additions & 72 deletions

File tree

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
#include "Arduino.h"
22
#include <Pelco_And_Arduino.h>
33

4-
PelcoCamBus MyPelcoBus(6, // TX pin (Arduino to Cam)
5-
7, // RX pin (Cam to Arduino)
6-
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
4+
PelcoBus MyPelcoBus(6, // TX pin (Arduino to Cam)
5+
7, // RX pin (Cam to Arduino)
6+
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
77

8-
void setup() {
8+
PelcoBus::PelcoCam Camera1; //Declare a camera
99

10+
void setup() {
1011
Serial.begin(9600);
1112

1213
// Begin the serial communication
1314
MyPelcoBus.begin(PELCO_D9600, true);
1415

16+
// Set camera settings
17+
Camera1.address = 1; //Address of the camera
18+
Camera1.disable_ack = false; //For this exemple, this must set to false!
19+
20+
1521
// Send stop, send_command will return a bool that indicates if the cameras sended its ACK
1622
// It's a good way to see if the camera is plugged in
17-
while (!MyPelcoBus.send_command(1, STOP)) {
23+
while (!MyPelcoBus.send_command(Camera1, STOP)) {
1824
Serial.println("Camera not plugged?");
1925
delay(1000);
2026
}
2127
Serial.println("Init Finished!");
2228
}
2329

2430
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-
MyPelcoBus.send_command(1, SET_PAN, i); // Hundered of degrees, max is 35999 == 359,99°
29-
delay(1000);
31+
MyPelcoBus.send_command(Camera1, PAN_L, 0x3F); // Spin for a random time
32+
delay(random(1000, 2000));
3033

31-
int angle100 = MyPelcoBus.send_request(1, QUERY_PAN); // Get the current angle of the camera
34+
int angle100 = MyPelcoBus.send_request(Camera1, QUERY_PAN); // Get the current angle of the camera
3235

3336
float angle = angle100 / 100.0; // Convert to float
3437

3538
Serial.println("Angle detected: " + String(angle));
36-
}
37-
}
39+
}

examples/old_model/old_model.ino

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
#include "Arduino.h"
22
#include <Pelco_And_Arduino.h>
33

4-
/*
5-
This exemple is based on the simple example from examples\simple\simple.ino
6-
However, it is designed for the old model of the Pelco cameras.
7-
The old models works in the protocol RS422 so it need 2 pair of cable to communicate.
8-
If you have only one pair (preferably for sending commands), you can disable acknoledge when sending commands
4+
PelcoBus MyPelcoBus(6, // TX pin (Arduino to Cam)
5+
7, // RX pin (Cam to Arduino)
6+
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
97

10-
*/
11-
12-
PelcoCamBus MyPelcoBus(6, // TX pin (Arduino to Cam)
13-
7, // RX pin (Cam to Arduino)
14-
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
8+
PelcoBus::PelcoCam Camera1; //Declare a camera
159

1610
void setup() {
17-
1811
Serial.begin(9600);
1912

2013
// Begin the serial communication
2114
MyPelcoBus.begin(PELCO_D9600, true);
2215

16+
// Set camera settings
17+
Camera1.address = 1; //Address of the camera
18+
Camera1.disable_ack = true; //Old models function one 2 pair of wires and only one is usable, the TX one, so it's impossible that it can send back a reqponse
19+
20+
2321
// Send stop, send_command will return a bool that indicates if the cameras sended its ACK
2422
// It's a good way to see if the camera is plugged in
25-
while (!MyPelcoBus.send_command(1, STOP)) {
23+
while (!MyPelcoBus.send_command(Camera1, STOP)) {
2624
Serial.println("Camera not plugged?");
2725
delay(1000);
2826
}
@@ -32,13 +30,13 @@ void setup() {
3230
void loop() {
3331

3432
//Pan to the left with max speed before turbo
35-
MyPelcoBus.send_command(1, PAN_L, 0x3F, 0x00, true);
33+
MyPelcoBus.send_command(Camera1, PAN_L, 0x3F);
3634
delay(1000);
37-
MyPelcoBus.send_command(1, STOP); //Stop the camera
35+
MyPelcoBus.send_command(Camera1, STOP); //Stop the camera
3836
delay(1000);
3937
//Pan to the left with less speed than the tilt speed that goes up
40-
MyPelcoBus.send_command(1, PAN_L_TILT_U, 0x10, 0xFF, true);
38+
MyPelcoBus.send_command(Camera1, PAN_R, 0x3F);
4139
delay(1000);
42-
MyPelcoBus.send_command(1, STOP); //Stop the camera
40+
MyPelcoBus.send_command(Camera1, STOP); //Stop the camera
4341
delay(1000);
4442
}

examples/send_raw/send_raw.ino

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
#include "Arduino.h"
22
#include <Pelco_And_Arduino.h>
33

4-
PelcoCamBus MyPelcoBus(6, // TX pin (Arduino to Cam)
5-
7, // RX pin (Cam to Arduino)
6-
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
4+
PelcoBus MyPelcoBus(6, // TX pin (Arduino to Cam)
5+
7, // RX pin (Cam to Arduino)
6+
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
77

88
void setup() {
9+
//Here we don't need to declare a camera because all we send is raw
910

1011
Serial.begin(9600);
1112

1213
// Begin the serial communication
1314
MyPelcoBus.begin(PELCO_D9600, true);
1415

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 (!MyPelcoBus.send_command(1, STOP)) {
18-
Serial.println("Camera not plugged?");
19-
delay(1000);
20-
}
2116
Serial.println("Init Finished!");
2217
}
2318

2419
void loop() {
2520
while(Serial.available()) {
2621
String in = Serial.readString(); //Read from the serial
2722
MyPelcoBus.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-
MyPelcoBus.send_command(1, STOP); //Stop it after a while
23+
//You will need to stop it manually
24+
// Very simple: just send this commnad : "FF *address* 00 00 00 00 01"
3025
}
3126
}

examples/set_position/set_position.ino

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
#include "Arduino.h"
22
#include <Pelco_And_Arduino.h>
33

4-
PelcoCamBus MyPelcoBus(6, // TX pin (Arduino to Cam)
5-
7, // RX pin (Cam to Arduino)
6-
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
4+
PelcoBus MyPelcoBus(6, // TX pin (Arduino to Cam)
5+
7, // RX pin (Cam to Arduino)
6+
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
77

8-
void setup() {
8+
PelcoBus::PelcoCam Camera1; //Declare a camera
99

10+
void setup() {
1011
Serial.begin(9600);
1112

1213
// Begin the serial communication
1314
MyPelcoBus.begin(PELCO_D9600, true);
1415

16+
// Set camera settings
17+
Camera1.address = 1; //Address of the camera
18+
Camera1.disable_ack = false; //If you want to disable the ACK, set it to true
19+
20+
1521
// Send stop, send_command will return a bool that indicates if the cameras sended its ACK
1622
// It's a good way to see if the camera is plugged in
17-
while (!MyPelcoBus.send_command(1, STOP)) {
23+
while (!MyPelcoBus.send_command(Camera1, STOP)) {
1824
Serial.println("Camera not plugged?");
1925
delay(1000);
2026
}
@@ -23,9 +29,8 @@ void setup() {
2329

2430
void loop() {
2531
//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-
MyPelcoBus.send_command(SET_PAN, i); //Hundered of degrees, max is 35999 == 359,99°
32+
for(int i=0; i<35999; i=i+100){
33+
MyPelcoBus.send_command(Camera1, SET_PAN, i); //Hundered of degrees, max is 35999 == 359,99°
2934
delay(500);
3035
}
3136

examples/simple/simple.ino

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
#include "Arduino.h"
22
#include <Pelco_And_Arduino.h>
33

4-
PelcoCamBus MyPelcoBus(6, // TX pin (Arduino to Cam)
5-
7, // RX pin (Cam to Arduino)
6-
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
4+
PelcoBus MyPelcoBus(6, // TX pin (Arduino to Cam)
5+
7, // RX pin (Cam to Arduino)
6+
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
77

8-
void setup() {
8+
PelcoBus::PelcoCam Camera1; //Declare a camera
99

10+
void setup() {
1011
Serial.begin(9600);
1112

1213
// Begin the serial communication
1314
MyPelcoBus.begin(PELCO_D9600, true);
1415

16+
// Set camera settings
17+
Camera1.address = 1; //Address of the camera
18+
Camera1.disable_ack = false; //If you want to disable the ACK, set it to true
19+
20+
1521
// Send stop, send_command will return a bool that indicates if the cameras sended its ACK
1622
// It's a good way to see if the camera is plugged in
17-
while (!MyPelcoBus.send_command(1, STOP)) {
23+
while (!MyPelcoBus.send_command(Camera1, STOP)) {
1824
Serial.println("Camera not plugged?");
1925
delay(1000);
2026
}
2127
Serial.println("Init Finished!");
2228
}
2329

2430
void loop() {
25-
2631
//Pan to the left with max speed before turbo
27-
MyPelcoBus.send_command(1, PAN_L, 0x3F);
32+
MyPelcoBus.send_command(Camera1, PAN_L, 0x3F);
2833
delay(1000);
29-
MyPelcoBus.send_command(1, STOP); //Stop the camera
34+
MyPelcoBus.send_command(Camera1, STOP); //Stop the camera
3035
delay(1000);
3136
//Pan to the left with less speed than the tilt speed that goes up
32-
MyPelcoBus.send_command(1, PAN_L_TILT_U, 0x10, 0xFF);
37+
MyPelcoBus.send_command(Camera1, PAN_R, 0x3F);
3338
delay(1000);
34-
MyPelcoBus.send_command(1, STOP); //Stop the camera
39+
MyPelcoBus.send_command(Camera1, STOP); //Stop the camera
3540
delay(1000);
3641
}

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# Datatypes (KEYWORD1)
77
#######################################
88

9+
PelcoBus KEYWORD1
910
PelcoCam KEYWORD1
1011

1112
#######################################

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Pelco_And_Arduino
2-
version=2.0.0
2+
version=2.1.0
33
author=Boris Hilkens <pixelbo21@gmail.com>
44
maintainer=Boris Hilkens <pixelbo21@gmail.com>
55
sentence=This library makes Pelco cameras moves!

src/Pelco_And_Arduino.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
*/
3131

32-
PelcoCamBus::PelcoCamBus(uint8_t rxPin, uint8_t txPin, uint8_t readEnPin) {
32+
PelcoBus::PelcoBus(uint8_t txPin, uint8_t rxPin, uint8_t readEnPin) {
3333
txPin_ = txPin;
3434
rxPin_ = rxPin;
3535
rePin_ = readEnPin;
@@ -40,7 +40,7 @@ PelcoCamBus::PelcoCamBus(uint8_t rxPin, uint8_t txPin, uint8_t readEnPin) {
4040
*
4141
*/
4242

43-
void PelcoCamBus::begin(uint32_t config, bool log_messages = false) {
43+
void PelcoBus::begin(uint32_t config, bool log_messages = false) {
4444
uint16_t baud;
4545

4646
switch (config) {
@@ -98,8 +98,11 @@ void PelcoCamBus::begin(uint32_t config, bool log_messages = false) {
9898
*
9999
*/
100100

101-
bool PelcoCamBus::send_command(uint8_t address, uint8_t command, uint16_t data1 = 0x00, uint8_t data2 = 0x00,
101+
bool PelcoBus::send_command(PelcoBus::PelcoCam camera, uint8_t command, uint16_t data1 = 0x00, uint8_t data2 = 0x00,
102102
bool disableACK) {
103+
104+
uint8_t address = camera.address;
105+
103106
messToCamera[0] = 0xFF; // The first byte is always FF (sync)
104107
messToCamera[1] = address; // the second is the adress
105108

@@ -158,7 +161,7 @@ bool PelcoCamBus::send_command(uint8_t address, uint8_t command, uint16_t data1
158161

159162
(*SerialCamBus).write(messToCamera, sizeof(messToCamera)); // Write to the camera
160163

161-
if (!disableACK) { // Check the response of the camera only if it isn't a request (aka a query aka check for ack)
164+
if ((!disableACK) && (!camera.disable_ack)) { // Check the response of the camera only if it isn't a request or the camera does not support return
162165
int timeout = 10000; // 10 millissecond wait
163166

164167
if (!autoModule_)
@@ -258,8 +261,9 @@ bool PelcoCamBus::send_command(uint8_t address, uint8_t command, uint16_t data1
258261
* @return true if succeded, false if an error occured
259262
*/
260263

261-
uint16_t PelcoCamBus::send_request(uint8_t address, uint8_t request, int timeout = 1000) {
264+
uint16_t PelcoBus::send_request(PelcoBus::PelcoCam camera, uint8_t request, int timeout = 1000) {
262265
byte response_command;
266+
uint8_t address = camera.address;
263267

264268
if (searchIndexPROGMEM(QUERY_CMND, request) != -1) {
265269
response_command = pgm_read_byte(&RESP_CMND[searchIndexPROGMEM(QUERY_CMND, request)]); // Magic!
@@ -272,7 +276,7 @@ uint16_t PelcoCamBus::send_request(uint8_t address, uint8_t request, int timeout
272276
return -1;
273277
}
274278

275-
send_command(request, 0x00, 0x00, true); // Send the query
279+
send_command(camera, request, 0x00, 0x00, true); // Send the query with ack diabled
276280

277281
if (!autoModule_) {
278282
digitalWrite(rePin_, LOW); // Set the module at RX mode
@@ -344,7 +348,7 @@ uint16_t PelcoCamBus::send_request(uint8_t address, uint8_t request, int timeout
344348
}
345349

346350
////////todo get the response if it is a query or extended one!!
347-
bool PelcoCamBus::send_raw(String hex_string) {
351+
bool PelcoBus::send_raw(String hex_string) {
348352
hex_string.replace(" ", ""); // Replace spaces
349353

350354
int size = hex_string.length() / 2; // Size of the string without space
@@ -403,7 +407,7 @@ bool PelcoCamBus::send_raw(String hex_string) {
403407
* @return the index of the element found
404408
*/
405409

406-
int PelcoCamBus::searchIndexPROGMEM(const byte look_array[], byte value) {
410+
int PelcoBus::searchIndexPROGMEM(const byte look_array[], byte value) {
407411
int i = 0;
408412
for (i = 0; i <= (sizeof(look_array) / sizeof(*look_array)); i++) {
409413
if (pgm_read_byte(&look_array[i]) == value) {
@@ -413,7 +417,7 @@ int PelcoCamBus::searchIndexPROGMEM(const byte look_array[], byte value) {
413417
return -1;
414418
}
415419

416-
int PelcoCamBus::searchIndex(byte look_array[], byte value,
420+
int PelcoBus::searchIndex(byte look_array[], byte value,
417421
size_t size) { // For an x or y reason sizeof don't work properly
418422

419423
int i = 0;

src/Pelco_And_Arduino.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include "constants.h"
2020

21-
class PelcoCamBus
21+
class PelcoBus
2222
{
2323

2424
private:
@@ -65,11 +65,18 @@ class PelcoCamBus
6565
int searchIndexPROGMEM(const byte look_array[], byte value);
6666

6767
public:
68-
PelcoCamBus(uint8_t rxPin, uint8_t txPin, uint8_t readEnPin = NOT_A_PIN);
68+
PelcoBus(uint8_t txPin, uint8_t rxPin, uint8_t readEnPin = NOT_A_PIN);
69+
70+
struct PelcoCam{
71+
uint8_t address;
72+
bool disable_ack;
73+
};
74+
75+
6976
void begin(uint32_t config, bool log_messages = false);
7077

71-
bool send_command(uint8_t address, uint8_t command, uint16_t data1 = 0x00, uint8_t data2 = 0x00, bool disableACK = false);
72-
uint16_t send_request(uint8_t address, uint8_t request, int timeout = 1000);
78+
bool send_command(PelcoBus::PelcoCam camera, uint8_t command, uint16_t data1 = 0x00, uint8_t data2 = 0x00, bool disableACK = false);
79+
uint16_t send_request(PelcoBus::PelcoCam camera, uint8_t request, int timeout = 1000);
7380
bool send_raw(String hex_string); // TODO: get ACK
7481
};
7582

0 commit comments

Comments
 (0)