Class: FtdiC::Context

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.newObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'ext/ftdic.c', line 16

static VALUE ftdi_context_s_new(VALUE klass)
{
  /* TODO: not exception-safe */

  struct ftdi_context * ctx = ftdi_new();

  if (ctx == 0)
  {
    rb_raise(rb_eRuntimeError, "unable to create ftdi context");
  }
 
  return Data_Wrap_Struct(rb_cFtdi_Context, 0, ftdi_free, ctx);
}

Instance Method Details

#baudrate=(v_baud) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/ftdic.c', line 82

static VALUE ftdi_context_set_baudrate(VALUE self, VALUE v_baud)
{
  struct ftdi_context * ctx;
  int baud = NUM2INT(v_baud);
  int result;

  Data_Get_Struct(self, struct ftdi_context, ctx);

  result = ftdi_set_baudrate(ctx, baud);
  check_ftdi_result(ctx, result);

  return Qnil;
}

#chipidObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'ext/ftdic.c', line 68

static VALUE ftdi_context_chipid(VALUE self)
{
  struct ftdi_context * ctx;
  int result;
  unsigned int chipid;

  Data_Get_Struct(self, struct ftdi_context, ctx);
  result = ftdi_read_chipid(ctx, &chipid);
  check_ftdi_result(ctx, result);

  return UINT2NUM(chipid);

}

#closeObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'ext/ftdic.c', line 45

static VALUE ftdi_context_close(VALUE self)
{
  struct ftdi_context * ctx;
  int result;

  Data_Get_Struct(self, struct ftdi_context, ctx);

  result = ftdi_usb_close(ctx);
  check_ftdi_result(ctx, result);

  return Qnil;
}

#open(v_desc) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'ext/ftdic.c', line 30

static VALUE ftdi_context_open(VALUE self, VALUE v_desc)
{
  struct ftdi_context * ctx;
  char const * desc;
  int result;

  Data_Get_Struct(self, struct ftdi_context, ctx);
  desc = StringValuePtr(v_desc);

  result = ftdi_usb_open_string(ctx, desc);
  check_ftdi_result(ctx, result);

  return Qnil;
}

#read_data(v_size) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'ext/ftdic.c', line 111

static VALUE ftdi_context_read_data(VALUE self, VALUE v_size)
{
  struct ftdi_context * ctx;
  int size = NUM2INT(v_size);
  VALUE v_buf = rb_str_buf_new(size);
  char * buf = StringValuePtr(v_buf);
  int result;

  Data_Get_Struct(self, struct ftdi_context, ctx);

  result = ftdi_read_data(ctx, (unsigned char *)buf, size);
  check_ftdi_result(ctx, result);

  RSTRING(v_buf)->len = result;

  return Qnil;
}

#set_bitmode(v_bitmask, v_mode) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'ext/ftdic.c', line 96

static VALUE ftdi_context_set_bitmode(VALUE self, VALUE v_bitmask, VALUE v_mode)
{
  struct ftdi_context * ctx;
  unsigned char bitmask = NUM2INT(v_bitmask);
  unsigned char mode = NUM2INT(v_mode);
  int result;

  Data_Get_Struct(self, struct ftdi_context, ctx);

  result = ftdi_set_bitmode(ctx, bitmask, mode);
  check_ftdi_result(ctx, result);

  return Qnil;
}

#typeObject



58
59
60
61
62
63
64
65
66
# File 'ext/ftdic.c', line 58

static VALUE ftdi_context_type(VALUE self)
{
  struct ftdi_context * ctx;

  Data_Get_Struct(self, struct ftdi_context, ctx);

  return INT2NUM(ctx->type);

}

#write_data(v_str) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'ext/ftdic.c', line 129

static VALUE ftdi_context_write_data(VALUE self, VALUE v_str)
{
  struct ftdi_context * ctx;
  char * buf = StringValuePtr(v_str);
  int size = RSTRING(v_str)->len;
  int result;

  Data_Get_Struct(self, struct ftdi_context, ctx);

  result = ftdi_write_data(ctx, (unsigned char *)buf, size);
  check_ftdi_result(ctx, result);

  return Qnil;
}