Class: Pin

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

Instance Method Summary collapse

Constructor Details

#initialize(pin, mode = Input) ⇒ Pin

Returns a new Pin object with default value of 0

Parameters:

  • pin (Integer)

    the beaglebone pin, identified by header (P8 or P9) and number (1-46) (ie. P9_31)

  • mode (Integer)

    the pin mode to be used (Input or Output) (default is Input)



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
# File 'ext/bonekit/pin_class.c', line 62

static VALUE Pin_initialize(int argc, VALUE* argv, VALUE self)
{
  if (argc > 2 || argc == 0)  // there should only be 1 or 2 arguments
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
  
  pin_t * ptr;
  Data_Get_Struct(self, pin_t, ptr);
  
  if(Beaglebone_global_const_check_type(argv[0]) == 0)
    rb_raise(rb_eArgError, "wrong pin argument type Fixnum (expected BeaglebonePin)");
  
  beaglebone_t * bp_ptr = (beaglebone_t *)Beaglebone_global_const_to_beaglebone_t(argv[0]);
  beaglebone_t bp;
  memcpy(&bp, bp_ptr, sizeof(beaglebone_t));
  
  if(pin_init(ptr, bp) < 0) // pin must support gpio, ain, pwm
    rb_raise(rb_eArgError, "invalid pin (%d GPIO not supported)", NUM2UINT(argv[0]));
  
  int mode = INPUT;
  
  if(argc == 2)
    mode = NUM2INT(argv[1]);

  pin_set_mode(ptr, mode);
  
  return self;
}

Instance Method Details

#analog_valueFloat

Reads the analog value from the pin. The range of values is [0.0..1.0]. NOTE: If the pin doesn’t support ADC, analog_value is the floating point equivalent of value. If the pin has been previously used to set analog values (PWM) its returns the last analog value set.

Returns:

  • (Float)


113
114
115
116
117
118
119
# File 'ext/bonekit/pin_class.c', line 113

static VALUE Pin_analog_value(VALUE self)
{
  pin_t * ptr;
  Data_Get_Struct(self, pin_t, ptr);
  
  return rb_float_new(pin_analog_value(ptr));
}

#analog_value=(value) ⇒ Float

Write the analog value to the pin. The pin must support Pulse-Width Modulation (PWM). Possible values: 0.0 to 1.0

Returns:

  • (Float)


144
145
146
147
148
149
150
151
# File 'ext/bonekit/pin_class.c', line 144

static VALUE Pin_set_analog_value(VALUE self, VALUE value)
{
  pin_t * ptr;
  Data_Get_Struct(self, pin_t, ptr);
  pin_set_analog_value(ptr, NUM2DBL(value));
  
  return value;
}

#modeInteger

Returns the mode of the pin. Possible modes: Input, Output.

Returns:

  • (Integer)


160
161
162
163
164
165
# File 'ext/bonekit/pin_class.c', line 160

static VALUE Pin_mode(VALUE self)
{
  pin_t * ptr;
  Data_Get_Struct(self, pin_t, ptr);
  return INT2NUM(pin_mode(ptr));
}

#mode=(mode) ⇒ Integer

Sets the mode of the pin. Possible modes: Input, Output.

Returns:

  • (Integer)


174
175
176
177
178
179
180
181
# File 'ext/bonekit/pin_class.c', line 174

static VALUE Pin_set_mode(VALUE self, VALUE value)
{
  pin_t * ptr;
  Data_Get_Struct(self, pin_t, ptr);
  pin_set_mode(ptr, NUM2INT(value));
  
  return value;
}

#valueInteger

Reads the value from the pin. The values are [0,1].

Returns:

  • (Integer)


96
97
98
99
100
101
102
# File 'ext/bonekit/pin_class.c', line 96

static VALUE Pin_value(VALUE self)
{
  pin_t * ptr;
  Data_Get_Struct(self, pin_t, ptr);
  
  return INT2NUM(pin_value(ptr));
}

#value=(value) ⇒ Integer

Write the value to the pin. The pin mode must be set to Output. Possible values: High, Low.

Returns:

  • (Integer)


128
129
130
131
132
133
134
135
# File 'ext/bonekit/pin_class.c', line 128

static VALUE Pin_set_value(VALUE self, VALUE value)
{
  pin_t * ptr;
  Data_Get_Struct(self, pin_t, ptr);
  pin_set_value(ptr, NUM2INT(value));
  
  return value;
}