Skip to content

クラスの解説

Ko-ichiro Sugiyama edited this page Jun 1, 2021 · 9 revisions

GPIO : 汎用デジタル入出力

コードURL ​github.com/gfd-dennou-club/iotex-esp32-mrubyc/blob/master/mrblib/models/gpio.rb

Constructor

GPIO.new( pin, mode, pull_mode )

  • pin : GPIO PIN number

  • mode : GPIO::IN or GPIO::OUT

  • pull_mode : use GPIO::PULL_UP if necessary

example

led1 = GPIO.new( 13, GPIO::OUT ) # create output pin on GPIO13

sw1  = GPIO.new( 34, GPIO::IN, GPIO::PULL_UP )  # create output pin on GPIO34 (enable internal pull-up resistor)

Output

GPIO.write( value )

  • value : 0 (low) or 1 (high)

example

led1.write(1)    # set pin to "high" level
led1.write(0)    # set pin to "low" level

Input

GPIO.read( )

example

value = sw1.read()  # read from input pin (0 or 1)

IRQ handler

GPIO.irq(callback)

* callback : function created by user

GPIO.setpullmode( mode )

* mode : GPIO::PULL_HOLD or nil

example

led1 = GPIO.new( 13, GPIO::OUT )
sw1  = GPIO.new( 34, GPIO::IN, GPIO::PULL_UP )
sw2  = GPIO.new( 35, GPIO::IN, GPIO::PULL_UP )

# スイッチ1にIRQを登録し、スイッチ1に変更があるたびにcallback関数が呼ばれるようにする
callback = Proc.new { |p|
  print(p)
  puts("called")
}
sw1.irq(callback)

while true
  # スイッチ1の値に応じてLED1の出力を切り替える
  if (sw1.read == 1)
    led1.write(1)
  else
    led1.write(0)
  end

  # スイッチ2がONであれば、LED1をホールドモード(解除されるまで対象の値が動かない)にする
  # OFFであれば、ホールドモードを解除する
  if(sw2.read == 1)
    led1.setpullmode(GPIO::PULL_HOLD)
  else
    led1.setpullmode(nil)
  end
  sleep 1
end

PWM : pulse width modulation

コードURL github.com/gfd-dennou-club/iotex-esp32-mrubyc/blob/master/mrblib/models/pwm.rb

Constructor

PWM.new( pin )

  • pin : GPIO PIN number

example

pwm1 = PWM( 15 )     # create PWM object from a GPIO 15

set frequency

PWM.freq( freq )

  • freq : frequency

example

pwm1.freq(1000)         # set frequency

set duty

PWM.duty( duty )

  • duty : duty (default 8 bit counter)

example

pwm1 = pwm0.duty(128)          # set duty cycle. the value of 128 means duty ratio is 0.5 (default 8 bit).

stop

PWM.deinit()

example

pwm1.deinit()           # turn off PWM on the pin

TODO

pwm0.freq()             # get current frequency
pwm0.duty()             # get current duty cycle

ADC : analog to digital conversion

​コードURL github.com/gfd-dennou-club/iotex-esp32-mrubyc/blob/master/mrblib/models/adc.rb

Constructor

ADC.new( pin, often, width )

  • pin : GPIO PIN number

  • often : set input attenuation (voltage range roughly 0.0v - 3.6v) with ATTEN_0DB, ATTEN_2DB, ATTEN_6DB, and ATTEN_11DB

  • width : set return values across voltage range 0.0v - 1.0v with WIDTH_9BIT, WIDTH_10BIT, WIDTH_11BIT, and WIDTH_12BIT

example

adc1 = ADC.new( 39, ADC::ATTEN_11DB, ADC::WIDTH_9BIT ) 
     # create ADC object on ADC pin
     # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
     # 第 2 引数: 計測可能範囲を 0 ~ 3.6 V に設定 (ADC::ATTEN_11DB).
     # set 9 bit return values (returned range 0-511) across voltage range 0.0v - 1.0v
     # 第 3 引数: AD 変換の解像度を 12 bit => 0 ~ 3.6 V の範囲を 0 ~ 4095 を対応させて計測 (ADC::WIDTH_12BIT).

​​== Read ADC.read() -> Float ​example

value = adc.read()    # read value using the newly configured attenuation and width

シリアル通信 I2C

​コードURL github.com/gfd-dennou-club/iotex-esp32-mrubyc/blob/master/mrblib/models/i2c.rb

コンストラクタ

I2C.new( scl ,sda, freq, port )

  • scl: SCL PIN

  • sda: SDA PIN

  • freq: frequency (default 40000)

  • port: port (default 0)

​example

i2c = I2C.new(0,22,21)

出力

I2C.write( i2c_adrs_7, [data1, data2,…] )

  • i2c_adrs_7: I2C address

  • [data1, data2, …]: data array

​example

i2c.write( i2c_ADRS_7, 0x02, 0x16, 0x00 )

入力

I2C.read_integer( i2c_adrs_7, read_bytes )

  • i2c_adrs_7: I2C address

  • read_bytes: read_bytes (integer)

example​

s = i2c.read_integer( ADRS, 2) #指定されたバイト数のデータを読み込む.

​​

以下,未整理

シリアル通信 UART UARTシリアルインターフェースを扱います。​## コンストラクタ### UART.new( machine dependent parameters ) ​例 (mruby/c devkit 02)

uart1 = UART.new( 1 )	# param: grove pin number.
​## 出力### write( string ) 指定された文字列を出力します。​例 (mruby/c devkit 02)
uart1.write("Output string¥r¥n")
​## 入力### read( n_bytes ) -> String, Nil 指定されたバイト数のデータを読み込みます。指定されたバイト数のデータが到着していない場合、nilを返します。​例 (mruby/c devkit 02)
val = uart1.read( 10 )
​### read_nonblock( maxlen ) -> String 指定されたバイト数のデータを読み込みます。指定されたバイト数のデータが到着していない場合、到着している分のデータを返します。​例 (mruby/c devkit 02)
val = uart1.read_nonblock( 1024 )
​## その他### clear_tx_buffer() 読み込みバッファをクリアします。​例 (mruby/c devkit 02)
uart1.clear_tx_buffer()
​### clear_rx_buffer() 書き込みバッファをクリアします。​例 (mruby/c devkit 02)
uart1.clear_rx_buffer()
​​​

​# シリアル通信 SPI SPIシリアルインターフェースを扱います。​## コンストラクタ### SPI.new( machine dependent parameters ) ​例 (mruby/c devkit 02)

spi = SPI.new()
​## 出力### write( data1, data2,… ) 指定されたバイトを順次出力します。​例 (mruby/c devkit 02)
spi.write( 0x02, 0x16, 0x00 )
​### write( “string” ) 指定された文字列を出力します。​例 (mruby/c devkit 02)
spi.write( "string" )
​## 入力### read( read_bytes ) -> String 指定されたバイト数のデータを読み込みます。​#### 戻り値​例 (mruby/c devkit 02)
s = spi.read( 2 )
​## 汎用転送### transfer( [d1, d2,…], recv_size ) -> String d1,d2…を送信し、その後recv_size分の 0x00 を送信します。

戻り値は、受信した長さ recv_size バイトの文字列となります。