11. UserSW
The userSW object is a built-in object that provides a user switch port input function.
userSW Global Object
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
userSW.on() | Registers the event handler. | 01.00.00+ | |
userSW.stopEvent() | Stops the event handler. | 01.00.00+ | |
userSW.getStatus() | Gets the current state of the user switch. | 01.00.00+ |
Details
userSW.on(event,callback[,opt1[,opt2]])
Registers the event handler.
The event names that can be used are assert and change.
Registering event handlers is performed exclusively, so if the event handler already exists, it will be stopped before the new event handler is registered.
event : ’assert’
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
event | string | mandatory | Handles the event when the user switch is pressed. 'assert' | If you registered an event handler while the switch is pressed, it is determined that the switch has been pressed from that point. |
callback(term) | function | mandatory | Executes callback processing when an event occurs. term : {number} Continuous pressing time measured internally [ms] | Start the measurement from after the switch press event occurs. |
opt1 | number | optional | Switch press detection time [ms] Specifies the minimum time required to detect a state in which the switch is pressed. The default value is 0. | |
opt2 | number | optional | Switch release wait timer [ms] This timer starts after the condition of opt1 is satisfied. If switch is released before timeout, stop the timer and execute immediate callback. If a timeout occurs, the callback executes at that point even if the switch is pressed. The default value is 0. | opt2 is valid only when opt1 is specified. If opt2 is 0, it will be immediately timeout state. |
return | undefined | - | - | When an error occurs, an exception is raised. |
event : ’change’
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
event | string | mandatory | Handles the state change event of the user switch. 'change' | The initial state immediately after registration of the event handler is also regarded as changed. |
callback(status) | function | mandatory | Executes callback processing when an event occurs. status : {number} Indicates the switch status. 0: Released state 1: Pressed state | The value at the moment when the event occurs and the current value may not be the same. |
return | undefined | - | - | When an error occurs, an exception is raised. |
userSW.stopEvent()
Stops the event handler.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | undefined | - | - |
userSW.getStatus()
Gets the current state of the user switch.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | number | - | Indicates the switch status. 0: Released state 1: Pressed state |
Object Usage Examples
Sample 1
This sample handles long and short presses of the user switch.
In opt1, set the time until the pressed state becomes valid, In opt2, set the waiting time until a long press is determined.
var minTerm = 100; //ms
var maxTerm = 2000; //ms
userSW.on('assert' ,function(realTerm) {
print('callback assertion time= ' + realTerm + 'ms');
if (realTerm >= maxTerm){
print('long');
}else if (realTerm >= 300){
print('short');
}else{
print('minimal');
}
} ,minTerm ,maxTerm);
Sample 2
This is a sample that handles the change of user switch.
userSW.on('change' ,function(status) {
print('callback SW status= ' + status);
print('getStatus() SW status= ' + userSW.getStatus());
});
Updated: 2021-08-23