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)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'ext/bonekit/pin_class.c', line 61

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(pin_init(ptr, NUM2UINT(argv[0])) < 0) // pin must support gpio
    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.

Returns:

  • (Float)


103
104
105
106
107
108
109
# File 'ext/bonekit/pin_class.c', line 103

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));
}

#modeInteger

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

Returns:

  • (Integer)


135
136
137
138
139
140
# File 'ext/bonekit/pin_class.c', line 135

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)


149
150
151
152
153
154
155
156
# File 'ext/bonekit/pin_class.c', line 149

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)


88
89
90
91
92
93
94
# File 'ext/bonekit/pin_class.c', line 88

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)


119
120
121
122
123
124
125
126
# File 'ext/bonekit/pin_class.c', line 119

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;
}