-
Notifications
You must be signed in to change notification settings - Fork 1
/
vserialport.v
743 lines (632 loc) · 17.2 KB
/
vserialport.v
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// Copyright(C) 2021 Erdet Nasufi. All rights reserved.
module vserialport
#pkgconfig --libs --cflags libserialport
#include <libserialport.h>
#flag -I /usr/include
#flag -l serialport
pub enum Return {
ok = C.SP_OK
invalid_arguments = C.SP_ERR_ARG
fail = C.SP_ERR_FAIL
memory_error = C.SP_ERR_MEM
not_supported = C.SP_ERR_SUPP
}
pub enum Mode {
read = C.SP_MODE_READ
write = C.SP_MODE_WRITE
read_write = C.SP_MODE_READ_WRITE
}
pub enum Event {
received_ready = C.SP_EVENT_RX_READY
transmit_ready = C.SP_EVENT_TX_READY
event_error = C.SP_EVENT_ERROR
}
pub enum Buffer {
input = C.SP_BUF_INPUT
output = C.SP_BUF_OUTPUT
both = C.SP_BUF_BOTH
}
pub enum Parity {
invalid = C.SP_PARITY_INVALID
@none = C.SP_PARITY_NONE
odd = C.SP_PARITY_ODD
event = C.SP_PARITY_EVEN
mark = C.SP_PARITY_MARK
space = C.SP_PARITY_SPACE
}
pub enum Rts {
invalid = C.SP_RTS_INVALID
off = C.SP_RTS_OFF
on = C.SP_RTS_ON
flow_control = C.SP_RTS_FLOW_CONTROL
}
pub enum Cts {
invalide = C.SP_CTS_INVALID
ignore = C.SP_CTS_IGNORE
flow_control = C.SP_CTS_FLOW_CONTROL
}
pub enum Dtr {
invalide = C.SP_DTR_INVALID
off = C.SP_DTR_OFF
on = C.SP_DTR_ON
flow_control = C.SP_DTR_FLOW_CONTROL
}
pub enum Dsr {
invalide = C.SP_DSR_INVALID
ignore = C.SP_DSR_IGNORE
flow_control = C.SP_DSR_FLOW_CONTROL
}
pub enum XonXoff {
invalide = C.SP_XONXOFF_INVALID
disabled = C.SP_XONXOFF_DISABLED
@in = C.SP_XONXOFF_IN
out = C.SP_XONXOFF_OUT
inout = C.SP_XONXOFF_INOUT
}
pub enum FlowControl {
@none = C.SP_FLOWCONTROL_NONE
xon_xoff = C.SP_FLOWCONTROL_XONXOFF
rts_cts = C.SP_FLOWCONTROL_RTSCTS
dtr_dsr = C.SP_FLOWCONTROL_DTRDSR
}
pub enum Signal {
cts = C.SP_SIG_CTS
dsr = C.SP_SIG_DSR
dcd = C.SP_SIG_DCD
ri = C.SP_SIG_RI
}
pub enum Transport {
native = C.SP_TRANSPORT_NATIVE
usb = C.SP_TRANSPORT_USB
bluetooth = C.SP_TRANSPORT_BLUETOOTH
}
struct C.sp_port{}
pub struct Port {
mut:
port_name string
ptr &C.sp_port
is_connected bool
}
fn C.sp_get_port_by_name(portname &char, port_ptr &&C.sp_port) Return
pub fn new_port(port_name string) ?Port {
mut this := Port{
ptr: voidptr(0)
}
this.port_name = port_name
cfn := &char(port_name.str)
rc := unsafe {
C.sp_get_port_by_name(cfn, &(this.ptr))
}
if rc != Return(C.SP_OK) {
return error('Failed to create new Port by name \'${port_name}\'. Error: ${int(rc)}')
}
return this
}
fn C.sp_free_port(&C.sp_port)
pub fn (mut this Port)free() {
C.sp_free_port(this.ptr)
}
fn C.sp_open(port &C.sp_port, flags Mode) Return
pub fn (mut this Port)open(flags Mode) bool {
rc := C.sp_open(this.ptr, Mode(flags))
this.is_connected = rc == Return(C.SP_OK)
return this.is_connected
}
pub fn (this Port)is_connected() bool {
return this.is_connected
}
fn C.sp_close(port &C.sp_port) Return
pub fn (this Port)close() bool {
rc := C.sp_close(this.ptr)
return rc == Return(C.SP_OK)
}
fn C.sp_get_port_name(port &C.sp_port) &char
pub fn (this Port)name() string {
cpn := unsafe {
cstring_to_vstring(C.sp_get_port_name(this.ptr))
}
return cpn
}
fn C.sp_get_port_description(port &C.sp_port) &char
pub fn (this Port)description() string {
cpd := unsafe {
cstring_to_vstring(C.sp_get_port_description(this.ptr))
}
return cpd
}
fn C.sp_get_port_transport(port &C.sp_port) Transport
pub fn (this Port)transport() Transport {
pt := Transport(C.sp_get_port_transport(this.ptr))
return pt
}
pub struct UsbBus {
usb_bus int
usb_address int
}
pub fn (this UsbBus)str() string {
ub := this.usb_bus
ua := this.usb_address
str := '{usb_bus: ${ub}, usb_address: ${ua}}'
return str
}
fn C.sp_get_port_usb_bus_address(port &C.sp_port, usb_bus &int, usb_address &int) Return
pub fn (this Port)usb_bus() UsbBus {
ub := int(0)
ua := int(0)
rc := unsafe {
C.sp_get_port_usb_bus_address(this.ptr, &ub, &ua)
}
if rc != Return(C.SP_OK) {
return UsbBus{0, 0}
}
return UsbBus{
usb_bus: ub,
usb_address: ua
}
}
pub struct UsbID {
vendor_id int
product_id int
}
pub fn (this UsbID)str() string {
vid := this.vendor_id
pid := this.product_id
str := '{vendor_id: 0x${vid.hex()}, product_id = 0x${pid.hex()}}'
return str
}
fn C.sp_get_port_usb_vid_pid(port &C.sp_port, usb_vid &int, usb_pid &int) Return
pub fn (this Port)usb_id() UsbID {
vid := int(0)
pid := int(0)
rc := unsafe {
C.sp_get_port_usb_vid_pid(this.ptr, &vid, &pid)
}
if rc != Return(C.SP_OK) {
return UsbID{0, 0}
}
return UsbID{
vendor_id: vid
product_id: pid
}
}
fn C.sp_get_port_usb_manufacturer(port &C.sp_port) &char
pub fn (this Port)usb_manufacturer() string {
cstr := unsafe {
cstring_to_vstring(C.sp_get_port_usb_manufacturer(this.ptr))
}
return cstr
}
fn C.sp_get_port_usb_product(port &C.sp_port) &char
pub fn (this Port)usb_product() string {
cstr := unsafe {
cstring_to_vstring(C.sp_get_port_usb_product(this.ptr))
}
return cstr
}
fn C.sp_get_port_usb_serial(port &C.sp_port) &char
pub fn (this Port)usb_serial_number() string {
cstr := unsafe {
cstring_to_vstring(C.sp_get_port_usb_serial(this.ptr))
}
return cstr
}
fn C.sp_get_port_bluetooth_address(port &C.sp_port) &char
pub fn (this Port)bluetooth_address() string {
cstr := unsafe {
cstring_to_vstring(C.sp_get_port_bluetooth_address(this.ptr))
}
return cstr
}
fn C.sp_set_baudrate(port &C.sp_port, baudrate int) Return
pub fn (mut this Port)set_baudrate(baudrate int) bool {
rc := C.sp_set_baudrate(this.ptr, baudrate)
return rc == Return(C.SP_OK)
}
fn C.sp_set_bits(port &C.sp_port, bits int) Return
pub fn (this Port)set_bits(bits int) bool {
rc := C.sp_set_bits(this.ptr, bits)
return rc == Return(C.SP_OK)
}
fn C.sp_set_parity(port &C.sp_port, parity Parity) Return
pub fn (this Port)set_parity(parity Parity) bool {
rc := C.sp_set_parity(this.ptr, Parity(parity))
return rc == Return(C.SP_OK)
}
fn C.sp_set_stopbits(port &C.sp_port, stopbits int) Return
pub fn (this Port)set_stopbits(stopbits int) bool {
rc := C.sp_set_stopbits(this.ptr, stopbits)
return rc == Return(C.SP_OK)
}
fn C.sp_set_rts(port &C.sp_port, rts Rts) Return
pub fn (this Port)set_rts(rts Rts) bool {
rc := C.sp_set_rts(this.ptr, Rts(rts))
return rc == Return(C.SP_OK)
}
fn C.sp_set_cts(port &C.sp_port, cts Cts) Return
pub fn (this Port)set_cts(cts Cts) bool {
rc := C.sp_set_cts(this.ptr, Cts(cts))
return rc == Return(C.SP_OK)
}
fn C.sp_set_dtr(port &C.sp_port, dtr Dtr) Return
pub fn (this Port)set_dtr(dtr Dtr) bool {
rc := C.sp_set_dtr(this.ptr, Dtr(dtr))
return rc == Return(C.SP_OK)
}
fn C.sp_set_dsr(port &C.sp_port, dsr Dsr) Return
pub fn (this Port)set_dsr(dsr Dsr) bool {
rc := C.sp_set_dsr(this.ptr, Dsr(dsr))
return rc == Return(C.SP_OK)
}
fn C.sp_set_xon_xoff(port &C.sp_port, xon_xoff XonXoff) Return
pub fn (this Port)set_xon_xoff(xon_xoff XonXoff) bool {
rc := C.sp_set_xon_xoff(this.ptr, XonXoff(xon_xoff))
return rc == Return(C.SP_OK)
}
fn C.sp_set_flowcontrol(port &C.sp_port, flowcontrol FlowControl) Return
pub fn (this Port)set_flowcontrol(flow_control FlowControl) bool {
rc := C.sp_set_flowcontrol(this.ptr, FlowControl(flow_control))
return rc == Return(C.SP_OK)
}
pub enum ReaderMode{
blocking = 0
next = 1
nonblocking = 2
}
fn C.sp_blocking_read(port &C.sp_port, buf voidptr, count C.size_t, timeout_ms u32) Return
fn C.sp_blocking_read_next(port &C.sp_port, buf voidptr, count C.size_t, timeout_ms u32) Return
fn C.sp_nonblocking_read(port &C.sp_port, buf voidptr, count C.size_t) Return
pub fn (this Port)read(mode ReaderMode, max_length u32, timeout_ms u32) []byte {
mut rc := int(0)
if max_length == 0 {
return []byte{}
}
mut buff := []byte{len: int(max_length), cap: int(max_length)}
match mode {
.blocking {
rc = unsafe {
int(C.sp_blocking_read(this.ptr, &buff[0], C.size_t(max_length), timeout_ms))
}
if rc < 0 {
return []byte{}
}
return buff
}
.next {
rc = unsafe {
int(C.sp_blocking_read_next(this.ptr, &buff[0], C.size_t(max_length), timeout_ms))
}
if rc < 0 {
return []byte{}
}
return buff
}
.nonblocking {
rc = unsafe {
int(C.sp_nonblocking_read(this.ptr, &buff[0], C.size_t(max_length)))
}
if rc < 0 {
return []byte{}
}
return buff
}
}
return []byte{}
}
fn C.sp_blocking_write(port &C.sp_port, buf voidptr, count C.size_t, timeout_ms u32) Return
fn C.sp_nonblocking_write(port &C.sp_port, buf voidptr, count C.size_t) Return
pub fn (this Port)write(block bool, buffer []byte, timeout_ms u32) ?int {
mut rc := int(0)
if buffer.len == 0 {
return error('Error: empty buffer.')
}
if block == true {
rc = unsafe {
int(C.sp_blocking_write(this.ptr, &buffer[0], C.size_t(buffer.len), timeout_ms))
}
if rc < 0 {
return error('Error ${rc}. Failed to write.')
}
return rc
}
rc = unsafe {
int(C.sp_nonblocking_write(this.ptr, &buffer[0], C.size_t(buffer.len)))
}
if rc < 0 {
return error('Error ${rc}. Failed to write.')
}
return rc
}
fn C.sp_input_waiting(port &C.sp_port) Return
pub fn (this Port)bytes_to_read() int {
return int(C.sp_input_waiting(this.ptr))
}
fn C.sp_output_waiting(port &C.sp_port) Return
pub fn (this Port)bytes_to_write() int {
return int(C.sp_output_waiting(this.ptr))
}
fn C.sp_flush(port &C.sp_port, buffers Buffer) Return
pub fn (this Port)flush(buffer Buffer) bool {
rc := C.sp_flush(this.ptr, Buffer(buffer))
return rc == Return(C.SP_OK)
}
fn C.sp_drain(port &C.sp_port) Return
pub fn (this Port)drain() bool {
rc := C.sp_drain(this.ptr)
return rc == Return(C.SP_OK)
}
fn C.sp_get_signals(port &C.sp_port, signal_mask &Signal) Return
pub fn (this Port)signal() ?Signal{
csig := Signal(0)
rc := unsafe {
C.sp_get_signals(this.ptr, &csig)
}
if rc != Return(C.SP_OK) {
err_rc := int(rc)
return error('Error ${err_rc}. Failed to get signals.')
}
return Signal(csig)
}
fn C.sp_start_break(port &C.sp_port) Return
pub fn (this Port)start_break() bool {
rc := C.sp_start_break(this.ptr)
return rc == Return(C.SP_OK)
}
fn C.sp_end_break(port &C.sp_port) Return
pub fn (this Port)end_break() bool {
rc := C.sp_end_break(this.ptr)
return rc == Return(C.SP_OK)
}
//
// Error handling
//
fn C.sp_last_error_code() int
pub fn error_code() int {
return C.sp_last_error_code()
}
fn C.sp_last_error_message() &char
pub fn error_message() string {
cstr := unsafe {
cstring_to_vstring(C.sp_last_error_message())
}
return cstr
}
fn C.sp_free_error_message(message &char)
//
// Version
//
fn C.sp_get_major_package_version() int
pub fn major_version() int {
return C.sp_get_major_package_version()
}
fn C.sp_get_minor_package_version() int
pub fn minor_version() int {
return C.sp_get_minor_package_version()
}
fn C.sp_get_micro_package_version() int
pub fn micro_version() int {
return C.sp_get_micro_package_version()
}
fn C.sp_get_package_version_string() &char
pub fn package_version() string {
cstr := unsafe {
cstring_to_vstring(C.sp_get_package_version_string())
}
return cstr
}
fn C.sp_get_current_lib_version() int
[inline]
pub fn current_lib_version() int {
return C.sp_get_current_lib_version()
}
fn C.sp_get_revision_lib_version() int
[inline]
pub fn revision_lib_version() int {
return C.sp_get_revision_lib_version()
}
fn C.sp_get_age_lib_version() int
[inline]
pub fn age_lib_version() int {
return C.sp_get_age_lib_version()
}
fn C.sp_get_lib_version_string() &char
pub fn version() string {
mut cstr := unsafe {
cstring_to_vstring(C.sp_get_lib_version_string())
}
cstr = "libserialport: " + cstr
return cstr
}
//
// PortConfiguration
//
struct C.sp_port_config{}
pub struct Configuration{
ptr &C.sp_port_config
}
fn C.sp_new_config(&&C.sp_port_config) Return
pub fn new_configuration() ?Configuration {
this := Configuration{
ptr: voidptr(0)
}
rc := unsafe {
C.sp_new_config(&this.ptr)
}
if rc != Return(C.SP_OK) {
return error('Failed to create new serial port Configuration')
}
return this
}
fn C.sp_free_config(&C.sp_port_config)
pub fn (mut this Configuration)free() {
C.sp_free_config(this.ptr)
}
fn C.sp_get_config(port &C.sp_port, config &C.sp_port_config) Return
pub fn (this Configuration)get(port Port) bool {
rc := C.sp_get_config(port.ptr, this.ptr)
return rc == Return(C.SP_OK)
}
fn C.sp_set_config(port &C.sp_port, config &C.sp_port_config) Return
pub fn (this Configuration)set(port Port) bool {
rc := C.sp_set_config(port.ptr, this.ptr)
return rc == Return(C.SP_OK)
}
fn C.sp_get_config_baudrate(config &C.sp_port_config, baudrate_ptr &int) Return
pub fn (this Configuration)baudrate() int {
br := int(0)
unsafe {
C.sp_get_config_baudrate(this.ptr, &br)
}
return br
}
fn C.sp_set_config_baudrate(config &C.sp_port_config, baudrate int) Return
pub fn (this Configuration)set_baudrate(baudrate int) bool {
rc := C.sp_set_config_baudrate(this.ptr, baudrate)
return rc == Return(C.SP_OK)
}
fn C.sp_get_config_bits(config &C.sp_port_config, bits_ptr &int) Return
pub fn (this Configuration)bits() int {
bits := int(0)
unsafe {
C.sp_get_config_bits(this.ptr, &bits)
}
return bits
}
fn C.sp_set_config_bits(config &C.sp_port_config, bits int) Return
pub fn (this Configuration)set_bits(bits int) bool {
rc := C.sp_set_config_bits(this.ptr, bits)
return rc == Return(C.SP_OK)
}
fn C.sp_get_config_parity(config &C.sp_port_config, parity_ptr &Parity) Return
pub fn (this Configuration)parity() Parity {
prt := Parity(0)
unsafe {
C.sp_get_config_parity(this.ptr, &prt)
}
return Parity(prt)
}
fn C.sp_set_config_parity(config &C.sp_port_config, parity Parity) Return
pub fn (this Configuration)set_parity(parity Parity) bool {
rc := C.sp_set_config_parity(this.ptr, Parity(parity))
return rc == Return(C.SP_OK)
}
fn C.sp_get_config_stopbits(config &C.sp_port_config, stopbits_ptr &int) Return
pub fn (this Configuration)stopbits() int {
sb := int(0)
unsafe {
C.sp_get_config_stopbits(this.ptr, &sb)
}
return sb
}
fn C.sp_set_config_stopbits(config &C.sp_port_config, stopbits int) Return
pub fn (this Configuration)set_stopbits(stopbits int) bool {
rc := C.sp_set_config_stopbits(this.ptr, stopbits)
return rc == Return(C.SP_OK)
}
fn C.sp_get_config_rts(config &C.sp_port_config, rts_ptr &Rts) Return
pub fn (this Configuration)rts() Rts {
rts := Rts(0)
unsafe {
C.sp_get_config_rts(this.ptr, &rts)
}
return Rts(rts)
}
fn C.sp_set_config_rts(config &C.sp_port_config, rts Rts) Return
pub fn (this Configuration)set_rts(rts Rts) bool {
rc := C.sp_set_config_rts(this.ptr, Rts(rts))
return rc == Return(C.SP_OK)
}
fn C.sp_get_config_cts(config &C.sp_port_config, cts_ptr &Cts) Return
pub fn (this Configuration)cts() Cts {
cts := Cts(0)
unsafe {
C.sp_get_config_cts(this.ptr, &cts)
}
return Cts(cts)
}
fn C.sp_set_config_cts(config &C.sp_port_config, cts Cts) Return
pub fn (this Configuration)set_cts(cts Cts) bool {
rc := C.sp_set_config_cts(this.ptr, Cts(cts))
return rc == Return(C.SP_OK)
}
fn C.sp_get_config_dsr(config &C.sp_port_config, dsr_ptr &Dsr) Return
pub fn (this Configuration)dsr() Dsr {
dsr := Dsr(0)
unsafe {
C.sp_get_config_dsr(this.ptr, &dsr)
}
return Dsr(dsr)
}
fn C.sp_set_config_dsr(config &C.sp_port_config, dsr Dsr) Return
pub fn (this Configuration)set_dsr(dsr Dsr) bool {
rc := C.sp_set_config_dsr(this.ptr, Dsr(dsr))
return rc == Return(C.SP_OK)
}
fn C.sp_get_config_dtr(config &C.sp_port_config, dtr_ptr &Dtr) Return
pub fn (this Configuration)dtr() Dtr {
dtr := Dtr(0)
unsafe {
C.sp_get_config_dtr(this.ptr, &dtr)
}
return Dtr(dtr)
}
fn C.sp_set_config_dtr(config &C.sp_port_config, dtr Dtr) Return
pub fn (this Configuration)set_dtr(dtr Dtr) bool {
rc := C.sp_set_config_dtr(this.ptr, Dtr(dtr))
return rc == Return(C.SP_OK)
}
fn C.sp_get_config_xon_xoff(config &C.sp_port_config, xon_xoff_ptr &XonXoff) Return
pub fn (this Configuration)xon_xoff() XonXoff {
xx := XonXoff(0)
unsafe {
C.sp_get_config_xon_xoff(this.ptr, &xx)
}
return XonXoff(xx)
}
fn C.sp_set_config_xon_xoff(config &C.sp_port_config, xon_xoff &XonXoff) Return
pub fn (this Configuration)set_xon_xoff(xon_xoff XonXoff) bool {
rc := C.sp_set_config_xon_xoff(this.ptr, XonXoff(xon_xoff))
return rc == Return(C.SP_OK)
}
fn C.sp_set_config_flowcontrol(config &C.sp_port_config, flowcontrol FlowControl) Return
pub fn (this Configuration)set_flowcontrol(flow_control FlowControl) bool {
rc := C.sp_set_config_flowcontrol(this.ptr, FlowControl(flow_control))
return rc == Return(C.SP_OK)
}
//
// EventSet
//
struct C.sp_event_set {
handles voidptr
masks &C.Event
count u32
}
pub struct EventSet {
ptr &C.sp_event_set
}
fn C.sp_new_event_set(result_ptr &&C.sp_event_set) Return
pub fn new_event_set() ?EventSet{
mut this := EventSet{
ptr: voidptr(0)
}
rc := unsafe {
C.sp_new_event_set(&this.ptr)
}
if rc != Return(C.SP_OK) {
return error('Error ${int(rc)}. Failed to create new EventSet.')
}
return this
}
fn C.sp_add_port_events(event_set &C.sp_event_set, port &C.sp_port, mask Event) Return
pub fn (this EventSet)add_port_events(port Port, event Event) bool {
rc := C.sp_add_port_events(this.ptr, port.ptr, Event(event))
return rc == Return(C.SP_OK)
}
fn C.sp_wait(event_set &C.sp_event_set, timeout_ms u32) Return
pub fn (this EventSet)wait(timeout_ms u32) bool {
rc := C.sp_wait(this.ptr, timeout_ms)
return rc == Return(C.SP_OK)
}
fn C.sp_free_event_set(event_set &C.sp_event_set)
pub fn (mut this EventSet)free() {
C.sp_free_event_set(this.ptr)
}