Skip to content

Commit 2504d33

Browse files
Copilotwysaid
andcommitted
Add comprehensive documentation for error callback system
Co-authored-by: wysaid <1430725+wysaid@users.noreply.github.com>
1 parent 1150fe3 commit 2504d33

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,42 @@ int main() {
113113
}
114114
```
115115
116+
##### Error Handling in C++
117+
118+
Starting from v1.1.0, C++ interface supports error callbacks for detailed error reporting:
119+
120+
```cpp
121+
#include <ccap.h>
122+
#include <iostream>
123+
124+
int main() {
125+
ccap::Provider provider;
126+
127+
// Set error callback to receive detailed error information
128+
provider.setErrorCallback([](ccap::ErrorCode errorCode, const std::string& description) {
129+
std::cerr << "Camera Error - Code: " << static_cast<int>(errorCode)
130+
<< ", Description: " << description << std::endl;
131+
});
132+
133+
// Camera operations - errors will trigger the callback
134+
if (!provider.open("", true)) {
135+
std::cerr << "Failed to open camera" << std::endl;
136+
}
137+
138+
return 0;
139+
}
140+
```
141+
142+
Available error codes include:
143+
- `ErrorCode::NoDeviceFound` - No camera device found
144+
- `ErrorCode::InvalidDevice` - Invalid device name or index
145+
- `ErrorCode::DeviceOpenFailed` - Camera open failed
146+
- `ErrorCode::DeviceStartFailed` - Camera start failed
147+
- `ErrorCode::UnsupportedResolution` - Unsupported resolution
148+
- `ErrorCode::UnsupportedPixelFormat` - Unsupported pixel format
149+
- `ErrorCode::FrameCaptureTimeout` - Frame capture timeout
150+
- `ErrorCode::FrameCaptureFailed` - Frame capture failed
151+
116152
#### Pure C Interface
117153
118154
```c
@@ -458,6 +494,54 @@ typedef enum {
458494
} CcapPixelFormat;
459495
```
460496
497+
##### Error Handling
498+
499+
Starting from v1.1.0, ccap supports error callbacks for detailed error reporting:
500+
501+
```c
502+
// Error codes
503+
typedef enum {
504+
CCAP_ERROR_NONE = 0,
505+
CCAP_ERROR_NO_DEVICE_FOUND = 0x1001, // No camera device found
506+
CCAP_ERROR_INVALID_DEVICE = 0x1002, // Invalid device name or index
507+
CCAP_ERROR_DEVICE_OPEN_FAILED = 0x1003, // Camera open failed
508+
CCAP_ERROR_DEVICE_START_FAILED = 0x1004, // Camera start failed
509+
CCAP_ERROR_UNSUPPORTED_RESOLUTION = 0x2001, // Unsupported resolution
510+
CCAP_ERROR_UNSUPPORTED_PIXEL_FORMAT = 0x2002, // Unsupported pixel format
511+
CCAP_ERROR_FRAME_CAPTURE_TIMEOUT = 0x3001, // Frame capture timeout
512+
CCAP_ERROR_FRAME_CAPTURE_FAILED = 0x3002, // Frame capture failed
513+
// More error codes...
514+
} CcapErrorCode;
515+
516+
// Error callback function
517+
typedef void (*CcapErrorCallback)(CcapErrorCode errorCode, const char* errorDescription, void* userData);
518+
519+
// Set error callback
520+
bool ccap_provider_set_error_callback(CcapProvider* provider, CcapErrorCallback callback, void* userData);
521+
522+
// Get error description
523+
const char* ccap_error_code_to_string(CcapErrorCode errorCode);
524+
525+
// Usage example
526+
void error_callback(CcapErrorCode errorCode, const char* errorDescription, void* userData) {
527+
printf("Camera Error - Code: %d, Description: %s\n", (int)errorCode, errorDescription);
528+
}
529+
530+
int main() {
531+
CcapProvider* provider = ccap_provider_create();
532+
533+
// Set error callback to receive error notifications
534+
ccap_provider_set_error_callback(provider, error_callback, NULL);
535+
536+
if (!ccap_provider_open_by_index(provider, 0, true)) {
537+
printf("Failed to open camera\n"); // Error callback will also be called
538+
}
539+
540+
ccap_provider_destroy(provider);
541+
return 0;
542+
}
543+
```
544+
461545
#### Compilation and Linking
462546
463547
##### macOS

docs/C_Interface.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,65 @@ C 接口使用以下错误处理策略:
243243
1. **返回值**: 大多数函数返回 `bool` 类型,`true` 表示成功,`false` 表示失败
244244
2. **空指针**: 当操作失败时,指针返回函数返回 `NULL`
245245
3. **NaN**: 数值返回函数在失败时返回 `NaN`
246+
4. **错误回调**: 可以设置错误回调函数来接收详细的错误信息
247+
248+
### 错误回调
249+
250+
从 v1.1.0 开始,ccap 支持设置错误回调函数来接收详细的错误信息:
251+
252+
#### 错误码
253+
254+
```c
255+
typedef enum {
256+
CCAP_ERROR_NONE = 0, // 无错误
257+
CCAP_ERROR_NO_DEVICE_FOUND = 0x1001, // 未找到相机设备
258+
CCAP_ERROR_INVALID_DEVICE = 0x1002, // 设备名称或索引无效
259+
CCAP_ERROR_DEVICE_OPEN_FAILED = 0x1003, // 相机设备打开失败
260+
CCAP_ERROR_DEVICE_START_FAILED = 0x1004, // 相机启动失败
261+
CCAP_ERROR_UNSUPPORTED_RESOLUTION = 0x2001, // 不支持的分辨率
262+
CCAP_ERROR_UNSUPPORTED_PIXEL_FORMAT = 0x2002, // 不支持的像素格式
263+
CCAP_ERROR_FRAME_CAPTURE_TIMEOUT = 0x3001, // 帧捕获超时
264+
CCAP_ERROR_FRAME_CAPTURE_FAILED = 0x3002, // 帧捕获失败
265+
// 更多错误码...
266+
} CcapErrorCode;
267+
```
268+
269+
#### 错误回调函数
270+
271+
```c
272+
// 错误回调函数类型
273+
typedef void (*CcapErrorCallback)(CcapErrorCode errorCode, const char* errorDescription, void* userData);
274+
275+
// 设置错误回调
276+
bool ccap_provider_set_error_callback(CcapProvider* provider, CcapErrorCallback callback, void* userData);
277+
278+
// 获取错误码描述
279+
const char* ccap_error_code_to_string(CcapErrorCode errorCode);
280+
```
281+
282+
#### 使用示例
283+
284+
```c
285+
// 错误回调函数
286+
void error_callback(CcapErrorCode errorCode, const char* errorDescription, void* userData) {
287+
printf("Camera Error - Code: %d, Description: %s\n", (int)errorCode, errorDescription);
288+
}
289+
290+
int main() {
291+
CcapProvider* provider = ccap_provider_create();
292+
293+
// 设置错误回调
294+
ccap_provider_set_error_callback(provider, error_callback, NULL);
295+
296+
// 执行相机操作,如果出错会调用回调函数
297+
if (!ccap_provider_open_by_index(provider, 0, true)) {
298+
printf("Failed to open camera\n");
299+
}
300+
301+
ccap_provider_destroy(provider);
302+
return 0;
303+
}
304+
```
246305

247306
## 注意事项
248307

0 commit comments

Comments
 (0)