main()

The drawing entry of the instrument script. CapDrive calls main() to obtain this screen and is responsible for layout, drawing and subsequent refresh.

Execution process

Each time it accepts new input, CapDrive updates the current data and display environment, executes the entire file in the new local scope, and then calls the main() defined this time. The function returns the built-in View, View array or empty content synchronously.

Top-level fetches and calculations are also updated on each execution. Ordinary variables are reinitialized each time, and $sessionStorage is used for the state retained between calls.

Changes in the current input, input definition, or display environment will trigger execution, and there is no uniformly fixed frequency. Identical inputs are skipped and only the latest pending inputs are kept in processing. History window growth, cleanup, and internal sample time changes do not trigger execution individually; explicitly defined time input changes still trigger. Animation is performed independently by the drawing side.

Example: Draw with current readings

Select the speed in the meter settings. Each call reads the latest speed, generates numbers and units; displays a horizontal line when no speed is available.

function main() {
  const speed = $data.current.speed;
  return (
    <VStack spacing={8}>
      <Text fontSize={64} color={$env.foreground}>
        {speed?.value == null ? '—' : Math.round(speed.value)}
      </Text>
      <Text fontSize={16} color={$env.accent}>
        {speed?.unit ?? $env.speedUnit}
      </Text>
    </VStack>
  );
}

State between function calls

Ordinary local variables are only used for this function call. Use $sessionStorage when you need to retain peak value, count, etc. status within the current trip; it is isolated by trip and instrument instance, and will be cleared when the trip ends or the instrument is reloaded.

When a list is needed, View can be generated from the array map. Provides a stable key for items that update or reflow so that draw updates and animations continue to correspond to the same item.

Operational requirements and errors

main() should be completed within a synchronous call. Avoid continuous loops, blocking waits, and data structures that grow unbounded; the runtime will limit execution time, steps, and drawing content size. The values ​​in the drawing attributes need to be finite numbers, and the calculation results should first deal with missing readings, division by zero, etc.

Script errors are displayed in the instrument error status and corresponding logs. Preview is used to verify the display code; actual meter readings depend on the selected data and the current data source.