Mattzobricks / MattzoControllers

MattzoController firmware
20 stars 10 forks source link

Auto Lights for MTC4BT #2

Closed raymondbrink closed 2 years ago

raymondbrink commented 2 years ago

I would like to have more powerful and configurable auto lights for MTC4BT, which also support bipolar lights. Bipolar lights show a different color depending on the polarity of the voltage that you put into the LEDs. Typical colors in our most common scenarios are white and red, but other combinations are also possible. Different colors may require different voltages.

In the MTC4BT configuration, the following concepts should be implemented: a) light b) event c) trigger d) action

Meaning: a) light A physical, switchable light emitting unit. A light has a status. In the issue, two light status are defined: I) "on" II) "off" A status may have none, one or multiple additional parameters. Status "off" has no additional parameters. Status "on" has the additional parameter "intensity". The polarity of the intensity is relevant and is indicated with a positive or negative value. The polarity range is -100..100. 100 equals "max power", 0 equals "power off". More status like "blink" or "flash" may be added at a later state (not in scope of this issue). Those future status may have additional parameters like "duration", "numFlashes", "morseMessage" etc.

b) event An event is combination of one or more triggers and one or more actions.

c) trigger A trigger cause one or more actions to be executed. In the scope of this issue, the following triggers should be implemented: I) A function key in Rocrail II) A train event (forward, backwards, stop) More triggers may be added later (not in scope of this issue)

d) action An action is initiated by a trigger, and results in a status change of a light. The status change may happen immediately or with a configurable delay (in ms). Any additional parameters for the status is part of the action. E.g. an action must indicate the intensity of a light that is set to status "on".

In the following comment, I have pasted a couple of example configurations that illustrate the requested features. They also act as a proposition for the enhanced configuration structure.

Original issue from old repo: https://github.com/Mattze0815/roc2bricks/issues/212

raymondbrink commented 2 years ago
/*
Example 1:
German Diesel engine V100

HARDWARE SETUP
Loco is fitted with SBrick and 3 white head lights.
Head lights are wired in series and operated via a Power Functions port on an SBrick.
To light the head lights, 80% power shall be applied

MANUALLY SWITCHED LIGHTS
To manually switching the head lights, f0 in Rocrail shall be used:
F0: switches head lights on and off

AUTOMATICALLY SWITCHED LIGHTS
When loco moves forward, the front lights shall be switched on
When loco moves backward, the front lights shall be switched off
After stopping, the lights shall be switched off automatically after a delay of 10000ms

Proposed configuration:
*/

{
    "address": 100,
    "name": "V100",
    "speedStep": 5,
    "brakeStep": 20,
    "bleHubs": [
        {
            "type": "SBrick",
            "address": "58:8e:81:58:b2:00",
            "channels": [
                {
                    "channel": "B",
                    "attachedDevice": "motor",
                    "direction": "reverse"
                },
                {
                    "channel": "D",
                    "attachedDevice": "light",
                    "name": "Head lights"
                }
            ]
        }
    ],
    "events": [
        {
            "triggers": [
                {
                    "trigger": "fn",
                    "fn": 0,
                    "status": "on"
                },
                {
                    "trigger": "event",
                    "event": "forward"
                }
            ],
            "actions": [
                {
                    "light": "Head lights",
                    "status": "on",
                    "intensity": 80
                }
            ]
        },
        {
            "triggers": [
                {
                    "trigger": "fn",
                    "fn": 0,
                    "status": "off"
                },
                {
                    "trigger": "event",
                    "event": "stop",
                    "delay_ms": 10000
                }
                {
                    "trigger": "event",
                    "event": "backward"
                }
            ],
            "actions": [
                {
                    "light": "Head lights",
                    "status": "off"
                }
            ]
        }
    ]
}

/*
Example 2:
Eurostar

HARDWARE SETUP
Front and rear loco (loco 1 and 2)
Each loco fitted with SBrick and a white Powered Up light.

MANUALLY SWITCHED LIGHTS
F1: switches the lights in loco 1 on and in loco 2 off
F2: switches the lights in loco 1 off and in loco 2 on
F3: switches both lights off

AUTOMATICALLY SWITCHED LIGHTS
When train moves forward, lights in loco 1 shall be on and in loco 2 off (and vice versa)
After stopping, the lights shall be switched off automatically after a delay of 5000ms

Proposed configuration:
*/

{
    "address": 6197,
    "name": "EST",
    "speedStep": 5,
    "brakeStep": 20,
    "bleHubs": [
        {
            "name": "Loco1",
            "type": "SBrick",
            "address": "58:8e:81:58:b2:00",
            "channels": [
                {
                    "channel": "B",
                    "attachedDevice": "motor",
                    "direction": "reverse"
                },
                {
                    "channel": "D",
                    "attachedDevice": "light",
                    "name": "front head lights"
                }
            ]
        },
        {
            "name": "Loco2",
            "type": "SBrick",
            "address": "84:2e:14:51:69:92",
            "channels": [
                {
                    "channel": "B",
                    "attachedDevice": "motor",
                    "direction": "forward"
                },
                {
                    "channel": "D",
                    "attachedDevice": "light",
                    "name": "rear head lights"
                }
            ]
        }
    ],
    "events": [
        {
            "name": "lights forward",
            "triggers": [
                {
                    "trigger": "fn",
                    "fn": 1,
                    "status": "on"
                },
                {
                    "trigger": "event",
                    "event": "forward"
                }
            ],
            "actions": [
                {
                    "light": "front head lights",
                    "status": "on",
                    "intensity": 100
                },
                {
                    "light": "rear head lights",
                    "status": "off"
                }
            ]
        },
        {
            "name": "lights backwards",
            "triggers": [
                {
                    "trigger": "fn",
                    "fn": 2,
                    "status": "on"
                },
                {
                    "trigger": "event",
                    "event": "backward"
                }
            ],
            "actions": [
                {
                    "light": "front head lights",
                    "status": "off"
                },
                {
                    "light": "rear head lights",
                    "status": "on",
                    "intensity": 100
                }
            ]
        },
        {
            "name": "lights off",
            "triggers": [
                {
                    "trigger": "fn",
                    "fn": 3,
                    "status": "on"
                },
                {
                    "trigger": "event",
                    "event": "stop",
                    "delay_ms": 10000
                },
            ],
            "actions": [
                {
                    "light": "front head lights",
                    "status": "off"
                },
                {
                    "light": "rear head lights",
                    "status": "off"
                }
            ]
        }
    ]
}

/*
Example 3:
LEGO ICE 60051
Commuter Train with white and red auto lights

HARDWARE SETUP
Front loco (loco 1) and rear loco (2)
Each loco fitted with SBrick, a train motor and 2 Power Functions Lights
In each loco, one PF Light is used for white, the other one for red lights

MANUALLY SWITCHED LIGHTS
The train is set to have 3 functions.
In Rocrail, fn1, fn2 and fn3 are used to switch the lights.
The modus of the function keys is set to "momentarily".
When pressing the keys, the following happens:
fn1:
loco 1: white lights on, red lights off
loco 2: red lights on, white lights off
fn2:
loco 1: red lights on, white lights off
loco 2: white lights on, red lights off
fn3:
all lights off

AUTOMATICALLY SWITCHED LIGHTS
When train starts to move FORWARD, the same should happen as when pressing fn1.
When train starts to move BACKWARD, the same should happen as when pressing fn2.
After the train has stopped, the lights shall be switched off after a delay of 5000ms.

Proposed configuration:
*/

{
    "address": 6051,
    "name": "ICE",
    "speedStep": 5,
    "brakeStep": 20,
    "bleHubs": [
        {
            "name": "Loco 1",
            "type": "SBrick",
            "address": "58:8e:81:58:b2:00",
            "channels": [
                {
                    "channel": "B",
                    "attachedDevice": "motor",
                    "direction": "reverse"
                },
                {
                    "channel": "D",
                    "attachedDevice": "light",
                    "name": "Loco 1 white lights"
                },
                {
                    "channel": "A",
                    "attachedDevice": "light",
                    "name": "Loco 1 red lights"
                }
            ]
        },
        {
            "name": "Loco 2",
            "type": "SBrick",
            "address": "84:2e:14:51:69:92",
            "channels": [
                {
                    "channel": "B",
                    "attachedDevice": "motor",
                    "direction": "forward"
                },
                {
                    "channel": "D",
                    "attachedDevice": "light",
                    "name": "Loco 2 white lights"
                },
                {
                    "channel": "A",
                    "attachedDevice": "light",
                    "name": "Loco 2 red lights"
                }
            ]
        }
    ],
    "events": [
        {
            "name": "lights forward",
            "triggers": [
                {
                    "trigger": "fn",
                    "fn": 1,
                    "status": "on"
                },
                {
                    "trigger": "event",
                    "event": "forward"
                }
            ],
            "actions": [
                {
                    "light": "Loco 1 white lights",
                    "status": "on",
                    "intensity": 100
                },
                {
                    "light": "Loco 1 red lights",
                    "status": "off"
                },
                {
                    "light": "Loco 2 white lights",
                    "status": "off"
                },
                {
                    "light": "Loco 2 red lights",
                    "status": "on",
                    "intensity": 100
                }
            ]
        },
        {
            "name": "lights backwards",
            "triggers": [
                {
                    "trigger": "fn",
                    "fn": 2,
                    "status": "on"
                },
                {
                    "trigger": "event",
                    "event": "backward"
                }
            ],
            "actions": [
                {
                    "light": "Loco 1 white lights",
                    "status": "off"
                },
                {
                    "light": "Loco 1 red lights",
                    "status": "on",
                    "intensity": 100
                },
                {
                    "light": "Loco 2 white lights",
                    "status": "on",
                    "intensity": 100
                },
                {
                    "light": "Loco 2 red lights",
                    "status": "off"
                }
            ]
        },
        {
            "name": "lights off",
            "triggers": [
                {
                    "trigger": "fn",
                    "fn": 3,
                    "status": "on"
                },
                {
                    "trigger": "event",
                    "event": "stop",
                    "delay_ms": 5000
                },
            ],
            "actions": [
                {
                    "light": "Loco 1 white lights",
                    "status": "off"
                },
                {
                    "light": "Loco 1 red lights",
                    "status": "off"
                },
                {
                    "light": "Loco 2 white lights",
                    "status": "off"
                },
                {
                    "light": "Loco 2 red lights",
                    "status": "off"
                }
            ]
        }
    ]
}

/*
Example 4:
LEGO TGV 10233
Commuter Train with white and red auto lights

HARDWARE SETUP
Front loco (loco 1) and rear loco (2)
Each loco fitted with SBrick, a train motor and a custom Power Functions lights
The custom Power Functions lights light show a different color depending on the polarity of the input current:
+/- polarity -> white
-/+ polarity -> red
Intensity (equals the power percentage on the Power Function port) for white must be 100 (~9V), for red -60 (~6V).

MANUALLY SWITCHED LIGHTS
The train is set to have 3 functions.
In Rocrail, fn1, fn2 and fn3 are used to switch the lights.
The modus of the function keys is set to "momentarily".
When pressing the keys, the following happens:
fn1:
loco 1: white lights on, red lights off
loco 2: red lights on, white lights off
fn2:
loco 1: red lights on, white lights off
loco 2: white lights on, red lights off
fn3:
all lights off

AUTOMATICALLY SWITCHED LIGHTS
When train starts to move FORWARD, the same should happen as when pressing fn1.
When train starts to move BACKWARD, the same should happen as when pressing fn2.
After the train has stopped, the lights shall be switched off after a delay of 5000ms.

Proposed configuration:
*/

{
    "address": 10233,
    "name": "TGV",
    "speedStep": 5,
    "brakeStep": 20,
    "bleHubs": [
        {
            "name": "Loco 1",
            "type": "SBrick",
            "address": "58:8e:81:58:b2:00",
            "channels": [
                {
                    "channel": "B",
                    "attachedDevice": "motor",
                    "direction": "reverse"
                },
                {
                    "channel": "D",
                    "attachedDevice": "light",
                    "name": "Loco 1 lights"
                }
            ]
        },
        {
            "name": "Loco 2",
            "type": "SBrick",
            "address": "84:2e:14:51:69:92",
            "channels": [
                {
                    "channel": "B",
                    "attachedDevice": "motor",
                    "direction": "forward"
                },
                {
                    "channel": "D",
                    "attachedDevice": "light",
                    "name": "Loco 2 lights"
                }
            ]
        }
    ],
    "events": [
        {
            "name": "lights forward",
            "triggers": [
                {
                    "trigger": "fn",
                    "fn": 1,
                    "status": "on"
                },
                {
                    "trigger": "event",
                    "event": "forward"
                }
            ],
            "actions": [
                {
                    "light": "Loco 1 lights",
                    "status": "on",
                    "intensity": 100
                },
                {
                    "light": "Loco 2 lights",
                    "status": "on",
                    "intensity": -60
                }
            ]
        },
        {
            "name": "lights backwards",
            "triggers": [
                {
                    "trigger": "fn",
                    "fn": 2,
                    "status": "on"
                },
                {
                    "trigger": "event",
                    "event": "backward"
                }
            ],
            "actions": [
                {
                    "light": "Loco 1 lights",
                    "status": "on",
                    "intensity": -60
                },
                {
                    "light": "Loco 2 lights",
                    "status": "on",
                    "intensity": 100
                }
            ]
        },
        {
            "name": "lights off",
            "triggers": [
                {
                    "trigger": "fn",
                    "fn": 3,
                    "status": "on"
                },
                {
                    "trigger": "event",
                    "event": "stop",
                    "delay_ms": 5000
                },
            ],
            "actions": [
                {
                    "light": "Loco 1 lights",
                    "status": "off"
                },
                {
                    "light": "Loco 2 lights",
                    "status": "off"
                }
            ]
        }
    ]
}

/*
Example 5:
Bus VT98
Single car commuter Train with white and red auto lights

HARDWARE SETUP
Single car with red/white exterior lights in the front and in the back
Loco fitted with PU Hub and a PU train motor.
ESP32 is onboard, pins of the ESP32 are used to power the exterior lights.
The exterior lights are bi-polar LEDs. They require the following voltage:
white: +/- polarity, +3.3V (100% percent of power)
red: -/+ polarity, +2.0V (60% percent of power)

MANUALLY SWITCHED LIGHTS
The train is set to have 3 functions.
In Rocrail, fn1, fn2 and fn3 are used to switch the lights.
The modus of the function keys is set to "momentarily".
When pressing the keys, the following happens:
fn1:
head lights white, read lights red
fn2:
head lights red, read lights white
fn3:
all lights off

AUTOMATICALLY SWITCHED LIGHTS
When train starts to move FORWARD, the same should happen as when pressing fn1.
When train starts to move BACKWARD, the same should happen as when pressing fn2.
After the train has stopped, the lights shall remain on in their last state.

Proposed configuration:
*/
{
    "name": "MTC4BT",
    "espPins": [
        {
            "pin": 5,
            "inverted": false,
            "attachedDevice": "light",
            "name": "Front white"
        },
        {
            "pin": 6,
            "inverted": false,
            "attachedDevice": "light",
            "name": "Front red"
        },
        {
            "pin": 22,
            "inverted": false,
            "attachedDevice": "light",
            "name": "Rear white"
        },
        {
            "pin": 23,
            "inverted": false,
            "attachedDevice": "light",
            "name": "Rear red"
        }
    ],
    "locos": [
        {
            "address": 98,
            "name": "BUS",
            "speedStep": 2,
            "brakeStep": 10,
            "bleHubs": [
                {
                    "type": "PU",
                    "address": "58:8e:81:58:b2:00",
                    "channels": [
                        {
                            "channel": "A",
                            "attachedDevice": "motor",
                            "direction": "reverse"
                        }
                    ]
                }
            ],
            "events": [
                {
                    "name": "lights forward",
                    "triggers": [
                        {
                            "trigger": "fn",
                            "fn": 1,
                            "status": "on"
                        },
                        {
                            "trigger": "event",
                            "event": "forward"
                        }
                    ],
                    "actions": [
                        {
                            "light": "Front white",
                            "status": "on",
                            "intensity": 100
                        },
                        {
                            "light": "Front red",
                            "status": "on",
                            "intensity": 0
                        },
                        {
                            "light": "Rear white",
                            "status": "on",
                            "intensity": 0
                        },
                        {
                            "light": "Rear red",
                            "status": "on",
                            "intensity": 60
                        }
                    ]
                },
                {
                    "name": "lights backwards",
                    "triggers": [
                        {
                            "trigger": "fn",
                            "fn": 2,
                            "status": "on"
                        },
                        {
                            "trigger": "event",
                            "event": "backward"
                        }
                    ],
                    "actions": [
                        {
                            "light": "Front white",
                            "status": "on",
                            "intensity": 0
                        },
                        {
                            "light": "Front red",
                            "status": "on",
                            "intensity": 60
                        },
                        {
                            "light": "Rear white",
                            "status": "on",
                            "intensity": 100
                        },
                        {
                            "light": "Rear red",
                            "status": "on",
                            "intensity": 0
                        }
                    ]
                },
                {
                    "name": "lights off",
                    "triggers": [
                        {
                            "trigger": "fn",
                            "fn": 3,
                            "status": "on"
                        }
                    ],
                    "actions": [
                        {
                            "light": "Front white",
                            "status": "on",
                            "intensity": 0
                        },
                        {
                            "light": "Front red",
                            "status": "on",
                            "intensity": 0
                        },
                        {
                            "light": "Rear white",
                            "status": "on",
                            "intensity": 0
                        },
                        {
                            "light": "Rear red",
                            "status": "on",
                            "intensity": 0
                        }
                    ]
                }
            ]
        }
    }