Class: Fubuki::SPI
- Inherits:
-
Object
- Object
- Fubuki::SPI
- Defined in:
- ext/fubuki/fubuki.c
Instance Method Summary collapse
Constructor Details
#initialize(*args) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'ext/fubuki/fubuki.c', line 41
static VALUE spi_init(int argc, VALUE* argv, VALUE self) {
VALUE bus, device;
rb_scan_args(argc, argv, "02", &bus, &device);
int bus_id = 0;
switch (TYPE(bus)) {
case T_FIXNUM:
bus_id = NUM2UINT(bus);
break;
case T_NIL:
break;
default:
rb_raise(rb_eTypeError, "not valid SPI bus id");
break;
}
int device_id = 0;
switch (TYPE(device)) {
case T_FIXNUM:
device_id = NUM2UINT(device);
break;
case T_NIL:
break;
default:
rb_raise(rb_eTypeError, "not valid SPI device id");
break;
}
char path[1000] = "";
snprintf(path, 1000, "/dev/spidev%d.%d", bus_id, device_id);
int fd = open(path, O_RDWR);
if (fd < 0) {
rb_raise(rb_eRuntimeError, "cannot open SPI device");
}
struct spi_malloc *ptr;
Data_Get_Struct(self, struct spi_malloc, ptr);
ptr->fd = fd;
return self;
}
|
Instance Method Details
#close ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'ext/fubuki/fubuki.c', line 174
static VALUE spi_close(VALUE self) {
struct spi_malloc *ptr;
Data_Get_Struct(self, struct spi_malloc, ptr);
int fd = ptr->fd;
if (fd < 0) {
rb_raise(rb_eRuntimeError, "SPI device not opened");
}
close(fd);
ptr->fd = -1;
return Qtrue;
}
|
#transfer(*args) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'ext/fubuki/fubuki.c', line 83
static VALUE spi_transfer(int argc, VALUE* argv, VALUE self) {
char error_message[1000] = "";
int i = 0;
int status = 0;
struct spi_malloc *ptr;
Data_Get_Struct(self, struct spi_malloc, ptr);
int fd = ptr->fd;
if (fd < 0) {
rb_raise(rb_eRuntimeError, "SPI device not opened");
}
VALUE tx_data, speed_hz, delay_usec;
rb_scan_args(argc, argv, "12", &tx_data, &speed_hz, &delay_usec);
if (TYPE(tx_data) != T_ARRAY) {
rb_raise(rb_eTypeError, "input data should be array of bytes");
}
int spi_speed = 1000000;
switch (TYPE(speed_hz)) {
case T_FIXNUM:
spi_speed = NUM2UINT(speed_hz);
break;
case T_NIL:
break;
default:
rb_raise(rb_eTypeError, "not valid SPI speed");
break;
}
int spi_delay = 0;
switch (TYPE(delay_usec)) {
case T_FIXNUM:
spi_delay = NUM2UINT(delay_usec);
break;
case T_NIL:
break;
default:
rb_raise(rb_eTypeError, "not valid SPI delay");
break;
}
long buf_len = RARRAY_LEN(tx_data);
uint8_t *tx_buf = calloc(buf_len, sizeof(uint8_t));
uint8_t *rx_buf = calloc(buf_len, sizeof(uint8_t));
for (i = 0; i < buf_len; i++) {
VALUE element = rb_ary_entry(tx_data, i);
if (TYPE(element) != T_FIXNUM) {
snprintf(error_message, 1000, "input data should be array of bytes");
goto cleanup_buffer;
}
tx_buf[i] = (uint8_t)NUM2UINT(element);
}
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx_buf,
.rx_buf = (unsigned long)rx_buf,
.len = buf_len,
.delay_usecs = spi_delay,
.speed_hz = spi_speed,
.bits_per_word = 8,
};
status = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (status < 1) {
snprintf(error_message, 1000, "cannot send SPI message");
goto cleanup_buffer;
}
VALUE rx_data = rb_ary_new2(buf_len);
for (i = 0; i < buf_len; i++) {
rb_ary_store(rx_data, i, UINT2NUM(rx_buf[i]));
}
cleanup_buffer:
free(tx_buf);
free(rx_buf);
if (strlen(error_message) > 0) {
rb_raise(rb_eRuntimeError, "%s", error_message);
}
return rx_data;
}
|