Refact: Separate LED selection and state control (#957)

Separation of class LED selection and state control.
The label of the LED corresponds to the silk screen of the iot2050.

Before we use one list to enumerate all the led and its state.
like: User1 Led Red User1 Led Green User1 Led Orange
      User2 Led Red User2 Led Green User2 Led Orange
if we need to add or delete the LED, we should change this list
After Separation of class LED selection and state control
one is Led selection: USER1 USER2
the other is status selection: Green Red Orange

For this structure, if the led or status changes, We don't need
to enumerate all the states.

Also there is a problem with the old version:
When we use one node to control led show green, then we use another node to control
led show red. The result we expect is led red, but the actually result is led orange.
This is caused the previous green do not turn off. State change is wrong.
This refact would also fix it.

Signed-off-by: chao zeng <chao.zeng@siemens.com>

Signed-off-by: chao zeng <chao.zeng@siemens.com>
This commit is contained in:
Chao Zeng 2022-10-26 22:17:28 +08:00 committed by GitHub
parent 06c9bd65a7
commit 3ca4456c2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 58 deletions

View File

@ -7,6 +7,7 @@
defaults: {
name: {value:""},
pin: {value:"", required: true},
color: {value:"", required: true},
},
inputs:1,
outputs:0,
@ -56,14 +57,21 @@
<label for="node-input-pin"><i class="fa fa-circle"></i> Led</label>
<select type="text" id="node-input-pin" style="width: 250px;">
<option value='' disabled selected style='display:none;'><span data-i18n="rpi-gpio.label.selectpin"></span></option>
<option value="0">User1 Led Green</option>
<option value="1">User1 Led Red</option>
<option value="2">User1 Led Orange</option>
<option value="3">User2 Led Green</option>
<option value="4">User2 Led Red</option>
<option value="5">User2 Led Orange</option>
<option value="0">USER1</option>
<option value="1">USER2</option>
</select>
</div>
<div class="form-row">
<label for="node-input-color"><i class="fa fa-circle"></i> Color</label>
<select type="text" id="node-input-color" style="width:250px;">
<option value='' disabled selected style='display:none;'><span data-i18n="rpi-gpio.label.selectpin"></span></option>
<option value="0">GREEN</option>
<option value="1">RED</option>
<option value="2">ORANGE</option>
</select>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" style="width: 250px;">

View File

@ -3,71 +3,75 @@ module.exports = function(RED) {
function LEDNode(n) {
RED.nodes.createNode(this, n);
this.pin = Number(n.pin);
this.led0 = new m.Led(0); /*user-led1-green*/
this.led1 = new m.Led(1); /*user-led1-red*/
this.led2 = new m.Led(2); /*user-led2-green*/
this.led3 = new m.Led(3); /*user-led2-red*/
this.color = Number(n.color);
if (this.pin == 0) {
this.user1_green = new m.Led(0); /*user-led1-green*/
this.user1_red = new m.Led(1); /*user-led1-red*/
} if(this.pin == 1) {
this.user2_green = new m.Led(2); /*user-led2-green*/
this.user2_red = new m.Led(3); /*user-led2-red*/
}
function set_led_green(led_green, led_red) {
led_green.setBrightness(1);
led_red.setBrightness(0);
}
function set_led_red(led_green, led_red) {
led_green.setBrightness(0);
led_red.setBrightness(1);
}
function set_led_orange(led_green, led_red) {
led_green.setBrightness(1);
led_red.setBrightness(1);
}
function turn_off_led(led_green, led_red) {
led_green.setBrightness(0);
led_red.setBrightness(0);
}
this.on("input", function(msg) {
if (this.pin == 0) {
this.led_green = this.user1_green;
this.led_red = this.user1_red;
}
else if (this.pin == 1) {
this.led_green = this.user2_green;
this.led_red = this.user2_red;
}
if (msg.payload == "1") {
switch(this.pin)
{
case 0: /*User0 Led Green*/
this.led0.setBrightness(1);
switch(this.color) {
case 0:
set_led_green(this.led_green, this.led_red);
break;
case 1: /*User0 Led Red*/
this.led1.setBrightness(1);
case 1:
set_led_red(this.led_green, this.led_red);
break;
case 2: /*User0 Orange*/
this.led0.setBrightness(1);
this.led1.setBrightness(1);
break;
case 3: /*User1 Led Green*/
this.led2.setBrightness(1);
break;
case 4: /*User1 Led Red*/
this.led3.setBrightness(1);
break;
case 5: /*User1 Orange*/
this.led2.setBrightness(1);
this.led3.setBrightness(1);
case 2:
set_led_orange(this.led_green, this.led_red);
break;
default:
console.log("unexpected");
break;
}
}
else {
switch(this.pin)
{
case 0: /*User1 Led Green*/
this.led0.setBrightness(0);
break;
case 1: /*User1 Led Red*/
this.led1.setBrightness(0);
break;
case 2: /*User1 Orange*/
this.led0.setBrightness(0);
this.led1.setBrightness(0);
break;
case 3: /*User2 Led Green*/
this.led2.setBrightness(0);
break;
case 4: /*User2 Led Red*/
this.led3.setBrightness(0);
break;
case 5: /*User2 Orange*/
this.led2.setBrightness(0);
this.led3.setBrightness(0);
break;
default:
break;
}
turn_off_led(this.led_green, this.led_red);
}
});
this.on('close', function() {
this.led0.close();
this.led1.close();
this.led2.close();
this.led3.close();
if (this.pin == 0) {
this.user1_green.close();
this.user1_red.close();
} if(this.pin == 1) {
this.user2_green.close();
this.user2_red.close();
}
});
}
RED.nodes.registerType("mraa-gpio-led", LEDNode);