Skip to content

Led Control Panel Example

This example demonstrates a simple program execution panel in EcuBus-Pro, including status indication and message display.

Features Demonstrated

  1. Program Execution Control

    • A "Start" button toggles the execution of a simulated program.
    • When started, the program runs for a random time (2-5 seconds) and then fails automatically.
  2. Status Indication

    • Two large LED indicators:
      • Red LED: Lights up when the program fails.
      • Blue LED: Lights up when the program succeeds (not used in this demo, but present for extension).
    • Status message area displays the current state (e.g., "Starting program execution", "Program failed after random time", "Manual stop of program execution").
  3. Panel Layout

    • Includes a logo and title for easy identification.
    • All controls and indicators are arranged in a grid for clarity.

Variables Used

Variable NameTypePurpose / Description
Program.runnumberControls the start/stop of the program (triggered by the Start button).
Program.successnumberIndicates if the program succeeded (set to 1 on start, 0 on failure/stop).
Program.failednumberIndicates if the program failed (set to 1 on failure, 0 otherwise).
Program.msgstringDisplays the current status message in the panel.

More information about variables can be found in the Variable section.

How It Works

  • Press the Start button to begin the program.
  • The message area will show "Starting program execution".
  • After a random interval (2-5 seconds), the program will simulate a failure:
    • The message updates to "Program failed after random time".
    • The red LED lights up.
    • The Start button resets.
  • You can manually stop the program by toggling the Start button off, which will show "Manual stop of program execution".

Example Panel

Demo

Code Overview

  • panel.ecb: Defines the panel layout, variables, and UI elements.
  • program.ts: Handles the logic for starting, stopping, and simulating program failure.
typescript
import { setVar } from 'ECB'
let timer: NodeJS.Timeout
Util.OnVar('Program.run', async (args) => {
  if (args.value == 1) {
    setVar('Program.success', 1)
    setVar('Program.failed', 0)
    setVar('Program.msg', 'Starting program execution')
    timer = setTimeout(async () => {
      setVar('Program.msg', 'Program failed after random time')
      setVar('Program.failed', 1)
      setVar('Program.success', 0)
      setVar('Program.run', 0)
    }, randomTime)
  } else {
    setVar('Program.msg', 'Manual stop of program execution')
    setVar('Program.success', 0)
    setVar('Program.failed', 0)
    clearTimeout(timer)
  }
})
const randomTime = Math.floor(Math.random() * (5000 - 2000 + 1)) + 2000