rawhid 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +1 -1
- data/ext/teensy_rawhid/teensy_rawhid.c +5 -8
- data/lib/rawhid.rb +32 -0
- data/lib/rawhid/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6687a92269d84106c45e377dc80e608509ca0ded
|
4
|
+
data.tar.gz: 2b780571990dde4bad71c010a38b682918fd70c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa031a1b89150e4d4d0522c5f52971e9a61b7dfaeba58ea9fdc0afea1493297a9d2fa2fd56716ae7f31f14016b14d47e77db40a765bf34a5b14e4c4b29807ecb
|
7
|
+
data.tar.gz: df77c46ca12185600d029d3630bec5325d4139c41f877e744aff562b84b531f0c6a1e886b57d515603d010380144a5c4d03f7dbdf31e1a45a70bf5beee945019
|
data/Rakefile
CHANGED
@@ -14,7 +14,7 @@ VALUE RawHIDError;
|
|
14
14
|
VALUE RawHID_new(VALUE class, VALUE vendorId, VALUE productId) {
|
15
15
|
void *dev = NULL;
|
16
16
|
int vId = NUM2INT(vendorId);
|
17
|
-
int pId = NUM2INT(
|
17
|
+
int pId = NUM2INT(productId);
|
18
18
|
int devices_opened = rawhid_open(1, &dev, vId, pId, -1, -1);
|
19
19
|
if(devices_opened == 0 || dev == NULL) {
|
20
20
|
rb_raise(RawHIDError, "Unable to open device");
|
@@ -38,9 +38,6 @@ VALUE RawHID_send(int argc, VALUE *argv, VALUE class) {
|
|
38
38
|
return INT2NUM(0);
|
39
39
|
}
|
40
40
|
int sent = rawhid_send(dev, buf, len, timeout);
|
41
|
-
if(sent == -1) {
|
42
|
-
rb_raise(RawHIDError, "Error sending data to device");
|
43
|
-
}
|
44
41
|
return INT2NUM(sent);
|
45
42
|
}
|
46
43
|
|
@@ -66,8 +63,8 @@ VALUE RawHID_recv(int argc, VALUE *argv, VALUE class) {
|
|
66
63
|
}
|
67
64
|
int received = rawhid_recv(dev, buf, len, timeout);
|
68
65
|
|
69
|
-
if(received
|
70
|
-
|
66
|
+
if(received < 0) {
|
67
|
+
return INT2NUM(received);
|
71
68
|
}
|
72
69
|
data = rb_str_new(buf, received);
|
73
70
|
return data;
|
@@ -78,8 +75,8 @@ void Init_teensy_rawhid() {
|
|
78
75
|
RawHIDError = rb_define_class_under(RawHID, "RawHIDError", rb_eRuntimeError);
|
79
76
|
|
80
77
|
rb_define_singleton_method(RawHID, "new", RawHID_new, 2);
|
81
|
-
rb_define_method(RawHID, "
|
82
|
-
rb_define_method(RawHID, "
|
78
|
+
rb_define_method(RawHID, "raw_send", RawHID_send, -1);
|
79
|
+
rb_define_method(RawHID, "raw_recv", RawHID_recv, -1);
|
83
80
|
|
84
81
|
}
|
85
82
|
|
data/lib/rawhid.rb
CHANGED
@@ -1,2 +1,34 @@
|
|
1
1
|
require "rawhid/version"
|
2
2
|
require "teensy_rawhid"
|
3
|
+
|
4
|
+
class RawHID
|
5
|
+
class RawHIDError
|
6
|
+
def initialize(msg, error_code=nil)
|
7
|
+
@code = error_code
|
8
|
+
super msg
|
9
|
+
end
|
10
|
+
attr_accessor :code
|
11
|
+
end
|
12
|
+
private :raw_send, :raw_recv
|
13
|
+
|
14
|
+
def send(data, timeout=0)
|
15
|
+
if Array === data
|
16
|
+
data = data.map(&:chr).join
|
17
|
+
end
|
18
|
+
ret = raw_send(data, timeout)
|
19
|
+
if ret > 0
|
20
|
+
ret
|
21
|
+
else
|
22
|
+
raise RawHIDError.new("Failed sending data", ret)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def recv(len, timeout)
|
27
|
+
ret = raw_recv(len, timeout)
|
28
|
+
if String === data
|
29
|
+
return ret
|
30
|
+
else
|
31
|
+
raise RawHIDError.new("Failed receiving data", ret)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/rawhid/version.rb
CHANGED