Beispiel #1
0
 protected AnaloguePortDeviceBase(AnaloguePort port, int deviceAddress, II2CDeviceFactory i2CDeviceFactory)
 {
     DisplayProperties = new List <DisplayPropertyBase>();
     DeviceAddress     = deviceAddress;
     I2CDeviceFactory  = i2CDeviceFactory;
     Port = port;
 }
Beispiel #2
0
        public T GetOrCreateAnalogueDevice <T>(AnaloguePort port, int deviceAddress = DefaultI2CAddress) where T : AnaloguePortDeviceBase
        {
            if (!_factories.TryGetValue(typeof(T), out var factory))
            {
                var ctor = typeof(T).GetConstructor(new[] { typeof(AnaloguePort), typeof(int) });
                if (ctor != null)
                {
                    factory = (p) =>
                    {
                        var address = p as AnalogAddress;
                        return(Activator.CreateInstance(typeof(T), address.Port, address.Address));
                    };
                    _factories[typeof(T)] = factory;
                }
            }

            if (factory != null)
            {
                var address = new AnalogAddress
                {
                    Port    = port,
                    Address = deviceAddress
                };
                var newDevice = factory.Invoke(address) as T;
                _disposables.Add(newDevice);
                _analoguePortDevices[port] = newDevice;
                return(newDevice);
            }

            throw new InvalidOperationException();
        }
Beispiel #3
0
 public RoverRobotConfiguration(
     EncoderMotorPort leftMotorPort,
     EncoderMotorPort rightMotorPort,
     ServoMotorPort panMotorPort,
     ServoMotorPort tiltMotorPort,
     DigitalPort frontUltrasoundSensorPort,
     DigitalPort backUltrasoundSensorPort,
     DigitalPort frontRightLedPort,
     DigitalPort frontLeftLedPort,
     DigitalPort backRightLedPort,
     DigitalPort backLeftLedPort,
     AnaloguePort soundSensorPort,
     Color frontRightLedColor,
     Color frontLeftLedColor,
     Color backRightLedColor,
     Color backLeftLedColor)
 {
     LeftMotorPort             = leftMotorPort;
     RightMotorPort            = rightMotorPort;
     PanMotorPort              = panMotorPort;
     TiltMotorPort             = tiltMotorPort;
     FrontUltrasoundSensorPort = frontUltrasoundSensorPort;
     BackUltrasoundSensorPort  = backUltrasoundSensorPort;
     FrontRightLedPort         = frontRightLedPort;
     FrontLeftLedPort          = frontLeftLedPort;
     BackRightLedPort          = backRightLedPort;
     BackLeftLedPort           = backLeftLedPort;
     SoundSensorPort           = soundSensorPort;
     FrontRightLedColor        = frontRightLedColor;
     FrontLeftLedColor         = frontLeftLedColor;
     BackRightLedColor         = backRightLedColor;
     BackLeftLedColor          = backLeftLedColor;
 }
Beispiel #4
0
        private static Task TestPotentiometer(AnaloguePort port)
        {
            var cancellationSource = new CancellationTokenSource();

            var board = PiTop4Board.Instance;
            var plate = board.GetOrCreatePlate <FoundationPlate>();

            Task.Run(() =>
            {
                var potentiometer = plate.GetOrCreatePotentiometer(port);

                Observable
                .Interval(TimeSpan.FromSeconds(0.5))
                .Select(_ => potentiometer.Position)
                .Subscribe(Console.WriteLine);

                Console.WriteLine("press enter key to exit");
            }, cancellationSource.Token);

            return(Task.Run(() =>
            {
                Console.ReadLine();
                board.Dispose();
                cancellationSource.Cancel(false);
            }, cancellationSource.Token));
        }
Beispiel #5
0
        public LightSensor(AnaloguePort port, int deviceAddress, II2CDeviceFactory i2CDeviceFactory, bool normalizeValue = true) : base(port, deviceAddress, i2CDeviceFactory)
        {
            _normalizeValue = normalizeValue;
            var(pin1, _)    = Port.ToPinPair();
            var bus = I2CDeviceFactory.GetOrCreateI2CDevice(deviceAddress);

            _adc = new AnalogueDigitalConverter(bus, pin1);
            AddToDisposables(_adc);
        }
Beispiel #6
0
        public Potentiometer(AnaloguePort port, int deviceAddress, bool normalizeValue = true) : base(port, deviceAddress)
        {
            _normalizeValue = normalizeValue;
            var(pin1, _)    = Port.ToPinPair();
            var bus = PiTopModule.CreateI2CDevice(deviceAddress);

            _adc = new AnalogueDigitalConverter(bus, pin1);

            AddToDisposables(_adc);
        }
Beispiel #7
0
        public T GetOrCreateAnalogueDevice <T>(AnaloguePort port, Func <AnaloguePort, int, T> factory, int deviceAddress = DefaultI2CAddress) where T : AnaloguePortDeviceBase
        {
            if (_analoguePortDevices.TryGetValue(port, out var analogueDevice) && analogueDevice is T requestedDevice)
            {
                return(requestedDevice);
            }

            var newDevice = factory(port, deviceAddress);

            _disposables.Add(newDevice);
            _analoguePortDevices[port] = newDevice;
            return(newDevice);
        }
Beispiel #8
0
        public static (int pin1, int pin2) ToPinPair(this AnaloguePort analoguePort)
        {
            switch (analoguePort)
            {
            case AnaloguePort.A0:
                return(0, 1);

            case AnaloguePort.A1:
                return(2, 3);

            case AnaloguePort.A2:
                return(4, 5);

            case AnaloguePort.A3:
                return(6, 7);

            default:
                throw new ArgumentOutOfRangeException(nameof(analoguePort), analoguePort, null);
            }
        }
Beispiel #9
0
        private static Task TestPotentiometer(AnaloguePort port)
        {
            var cancellationSource = new CancellationTokenSource();
            var plate = new Plate();

            Task.Run(() =>
            {
                var potentiometer = plate.GetOrCreateAnalogueDevice <Potentiometer>(port);

                Observable
                .Interval(TimeSpan.FromSeconds(0.5))
                .Select(_ => potentiometer.Position)
                .Subscribe(Console.WriteLine);
                PrintPlate(plate);
                Console.WriteLine("press enter key to exit");
            }, cancellationSource.Token);

            return(Task.Run(() =>
            {
                Console.ReadLine();
                plate.Dispose();
                cancellationSource.Cancel(false);
            }, cancellationSource.Token));
        }
Beispiel #10
0
 public Potentiometer(AnaloguePort port, int deviceAddress) : this(port, deviceAddress, true)
 {
 }
Beispiel #11
0
 public static Potentiometer GetOrCreatePotentiometer(this IFoundationPlate plate, AnaloguePort port)
 {
     return(plate.GetOrCreateDevice <Potentiometer>(port));
 }
Beispiel #12
0
 public static LightSensor GetOrCreateLightSensor(this IFoundationPlate plate, AnaloguePort port)
 {
     return(plate.GetOrCreateDevice <LightSensor>(port));
 }
 public SoundSensor(AnaloguePort port, int deviceAddress, II2CDeviceFactory i2CDeviceFactory) : this(port, deviceAddress, i2CDeviceFactory, true)
 {
 }
 public T GetOrCreateDevice <T>(AnaloguePort port) where T : AnaloguePortDeviceBase
 {
     return(_foundationPlate.GetOrCreateDevice <T>(port));
 }
 public T GetOrCreateDevice <T>(AnaloguePort port, Func <AnaloguePort, int, II2CDeviceFactory, T> factory, int deviceAddress) where T : AnaloguePortDeviceBase
 {
     return(_foundationPlate.GetOrCreateDevice(port, factory, deviceAddress));
 }
 public T GetOrCreateDevice <T>(AnaloguePort port, Func <AnaloguePort, int, II2CDeviceFactory, T> factory, int deviceAddress) where T : AnaloguePortDeviceBase
 {
     return(_analogueConnectedDeviceFactory.GetOrCreateDevice <T>(port, (p) => factory(p, deviceAddress, PiTop4Board)));
 }
 public T GetOrCreateDevice <T>(AnaloguePort port) where T : AnaloguePortDeviceBase
 {
     return(_analogueConnectedDeviceFactory.GetOrCreateDevice <T>(port));
 }
 protected AnaloguePortDeviceBase(AnaloguePort port, int deviceAddress)
 {
     DisplayProperties = new List <DisplayPropertyBase>();
     DeviceAddress     = deviceAddress;
     Port = port;
 }
Beispiel #19
0
 public Potentiometer(AnaloguePort port, int deviceAddress, II2CDeviceFactory i2CDeviceFactory) : this(port, deviceAddress, i2CDeviceFactory, true)
 {
 }
Beispiel #20
0
 public SoundSensor(AnaloguePort port, int deviceAddress) : this(port, deviceAddress, true)
 {
 }