Skip to content

Commit

Permalink
Initialize.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuuu committed Sep 2, 2023
0 parents commit 76629d3
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 0 deletions.
Empty file added .gitignore
Empty file.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 yuuu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# mruby-esp32-adc

ADC library for mruby-esp32.

## Installation

You need [esp-idf v5.0](https://docs.espressif.com/projects/esp-idf/en/release-v5.0/esp32/index.html) to use this mrbgems.

Add the line below to your `build_config.rb`:

```ruby
conf.gem :github => 'mruby-esp32/mruby-esp32-adc'
```

## Examples

```ruby
adc = ADC.new(ADC::CHANNEL_0)
value = adc.read_raw()

# If you want to use ADC_UNIT_2
adc = ADC.new(ADC::CHANNEL_0, unit: ADC::UNIT_2)
value = adc.read_raw()
```

## LICENSE

MIT License.
5 changes: 5 additions & 0 deletions mrbgem.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MRuby::Gem::Specification.new('mruby-esp32-adc') do |spec|
spec.license = 'MIT'
spec.author = 'yuuu'
spec.summary = 'ADC class on ESP32'
end
24 changes: 24 additions & 0 deletions mrblib/adc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class ADC
attr_accessor :pin, :unit

def initialize(pin, **kwargs)
self.pin = pin
self.unit = kwargs[:unit] || 0

__initialize(self.pin, self.unit)
end

def read_raw
__read_raw
end

def read_voltage
raise not_inplemented_error('ADC#read_voltage, ADC#read')
end

alias read read_voltage

def not_inplemented_error(name)
NotImplementedError.new("'#{name}' is not implemented")
end
end
127 changes: 127 additions & 0 deletions src/adc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <mruby.h>
#include <mruby/data.h>
#include <mruby/variable.h>
#include <mruby/string.h>
#include <mruby/array.h>
#include <mruby/presym.h>

#include <stdio.h>
#include <string.h>

#if ESP_PLATFORM
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_adc/adc_oneshot.h"
#include "esp_log.h"
#else
#define ESP_LOGI(tag, fmt, ...) printf(fmt, __VA_ARGS__)
#endif

#define TAG ("mruby-esp32-adc")

#ifdef ESP_PLATFORM
static adc_oneshot_unit_handle_t mrb_adc_oneshot_unit_handle[2];
#endif

typedef struct adc_t {
mrb_int pin;
mrb_int unit;
mrb_state *mrb;
} adc_t;

static void mrb_adc_free(mrb_state *mrb, void *p);

static const struct mrb_data_type mrb_adc = {
"mrb_mruby_esp32_adc", mrb_adc_free
};

static void
mrb_adc_free(mrb_state *mrb, void *p) {
adc_t *adc = (adc_t *)p;

mrb_free(mrb, p);
}

static mrb_value
mrb_adc_init(mrb_state *mrb, mrb_value self) {
adc_t *adc = mrb_malloc(mrb, sizeof(adc_t));

mrb_get_args(mrb, "ii", &adc->pin, &adc->unit);
adc->mrb = mrb;

ESP_LOGI(TAG, "initialize(pin: %d, unit: %d)", adc->pin, adc->unit);

#ifdef ESP_PLATFORM
adc_oneshot_chan_cfg_t chan_cfg = {
.bitwidth = ADC_BITWIDTH_DEFAULT,
.atten = ADC_ATTEN_DB_11,
};
ESP_ERROR_CHECK(adc_oneshot_config_channel(mrb_adc_oneshot_unit_handle[adc->unit], adc->pin, &chan_cfg));
#endif

mrb_data_init(self, adc, &mrb_adc);

return self;
}

static mrb_value
mrb_adc_read_raw(mrb_state *mrb, mrb_value self) {
adc_t *adc = (adc_t *) DATA_PTR(self);
mrb_int value = 0;

ESP_LOGI(TAG, "read_raw(%s)", "");

#ifdef ESP_PLATFORM
ESP_ERROR_CHECK(adc_oneshot_read(mrb_adc_oneshot_unit_handle[adc->unit], adc->pin, &value));
#endif

return mrb_int_value(adc->mrb, value);
}

void
mrb_mruby_esp32_adc_gem_init(mrb_state* mrb) {
struct RClass *class = mrb_define_class(mrb, "ADC", mrb->object_class);

#ifdef ESP_PLATFORM
for(int i = 0 ; i < 2 ; i++) {
adc_oneshot_unit_init_cfg_t init_cfg = {
.unit_id = i,
};
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_cfg, &mrb_adc_oneshot_unit_handle[i]));
}

mrb_define_const(mrb, class, "CHANNEL_0", mrb_fixnum_value(ADC_CHANNEL_0));
mrb_define_const(mrb, class, "CHANNEL_1", mrb_fixnum_value(ADC_CHANNEL_1));
mrb_define_const(mrb, class, "CHANNEL_2", mrb_fixnum_value(ADC_CHANNEL_2));
mrb_define_const(mrb, class, "CHANNEL_3", mrb_fixnum_value(ADC_CHANNEL_3));
mrb_define_const(mrb, class, "CHANNEL_4", mrb_fixnum_value(ADC_CHANNEL_4));

#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2) || \
defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6)
mrb_define_const(mrb, class, "CHANNEL_5", mrb_fixnum_value(ADC_CHANNEL_5));
mrb_define_const(mrb, class, "CHANNEL_6", mrb_fixnum_value(ADC_CHANNEL_6));
#endif

#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2) || \
defined(CONFIG_IDF_TARGET_ESP32S3)
mrb_define_const(mrb, class, "CHANNEL_7", mrb_fixnum_value(ADC_CHANNEL_7));
mrb_define_const(mrb, class, "CHANNEL_8", mrb_fixnum_value(ADC_CHANNEL_8));
mrb_define_const(mrb, class, "CHANNEL_9", mrb_fixnum_value(ADC_CHANNEL_9));
#endif

mrb_define_const(mrb, class, "UNIT_1", mrb_fixnum_value(ADC_UNIT_1));
mrb_define_const(mrb, class, "UNIT_2", mrb_fixnum_value(ADC_UNIT_2));
#endif

mrb_define_method(mrb, class, "__initialize", mrb_adc_init, MRB_ARGS_REQ(2));
mrb_define_method(mrb, class, "__read_raw", mrb_adc_read_raw, MRB_ARGS_NONE());
}

void
mrb_mruby_esp32_adc_gem_final(mrb_state* mrb) {
#ifdef ESP_PLATFORM
for(int i = 0 ; i < 2 ; i++) {
ESP_ERROR_CHECK(adc_oneshot_del_unit(mrb_adc_oneshot_unit_handle[i]));
}
#endif
}
29 changes: 29 additions & 0 deletions test/adc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
assert('ADC.new') do
adc = ADC.new(1)
assert_equal(adc.pin, 1)
assert_equal(adc.unit, 0)

adc = ADC.new(2, unit: 3)
assert_equal(adc.pin, 2)
assert_equal(adc.unit, 3)
end

assert('ADC#read_raw') do
adc = ADC.new(1)
ret = adc.read_raw
assert_equal(ret, 0)
end

assert('ADC#read') do
adc = ADC.new(1)
assert_raise(NotImplementedError) do
ret = adc.read
end
end

assert('ADC#read_voltage') do
adc = ADC.new(1)
assert_raise(NotImplementedError) do
ret = adc.read_voltage
end
end

0 comments on commit 76629d3

Please sign in to comment.