This content is based on a pre-release version (mruby/c3.3).
- mruby/cIDE version:1.2
- mrbwrite version:1.2
- mrbc compiler version:3.0
The corresponding firmware can be downloaded from here.
Ruby
For Ruby classes supported by mruby/c, please refer to the official URL below.https://www.s-itoc.jp/activity/research/mrubyc/mrubyc_docs/library The implementation follows the standardization of the IO class in mruby/c.
I/O API Guidelines
The following explains the content based on the RBoard firmware.
GPIO
Class method
GPIO.setmode( pin, params ) -> nil
pin : 0~20 or Strings such as "B1", "A2"params : GPIO::IN GPIO::OUT etc..
# Set pin number 1 output.
GPIO.setmode( 1, GPIO::OUT )
# Set B1 pin to input, with internal pull-up.
GPIO.setmode( "B1", GPIO::IN|GPIO::PULL_UP )
GPIO.read_at( pin ) -> Integer
pin : 0~20 or Strings such as "B1", "A2"Returns the value read from the specified pin as either 0 or 1.
When reading from the pin set for output, if the hardware permits, the actual value should be read and returned instead of the set value.
GPIO.setmode( 1, GPIO::IN )
v1 = GPIO.read_at( 1 ) # read from pin 1.
GPIO.high_at?( pin ) -> bool
pin : 0~20 or Strings such as "B1", "A2"Return true If the value read from the specified pin is high (==1)
if GPIO.high_at?( 1 )
GPIO.low_at?( pin ) -> bool
pin : 0~20 or Strings such as "B1", "A2"If the value loaded from the specified pin is low-level (== 0), return true.
if GPIO.low_at?( 1 )
GPIO.write_at( pin, integer_data )
pin : 0~20 or Strings such as "B1", "A2"Output a value to the specified pin.
The value must be specified as either 0 or 1.
If the data is out of range (i.e. not 0 or 1), a RangeError will occur.
GPIO.setmode( 1, GPIO::OUT )
GPIO.write_at( 1, 0 ) # output zero to pin 1.
Constructor
GPIO.new( pin, params )
pin : 0~20 or Strings such as "B1", "A2"params : See Constants
Specify the physical pin indicated by the pin and generate a GPIO object.
At the same time, specify a param to indicate the mode, such as input/output direction.
The pin is normally specified as an integer, but PIC-specific pin numbers such as "B1" can be specified as a string.
Use the following constants for param and specify them connected by |.
IN, OUT, or HIGH_Z instruction is mandatory, and an ArgumentError will occur if it is absent.
Constants:
GPIO::IN # Set as input
GPIO::OUT # Set as output
GPIO::HIGH_Z # Set as high impedance
GPIO::PULL_UP # Enable internal pull-up
GPIO::PULL_DOWN # Enable internal pull-down
GPIO::OPEN_DRAIN # Set to open-drain mode
# Set GPIO pin 1 as output.
gpio1 = GPIO.new(1, GPIO::OUT)
# Set B1 pin as input with internal pull-up.
gpio1 = GPIO.new("B1", GPIO::IN|GPIO::PULL_UP)
Instance methods
read() -> Integer
Return the loaded value as 0 or 1.
v1 = gpio1.read()
high?() -> bool
If the loaded value is high level (==1), it returns true.
if gpio1.high?()
low?() -> bool
If the loaded value is low-level (==0), return true.
if gpio1.low?()
write( integer_data )
integer_data : 0 or 1Specify the value to output to the pin as either 0 or 1.
gpio1.write( 1 )
setmode( param ) -> nil
params : GPIO::IN GPIO::OUTなどChange the GPIO mode at any timing.
When IN, OUT, or HIGH_Z is specified while PULL_UP or other settings have already been set, the previous settings will be invalidated.
# Enable internal pullup.
gpio1.setmode( GPIO::PULL_UP )
# Switch to input and enable internal pullup.
gpio1.setmode( GPIO::IN|GPIO::PULL_UP )
ADC
A class that supports Analog-to-Digital Conversion (ADC) functionality.Generally, it is capable of converting analog voltage values to digital values.
Constructor
ADC.new( pin )
Generate an ADC object by specifying the physical pin indicated by "pin."The pin is normally specified as an integer, but PIC-specific pin numbers such as "B1" can be specified as a string.
If the pin is already used in GPIO, etc., switch to ADC.
adc1 = ADC.new( 1 )
read_voltage() -> Float
Reads the value and returns the voltage value (V).0 ~ 3.3V
v1 = adc1.read_voltage()
read() -> Float
Alias for read_voltageread_raw() -> Integer
Read a value and return the raw value (the value before conversion to voltage). 0 ~ 1023
v1 = adc1.read_raw()
I2C
A class that supports I2C bus.Supports master devices, 7-bit addresses only.
Constructor
I2C.new( *params )
Optional Parametersparams | type | description |
---|---|---|
frequency | Integer | Frequency (default 100kHz) |
freq | (Same as above) | (Same as above) |
scl_pin | --- | SCL pin specification |
sda_pin | --- | SDA pin specification |
# Generate an I2C object with the default settings.
i2c = I2C.new()
# Use an I2C device of unit 1 with a frequency of 400kHz.
i2c = I2C.new(1, frequency:400_000 ) # 400kHz
Instance Methods
read( i2c_adrs_7, read_bytes, *param ) -> String
Reads data of read_bytes bytes from the device with the address i2c_adrs_7.If the device returns NAK in the middle, a String of shorter length than read_bytes may be returned.
If data is specified in the param, it will be output before the repeated start, and then reading will start.
s = i2c.read( 0x45, 3 ) # case 1
s = i2c.read( 0x45, 3, 0xf3, 0x2d ) # case 2
(case 1) S -- adrs(45) R A -- data1 A -- data2 A -- data3 A|N -- P (case 2) S -- adrs(45) W A -- out1(f3) A -- out2(2d) A -- Sr -- adrs(45) R A -- data1 A -- data2 A -- data3 N -- P S : Start condition P : Stop condition Sr: Repeated start condition A : Ack N : Nack R : Read bit W : Write bit
write( i2c_adrs_7 , *outputs ) -> Integer
Writes data specified in outputs to the device with the address i2c_adrs_7.The number of bytes successfully written is returned as the return value.
outputs can be specified as an Integer, Array, or String.
i2c.write( 0x45, 0x61, 0x47 )
i2c.write( 0x50, 0x00, 0x80, "aG" ) # useful for EEPROM
i2c.write( 0x11, [0x61, 0x47] )
S -- adrs W A -- data_1 A -- ... -- data_n N -- P S : Start condition P : Stop condition A : Ack N : Nack W : Write bit
send_start()
Low level method.Outputs a StartCondition to the I2C bus.
i2c.send_start
send_restart()
Low level method.Outputs a Restart (RepeatedStart) Condition to the I2C bus.。
i2c.send_restart
send_stop()
Low level method.Outputs a StopCondition to the I2C bus.
i2c.send_stop
raw_read( read_bytes, ack_nack = false ) -> String
Low level method.Reads read_bytes bytes from the I2C bus and returns them.
ack_nack = true outputs ACK at the last byte reading, and false outputs NACK.
str = i2c.raw_read( 20 )
raw_write( *outputs ) -> Integer
Low level method.Writes data specified in outputs to the I2C bus.
The number of bytes successfully written is returned as the return value.
outputs can be specified as an Integer, Array, or String.
i2c.raw_write( 0x45, 0x30, 0xa2 )
SPI
A class that supports SPI bus.This specification defines only master devices and transfers in 8-bit units.
Use the GPIO function for Chip Select(CS/SS).
Constructor
SPI.new( id=nil, *params )
id : 1 or 2Generates an SPI object by specifying the physical unit indicated by "id."
Optional Parameters
params | type | description |
---|---|---|
unit | --- | Specification of the SPI unit |
frequency | Integer | Frequency (default 1MHz) |
mode | Integer | 0 to 3 (default 0) |
first_bit | Constant | SPI::MSB_FIRST or SPI::LSB_FIRST (default MSB_FIRST) |
mode | CPOL | CPHA | Idle state clock polarity | Sampling timing |
---|---|---|---|---|
0 | 0 | 0 | Low | Rising edge |
1 | 0 | 1 | Low | Falling edge |
2 | 1 | 0 | High | Falling edge |
3 | 1 | 1 | High | Rising edge |
# デフォルトの設定で、spiオブジェクトを生成する。
spi = SPI.new()
# ユニット1 の SPI デバイスを、周波数 10MHz で使う。
spi = SPI.new( unit:1, frequency:10_000_000 )
Instance Methods
setmode( *params )
Changes the operating mode (parameters) of the SPI.The parameters are specified according to the constructor.
spi.setmode( mode:3 )
read( read_bytes ) -> String
Reads data of read_bytes bytes from the SPI bus.At the same time, data will be output as 0.
data = spi.read( 32 )
write( *outputs ) -> nil
Outputs data specified in outputs to the SPI bus.outputs can be specified as an Integer, Array, or String.
spi.write( 0x30, 0xa2 )
spi.write( "\x30\xa2" )
spi.write( 0x02, 0xee, 0xad, 0x00, data_string ) # useful for EEPROM
transfer( outputs, additional_read_bytes = 0 ) -> String
Outputs data specified in outputs to the SPI bus while simultaneously reading data (General-purpose transfer).outputs can be specified as an Integer, Array, or String.
If additional_read_bytes is specified, it will output 0x00 after the outputs.
s = spi.transfer( 0b0000_0101, 1 ) # s will return a 2-byte String
PWM
A class that supports Pulse Width Modulation (PWM) functionality.Constructor
PWM.new( pin, *params )
pin : 0~20 or Strings such as "B1", "A2"Generate a PWM object by specifying the physical pin indicated by "pin."
The pin is normally specified as an integer, but PIC-specific pin numbers such as "B1" can be specified as a string.
When specifying the parameter frequency, the output starts with a duty cycle of 50%.
If you want to start the output with a duty cycle other than 50%, specify the parameter duty simultaneously.
Optional Parameters
params | type | description |
---|---|---|
frequency | Integer,Float | Specifies the frequency. |
freq | (Same as above) | (Same as above) |
duty | Integer,Float | Specifies the duty cycle. |
# Generate an object for PWM output on pin 1. Output is not started yet.
pwm1 = PWM.new( 1 )
# Generate an object for PWM output on pin 1 and start the output with a frequency of 440Hz and a duty cycle of 30%.
pwm1 = PWM.new( 1, frequency:440, duty:30 )
Instance Methods
frequency( freq )
Starts the output or changes the frequency by specifying the frequency.freq is specified as an Integer or Float.
Specifying 0 stops the output.
Changing the frequency does not affect the duty cycle.
pwm1.frequency( 440 ) # Output at 440Hz
period_us( micro_sec )
Starts the output or changes the period by specifying the time of one cycle in microseconds.micro_sec is specified as an Integer.
Specifying 0 stops the output.
Changing the period does not affect the duty cycle.
pwm1.period_us( 2273 ) # 1/2273us = 440Hz
duty( percent )
Changes the duty cycle by specifying a percentage from 0 to 100.percent is specified as an Integer or Float.
pwm1.duty( 50 )
pulse_width_us( micro_sec )
Specifies the time in microseconds for which the output stays ON in one cycle.micro_sec is specified as an Integer.
If a time longer than one cycle is specified, it will be set to the maximum value that can be ON without an error.
pwm1.pulse_width_us( 1000 )
UART
A class that supports asynchronous serial communication.Constructor
UART.new( id=nil, *params )
id : 1 or 2(default is 2)Generates a UART object by specifying the physical unit indicated by "id."
There are only 8 data bits.
Since flow control is not implemented, RTSCTS cannot be specified.
The default parameter.
- ・Baud rate: 9600
- ・Data bits: 8 bits
- ・Stop bits: 1 bit
- ・Parity: None
- ・Flow control: None
baudrate | Integer | Baud rate (default 9600) |
---|---|---|
baud | (Same as above) | |
stop_bits | Integer | Stop bits (default 1) |
parity | Const | Parity bit (default NONE) |
txd_pin | --- | Specifies the TxD pin |
rxd_pin | --- | Specifies the RxD pin |
UART::NONE UART::EVEN UART::ODD UART::RTSCTS
# Use UART1 with all default parameters.
uart1 = UART.new( 1 )
# Use the serial device with the specified device node at 19200 bps, even parity.
uart2 = UART.new("/dev/cu.usbserial1", baud:19200, parity:UART::EVEN )
Instance Methods
setmode( *params )
Changes the mode (parameters) of UART.The parameter specification follows that of the constructor.
uart1.setmode( baudrate:38400 )
read( read_bytes ) -> String
Reads data of the specified number of bytes, read_bytes.If the specified number of bytes has not arrived, it will block until they arrive.
val = uart1.read( 10 )
write( string ) -> Integer
Sends data.Returns the number of bytes sent.
uart1.write("Output string\r\n")
gets() -> String
Reads a line of string. Internally, it returns the byte sequence until "\n" in the read buffer.If a complete line of data has not arrived, it will block until it arrives.
val = uart1.gets()
puts( string ) -> nil
Sends one line and sends a newline code at the end of the argument string.The newline code is LF only by default.
uart1.puts("Output string")
bytes_available() -> Integer
Returns the number of readable bytes in the read buffer.
len = uart1.bytes_available()
bytes_to_write() -> 0
Returns the number of bytes of data in the transmission buffer that have not been actually sent.
bytes = uart1.bytes_to_write()
can_read_line() -> bool
Returns true if reading a line of data is possible.
flag = uart1.can_read_line()
flush()
Block until transmission of data accumulated in the transmission buffer is completed.There is no send buffer so it actually does nothing.
uart1.flush()
clear_rx_buffer()
Clears the receive buffer.
uart1.clear_rx_buffer()
clear_tx_buffer()
Clears the transmission buffer.There is no send buffer so it actually does nothing.
uart1.clear_tx_buffer()
send_break( time )
Sends a break signal.The time is optional and specified in seconds.
uart1.send_break( 0.1 )
Unique Methods
leds_write(bit)
on board LED controlbit : Integer
led | Decimal | Binary number |
---|---|---|
1 | 1 | 0b0001 |
2 | 2 | 0b0010 |
3 | 4 | 0b0100 |
4 | 8 | 0b1000 |
leds_write( 9 ) # LED1, LED4 ON
leds_write( 0x06 ) # LED1, LED4 OFF / LED2, LED3 ON
sw()
Read switch statusDefault pullup.(When pressed, 0 is output.)
return:0 or 1
switch_status = sw()
puts(txt)
Output stringClick here for information on setting up the terminal software.
txt:String
puts("Hello World")