Skip to content

Constructing the Brick Object

Martijn van der Elsen edited this page Sep 26, 2019 · 3 revisions

Dependency Injection and the BrickOptionsBuilder

Packages

  • Microsoft.Extensions.DependencyInjection
            ServiceProvider provider = new ServiceCollection()
            .AddBrick(opt => 
            {
                opt.DisablePowerUpSelfTest();
                opt.AddTouchSensor("touchSensorId", InputPortName.One);
            }) //adds the brick as singleton instance to servicecollection
            .BuildServiceProvider();

Dependency Injection and Configuration

Packages

  • Microsoft.Extensions.Configuration.FileExtensions
  • Microsoft.Extensions.Configuration.Json
  • Microsoft.Extensions.DependencyInjection
            IConfigurationBuilder builder = new ConfigurationBuilder()
            .SetBasePath(System.IO.Directory.GetCurrentDirectory())
            .AddJsonFile("brick.json", optional: true, reloadOnChange: true);
            IConfigurationRoot configuration = builder.Build();

            ServiceProvider provider = new ServiceCollection()
            .AddBrick(configuration) //adds the brick as singleton instance to servicecollection
            .BuildServiceProvider();

Json Configuration File

{
  "Brick": {
    "Socket": { // Assigned values are default values and can be omitted if use usb socket
      "Type": "Usb", // Usb, BlueTooth or Network
      "Address": null // Required if BlueTooth set to com port 'COM5', if Network set to ip address '192.168.2.11'
    },
    "EventMonitor": { // Assigned values are default values and can be omitted
      "Enabled": true, // if false settings below are skipped
      "Interval": 100
    },
    "PowerUpSelfTest": { // Assigned values are default values and can be omitted
      "Enabled": true, // if false settings below are skipped
      "BeepOnOK": true,
      "BeepOnAutoConnect": true,
      "BeepOnError": true,
      "AutoConnectDevices": false,
      "DisconnectOnError": true
    },
    "Devices": [ //list of connected devices
      {
        "Id": "largeMotorId", //REQUIRED set to any id for easy device retrieval
        "Type": "LargeMotor", //REQUIRED set to DeviceType: MediumMotor, LargeMotor, ColorSensor, TouchSensor, etc.
        "Port": "A", //REQUIRED set to OutputPortName or InputPortName
        "Mode": "Forward", //OPTIONAL Mode see chosen device for the mode and the default mode. can be ommited to fall back on default device mode
        "Layer": "One" //OPTIONAL only specify if multiple bricks on a daisy chain by default ONe
      },
      {
        "Id": "touchSensorId",
        "Type": "TouchSensor",
        "Port": "One"
      }
    ]
  }
}


Calling the Constructor

            BrickOptions options = new BrickOptions();
            options.UseUsbSocket();
            options.AddMediumMotor("mediumMotorId", OutputPortName.A, Polarity.Backward);
            Brick brick = new Brick(options);