forked from srwalter/libsmartpen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pysmartpen.pyx
87 lines (70 loc) · 2.63 KB
/
pysmartpen.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
cdef extern from "smartpen.h" nogil:
ctypedef struct obex_t
ctypedef struct FILE
obex_t *smartpen_connect(short vendor, short product)
char *smartpen_get_changelist(obex_t *handle, int start_time)
void smartpen_disconnect (obex_t *handle)
int smartpen_get_guid (obex_t *handle, FILE *out, char *guid,
long long int start_time)
int smartpen_get_paperreplay (obex_t *handle, FILE *out,
long long int start_time)
int smartpen_get_penletlist (obex_t *handle, FILE *out)
char *smartpen_get_peninfo (obex_t *handle)
cdef extern from "stdio.h":
FILE *fopen(char *fn, char *mode)
void fclose(FILE *f)
cdef class Smartpen:
cdef obex_t *obex
def __cinit__(self):
self.obex = NULL
def connect(self, vendor=0x1cfb, product=0x1030):
if self.obex != NULL:
raise RuntimeError("Already connected")
self.obex = smartpen_connect(vendor, product)
if (self.obex == NULL):
raise RuntimeError("Failed to connect")
def get_changelist(self, start_time=0):
cdef char *list
if (self.obex == NULL):
raise RuntimeError("Not connected")
list = smartpen_get_changelist(self.obex, start_time)
if list != NULL:
return list
raise RuntimeError("Failed to get changelist")
def get_guid(self, filename, guid, start_time=0):
cdef FILE *f
cdef int rc
if (self.obex == NULL):
raise RuntimeError("Not connected")
filename_bytes = bytes(filename, "utf-8")
f = fopen(filename_bytes, "w")
if (f == NULL):
raise IOError("Failed to open %s" % filename)
guid_bytes = bytes(guid, "utf-8")
rc = smartpen_get_guid(self.obex, f, guid_bytes, start_time)
fclose(f)
return rc
def get_paperreplay(self, filename, start_time=0):
cdef FILE *f
cdef int rc
cdef long long int c_time
c_time = start_time
if (self.obex == NULL):
raise RuntimeError("Not connected")
filename_bytes = bytes(filename, "utf-8")
f = fopen(filename_bytes, "w")
if (f == NULL):
raise IOError("Failed to open %s" % filename)
with nogil:
rc = smartpen_get_paperreplay(self.obex, f, c_time)
fclose(f)
return rc
def get_info(self):
if (self.obex == NULL):
raise RuntimeError("Not connected")
return smartpen_get_peninfo(self.obex)
def disconnect(self):
if (self.obex == NULL):
raise RuntimeError("Not connected")
smartpen_disconnect(self.obex)
self.obex = NULL