3232// freenect_set_flag is the only function exposed in libfreenect.h
3333// The rest are available internally via #include flags.h
3434
35- FN_INTERNAL static int register_for_flag (int flag ) {
36- switch (flag ) {
37- case FREENECT_MIRROR_DEPTH :
38- return 0x17 ;
39- case FREENECT_MIRROR_VIDEO :
40- return 0x47 ;
41- default :
42- return -1 ;
35+ FN_INTERNAL int register_for_flag (int flag )
36+ {
37+ switch (flag )
38+ {
39+ case FREENECT_MIRROR_DEPTH :
40+ return 0x17 ;
41+ case FREENECT_MIRROR_VIDEO :
42+ return 0x47 ;
43+ default :
44+ return -1 ;
4345 }
4446}
4547
4648int freenect_set_flag (freenect_device * dev , freenect_flag flag , freenect_flag_value value )
4749{
48- if (flag >= (1 << 16 )) {
50+ if (flag >= (1 << 16 ))
51+ {
4952 int reg = register_for_flag (flag );
5053 if (reg < 0 )
5154 return reg ;
5255 return write_register (dev , reg , value );
5356 }
5457
5558 uint16_t reg = read_cmos_register (dev , 0x0106 );
56- if (reg < 0 )
57- return reg ;
59+ if (reg == UINT16_MAX )
60+ return -1 ;
5861 if (value == FREENECT_ON )
5962 reg |= flag ;
6063 else
@@ -138,19 +141,21 @@ FN_INTERNAL int send_cmd(freenect_device *dev, uint16_t cmd, void *cmdbuf, unsig
138141 return actual_len ;
139142}
140143
144+ // returns UINT16_MAX on error
141145FN_INTERNAL uint16_t read_register (freenect_device * dev , uint16_t reg )
142146{
143147 freenect_context * ctx = dev -> parent ;
148+
144149 uint16_t reply [2 ];
145- uint16_t cmd ;
146- int res ;
147-
148- cmd = fn_le16 (reg );
149-
150+ uint16_t cmd = fn_le16 (reg );
151+
150152 FN_DEBUG ("read_register: 0x%04x =>\n" , reg );
151- res = send_cmd (dev , 0x02 , & cmd , 2 , reply , 4 );
153+ int res = send_cmd (dev , 0x02 , & cmd , 2 , reply , 4 );
152154 if (res < 0 )
155+ {
153156 FN_ERROR ("read_register: send_cmd() failed: %d\n" , res );
157+ return UINT16_MAX ;
158+ }
154159 if (res != 4 )
155160 FN_WARNING ("read_register: send_cmd() returned %d [%04x %04x], 0000 expected\n" , res , reply [0 ], reply [1 ]);
156161
@@ -162,33 +167,40 @@ FN_INTERNAL int write_register(freenect_device *dev, uint16_t reg, uint16_t data
162167 freenect_context * ctx = dev -> parent ;
163168 uint16_t reply [2 ];
164169 uint16_t cmd [2 ];
165- int res ;
166170
167171 cmd [0 ] = fn_le16 (reg );
168172 cmd [1 ] = fn_le16 (data );
169173
170174 FN_DEBUG ("write_register: 0x%04x <= 0x%02x\n" , reg , data );
171- res = send_cmd (dev , 0x03 , cmd , 4 , reply , 4 );
175+ int res = send_cmd (dev , 0x03 , cmd , 4 , reply , 4 );
172176 if (res < 0 )
177+ {
178+ FN_ERROR ("write_register: send_cmd() returned %d\n" , res );
173179 return res ;
174- if (res != 2 ) {
175- FN_WARNING ("send_cmd() returned %d [%04x %04x], 0000 expected\n" , res , reply [0 ], reply [1 ]);
176180 }
181+ if (res != 2 )
182+ FN_WARNING ("write_register: send_cmd() returned %d [%04x %04x], 0000 expected\n" , res , reply [0 ], reply [1 ]);
183+
177184 return 0 ;
178185}
179186
187+ // returns UINT16_MAX on error
180188FN_INTERNAL uint16_t read_cmos_register (freenect_device * dev , uint16_t reg )
181189{
182190 freenect_context * ctx = dev -> parent ;
183191 uint16_t replybuf [0x200 ];
184192 uint16_t cmdbuf [3 ];
193+
185194 cmdbuf [0 ] = 1 ;
186195 cmdbuf [1 ] = reg & 0x7fff ;
187196 cmdbuf [2 ] = 0 ;
197+
198+ FN_DEBUG ("read_cmos_register: 0x%04x =>\n" , reg );
188199 int res = send_cmd (dev , 0x95 , cmdbuf , 6 , replybuf , 6 );
189- if (res < 0 ) {
200+ if (res < 0 )
201+ {
190202 FN_ERROR ("read_cmos_register: send_cmd() returned %d\n" , res );
191- return res ;
203+ return UINT16_MAX ;
192204 }
193205 return replybuf [2 ];
194206}
@@ -198,11 +210,14 @@ FN_INTERNAL int write_cmos_register(freenect_device *dev, uint16_t reg, uint16_t
198210 freenect_context * ctx = dev -> parent ;
199211 uint16_t replybuf [0x200 ];
200212 uint16_t cmdbuf [3 ];
213+
201214 cmdbuf [0 ] = 1 ;
202215 cmdbuf [1 ] = reg | 0x8000 ;
203216 cmdbuf [2 ] = value ;
217+
218+ FN_DEBUG ("write_cmos_register: 0x%04x <= 0x%02x\n" , reg , value );
204219 int res = send_cmd (dev , 0x95 , cmdbuf , 6 , replybuf , 6 );
205220 if (res < 0 )
206- FN_ERROR ("read_cmos_register : send_cmd() returned %d\n" , res );
221+ FN_ERROR ("write_cmos_register : send_cmd() returned %d\n" , res );
207222 return res ;
208223}
0 commit comments