Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node-RED-mcu Load manifest and import from comments #1317

Closed
wants to merge 3 commits into from

Conversation

NW-Lab
Copy link
Contributor

@NW-Lab NW-Lab commented Feb 27, 2024

Hello

This is a modification of #1313.
Read manifest and import from comment nodes in Node-RED MCU.

For manifest, name it “moddable_manifest” and write the content in the description.

{
    "include":"$(MODDABLE)/modules/drivers/aw3641/manifest.json"
}

For import, name it “moddable_import” and write the content in the description.

{
   "Variable name 1":"Module name 1",
   "Variable name 2":"Module name 2"
}

If the Function node code can only be executed by MCU, use RED.mcu

if(RED.mcu){
     code......
}

Sample flows.json
This is an example of running moddable/examples/drivers/aw3641 on Node-red MCU Edition.
image

[
    {
        "id": "a9469753d31dfb25",
        "type": "tab",
        "label": "sample flow",
        "disabled": false,
        "info": "",
        "env": [],
        "_mcu": {
            "mcu": true
        }
    },
    {
        "id": "4043457111bf2b23",
        "type": "inject",
        "z": "a9469753d31dfb25",
        "name": "inject",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "_mcu": {
            "mcu": true
        },
        "x": 130,
        "y": 140,
        "wires": [
            [
                "73b0486d25bfe6dc"
            ]
        ]
    },
    {
        "id": "c7d233f7729a0722",
        "type": "debug",
        "z": "a9469753d31dfb25",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "_mcu": {
            "mcu": true
        },
        "x": 440,
        "y": 140,
        "wires": []
    },
    {
        "id": "1298a2c40c7497d1",
        "type": "comment",
        "z": "a9469753d31dfb25",
        "name": "moddable_manifest",
        "info": "{\n    \"include\":\"$(MODDABLE)/modules/drivers/aw3641/manifest.json\"\n}",
        "_mcu": {
            "mcu": true
        },
        "x": 150,
        "y": 60,
        "wires": []
    },
    {
        "id": "73b0486d25bfe6dc",
        "type": "function",
        "z": "a9469753d31dfb25",
        "name": "function 2",
        "func": "msg.payload = \"abc\";\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "if(RED.mcu){\n    //const val=AW3641.off;\n    //const val=AW3641.Time220ms_Brightness100;\n    //const val=AW3641.Time220ms_Brightness90;\n    //const val=AW3641.Time220ms_Brightness80;\n    //const val=AW3641.Time220ms_Brightness70;\n    //const val=AW3641.Time220ms_Brightness60;\n    const val = AW3641.Time220ms_Brightness50;\n    //const val=AW3641.Time220ms_Brightness40;\n    //const val=AW3641.Time220ms_Brightness30;\n    //const val=AW3641.Time1_3s_Brightness100;\n    //const val=AW3641.Time1_3s_Brightness90;\n    //const val=AW3641.Time1_3s_Brightness80;\n    //const val=AW3641.Time1_3s_Brightness70;\n    //const val=AW3641.Time1_3s_Brightness60;\n    //const val=AW3641.Time1_3s_Brightness50;\n    //const val=AW3641.Time1_3s_Brightness40;\n    //const val=AW3641.Time1_3s_Brightness30;\t\n\n    const lamp = new AW3641({ pin: 26, });\n\n    Timer.repeat(() => {\n        lamp.flash(val);\n        trace(\"flash\\n\");\n    }, 2000);\n}",
        "finalize": "",
        "libs": [
            {
                "var": "Timer",
                "module": "timer"
            }
        ],
        "_mcu": {
            "mcu": true
        },
        "x": 280,
        "y": 140,
        "wires": [
            [
                "c7d233f7729a0722"
            ]
        ]
    },
    {
        "id": "27ca19e76c477ddb",
        "type": "comment",
        "z": "a9469753d31dfb25",
        "name": "moddable_import",
        "info": "{\n    \"Timer\":\"timer\",\n    \"AW3641\":\"aw3641\"\n}",
        "_mcu": {
            "mcu": true
        },
        "x": 400,
        "y": 60,
        "wires": []
    }
]

Thank you.

This was referenced Feb 27, 2024
@phoddie
Copy link
Collaborator

phoddie commented Mar 1, 2024

This is an elegantly simple solution to the problem we've been discussing. While it isn't the most developer-friendly solution, it is a great way to experiment further. We should consider this an experimental feature that might eventually be replaced.

There are two changes I'd like to request before merging your PR:

  1. nodered2mcu does not usually trace its progress to the console. Would you please remove the traces? Otherwise the output can become noisy.
  2. It seems possible that a project might want more than one manifest, or more than one import. To keep those organized, they should have different names. Instead of checking the node name is "moddable_manifest" or "moddable_import", the check could use startsWith for those two strings. That way you can have "moddable_manifest aw3641", for example. This would be nice for reusing nodes in different flows.

@NW-Lab
Copy link
Contributor Author

NW-Lab commented Mar 2, 2024

@phoddie san

I tried to fix it. Is this okay?

I attached trace because it can also be debugged. I deleted it because it is unnecessary.
It's a very good idea to only look at the first part of the comment's name. This is important for the Node-RED ecosystem.

Thank you.

@phoddie
Copy link
Collaborator

phoddie commented Mar 2, 2024

I tried to fix it. Is this okay?

Looks great!

I attached trace because it can also be debugged. I deleted it because it is unnecessary.

I sometimes commit traces from my debugging / development too. Not a big deal.

It's a very good idea to only look at the first part of the comment's name. This is important for the Node-RED ecosystem.

Thank you for being open to the suggestion.

We'll get this merged soon.

@phoddie
Copy link
Collaborator

phoddie commented Mar 6, 2024

This should be merged as part of Moddable SDK 4.5. Please confirm. Thank you.

@NW-Lab
Copy link
Contributor Author

NW-Lab commented Mar 7, 2024

I think that it is all right.
thank you.

@NW-Lab NW-Lab closed this Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants