11. GPIO
GPIO objects provide GPIO control functions.
Functional overview:
- Provides a function to set and control the input / output of the GPIO port.
- Provides PWM output function.
Abstracts
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
new GPIO() | Creates a GPIO instance. | 01.00.00+ |
{GPIO} Instance
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
.setMode() | Sets the GPIO operation mode. | 01.00.00+ | |
.getValue() | Gets the IO port state. | 01.00.00+ | |
.setValue() | Sets the IO port output. | 01.00.00+ | |
.setIrq() | Sets the IO port input interrupt event. | 01.00.00+ | |
.disableIrq() | Invalidates the IO port input interrupt event set by .setIrq(). | 01.00.00+ | |
.setPwm() | Sets the PWM output. | 01.00.00+ | |
.releasePwm() | Stops and invalidates the PWM output set by .setPwm() | 01.00.00+ | |
.setOnePulse() | Outputs one arbitrary pulse. | 01.00.00+ |
Details
new GPIO(gpioNo,mode)
Creates a GPIO instance.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
gpioNo | number | mandatory | Specify the GPIO number. Please refer to Pinout. | |
mode | number | mandatory | Specify the GPIO operation mode. Please refer to GPIO operation mode details. | |
return | {GPIO} | - | {GPIO} : Generated {GPIO} |
GPIO operation mode details
mode | I/O | Summary | Note |
---|---|---|---|
0 | IN | PULL-UP | - |
1 | IN | PULL-DOWN | - |
2 | IN | - | |
3 | OUT | PUSH-PULL PULL-UP | - |
4 | OUT | PUSH-PULL PULL-DOWN | - |
5 | OUT | PUSH-PULL | - |
6 | OUT | OPEN-DRAIN PULL-UP | - |
7 | OUT | OPEN-DRAIN PULL-DOWN | - |
8 | OUT | OPEN-DRAIN | - |
9 | OUT | PWM | Please refer to Pinout for the ports that can use the PWM function. |
.setMode(mode)
Sets the GPIO operation mode.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
mode | number | mandatory | GPIO operation mode Please refer to GPIO operation mode details. | |
return | boolean | - | true: Success false: Failure |
.getValue()
Gets the IO port state.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | number | - | IO port status 0: Low 1: High | If the status cannot be obtained due to the IO port setting, -1 is returned. |
.setValue(value)
Sets the IO port output.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
value | number | mandatory | IO port output value 0: Low 1: High | |
return | boolean | - | true: Success false: Failure |
.setIrq(edge,callback)
Sets the IO port input interrupt event.
When the specified interrupt condition occurs, the hardware interrupt is triggered and the callback is executed.
It is also used as a resume trigger in standby mode.
In addition, it is not possible to re-set the interrupt event within the interrupt event callback process execution.
This function can be used for up to 4 ports.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
edge | number | mandatory | Interrupt condition 1: Rising edge 2: Falling edge 3: Rising / Falling edge | |
callback(value) | function | mandatory | Executes callback processing when an interrupt event occurs. value : {number} IO port status at event occurrence 0: Low 1: High | |
return | boolean | - | true: Success false: Failure |
.disableIrq()
Invalidates the IO port input interrupt event set by .setIrq().
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | boolean | - | true: Success false: Failure |
.setPwm(frequency,duty)
Sets the PWM (Pulse Width Modulation) output.
Outputs a periodic pulse specified by the pulse frequency and duty ratio.
Specify GPIO operation mode 9 in advance.
The generated PWM signal starts with a Low output.
Then, when the percentage of High duration is reached by the specified duty ratio, the output is High until the start of the next pulse cycle.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
frequency | number | mandatory | Pulse frequency [Hz] Range: 1 - 4000000 | |
duty | number | mandatory | Duty ratio [N/65536] Specify the ratio (rate of High period) assuming that one cycle is 65536. Range: 1 - 65535 | The Low and High periods are calculated from the approximate value with specified ratio. Therefore the output waveform does not have an accurate duty ratio and may include rounding errors. Also, if the pulse frequency is High, the duty resolution will decrease and the error will increase. |
return | boolean | - | true: Success false: Failure |
.releasePwm()
Stops and invalidates the PWM output set by .setPwm().
To enable the PWM control again, specify GPIO operation mode 9 again.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | boolean | - | true: Success false: Failure |
.setOnePulse(value1,width1,value2,width2)
Outputs one arbitrary pulse.
This function outputs an arbitrary pulse generated by block type processing by software.
The cycle is not strictly guaranteed like PWM, so please use it after evaluating it.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
value1 | number | mandatory | 1st signal; Specify the value to be output to the IO port. 0: Low 1: High | |
width1 | number | mandatory | 1st signal; Specify the output time [ms] of the IO port. Range: 0 - 100 | If 0 is specified, it will be about 1us. |
value2 | number | mandatory | 2nd signal; Specify the value to be output to the IO port. 0: Low 1: High | |
width2 | number | mandatory | 2nd signal; Specify the output time [ms] of the IO port. Range: 0 - 100 | If 0 is specified, it will be about 1us. |
return | boolean | - | true: Success false: Failure |
Object Usage Examples
Sample 1
This is a sample that uses interrupts.
When the level of GPIO48 changes from H to L within 10 seconds, the callback is executed.
var gp = new GPIO(48,2);
gp.setIrq(2, function(sts) {
print('status fall.');
});
setTimeout(function() {
gp.disableIrq();
print('timeout!');
}, 10000).wait();
Sample 2
This is a sample that uses PWM control.
Using GPIO46, PWM output of 10ms period and 5ms pulse width is executed for 10 seconds.
var gp = new GPIO(46,9);
gp.setPwm(100, 32768);
setTimeout(function() {
gp.releasePwm();
print('timeout!');
}, 10000).wait();