[
    {
        "id": "5860f0641efc9be5",
        "type": "rpi-gpio in",
        "z": "b0fb2de0838ad91a",
        "name": "GPIO15",
        "pin": "15",
        "intype": "up",
        "debounce": "25",
        "read": false,
        "bcm": true,
        "x": 240,
        "y": 400,
        "wires": [
            [
                "570fbbd702d49c5a",
                "bfafc757f17f4917"
            ]
        ]
    },
    {
        "id": "570fbbd702d49c5a",
        "type": "debug",
        "z": "b0fb2de0838ad91a",
        "name": "debug 8",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 440,
        "y": 440,
        "wires": []
    },
    {
        "id": "39f39d6fdda82e27",
        "type": "i2c in",
        "z": "b0fb2de0838ad91a",
        "name": "",
        "busno": "1",
        "address": "29",
        "command": "128",
        "count": "8",
        "x": 590,
        "y": 500,
        "wires": [
            [
                "34bf447af16c7066"
            ]
        ]
    },
    {
        "id": "34bf447af16c7066",
        "type": "debug",
        "z": "b0fb2de0838ad91a",
        "name": "debug 10",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 720,
        "y": 560,
        "wires": []
    },
    {
        "id": "a1fc2cdf5e2cbfe0",
        "type": "inject",
        "z": "b0fb2de0838ad91a",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 340,
        "y": 580,
        "wires": [
            [
                "f515b76d95d5159e"
            ]
        ]
    },
    {
        "id": "f515b76d95d5159e",
        "type": "i2c out",
        "z": "b0fb2de0838ad91a",
        "name": "",
        "busno": "1",
        "address": "128",
        "command": "3",
        "payload": "payload",
        "payloadType": "msg",
        "count": "1",
        "x": 550,
        "y": 620,
        "wires": [
            [
                "34bf447af16c7066"
            ]
        ]
    },
    {
        "id": "326c0dd0e7bf0bda",
        "type": "inject",
        "z": "b0fb2de0838ad91a",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 340,
        "y": 500,
        "wires": [
            [
                "39f39d6fdda82e27"
            ]
        ]
    },
    {
        "id": "inject_start",
        "type": "inject",
        "z": "b0fb2de0838ad91a",
        "name": "Iniciar lectura RGB",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": "0.5",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 150,
        "y": 720,
        "wires": [
            [
                "i2c_enable",
                "i2c_atime",
                "a39d2b5d9df3d2ae"
            ]
        ]
    },
    {
        "id": "i2c_enable",
        "type": "i2c out",
        "z": "b0fb2de0838ad91a",
        "name": "Enable RGBC",
        "busno": "1",
        "address": "41",
        "command": "128",
        "payload": "3",
        "payloadType": "num",
        "count": "1",
        "x": 380,
        "y": 680,
        "wires": [
            []
        ]
    },
    {
        "id": "i2c_atime",
        "type": "i2c out",
        "z": "b0fb2de0838ad91a",
        "name": "Set integration time",
        "busno": "1",
        "address": "41",
        "command": "129",
        "payload": "-43",
        "payloadType": "num",
        "count": "1",
        "x": 390,
        "y": 740,
        "wires": [
            [
                "delay_read"
            ]
        ]
    },
    {
        "id": "delay_read",
        "type": "delay",
        "z": "b0fb2de0838ad91a",
        "name": "100ms delay",
        "pauseType": "delay",
        "timeout": "100",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "0",
        "randomLast": "0",
        "randomUnits": "milliseconds",
        "drop": false,
        "outputs": 1,
        "x": 580,
        "y": 720,
        "wires": [
            [
                "i2c_read"
            ]
        ]
    },
    {
        "id": "i2c_read",
        "type": "i2c in",
        "z": "b0fb2de0838ad91a",
        "name": "Leer colores",
        "address": "41",
        "command": "20",
        "count": "8",
        "x": 780,
        "y": 720,
        "wires": [
            [
                "function_rgb"
            ]
        ]
    },
    {
        "id": "function_rgb",
        "type": "function",
        "z": "b0fb2de0838ad91a",
        "name": "Convertir a R/G/B",
        "func": "let d = msg.payload;\n\nif (!d || d.length < 8) return null;\n\nlet clear = d[0] | (d[1] << 8);\nlet red   = d[2] | (d[3] << 8);\nlet green = d[4] | (d[5] << 8);\nlet blue  = d[6] | (d[7] << 8);\n\nmsg.payload = {clear, red, green, blue};\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 980,
        "y": 720,
        "wires": [
            [
                "debug_rgb"
            ]
        ]
    },
    {
        "id": "debug_rgb",
        "type": "debug",
        "z": "b0fb2de0838ad91a",
        "name": "Debug RGB",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "x": 1180,
        "y": 720,
        "wires": []
    },
    {
        "id": "a39d2b5d9df3d2ae",
        "type": "i2c out",
        "z": "b0fb2de0838ad91a",
        "name": "Enable RGBC",
        "busno": "1",
        "address": "41",
        "command": "128",
        "payload": "3",
        "payloadType": "num",
        "count": "1",
        "x": 280,
        "y": 820,
        "wires": [
            []
        ]
    },
    {
        "id": "70da6d1449733c45",
        "type": "inject",
        "z": "b0fb2de0838ad91a",
        "name": "Iniciar lectura RGB",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "3",
        "crontab": "",
        "once": true,
        "onceDelay": "0.5",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 220,
        "y": 980,
        "wires": [
            []
        ]
    },
    {
        "id": "6b196fdfe638f9bf",
        "type": "i2c out",
        "z": "b0fb2de0838ad91a",
        "name": "Power ON",
        "busno": "1",
        "address": "41",
        "command": "128",
        "payload": 1,
        "payloadType": "num",
        "count": "1",
        "x": 450,
        "y": 940,
        "wires": [
            [
                "ddb70bc2135fd21d"
            ]
        ]
    },
    {
        "id": "ddb70bc2135fd21d",
        "type": "delay",
        "z": "b0fb2de0838ad91a",
        "name": "10ms delay",
        "pauseType": "delay",
        "timeout": "10",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "0",
        "randomLast": "0",
        "randomUnits": "milliseconds",
        "drop": false,
        "outputs": 1,
        "x": 650,
        "y": 940,
        "wires": [
            [
                "b90079f1daf41cad"
            ]
        ]
    },
    {
        "id": "b90079f1daf41cad",
        "type": "i2c out",
        "z": "b0fb2de0838ad91a",
        "name": "Enable RGBC",
        "busno": "1",
        "address": "41",
        "command": "128",
        "payload": 3,
        "payloadType": "num",
        "count": "1",
        "x": 850,
        "y": 940,
        "wires": [
            [
                "566d5b7f265f669e"
            ]
        ]
    },
    {
        "id": "566d5b7f265f669e",
        "type": "i2c out",
        "z": "b0fb2de0838ad91a",
        "name": "Set integration time",
        "busno": "1",
        "address": "41",
        "command": "129",
        "payload": -43,
        "payloadType": "num",
        "count": "1",
        "x": 1050,
        "y": 940,
        "wires": [
            [
                "97c35c53a713d55b"
            ]
        ]
    },
    {
        "id": "97c35c53a713d55b",
        "type": "delay",
        "z": "b0fb2de0838ad91a",
        "name": "120ms delay",
        "pauseType": "delay",
        "timeout": "120",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "0",
        "randomLast": "0",
        "randomUnits": "milliseconds",
        "drop": false,
        "outputs": 1,
        "x": 1250,
        "y": 940,
        "wires": [
            [
                "d54e82cb6248804b"
            ]
        ]
    },
    {
        "id": "d54e82cb6248804b",
        "type": "i2c in",
        "z": "b0fb2de0838ad91a",
        "name": "Leer colores",
        "busno": "1",
        "address": "41",
        "command": "148",
        "count": "8",
        "x": 1450,
        "y": 940,
        "wires": [
            [
                "7287692ba3789f3f"
            ]
        ]
    },
    {
        "id": "7287692ba3789f3f",
        "type": "function",
        "z": "b0fb2de0838ad91a",
        "name": "Convertir a R/G/B",
        "func": "// Validar buffer\nlet d = msg.payload;\nif (!d || d.length < 8) return null;\n\n// LSB primero\nlet clear = d[0] | (d[1] << 8);\nlet red   = d[2] | (d[3] << 8);\nlet green = d[4] | (d[5] << 8);\nlet blue  = d[6] | (d[7] << 8);\n\nmsg.payload = {clear, red, green, blue};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1630,
        "y": 900,
        "wires": [
            [
                "c2b2faa131b8c982"
            ]
        ]
    },
    {
        "id": "c2b2faa131b8c982",
        "type": "function",
        "z": "b0fb2de0838ad91a",
        "name": "function 7",
        "func": "let {clear, red, green, blue} = msg.payload;\n\n// Evitar división por cero\nlet factor = clear > 0 ? 255 / clear : 0;\n\nmsg.payload = {\n    red: Math.min(Math.round(red * factor), 255),\n    green: Math.min(Math.round(green * factor), 255),\n    blue: Math.min(Math.round(blue * factor), 255)\n};\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1820,
        "y": 920,
        "wires": [
            [
                "f3a467be4b51bae1"
            ]
        ]
    },
    {
        "id": "3074d6b0508c9340",
        "type": "global-config",
        "env": [],
        "modules": {
            "node-red-node-pi-gpio": "2.0.7",
            "node-red-contrib-i2c": "0.9.0"
        }
    }
]