Skip to content

Commit ca03f7c

Browse files
committed
query is working
1 parent a498837 commit ca03f7c

7 files changed

Lines changed: 109 additions & 52 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "Arduino.h"
2+
#include <Pelco_And_Arduino.h>
3+
4+
5+
6+
PelcoCam MyPelcoCam(01, //Address of the camera
7+
PELCO_D9600,//Config
8+
6, //TX pin
9+
7, //RX pin
10+
false, //Enable logging?
11+
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
12+
13+
void setup() {
14+
// Begin the serial communication If you enabled logging, no need to begin the normal serial
15+
MyPelcoCam.begin();
16+
17+
Serial.begin(9600);
18+
// Send stop, send_command will return a bool that indicates if the cameras sended its ACK
19+
// It's a good way to see if the camera is plugged in
20+
while (!MyPelcoCam.send_command(STOP)) {
21+
Serial.println("Camera not plugged?");
22+
delay(1000);
23+
}
24+
Serial.println("Init Finished!");
25+
}
26+
27+
void loop() {
28+
// loops the camera increment angle of 1°; very fun because it shakes like hell
29+
int i = 0;
30+
for (i = 0; i < 35999; i = i + 100) {
31+
MyPelcoCam.send_command(SET_PAN, i); // Hundered of degrees, max is 35999 == 359,99°
32+
delay(1000);
33+
34+
int angle100 = MyPelcoCam.send_request(QUERY_PAN); // Get the current angle of the camera
35+
36+
float angle = angle100 / 100.0; // Convert to float
37+
38+
Serial.println("Angle detected: " + String(angle));
39+
}
40+
}

examples/send_raw/send_raw.ino

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

4-
PelcoCam MyPelcoCam(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)
4+
PelcoCam MyPelcoCam(01, //Address of the camera
5+
PELCO_D9600,//Config
6+
6, //TX pin
7+
7, //RX pin
8+
true, //Enable logging?
9+
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
1010

1111
void setup() {
1212
//Begin the serial communication If you enabled logging, no need to begin the normal serial

examples/set_position/set_position.ino

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

4-
PelcoCam MyPelcoCam(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)
4+
PelcoCam MyPelcoCam(01, //Address of the camera
5+
PELCO_D9600,//Config
6+
6, //TX pin
7+
7, //RX pin
8+
true, //Enable logging?
9+
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
1010

1111
void setup() {
1212
//Begin the serial communication If you enabled logging, no need to begin the normal serial

examples/simple/simple.ino

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

4-
PelcoCam MyPelcoCam(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)
4+
PelcoCam MyPelcoCam(01, //Address of the camera
5+
PELCO_D9600,//Config
6+
6, //TX pin
7+
7, //RX pin
8+
true, //Enable logging?
9+
8); // RE pin for manual switching modules set to -1 if it is a auto module (like a groove one)
1010

1111
void setup() {
1212
//Begin the serial communication If you enabled logging, no need to begin the normal serial

src/Pelco_And_Arduino.cpp

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ bool PelcoCam::send_command(uint8_t command, uint16_t data1, uint8_t data2, bool
103103
messToCamera[1] = address_; // the second is the adress
104104

105105
// Special commands handling:
106-
if (searchIndex(CMND1, command) != -1) {
106+
if (searchIndexPROGMEM(CMND1, command) != -1) {
107107
messToCamera[2] = command - 0b01100000; // Minus the thing that differentiate from others
108108
messToCamera[3] = 0x00;
109109
messToCamera[4] = 0x00;
@@ -113,21 +113,21 @@ bool PelcoCam::send_command(uint8_t command, uint16_t data1, uint8_t data2, bool
113113
messToCamera[2] = 0x00;
114114
messToCamera[3] = command;
115115

116-
if (searchIndex(DATA1, command) != -1) {
116+
if (searchIndexPROGMEM(DATA1, command) != -1) {
117117
messToCamera[4] = data1;
118118
messToCamera[5] = 0x00;
119119

120-
} else if (searchIndex(DATA_BOTH, command) != -1) {
120+
} else if (searchIndexPROGMEM(DATA_BOTH, command) != -1) {
121121
messToCamera[4] = data1;
122122
messToCamera[5] = data2;
123123

124-
} else if (searchIndex(SETPOS, command) != -1) {
124+
} else if (searchIndexPROGMEM(SETPOS, command) != -1) {
125125
uint16_t angle = (uint16_t)(data1 % 35999); // Centième de degrès! avec max = 35999 (359.99 deg)
126126

127127
messToCamera[4] = (uint8_t)((angle >> 0x08) & 0x00FF); // Get MSB
128128
messToCamera[5] = (uint8_t)(angle & 0x00FF); // Get LSB
129129

130-
} else if (searchIndex(QUERY_CMND, command) != -1) {
130+
} else if ((searchIndexPROGMEM(QUERY_CMND, command) != -1 && !request)) {
131131
if (log_messages_) {
132132
sprintf(log_buffer, "Cam %i: You are doing an query into send command ??????????", address_);
133133
Serial.print(log_buffer);
@@ -164,7 +164,7 @@ bool PelcoCam::send_command(uint8_t command, uint16_t data1, uint8_t data2, bool
164164
digitalWrite(rePin_, LOW); // Set the module at RX mode
165165

166166
while (!(*SerialCam).available()) { // Wait for the first bit
167-
if (timeout == 0) { // If timeout is reached
167+
if (timeout == 0) { // If timeout is reached
168168
if (log_messages_) {
169169
sprintf(
170170
log_buffer,
@@ -243,33 +243,35 @@ bool PelcoCam::send_command(uint8_t command, uint16_t data1, uint8_t data2, bool
243243
* @return true if succeded, false if an error occured
244244
*/
245245

246-
uint16_t PelcoCam::send_request(uint8_t request, int timeout, int maxbuffer) {
246+
uint16_t PelcoCam::send_request(uint8_t request, int timeout) {
247247
byte response_command;
248248

249-
if (searchIndex(QUERY_CMND, request) != -1) {
250-
response_command == RESP_CMND[searchIndex(QUERY_CMND, request)]; // Magic!
249+
if (searchIndexPROGMEM(QUERY_CMND, request) != -1) {
250+
response_command = pgm_read_byte(&RESP_CMND[searchIndexPROGMEM(QUERY_CMND, request)]); // Magic!
251251
} else {
252-
if (log_messages_){
252+
if (log_messages_) {
253253
sprintf(log_buffer, "Cam %i: No valid request provided\n", address_);
254254
Serial.print(log_buffer);
255+
Serial.println(request);
255256
}
256257
return -1;
257258
}
258259

259260
send_command(request, 0x00, 0x00, true); // Send the query
260261

261-
if (!autoModule_)
262+
if (!autoModule_){
262263
digitalWrite(rePin_, LOW); // Set the module at RX mode
264+
}
263265

264-
byte buffer[maxbuffer]; // Buffer for the reception from the camera
266+
byte buffer[7]; // Buffer for the reception from the camera
265267

266268
while (!(*SerialCam).available()) { // Wait for the first bit
267-
if (timeout == 0) { // If timeout is reached
268-
if (log_messages_){
269+
if (timeout == 0) { // If timeout is reached
270+
if (log_messages_) {
269271
sprintf(log_buffer, "Cam %i: ERROR timout reached\n", address_);
270272
Serial.print(log_buffer);
271273
}
272-
if (!autoModule_){
274+
if (!autoModule_) {
273275
digitalWrite(rePin_, HIGH); // set back at TX mode
274276
}
275277
return -1;
@@ -286,14 +288,14 @@ uint16_t PelcoCam::send_request(uint8_t request, int timeout, int maxbuffer) {
286288

287289
(*SerialCam).readBytes(buffer, 7); // Apparently this works
288290

289-
if (!autoModule_)
291+
if (!autoModule_) {
290292
digitalWrite(rePin_, HIGH); // set back at TX mode
293+
}
291294

292-
int command_index = searchIndex(buffer, 0x59); // Looks up where is the index of the response command
295+
int command_index = searchIndex(buffer, response_command, 7); // Looks up where is the index of the response command
293296

294297
if (command_index == -1) { // Checks if found
295-
296-
if (log_messages_){
298+
if (log_messages_) {
297299
sprintf(log_buffer, "Cam %i: Warn: no reponse from camera (bad index)\n", address_);
298300
Serial.print(log_buffer);
299301
}
@@ -342,7 +344,7 @@ bool PelcoCam::send_raw(String hex_string) {
342344
}
343345

344346
if (raw_command[0] != 0xFF) {
345-
if (log_messages_){
347+
if (log_messages_) {
346348
sprintf(log_buffer, "Cam %i: Wrong sync byte, updating to right sync byte\n", address_);
347349
Serial.print(log_buffer);
348350
}
@@ -352,7 +354,7 @@ bool PelcoCam::send_raw(String hex_string) {
352354
/////Check checksum
353355
uint8_t checksum = (raw_command[1] + raw_command[2] + raw_command[3] + raw_command[4] + raw_command[5]) % 0x100;
354356
if (checksum != raw_command[6]) {
355-
if (log_messages_){
357+
if (log_messages_) {
356358
sprintf(log_buffer, "Cam %i: Wrong checksum, updating to right checksum\n", address_);
357359
Serial.print(log_buffer);
358360
}
@@ -376,20 +378,32 @@ bool PelcoCam::send_raw(String hex_string) {
376378
}
377379

378380
/*!
379-
* @brief Dearch a value trough a array of bytes
381+
* @brief Search a value trough a array of bytes one function is for progmem the other one isn't for progmem
380382
*
381383
* @param look_array the array
382384
* @param value value to lookup
383385
*
384386
* @return the index of the element found
385387
*/
386388

387-
int PelcoCam::searchIndex(const byte look_array[], byte value) {
389+
int PelcoCam::searchIndexPROGMEM(const byte look_array[], byte value) {
388390
int i = 0;
389-
for (i = 0; i <= (sizeof(look_array) / sizeof(look_array[0])); i++) {
390-
if (look_array[i] == value) {
391+
for (i = 0; i <= (sizeof(look_array) / sizeof(*look_array)); i++) {
392+
if (pgm_read_byte(&look_array[i]) == value) {
391393
return i;
392394
}
393395
}
394396
return -1;
395397
}
398+
399+
int PelcoCam::searchIndex(byte look_array[], byte value, size_t size) {//For an x or y reason sizeof don't work properly
400+
401+
int i = 0;
402+
for (i = 0; i <= (size); i++) {
403+
404+
if (look_array[i] == value) {
405+
return i;
406+
}
407+
}
408+
return -1;
409+
}

src/Pelco_And_Arduino.h

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

66-
int searchIndex(const byte look_array[], byte value);
66+
int searchIndex(byte look_array[], byte value, size_t size);
67+
int searchIndexPROGMEM(const byte look_array[], byte value);
6768

6869
public:
6970
PelcoCam(uint8_t address, uint32_t config, uint8_t txPin, uint8_t rxPin, bool log_messages = false, uint8_t readEnPin = NOT_A_PIN);
7071
void begin();
7172

7273
bool send_command(uint8_t command, uint16_t data1 = 0x00, uint8_t data2 = 0x00, bool request = false);
73-
uint16_t send_request(uint8_t request, int timeout = 1000, int max_buffer = 20);
74+
uint16_t send_request(uint8_t request, int timeout = 1000);
7475
bool send_raw(String hex_string); // TODO: get ACK
7576
};
7677

src/constants.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,19 @@ a command
9797
*/
9898
//Special commands:
9999

100-
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
101-
const PROGMEM byte DATA1[2] = {PAN_L, PAN_R}; //List of all commands that use ONLY the DATA1 for data pass
100+
const byte CMND1[5] PROGMEM = {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
101+
const byte DATA1[2] PROGMEM = {PAN_L, PAN_R}; //List of all commands that use ONLY the DATA1 for data pass
102102

103-
const PROGMEM byte SETPOS[3] = {SET_PAN, SET_TILT, SET_ZOOM}; //List of all command that require an MSB and LSB
103+
const byte SETPOS[3] PROGMEM = {SET_PAN, SET_TILT, SET_ZOOM}; //List of all command that require an MSB and LSB
104104

105-
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+
const byte DATA_BOTH[5] PROGMEM= {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)
106106

107-
const PROGMEM byte PRESET_CMND[3] = {SET_PRESET, CLR_PRESET, GOTO_PRESET}; //will I use it?
107+
const byte RESP_CMND[3] PROGMEM = {RESP_PAN, RESP_TILT, RESP_ZOOM};
108+
109+
const byte QUERY_CMND[3] PROGMEM = {QUERY_PAN, QUERY_TILT, QUERY_ZOOM};
110+
111+
const byte PRESET_CMND[3] PROGMEM= {SET_PRESET, CLR_PRESET, GOTO_PRESET}; //will I use it?
108112

109-
const PROGMEM byte QUERY_CMND[3] = {QUERY_PAN, QUERY_TILT, QUERY_ZOOM};
110-
const PROGMEM byte RESP_CMND[3] = {RESP_PAN, RESP_TILT, RESP_ZOOM};
111113

112114
///////////////////////////////////////////////// Constants for the config parameter
113115

0 commit comments

Comments
 (0)