public static async Task TestLPF2Device(LPF2Device device) { if (device == null) { return; } switch (device) { case ColorDistanceSensor cds: Log.Information($"ColorDistanceSensor {cds} attached, setting up event handler..."); cds.OnColorChange += async(sensor, color) => { Log.Information($"Setting color to {color}..."); await Task.Delay(TimeSpan.FromMilliseconds(1000)); await device.Hub.LED.SetColor(color); }; cds.OnDistanceChange += async(sensor, distance) => { await Task.Delay(TimeSpan.FromMilliseconds(1000)); Log.Information($"Distance is {distance}..."); }; break; case Light light: { foreach (var n in Enumerable.Range(1, 9)) { await light.SetBrightness((byte)(n * 10)); await Task.Delay(TimeSpan.FromSeconds(1)); } await light.SetBrightness(100); break; } case TachoMotor tachoMotor: Log.Information($"Testing {device}..."); await tachoMotor.SetPower(50); await Task.Delay(TimeSpan.FromSeconds(3)); await tachoMotor.Stop(); Log.Information("Done."); break; } }
private async Task OnAttachedIo(Message msg) { var portId = msg.Payload[0]; var eventType = msg.Payload[1]; var deviceType = (eventType != 0) ? BitConverter.ToUInt16(msg.Payload.Slice(2)) : 0; switch (eventType) { case 0: // Device detachment await DetachDevice(portId); if (VirtualPortMap.ContainsKey(portId)) { // Remove virtual port var name = VirtualPortMap[portId]; VirtualPortMap.Remove(portId); PortIdMap[portId] = null; Log.Debug($"VirtualPort(name={name}, id={portId}) removed."); } break; case 1: // Device attachment await AttachDevice(LPF2Device.CreateInstance(this, (LPF2DeviceType)deviceType, portId), portId); break; case 2: // Virtual port creation var firstPortName = PortIdMap[msg.Payload[4]]; var secondPortName = PortIdMap[msg.Payload[5]]; var virtualPortName = $"{firstPortName}+{secondPortName}"; var virtualPortId = msg.Payload[0]; PortIdMap[virtualPortId] = virtualPortName; VirtualPortMap[virtualPortId] = virtualPortName; Log.Debug($"VirtualPort(name={virtualPortName}, id={virtualPortId}) created."); await AttachDevice(LPF2Device.CreateInstance(this, (LPF2DeviceType)deviceType, virtualPortId), virtualPortId); break; default: Log.Warning($"Unknown attached IO event {eventType}."); break; } }