Skip to content

Commit ebf14bc

Browse files
committed
Get all examples working
1 parent dd303ec commit ebf14bc

File tree

15 files changed

+73
-115
lines changed

15 files changed

+73
-115
lines changed

CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ option(PTP_STUFF "Include extra functions for debugging/testing" ON)
77
option(PTP_DEFAULT_LOGGING "Include default log/panic/error implementation" ON)
88
option(PTP_INCLUDE_CLI "Include CLI program" OFF)
99
option(PTP_USE_LIBUSB "Use LibUSB backend on UNIX platforms" ON)
10+
option(PTP_INCLUDE_EXAMPLES "Include examples" ON)
1011

1112
set(CAMLIB_CORE
1213
src/operations.c
@@ -62,3 +63,23 @@ if(PTP_INCLUDE_CLI)
6263
target_include_directories(camlib PUBLIC ${PROJECT_SOURCE_DIR}/src)
6364
target_link_libraries(camlib libcamlib ${LIBUSB_LIBRARIES} pcap)
6465
endif()
66+
67+
macro(add_example exe_name source)
68+
add_executable(${exe_name} ${source})
69+
target_include_directories(${exe_name} PUBLIC ${PROJECT_SOURCE_DIR}/src)
70+
target_link_libraries(${exe_name} libcamlib ${LIBUSB_LIBRARIES})
71+
endmacro()
72+
73+
if(PTP_INCLUDE_EXAMPLES)
74+
add_example(info examples/info.c)
75+
add_example(optest examples/optest.c)
76+
add_example(directprint examples/directprint.c)
77+
add_example(eos examples/eos.c)
78+
add_example(evtest examples/evtest.c)
79+
add_example(live examples/live.c)
80+
add_example(ml examples/ml.c)
81+
add_example(pktest examples/pktest.c)
82+
add_example(storage examples/storage.c)
83+
add_example(usb examples/usb.c)
84+
add_example(wifi examples/wifi.c)
85+
endif()

examples/bindtest.c

Lines changed: 0 additions & 34 deletions
This file was deleted.

examples/directprint.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
int main() {
1010
struct PtpRuntime r;
11-
ptp_generic_init(&r);
11+
ptp_init(&r);
1212

1313
struct PtpDeviceInfo di;
1414

@@ -19,8 +19,9 @@ int main() {
1919

2020
ptp_open_session(&r);
2121

22-
struct UintArray *arr;
22+
struct PtpArray *arr;
2323
int rc = ptp_get_storage_ids(&r, &arr);
24+
if (arr->length == 0) abort();
2425
int id = arr->data[0];
2526

2627
struct PtpObjectInfo oi;
@@ -32,7 +33,7 @@ int main() {
3233

3334
ptp_close_session(&r);
3435
ptp_device_close(&r);
35-
ptp_generic_close(&r);
36+
ptp_close(&r);
3637

3738
return 0;
3839
}

examples/eos.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <camlib.h>
88

9-
/* Some snippets
9+
/* Some snippets, if you dare to run them:
1010
Show green on display:
1111
ptp_eos_evproc_run(&r, "UndisplayPhysicalScreen");
1212
ptp_eos_evproc_run(&r, "CreateColor %d %d %d", 0, 70, 0);
@@ -19,19 +19,8 @@ ptp_eos_evproc_run(&r, "writeaddr %d %d", 0x3780, 0x12345678);
1919
*/
2020

2121
int main() {
22-
#if 0
23-
int length = 0;
24-
extern char *canon_evproc_pack(int *length, char *string);
25-
char *data = canon_evproc_pack(&length, "Printf 'asd'");
26-
FILE *f = fopen("DUMP", "wb");
27-
fwrite(data, 1, length, f);
28-
fclose(f);
29-
return 0;
30-
31-
#endif
32-
3322
struct PtpRuntime r;
34-
ptp_generic_init(&r);
23+
ptp_init(&r);
3524

3625
struct PtpDeviceInfo di;
3726

@@ -53,9 +42,7 @@ int main() {
5342
ptp_device_info_json(&di, (char*)r.data, r.data_length);
5443
printf("%s\n", (char*)r.data);
5544

56-
struct PtpDevPropDesc desc;
57-
58-
ptp_get_prop_desc(&r, PTP_DPC_EOS_ImageFormat, &desc);
45+
// ...
5946

6047
ptp_close_session(&r);
6148
ptp_device_close(&r);

examples/evtest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
struct PtpRuntime r;
1111

1212
int main() {
13-
ptp_generic_init(&r);
13+
ptp_init(&r);
1414

1515
if (ptp_device_init(&r)) {
1616
puts("Device connection error");

examples/info.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
int main() {
99
struct PtpRuntime r;
10-
ptp_generic_init(&r);
10+
ptp_init(&r);
1111

1212
struct PtpDeviceInfo di;
1313

@@ -25,7 +25,7 @@ int main() {
2525

2626
ptp_close_session(&r);
2727
ptp_device_close(&r);
28-
ptp_generic_close(&r);
28+
ptp_close(&r);
2929
return 0;
3030
}
3131

examples/live.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
int main() {
88
struct PtpRuntime r;
9-
ptp_generic_init(&r);
9+
ptp_init(&r);
1010

1111
struct PtpDeviceInfo di;
1212

examples/ml.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
// Test Magic Lantern things
22
#include <stdio.h>
33
#include <stdint.h>
4-
#include <stdlib.h>
5-
#include <string.h>
6-
74
#include <camlib.h>
85

96
int main(int argc, char **argv) {
107
struct PtpRuntime r;
11-
ptp_generic_init(&r);
8+
ptp_init(&r);
129

1310
struct PtpDeviceInfo di;
1411

@@ -22,18 +19,9 @@ int main(int argc, char **argv) {
2219
ptp_eos_set_remote_mode(&r, 1);
2320
ptp_eos_set_event_mode(&r, 1);
2421

25-
// Generate JSON data in packet buffer because I'm too lazy
26-
// to allocate a buffer (should be around 1-10kb of text)
27-
int rc = ptp_get_device_info(&r, &di);
28-
if (rc) return rc;
29-
r.di = &di;
30-
ptp_device_info_json(&di, (char*)r.data, r.data_length);
31-
printf("%s\n", (char*)r.data);
32-
33-
char *input = "/home/daniel/Documents/magiclantern/modules/mlrust/mlrust.mo";
34-
3522
ptp_chdk_get_version(&r);
36-
ptp_chdk_upload_file(&r, input, "B:/ML/MODULES/MLRUST.MO");
23+
// Upload the file with varargs
24+
ptp_chdk_upload_file(&r, argv[1], argv[2]);
3725

3826
ptp_close_session(&r);
3927
ptp_device_close(&r);

examples/pktest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void print_bytes(uint8_t *bytes, int n) {
2222

2323
int main() {
2424
struct PtpRuntime r;
25-
ptp_generic_init(&r);
25+
ptp_init(&r);
2626

2727
r.connection_type = PTP_IP;
2828

examples/storage.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,42 @@
77
#include <camlib.h>
88

99
int main() {
10-
struct PtpRuntime r;
11-
ptp_generic_init(&r);
10+
struct PtpRuntime *r = ptp_new(0);
1211

1312
struct PtpDeviceInfo di;
1413

15-
if (ptp_device_init(&r)) {
14+
if (ptp_device_init(r)) {
1615
puts("Device connection error");
17-
return 0;
16+
goto cleanup;
1817
}
1918

20-
ptp_open_session(&r);
19+
ptp_open_session(r);
2120

22-
struct UintArray *arr;
23-
int rc = ptp_get_storage_ids(&r, &arr);
21+
struct PtpArray *arr;
22+
int rc = ptp_get_storage_ids(r, &arr);
23+
if (arr->length == 0) {
24+
puts("No storage devices found.");
25+
goto exit;
26+
}
2427
int id = arr->data[0];
2528

26-
rc = ptp_get_object_handles(&r, id, PTP_OF_JPEG, 0, &arr);
27-
arr = ptp_dup_uint_array(arr);
29+
rc = ptp_get_object_handles(r, id, PTP_OF_JPEG, 0, &arr);
2830

2931
for (int i = 0; i < arr->length; i++) {
3032
struct PtpObjectInfo oi;
31-
ptp_get_object_info(&r, arr->data[i], &oi);
33+
ptp_get_object_info(r, arr->data[i], &oi);
3234

3335
printf("Filename: %s\n", oi.filename);
3436
printf("File size: %u\n", oi.compressed_size);
3537
}
3638

3739
free(arr);
3840

39-
ptp_close_session(&r);
40-
ptp_device_close(&r);
41-
ptp_generic_close(&r);
41+
exit:;
42+
ptp_close_session(r);
43+
ptp_device_close(r);
44+
cleanup:;
45+
ptp_close(r);
4246
return 0;
4347
}
4448

0 commit comments

Comments
 (0)