/camera/camlib

To get this branch, use:
bzr branch http://darksoft.org/webbzr/camera/camlib

« back to all changes in this revision

Viewing changes to pfcontrol/main.c

  • Committer: Suren A. Chilingaryan
  • Date: 2011-02-13 01:33:37 UTC
  • Revision ID: csa@dside.dyndns.org-20110213013337-ibm4w4n5a3hu4k7u
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Testprogram to communicate with the camera.
 
3
 *
 
4
 * test.exe <PropertyName> <Value>         #communicate on default port 0
 
5
 * test.exe <Port> <PropertyName> <Value>  #communicate on port Port
 
6
 *
 
7
 * test.exe                     #List all properties of port 0
 
8
 * test.exe Window.X            #Get value of Property Window.X on port 0
 
9
 * test.exe Window.X 100        #Set value of Property Window.X to 100 on port 0
 
10
 *
 
11
 * test.exe 1                   #List all properties of port 1
 
12
 * test.exe 1 Window.X          #Get value of Property Window.X on port 1
 
13
 * test.exe 1 Window.X 100      #Set value of Property Window.X to 100 on port 1
 
14
 *
 
15
 * test.exe 3                   #List all properties of port 3
 
16
 * test.exe 3 Window.Y          #Get value of Property Window.Y on port 3
 
17
 * test.exe 3 Window.Y 150      #Set value of Property Window.Y to 150 on port 3
 
18
 *
 
19
 * if the first argument is not a number, the communication is opened over default port 0
 
20
 *
 
21
 * if the first argument is a number, the communication is opend over given port number
 
22
 * 
 
23
 */
 
24
 
 
25
 
 
26
 
 
27
#include <stdio.h>
 
28
#include <stdlib.h>
 
29
#include "pfcam.h"
 
30
 
 
31
void list_children(int port, TOKEN t);
 
32
void print_type(int port, TOKEN t);
 
33
////////////////////////////////////////////////////////////////////////////
 
34
int handleError(error)
 
35
{
 
36
        const char *s;
 
37
 
 
38
        s = pfGetErrorString(error);
 
39
        if (error < 0) {
 
40
                printf("Error: %s\n", s);
 
41
        } else {
 
42
                printf("Warning: %s\n", s);
 
43
        }
 
44
        return error;
 
45
}
 
46
 
 
47
void list_children(int port, TOKEN t)
 
48
{
 
49
        TOKEN child;
 
50
        child = pfProperty_Select(port, t, t);
 
51
        while (child) {
 
52
                printf("Has Child: '%s'\n", pfProperty_GetName(port, child));
 
53
                child = pfProperty_Select(port, t, child); // iterate to next
 
54
        }
 
55
}
 
56
 
 
57
static unsigned char *g_types[] = {
 
58
        (unsigned char*)"Invalid value",
 
59
        (unsigned char*)"ROOT NODE",
 
60
        (unsigned char*)"Integer",
 
61
        (unsigned char*)"Float",
 
62
        (unsigned char*)"Boolean",
 
63
        (unsigned char*)"Mode",
 
64
        (unsigned char*)"Register",
 
65
        (unsigned char*)"String",
 
66
        (unsigned char*)"Buffer",
 
67
        (unsigned char*)"{Struct}",
 
68
        (unsigned char*)"{Array}",
 
69
        (unsigned char*)"[Command]",
 
70
        (unsigned char*)"[Event]"
 
71
};
 
72
 
 
73
void print_type(int port, TOKEN t)
 
74
{
 
75
        printf("Type : %s\n", g_types[pfProperty_GetType(port, t)]);
 
76
}
 
77
 
 
78
static char s_line[] = "----------------------------------------------\n";
 
79
 
 
80
int main(int argc, char **argv)
 
81
{
 
82
        int error, warn, i;
 
83
        char data[64];
 
84
        char name[50], manu[50];
 
85
        char *tmp;
 
86
        TOKEN t, root;
 
87
        int port, offset, arg;
 
88
        int numOfPorts, mBytes, nBytes, version, type;
 
89
 
 
90
        numOfPorts = 0;
 
91
        offset = 0;
 
92
        port = 0;
 
93
 
 
94
        //Init PFLib, get number of available port
 
95
        pfPortInit(&numOfPorts);
 
96
        printf("Number of Ports: %d\n", numOfPorts);
 
97
 
 
98
        //List ports    
 
99
        for(i=0; i<numOfPorts; i++){
 
100
                pfPortInfo(i, manu, &mBytes, name, &nBytes, &version, &type);
 
101
                printf("Port %d: Manufacturer: %s, %s", i, manu, name);
 
102
                printf(", mBytes %d, nBytes %d, ver. %d, type %d\n", mBytes, nBytes, version, type);
 
103
        }
 
104
        printf("\n");
 
105
        
 
106
        //Check if not default port, means "test.exe 5 " -> port 5
 
107
        if(argc > 1){
 
108
                arg = strtoul(argv[1], &tmp, 10);
 
109
                if(!*tmp){
 
110
                        port = arg;
 
111
                        offset = 1;
 
112
                        if(port >= numOfPorts){
 
113
                                port = 1;
 
114
                        }
 
115
                }
 
116
        }
 
117
 
 
118
        //show info about port to open
 
119
        pfPortInfo(port, manu, &mBytes, name, &nBytes, &version, &type);
 
120
        printf("Opening camera on port: Port %d: %s, %s...\n", port, manu, name);
 
121
        
 
122
        //open port:
 
123
        error = pfDeviceOpen(port);
 
124
        if(error < 0) {
 
125
                handleError(error);
 
126
                return -1;
 
127
        }
 
128
        else {
 
129
                warn = error;
 
130
        }
 
131
 
 
132
        printf("Device opened \n");
 
133
 
 
134
        //Check if higher baud rate is supported
 
135
        error = pfIsBaudRateSupported(port, 115200);
 
136
        if(error == 1){
 
137
                error = pfSetBaudRate(port, 115200);
 
138
                if(error < 0){
 
139
                        handleError(error);
 
140
                        pfDeviceClose(port);
 
141
                        return 0;
 
142
                }
 
143
        }
 
144
        else{
 
145
                error = pfIsBaudRateSupported(port, 57600);
 
146
                if(error == 1){
 
147
                        error = pfSetBaudRate(port, 57600);
 
148
                        if(error < 0){
 
149
                                handleError(error);
 
150
                                pfDeviceClose(port);
 
151
                                return 0;
 
152
                        }
 
153
                }
 
154
        }
 
155
        printf("%s", s_line);
 
156
 
 
157
        if(argc >= (2+offset)){//get or set value
 
158
                //parse propertyname to token
 
159
                t = pfProperty_ParseName(port, argv[1+offset]);
 
160
                if(t == INVALID_TOKEN) {
 
161
                        printf("Property not found or bad index\n");
 
162
                        pfDeviceClose(port);
 
163
                        return 0;
 
164
                }
 
165
                else{
 
166
                        print_type(port, t);
 
167
                }
 
168
                
 
169
                if(argc == 2+offset){//get property
 
170
                        list_children(port, t);
 
171
 
 
172
                        error = pfDevice_GetProperty_String(port, t, data, 64);
 
173
                        if(error < 0){
 
174
                                handleError(error);
 
175
                                pfDeviceClose(port);
 
176
                                return 0;
 
177
                        }
 
178
                        else{
 
179
                                warn = error;
 
180
                        }
 
181
                        printf("Value: %s\n", data);
 
182
                }
 
183
                else if(argc == 3+offset){//Set property
 
184
                        error = pfDevice_SetProperty_String(port, t, argv[2+offset]);
 
185
                        if(error){
 
186
                                handleError(error);
 
187
                                if(error < 0){
 
188
                                        pfDeviceClose(port);
 
189
                                        return 0;
 
190
                                }
 
191
                        }
 
192
                }
 
193
                else{
 
194
                        fprintf(stderr, "Wrong number of arguments\n");
 
195
                }
 
196
        }
 
197
        else{//list properties
 
198
                root = pfDevice_GetRoot(port);
 
199
                list_children(port, root);
 
200
        }
 
201
        if(warn){
 
202
                handleError(warn);
 
203
        }
 
204
        pfDeviceClose(port);
 
205
        return 0;
 
206
}