parent
12b520e787
commit
7f0aca015e
|
@ -0,0 +1,5 @@
|
|||
MRuby::Gem::Specification.new('machine-adc') do |spec|
|
||||
spec.license = 'MIT'
|
||||
spec.author = 'imi415'
|
||||
spec.summary = 'Analog to digital converter access'
|
||||
end
|
|
@ -0,0 +1,66 @@
|
|||
#include <mruby.h>
|
||||
#include <mruby/class.h>
|
||||
#include <mruby/data.h>
|
||||
#include <mruby/hash.h>
|
||||
|
||||
/* Private */
|
||||
#include "adc.h"
|
||||
|
||||
typedef struct {
|
||||
uint32_t pin;
|
||||
} mrb_adc_t;
|
||||
|
||||
static void mrb_adc_free(mrb_state *mrb, void *ptr);
|
||||
|
||||
const struct mrb_data_type mrb_adc_type = {
|
||||
.struct_name = "ADC",
|
||||
.dfree = mrb_adc_free,
|
||||
};
|
||||
|
||||
static void mrb_adc_free(mrb_state *mrb, void *ptr) {
|
||||
mrb_free(mrb, ptr);
|
||||
}
|
||||
|
||||
static mrb_value mrb_adc_initialize(mrb_state *mrb, mrb_value self) {
|
||||
mrb_value init_cfg = mrb_get_arg1(mrb);
|
||||
mrb_ensure_hash_type(mrb, init_cfg);
|
||||
|
||||
mrb_value pin_num = mrb_hash_get(mrb, init_cfg, mrb_symbol_value(mrb_intern_static(mrb, "pin", 3)));
|
||||
mrb_ensure_integer_type(mrb, pin_num);
|
||||
|
||||
mrb_adc_t *analog_pin = mrb_malloc(mrb, sizeof(mrb_adc_t));
|
||||
|
||||
analog_pin->pin = mrb_integer(pin_num);
|
||||
|
||||
mrb_machine_adc_impl_config(analog_pin->pin);
|
||||
|
||||
DATA_PTR(self) = analog_pin;
|
||||
DATA_TYPE(self) = &mrb_adc_type;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
static mrb_value mrb_adc_value(mrb_state *mrb, mrb_value self) {
|
||||
mrb_adc_t *analog_pin = mrb_data_get_ptr(mrb, self, &mrb_adc_type);
|
||||
if (analog_pin == NULL) {
|
||||
mrb_raise(mrb, E_RUNTIME_ERROR, "Uninitialized ADC struct");
|
||||
}
|
||||
|
||||
uint32_t adc_uv = mrb_machine_adc_impl_read(analog_pin->pin);
|
||||
|
||||
return mrb_int_value(mrb, adc_uv);
|
||||
}
|
||||
|
||||
void mrb_machine_adc_gem_init(mrb_state *mrb) {
|
||||
struct RClass *module_machine = mrb_define_module(mrb, "Machine");
|
||||
struct RClass *class_adc = mrb_define_class_under(mrb, module_machine, "ADC", mrb->object_class);
|
||||
|
||||
MRB_SET_INSTANCE_TT(class_adc, MRB_TT_CDATA);
|
||||
|
||||
mrb_define_method(mrb, class_adc, "initialize", mrb_adc_initialize, MRB_ARGS_ARG(1, 0));
|
||||
mrb_define_method(mrb, class_adc, "value", mrb_adc_value, MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
void mrb_machine_adc_gem_final(mrb_state *mrb) {
|
||||
/* Unused */
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef MRBGEMS_MACHINE_ADC_ADC_H
|
||||
#define MRBGEMS_MACHINE_ADC_ADC_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
int mrb_machine_adc_impl_config(uint32_t pin);
|
||||
int mrb_machine_adc_impl_read(uint32_t pin);
|
||||
|
||||
#endif // MRBGEMS_MACHINE_ADC_ADC_H
|
|
@ -62,7 +62,6 @@ static mrb_value mrb_gpio_initialize(mrb_state *mrb, mrb_value self) {
|
|||
}
|
||||
}
|
||||
|
||||
/* TODO: Allocate mrb_gpio_t and store configuration */
|
||||
mrb_gpio_t *pin = mrb_malloc(mrb, sizeof(mrb_gpio_t));
|
||||
|
||||
pin->pin = mrb_integer(pin_num);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
MRuby::GemBox.new do |conf|
|
||||
conf.gem :core => "../../mrbgems/machine-adc"
|
||||
conf.gem :core => "../../mrbgems/machine-devmem"
|
||||
conf.gem :core => "../../mrbgems/machine-gpio"
|
||||
end
|
Loading…
Reference in New Issue