/// <summary> Fires the <c>OnHubDisconnected</c> event. </summary> /// <param name="Hub"> The WeDo Hub that just disconnected. </param> /// <param name="Reason"> The disconnection reason code. </param> protected virtual void DoHubDisconnected(wclWeDoHub Hub, Int32 Reason) { if (OnHubDisconnected != null) { OnHubDisconnected(this, Hub, Reason); } }
private void WatcherHubFound(Object Sender, Int64 Address, String Name) { // First, check that we are not connected to the HUB. if (!FHubs.ContainsKey(Address)) { // Query application about interest of this HUB. Boolean Connect; DoHubFound(Address, Name, out Connect); if (Connect) { // Prepare HUB object. wclWeDoHub Hub = new wclWeDoHub(); // Setup HUB events. Hub.OnConnected += HubConnected; Hub.OnDisconnected += HubDisconnected; // Try to connect to the given HUB. if (Hub.Connect(FRadio, Address) == wclErrors.WCL_E_SUCCESS) { // If operation started with success add HUB to the list to prevent // From adding it one more time. FHubs.Add(Address, Hub); } } } }
/// <summary> Fires the <c>OnHubConnected</c> event. </summary> /// <param name="Hub"> The WeDo HUB object that has been connected. </param> /// <param name="Error"> The connection result code. If connection completed with success the /// value is<c>WCL_E_SUCCESS</c>. </param> /// <seealso cref="wclWeDoHub"/> protected virtual void DoHubConnected(wclWeDoHub Hub, Int32 Error) { if (OnHubConnected != null) { OnHubConnected(this, Hub, Error); } }
/// <summary> Creates new Battery Level service client. </summary> /// <param name="Client"> The <see cref="wclGattClient"/> object that handles the connection /// to a WeDo device. </param> /// <param name="Hub"> The <see cref="wclWeDoHub"/> object that owns the service. </param> /// <exception cref="wclEInvalidArgument"> The exception raises if the <c>Client</c> or <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoBatteryLevelService(wclGattClient Client, wclWeDoHub Hub) : base(Client, Hub) { Uninitialize(); OnBatteryLevelChanged = null; }
/// <summary> Creates new voltage sensor device object. </summary> /// <param name="Hub"> The Hub object that owns the device. If this parameter is <c>null</c> /// the <seealso cref="wclEInvalidArgument"/> exception raises. </param> /// <param name="ConnectionId"> The device's Connection ID. </param> /// <seealso cref="wclWeDoHub"/> /// <exception cref="wclEInvalidArgument"> The exception raises when the <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoVoltageSensor(wclWeDoHub Hub, Byte ConnectionId) : base(Hub, ConnectionId) { DefaultInputFormat = new wclWeDoInputFormat(ConnectionId, wclWeDoIoDeviceType.iodVoltageSensor, 0, 30, wclWeDoSensorDataUnit.suSi, true, 0, 1); OnVoltageChanged = null; }
/// <summary> Creates new IO service client. </summary> /// <param name="Client"> The <see cref="wclGattClient"/> object that handles the connection /// to a WeDo device. </param> /// <param name="Hub"> The <see cref="wclWeDoHub"/> object that owns the service. </param> /// <exception cref="wclEInvalidArgument"> The exception raises if the <c>Client</c> or <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoIoService(wclGattClient Client, wclWeDoHub Hub) : base(Client, Hub) { // Create input formats lists first. FInputFormats = new Dictionary <Byte, wclWeDoInputFormat>(); FMissingInputFormats = new Dictionary <Byte, Boolean>(); Uninitialize(); }
private void HubConnected(Object Sender, Int32 Error) { wclWeDoHub Hub = (wclWeDoHub)Sender; DoHubConnected(Hub, Error); if (Error != wclErrors.WCL_E_SUCCESS) { FHubs.Remove(Hub.Address); } }
private void HubDisconnected(Object Sender, Int32 Reason) { wclWeDoHub Hub = (wclWeDoHub)Sender; if (FHubs.ContainsKey(Hub.Address)) { FHubs.Remove(Hub.Address); DoHubDisconnected(Hub, Reason); } }
/// <summary> Creates new color sensor device object. </summary> /// <param name="Hub"> The Hub object that owns the device. If this parameter is <c>null</c> /// the <seealso cref="wclEInvalidArgument"/> exception raises. </param> /// <param name="ConnectionId"> The device's Connection ID. </param> /// <seealso cref="wclWeDoHub"/> /// <exception cref="wclEInvalidArgument"> The exception raises when the <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoColorSensor(wclWeDoHub Hub, Byte ConnectionId) : base(Hub, ConnectionId) { AddValidDataFormat(new wclWeDoDataFormat(1, 1, 0, wclWeDoSensorDataUnit.suRaw)); DefaultInputFormat = new wclWeDoInputFormat(ConnectionId, wclWeDoIoDeviceType.iodColorSensor, 0, 1, wclWeDoSensorDataUnit.suRaw, true, 0, 1); OnColorDetected = null; }
/// <summary> Creates new IO service client. </summary> /// <param name="Client"> The <see cref="wclGattClient"/> object that handles the connection /// to a WeDo device. </param> /// <param name="Hub"> The <see cref="wclWeDoHub"/> object that owns the service. </param> /// <exception cref="wclEInvalidArgument"> The exception raises if the <c>Client</c> or <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoHubService(wclGattClient Client, wclWeDoHub Hub) : base(Client, Hub) { OnButtonStateChanged = null; OnLowVoltageAlert = null; OnDeviceAttached = null; OnDeviceDetached = null; OnHighCurrentAlert = null; OnLowSignalAlert = null; Uninitialize(); }
/// <summary> Creates new WeDo Service Client object. </summary> /// <param name="Client"> The <see cref="wclGattClient"/> object that handles the connection /// to a WeDo device. </param> /// <param name="Hub"> The <see cref="wclWeDoHub"/> object that owns the service. </param> /// <exception cref="wclEInvalidArgument"> The exception raises if the <c>Client</c> or <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoService(wclGattClient Client, wclWeDoHub Hub) { if (Client == null || Hub == null) { throw new wclEInvalidArgument("Client parameter can not be null."); } FCharacteristics = null; FClient = Client; FConnected = false; FHub = Hub; FServices = null; }
/// <summary> Gets the IO device connected to the specified port. </summary> /// <param name="Hub"> The WeDo Hub object. </param> /// <param name="Port"> The port ID. </param> /// <returns> The IO device or null. </returns> /// <seealso cref="wclWeDoHub"/> /// <seealso cref="wclWeDoIo"/> public wclWeDoIo GetIoDevice(wclWeDoHub Hub, Byte Port) { if (Hub != null) { foreach (wclWeDoIo Io in Hub.IoDevices) { if (Io.PortId == Port) { return(Io); } } } return(null); }
private wclWeDoIo GetDevice(wclWeDoHub Hub, wclWeDoIoDeviceType IoType) { if (Hub != null) { foreach (wclWeDoIo Io in Hub.IoDevices) { if (Io.DeviceType == IoType) { return(Io); } } } return(null); }
/// <summary> Gets the Tilt sensor object for the given Hub. </summary> /// <param name="Hub"> The WeDo Hub object. </param> /// <returns> The WeDo Tilt sensor object or null if not found. </returns> /// <seealso cref="wclWeDoTiltSensor"/> /// <seealso cref="wclWeDoHub"/> public List <wclWeDoTiltSensor> GetTiltSensors(wclWeDoHub Hub) { List <wclWeDoTiltSensor> Sensors = new List <wclWeDoTiltSensor>(); if (Hub != null) { foreach (wclWeDoIo Io in Hub.IoDevices) { if (Io.DeviceType == wclWeDoIoDeviceType.iodTiltSensor) { Sensors.Add(Io as wclWeDoTiltSensor); } } } return(Sensors); }
/// <summary> Gets the list of connected Motor devices. </summary> /// <param name="Hub"> The WeDo Hub object. </param> /// <returns> Te list of connected motors. </returns> /// <seealso cref="wclWeDoMotor"/> /// <seealso cref="wclWeDoHub"/> public List <wclWeDoMotor> GetMotors(wclWeDoHub Hub) { List <wclWeDoMotor> Motors = new List <wclWeDoMotor>(); if (Hub != null) { foreach (wclWeDoIo Io in Hub.IoDevices) { if (Io.DeviceType == wclWeDoIoDeviceType.iodMotor) { Motors.Add(Io as wclWeDoMotor); } } } return(Motors); }
/// <summary> Gets the IO device by its type. </summary> /// <param name="Hub"> The WeDo Hub object. </param> /// <param name="IoType"> The device type. </param> /// <returns> The IO device or null. </returns> /// <seealso cref="wclWeDoHub"/> /// <seealso cref="wclWeDoIo"/> /// <seealso cref="wclWeDoIoDeviceType"/> public List <wclWeDoIo> GetIoDevices(wclWeDoHub Hub, wclWeDoIoDeviceType IoType) { if (Hub == null) { return(null); } List <wclWeDoIo> Devices = new List <wclWeDoIo>(); foreach (wclWeDoIo Io in Hub.IoDevices) { if (Io.DeviceType == IoType) { Devices.Add(Io); } } return(Devices); }
/// <summary> Creates new RGB light device object. </summary> /// <param name="Hub"> The Hub object that owns the device. If this parameter is <c>null</c> /// the <seealso cref="wclEInvalidArgument"/> exception raises. </param> /// <param name="ConnectionId"> The device's Connection ID. </param> /// <seealso cref="wclWeDoHub"/> /// <exception cref="wclEInvalidArgument"> The exception raises when the <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoRgbLight(wclWeDoHub Hub, Byte ConnectionId) : base(Hub, ConnectionId) { AddValidDataFormat(new wclWeDoDataFormat(1, 1, (Byte)wclWeDoRgbLightMode.lmDiscrete, wclWeDoSensorDataUnit.suRaw)); AddValidDataFormat(new wclWeDoDataFormat(1, 1, (Byte)wclWeDoRgbLightMode.lmDiscrete, wclWeDoSensorDataUnit.suPercentage)); AddValidDataFormat(new wclWeDoDataFormat(1, 4, (Byte)wclWeDoRgbLightMode.lmDiscrete, wclWeDoSensorDataUnit.suSi)); AddValidDataFormat(new wclWeDoDataFormat(3, 1, (Byte)wclWeDoRgbLightMode.lmAbsolute, wclWeDoSensorDataUnit.suRaw)); AddValidDataFormat(new wclWeDoDataFormat(3, 1, (Byte)wclWeDoRgbLightMode.lmAbsolute, wclWeDoSensorDataUnit.suPercentage)); AddValidDataFormat(new wclWeDoDataFormat(3, 4, (Byte)wclWeDoRgbLightMode.lmAbsolute, wclWeDoSensorDataUnit.suSi)); DefaultInputFormat = new wclWeDoInputFormat(ConnectionId, wclWeDoIoDeviceType.iodRgb, (Byte)wclWeDoRgbLightMode.lmDiscrete, 1, wclWeDoSensorDataUnit.suRaw, true, 0, 1); FColor = DefaultColor; FColorIndex = DefaultColorIndex; OnColorChanged = null; OnModeChanged = null; }
/// <summary> Creates new motion sensor device object. </summary> /// <param name="Hub"> The Hub object that owns the device. If this parameter is <c>null</c> /// the <seealso cref="wclEInvalidArgument"/> exception raises. </param> /// <param name="ConnectionId"> The device's Connection ID. </param> /// <seealso cref="wclWeDoHub"/> /// <exception cref="wclEInvalidArgument"> The exception raises when the <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoMotionSensor(wclWeDoHub Hub, Byte ConnectionId) : base(Hub, ConnectionId) { FCount = 0; FDistance = 0; AddValidDataFormat(new wclWeDoDataFormat(1, 1, (Byte)wclWeDoMotionSensorMode.mmDetect, wclWeDoSensorDataUnit.suRaw)); AddValidDataFormat(new wclWeDoDataFormat(1, 1, (Byte)wclWeDoMotionSensorMode.mmDetect, wclWeDoSensorDataUnit.suPercentage)); AddValidDataFormat(new wclWeDoDataFormat(1, 4, (Byte)wclWeDoMotionSensorMode.mmDetect, wclWeDoSensorDataUnit.suSi)); AddValidDataFormat(new wclWeDoDataFormat(1, 4, (Byte)wclWeDoMotionSensorMode.mmCount, wclWeDoSensorDataUnit.suRaw)); AddValidDataFormat(new wclWeDoDataFormat(1, 1, (Byte)wclWeDoMotionSensorMode.mmCount, wclWeDoSensorDataUnit.suPercentage)); AddValidDataFormat(new wclWeDoDataFormat(1, 4, (Byte)wclWeDoMotionSensorMode.mmCount, wclWeDoSensorDataUnit.suSi)); DefaultInputFormat = new wclWeDoInputFormat(ConnectionId, wclWeDoIoDeviceType.iodMotionSensor, 0, 1, wclWeDoSensorDataUnit.suRaw, true, 0, 4); OnCountChanged = null; OnDistanceChanged = null; OnModeChanged = null; }
/// <summary> Creates new IO device object. </summary> /// <param name="Hub"> The Hub object that owns the device. If this parameter is <c>null</c> /// the <seealso cref="wclEInvalidArgument"/> exception raises. </param> /// <param name="ConnectionId"> The device's Connection ID. </param> /// <seealso cref="wclWeDoHub"/> /// <exception cref="wclEInvalidArgument"> The exception raises when the <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoIo(wclWeDoHub Hub, Byte ConnectionId) { if (Hub == null) { throw new wclEInvalidArgument("Hub parameter can not be null."); } FAttached = true; // It is always attached on creation! FConnectionId = ConnectionId; FDataFormats = new List <wclWeDoDataFormat>(); FDefaultInputFormat = null; FDeviceType = wclWeDoIoDeviceType.iodUnknown; FFirmwareVersion = new wclWeDoVersion(); FHardwareVersion = new wclWeDoVersion(); FHub = Hub; FInputFormat = null; FInternal = true; FPortId = 0; FNumbersFromValueData = new List <Byte[]>(); FValidDataFormats = new List <wclWeDoDataFormat>(); FValue = null; }
/// <summary> Creates new tilt sensor device object. </summary> /// <param name="Hub"> The Hub object that owns the device. If this parameter is <c>null</c> /// the <seealso cref="wclEInvalidArgument"/> exception raises. </param> /// <param name="ConnectionId"> The device's Connection ID. </param> /// <seealso cref="wclWeDoHub"/> /// <exception cref="wclEInvalidArgument"> The exception raises when the <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoTiltSensor(wclWeDoHub Hub, Byte ConnectionId) : base(Hub, ConnectionId) { AddValidDataFormat(new wclWeDoDataFormat(2, 1, (Byte)wclWeDoTiltSensorMode.tmAngle, wclWeDoSensorDataUnit.suRaw)); AddValidDataFormat(new wclWeDoDataFormat(2, 1, (Byte)wclWeDoTiltSensorMode.tmAngle, wclWeDoSensorDataUnit.suPercentage)); AddValidDataFormat(new wclWeDoDataFormat(2, 4, (Byte)wclWeDoTiltSensorMode.tmAngle, wclWeDoSensorDataUnit.suSi)); AddValidDataFormat(new wclWeDoDataFormat(1, 1, (Byte)wclWeDoTiltSensorMode.tmTilt, wclWeDoSensorDataUnit.suRaw)); AddValidDataFormat(new wclWeDoDataFormat(1, 1, (Byte)wclWeDoTiltSensorMode.tmTilt, wclWeDoSensorDataUnit.suPercentage)); AddValidDataFormat(new wclWeDoDataFormat(1, 4, (Byte)wclWeDoTiltSensorMode.tmTilt, wclWeDoSensorDataUnit.suSi)); AddValidDataFormat(new wclWeDoDataFormat(3, 1, (Byte)wclWeDoTiltSensorMode.tmCrash, wclWeDoSensorDataUnit.suRaw)); AddValidDataFormat(new wclWeDoDataFormat(3, 1, (Byte)wclWeDoTiltSensorMode.tmCrash, wclWeDoSensorDataUnit.suPercentage)); AddValidDataFormat(new wclWeDoDataFormat(3, 4, (Byte)wclWeDoTiltSensorMode.tmCrash, wclWeDoSensorDataUnit.suSi)); DefaultInputFormat = new wclWeDoInputFormat(ConnectionId, wclWeDoIoDeviceType.iodTiltSensor, (Byte)wclWeDoTiltSensorMode.tmTilt, 1, wclWeDoSensorDataUnit.suRaw, true, 0, 1); OnAngleChanged = null; OnCrashChanged = null; OnDirectionChanged = null; OnModeChanged = null; }
/// <summary> Creates new device object. </summary> /// <param name="Hub"> The Hub object that owns the device. If this parameter is <c>null</c> /// the <seealso cref="wclEInvalidArgument"/> exception raises. </param> /// <param name="ConnectionId"> The device's Connection ID. </param> /// <seealso cref="wclWeDoHub"/> /// <exception cref="wclEInvalidArgument"> The exception raises when the <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoResetableSensor(wclWeDoHub Hub, Byte ConnectionId) : base(Hub, ConnectionId) { }
// The method called by HUB when new device found (attached). internal static wclWeDoIo Attach(wclWeDoHub Hub, Byte[] RawInfo) { if (RawInfo == null || RawInfo.Length < 2) { return(null); } if ((RawInfo[1] == 1 && RawInfo.Length != 12) || (RawInfo[1] == 0 && RawInfo.Length != 2)) { return(null); } if (RawInfo[1] == 0) { // Detached??? return(null); } wclWeDoIo Io; Byte ConnectionId = RawInfo[0]; switch (RawInfo[3]) { case WEDO_DEVICE_MOTOR: Io = new wclWeDoMotor(Hub, ConnectionId); Io.FDeviceType = wclWeDoIoDeviceType.iodMotor; break; case WEDO_DEVICE_VOLTAGE_SENSOR: Io = new wclWeDoVoltageSensor(Hub, ConnectionId); Io.FDeviceType = wclWeDoIoDeviceType.iodVoltageSensor; break; case WEDO_DEVICE_CURRENT_SENSOR: Io = new wclWeDoCurrentSensor(Hub, ConnectionId); Io.FDeviceType = wclWeDoIoDeviceType.iodCurrentSensor; break; case WEDO_DEVICE_PIEZO: Io = new wclWeDoPiezo(Hub, ConnectionId); Io.FDeviceType = wclWeDoIoDeviceType.iodPiezo; break; case WEDO_DEVICE_RGB: Io = new wclWeDoRgbLight(Hub, ConnectionId); Io.FDeviceType = wclWeDoIoDeviceType.iodRgb; break; case WEDO_DEVICE_TILT_SENSOR: Io = new wclWeDoTiltSensor(Hub, ConnectionId); Io.FDeviceType = wclWeDoIoDeviceType.iodTiltSensor; break; case WEDO_DEVICE_MOTION_SENSOR: Io = new wclWeDoMotionSensor(Hub, ConnectionId); Io.FDeviceType = wclWeDoIoDeviceType.iodMotionSensor; break; case WEDO_DEVICE_COLOR_SENSOR: Io = new wclWeDoColorSensor(Hub, ConnectionId); Io.FDeviceType = wclWeDoIoDeviceType.iodColorSensor; break; default: Io = null; break; } if (Io != null) { Byte[] Tmp = new Byte[4]; Array.Copy(RawInfo, 8, Tmp, 0, 4); Io.FFirmwareVersion = wclWeDoVersion.FromByteArray(Tmp); Array.Copy(RawInfo, 4, Tmp, 0, 4); Io.FHardwareVersion = wclWeDoVersion.FromByteArray(Tmp); Io.FPortId = RawInfo[2]; Io.FInternal = (Io.PortId > 50); } return(Io); }
/// <summary> Creates new Device Information service client. </summary> /// <param name="Client"> The <see cref="wclGattClient"/> object that handles the connection /// to a WeDo device. </param> /// <param name="Hub"> The <see cref="wclWeDoHub"/> object that owns the service. </param> /// <exception cref="wclEInvalidArgument"> The exception raises if the <c>Client</c> or <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoDeviceInformationService(wclGattClient Client, wclWeDoHub Hub) : base(Client, Hub) { Uninitialize(); }
/// <summary> Creates new motor class object. </summary> /// <param name="Hub"> The Hub object that owns the device. If this parameter is <c>null</c> /// the <seealso cref="wclEInvalidArgument"/> exception raises. </param> /// <param name="ConnectionId"> The device's Connection ID. </param> /// <seealso cref="wclWeDoHub"/> /// <exception cref="wclEInvalidArgument"> The exception raises when the <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoMotor(wclWeDoHub Hub, Byte ConnectionId) : base(Hub, ConnectionId) { FDirection = wclWeDoMotorDirection.mdBraking; FPower = 0; }
/// <summary> Creates new Piezo device object. </summary> /// <param name="Hub"> The Hub object that owns the device. If this parameter is <c>null</c> /// the <seealso cref="wclEInvalidArgument"/> exception raises. </param> /// <param name="ConnectionId"> The device's Connection ID. </param> /// <seealso cref="wclWeDoHub"/> /// <exception cref="wclEInvalidArgument"> The exception raises when the <c>Hub</c> /// parameter is <c>null</c>. </exception> public wclWeDoPiezo(wclWeDoHub Hub, Byte ConnectionId) : base(Hub, ConnectionId) { }
/// <summary> Gets the voltage sensor object for the given Hub. </summary> /// <param name="Hub"> The WeDo Hub object. </param> /// <returns> The WeDo Voltage Sensor object or null if not found. </returns> /// <seealso cref="wclWeDoVoltageSensor"/> /// <seealso cref="wclWeDoHub"/> public wclWeDoVoltageSensor GetVoltageSensor(wclWeDoHub Hub) { return(GetDevice(Hub, wclWeDoIoDeviceType.iodVoltageSensor) as wclWeDoVoltageSensor); }
/// <summary> Gets the current sensor object for the given Hub. </summary> /// <param name="Hub"> The WeDo Hub object. </param> /// <returns> The WeDo Current Sensor object or null if not found. </returns> /// <seealso cref="wclWeDoCurrentSensor"/> /// <seealso cref="wclWeDoHub"/> public wclWeDoCurrentSensor GetCurrentSensor(wclWeDoHub Hub) { return(GetDevice(Hub, wclWeDoIoDeviceType.iodCurrentSensor) as wclWeDoCurrentSensor); }
/// <summary> Gets the piezo device object for the given Hub. </summary> /// <param name="Hub"> The WeDo Hub object. </param> /// <returns> The WeDo Piezo device object or null if not found. </returns> /// <seealso cref="wclWeDoPiezo"/> /// <seealso cref="wclWeDoHub"/> public wclWeDoPiezo GetPiezoDevice(wclWeDoHub Hub) { return(GetDevice(Hub, wclWeDoIoDeviceType.iodPiezo) as wclWeDoPiezo); }
/// <summary> Gets the RGB device object for the given Hub. </summary> /// <param name="Hub"> The WeDo Hub object. </param> /// <returns> The WeDo RGB device object or null if not found. </returns> /// <seealso cref="wclWeDoRgbLight"/> /// <seealso cref="wclWeDoHub"/> public wclWeDoRgbLight GetRgbDevice(wclWeDoHub Hub) { return(GetDevice(Hub, wclWeDoIoDeviceType.iodRgb) as wclWeDoRgbLight); }