Skip to content

Class SetupAttribute

Namespace: Belay.Attributes
Assembly: Belay.Attributes.dll

Marks a method to be executed once during device initialization. Methods decorated with this attribute are automatically called when the device connects and establishes communication, providing a hook for device-specific setup.

csharp
[AttributeUsage(AttributeTargets.Method)]
public sealed class SetupAttribute : Attribute

Inheritance

objectAttributeSetupAttribute

Inherited Members

Attribute.Equals(object?), Attribute.GetCustomAttribute(Assembly, Type), Attribute.GetCustomAttribute(Assembly, Type, bool), Attribute.GetCustomAttribute(MemberInfo, Type), Attribute.GetCustomAttribute(MemberInfo, Type, bool), Attribute.GetCustomAttribute(Module, Type), Attribute.GetCustomAttribute(Module, Type, bool), Attribute.GetCustomAttribute(ParameterInfo, Type), Attribute.GetCustomAttribute(ParameterInfo, Type, bool), Attribute.GetCustomAttributes(Assembly), Attribute.GetCustomAttributes(Assembly, bool), Attribute.GetCustomAttributes(Assembly, Type), Attribute.GetCustomAttributes(Assembly, Type, bool), Attribute.GetCustomAttributes(MemberInfo), Attribute.GetCustomAttributes(MemberInfo, bool), Attribute.GetCustomAttributes(MemberInfo, Type), Attribute.GetCustomAttributes(MemberInfo, Type, bool), Attribute.GetCustomAttributes(Module), Attribute.GetCustomAttributes(Module, bool), Attribute.GetCustomAttributes(Module, Type), Attribute.GetCustomAttributes(Module, Type, bool), Attribute.GetCustomAttributes(ParameterInfo), Attribute.GetCustomAttributes(ParameterInfo, bool), Attribute.GetCustomAttributes(ParameterInfo, Type), Attribute.GetCustomAttributes(ParameterInfo, Type, bool), Attribute.GetHashCode(), Attribute.IsDefaultAttribute(), Attribute.IsDefined(Assembly, Type), Attribute.IsDefined(Assembly, Type, bool), Attribute.IsDefined(MemberInfo, Type), Attribute.IsDefined(MemberInfo, Type, bool), Attribute.IsDefined(Module, Type), Attribute.IsDefined(Module, Type, bool), Attribute.IsDefined(ParameterInfo, Type), Attribute.IsDefined(ParameterInfo, Type, bool), Attribute.Match(object?), Attribute.TypeId, object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Examples

Basic Device Setup

public class TemperatureSensor : Device
{
    [Setup]
    private async Task InitializeSensorAsync()
    {
        // Runs automatically when device connects
        await ExecuteAsync(@"
            import machine
            import time

            # Configure temperature sensor
            sensor_pin = machine.Pin(26)
            temp_adc = machine.ADC(sensor_pin)

            # Set reference voltage
            machine.ADC.ATTN_11DB = 3  # 3.3V range
            temp_adc.atten(machine.ADC.ATTN_11DB)

            print('Temperature sensor initialized')
        ");
    }
}

Multi-Step Setup

public class RobotController : Device
{
    [Setup]
    private async Task InitializeHardwareAsync()
    {
        await ExecuteAsync(@"
            import machine

            # Initialize motor pins
            motor_left = machine.PWM(machine.Pin(12))
            motor_right = machine.PWM(machine.Pin(13))
            motor_left.freq(1000)
            motor_right.freq(1000)
        ");
    }

    [Setup]
    private async Task LoadCalibrationAsync()
    {
        await ExecuteAsync(@"
            # Load calibration data from file
            try:
                with open('calibration.json', 'r') as f:
                    calibration = json.loads(f.read())
            except:
                # Use defaults if no calibration file
                calibration = {'motor_offset': 0, 'turn_rate': 1.0}
        ");
    }
}

Setup with Error Handling

public class DisplayController : Device
{
    [Setup]
    private async Task InitializeDisplayAsync()
    {
        await ExecuteAsync(@"
            import machine
            import ssd1306

            try:
                i2c = machine.I2C(0, scl=machine.Pin(22), sda=machine.Pin(21))
                display = ssd1306.SSD1306_I2C(128, 64, i2c)
                display.fill(0)
                display.text('Ready', 0, 0, 1)
                display.show()
                print('Display initialized successfully')
            except Exception as e:
                print(f'Display initialization failed: {e}')
                raise  # Re-raise to fail the setup
        ");
    }
}

Remarks

The is used to perform one-time initialization tasks when a device connection is established. This is ideal for configuring hardware, initializing sensors, setting up global variables, or preparing the device environment.

Setup methods are executed in the order they are declared in the class hierarchy, with base class setup methods running before derived class setup methods. Multiple setup methods in the same class are executed in declaration order.

If a setup method fails, the device connection will be considered failed and subsequent setup methods will not be executed.

Constructors

SetupAttribute()

Initializes a new instance of the class.

Properties

Critical

Gets or sets a value indicating whether gets or sets whether setup failure should be treated as a critical error. When true, setup failure will cause device connection to fail completely. When false, setup failure will be logged but connection will proceed.

Order

Gets or sets the order in which this setup method should be executed relative to other setup methods in the same class.

TimeoutMs

Gets or sets the timeout for setup method execution in milliseconds. Setup methods may need longer timeouts for hardware initialization.

Methods

ToString()

Returns a string that represents the current .

Released under the Apache License 2.0.