-
Notifications
You must be signed in to change notification settings - Fork 13
クラスの解説
コードURL github.com/gfd-dennou-club/iotex-esp32-mrubyc/blob/master/mrblib/models/gpio.rb
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)
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
GPIO.read( )
example
value = sw1.read() # read from input pin (0 or 1)
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
コードURL github.com/gfd-dennou-club/iotex-esp32-mrubyc/blob/master/mrblib/models/pwm.rb
PWM.new( pin )
-
pin : GPIO PIN number
example
pwm1 = PWM( 15 ) # create PWM object from a GPIO 15
PWM.freq( freq )
-
freq : frequency
example
pwm1.freq(1000) # set frequency
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).
PWM.deinit()
example
pwm1.deinit() # turn off PWM on the pin
pwm0.freq() # get current frequency pwm0.duty() # get current duty cycle
コードURL github.com/gfd-dennou-club/iotex-esp32-mrubyc/blob/master/mrblib/models/adc.rb
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
コード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.
uart1.write("Output string¥r¥n")
val = uart1.read( 10 )
val = uart1.read_nonblock( 1024 )
uart1.clear_tx_buffer()
uart1.clear_rx_buffer()
# シリアル通信 SPI SPIシリアルインターフェースを扱います。## コンストラクタ### SPI.new( machine dependent parameters ) 例 (mruby/c devkit 02)
spi = SPI.new()
spi.write( 0x02, 0x16, 0x00 )
spi.write( "string" )
s = spi.read( 2 )
戻り値は、受信した長さ recv_size バイトの文字列となります。