Class: MyHIDAPI::Handle

Inherits:
Object
  • Object
show all
Defined in:
ext/myhidapi/myhidapi.c

Instance Method Summary collapse

Instance Method Details

#read(size) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'ext/myhidapi/myhidapi.c', line 122

static VALUE
rb_hid_read(VALUE self, VALUE size)
{
    hid_device *handle;
    unsigned char * buf;
    int read;
    VALUE ret;

    TypedData_Get_Struct(self, hid_device, &myhidapi_handle_type, handle);

    buf = xcalloc(NUM2SIZET(size), sizeof(unsigned char));

    read = hid_read(handle, buf, NUM2SIZET(size));

    if (read > 0) {
  ret = rb_str_new((char *)buf, read);
    } else {
  ret = Qnil;
    }
    xfree(buf);
    return ret;
}

#read_timeout(size, timeout_ms) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'ext/myhidapi/myhidapi.c', line 145

static VALUE
rb_hid_read_timeout(VALUE self, VALUE size, VALUE timeout_ms)
{
    hid_device *handle;
    unsigned char * buf;
    int read;
    VALUE ret;

    TypedData_Get_Struct(self, hid_device, &myhidapi_handle_type, handle);

    buf = xcalloc(NUM2SIZET(size), sizeof(unsigned char));

    read = hid_read_timeout(handle, buf, NUM2SIZET(size), NUM2INT(timeout_ms));

    if (read > 0) {
  ret = rb_str_new((char *)buf, read);
    } else {
  ret = Qnil;
    }
    xfree(buf);
    return ret;
}

#set_nonblocking(val) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'ext/myhidapi/myhidapi.c', line 110

static VALUE
rb_hid_set_nonblocking(VALUE self, VALUE val)
{
    hid_device *handle;
    TypedData_Get_Struct(self, hid_device, &myhidapi_handle_type, handle);
    if (!hid_set_nonblocking(handle, NUM2INT(val))) {
  return Qtrue;
    } else {
  return Qnil;
    }
}

#write(str) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'ext/myhidapi/myhidapi.c', line 93

static VALUE
rb_hid_write(VALUE self, VALUE str)
{
    hid_device *handle;
    int written;

    TypedData_Get_Struct(self, hid_device, &myhidapi_handle_type, handle);

    written = hid_write(handle, (unsigned char *)StringValuePtr(str), RSTRING_LEN(str));

    if (written >= 0) {
  return INT2NUM(written);
    } else {
  return Qfalse;
    }
}