Module: Denko::GPIOD

Defined in:
lib/gpiod.rb,
ext/gpiod/gpiod.c

Class Method Summary collapse

Class Method Details

.close_chipObject



25
26
27
28
# File 'ext/gpiod/gpiod.c', line 25

static VALUE close_chip(VALUE self) {
  gpiod_chip_close(chip);
  return Qnil;
}

.close_line(gpio) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'ext/gpiod/gpiod.c', line 124

static VALUE close_line(VALUE self, VALUE gpio) {
  gpio_number = NUM2INT(gpio);
  validate_gpio_number(gpio_number);

  // Only try to close the line if it was opened before.
  if (lines[gpio_number] == NULL) return Qnil;

  gpiod_line_release(lines[gpio_number]);
  lines[gpio_number] = NULL;  
  return Qnil;
}

.get_value(gpio) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/gpiod/gpiod.c', line 108

static VALUE get_value(VALUE self, VALUE gpio) {
  gpio_number = NUM2INT(gpio);
  validate_gpio_number(gpio_number);
  
  return_value = gpiod_line_get_value(lines[gpio_number]);
  
  if (return_value < 0) {
    gpiod_chip_close(chip);
    VALUE error_message = rb_sprintf("libgpiod error: Could not get value from GPIO line %d", gpio_number);
    rb_raise(rb_eRuntimeError, "%s", StringValueCStr(error_message));
    return Qnil;
  }
  
  return INT2NUM(return_value);
}

.get_value_raw(gpio) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'ext/gpiod/gpiod.c', line 151

static VALUE get_value_raw(VALUE self, VALUE gpio) {
  gpio_number = NUM2INT(gpio);
  
  return_value = gpiod_line_get_value(lines[gpio_number]);
  
  if (return_value < 0) {
    gpiod_chip_close(chip);
    VALUE error_message = rb_sprintf("libgpiod error: Could not get value from GPIO line %d", gpio_number);
    rb_raise(rb_eRuntimeError, "%s", StringValueCStr(error_message));
    return Qnil;
  }
  return INT2NUM(return_value);
}

.open_chipObject



16
17
18
19
20
21
22
23
# File 'ext/gpiod/gpiod.c', line 16

static VALUE open_chip(VALUE self) {
  chip = gpiod_chip_open_by_name(GPIO_CHIP_NAME);
  if (!chip) {
    rb_raise(rb_eRuntimeError, "libgpiod error: Could not open GPIO chip");
    return Qnil;
  }
  return Qnil;
}

.open_line_input(gpio) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/gpiod/gpiod.c', line 85

static VALUE open_line_input(VALUE self, VALUE gpio) {
  gpio_number = NUM2INT(gpio);
  validate_gpio_number(gpio_number);
  
  lines[gpio_number] = gpiod_chip_get_line(chip, gpio_number);
  if (!lines[gpio_number]) {
    gpiod_chip_close(chip);
    VALUE error_message = rb_sprintf("libgpiod error: Could not get GPIO line %d", gpio_number);
    rb_raise(rb_eRuntimeError, "%s", StringValueCStr(error_message));
    return Qnil;
  }
  
  return_value = gpiod_line_request_input(lines[gpio_number], "GPIOD_RB");
  if (return_value < 0) {
    gpiod_chip_close(chip);
    VALUE error_message = rb_sprintf("libgpiod error: Could not request input for GPIO line %d", gpio_number);
    rb_raise(rb_eRuntimeError, "%s", StringValueCStr(error_message));
    return Qnil;
  }
  
  return Qnil;
}

.open_line_output(gpio) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/gpiod/gpiod.c', line 44

static VALUE open_line_output(VALUE self, VALUE gpio) {
  gpio_number = NUM2INT(gpio);
  validate_gpio_number(gpio_number);

  lines[gpio_number] = gpiod_chip_get_line(chip, gpio_number);
  if (!lines[gpio_number]) {
    gpiod_chip_close(chip);
    VALUE error_message = rb_sprintf("libgpiod error: Could not get GPIO line %d", gpio_number);
    rb_raise(rb_eRuntimeError, "%s", StringValueCStr(error_message));
    return Qnil;
  }
  
  return_value = gpiod_line_request_output(lines[gpio_number], "GPIOD_RB", 0);
  if (return_value < 0) {
    gpiod_chip_close(chip);
    VALUE error_message = rb_sprintf("libgpiod error: Could not request output for GPIO line %d", gpio_number);
    rb_raise(rb_eRuntimeError, "%s", StringValueCStr(error_message));
    return Qnil;
  }
  
  return Qnil;
}

.set_value(gpio, value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/gpiod/gpiod.c', line 67

static VALUE set_value(VALUE self, VALUE gpio, VALUE value) {
  gpio_number = NUM2INT(gpio);
  validate_gpio_number(gpio_number);
  gpio_value = NUM2INT(value);
  validate_gpio_value(gpio_value);

  return_value = gpiod_line_set_value(lines[gpio_number], gpio_value);
  
  if (return_value < 0) {
    gpiod_chip_close(chip);
    VALUE error_message = rb_sprintf("libgpiod error: Could not set value %d on GPIO line %d", gpio_value, gpio_number);
    rb_raise(rb_eRuntimeError, "%s", StringValueCStr(error_message));
    return Qnil;
  }

  return value;
}

.set_value_raw(gpio, value) ⇒ Object

These do no validation.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'ext/gpiod/gpiod.c', line 136

static VALUE set_value_raw(VALUE self, VALUE gpio, VALUE value) {
  gpio_number = NUM2INT(gpio);
  gpio_value = NUM2INT(value);

  return_value = gpiod_line_set_value(lines[gpio_number], gpio_value);
  
  if (return_value < 0) {
    gpiod_chip_close(chip);
    VALUE error_message = rb_sprintf("libgpiod error: Could not set value %d on GPIO line %d", gpio_value, gpio_number);
    rb_raise(rb_eRuntimeError, "%s", StringValueCStr(error_message));
    return Qnil;
  }
  return value;
}