public async Task TurnOffSensor() { try { Debug.WriteLine("Begin turn off sensor: " + SensorIndex.ToString()); // Turn on sensor if (SensorIndex >= 0 && SensorIndex != SensorIndexes.KEYS && SensorIndex != SensorIndexes.IO_SENSOR && SensorIndex != SensorIndexes.REGISTERS) { if (Configuration != null) { if (Configuration.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Write)) { var writer = new Windows.Storage.Streams.DataWriter(); if (SensorIndex == SensorIndexes.MOVEMENT) { byte[] bytes = new byte[] { 0x00, 0x00 };//Fixed writer.WriteBytes(bytes); } else { writer.WriteByte((Byte)0x00); } var status = await Configuration.WriteValueAsync(writer.DetachBuffer()); } } } } catch (Exception ex) { Debug.WriteLine("Error: TurnOffSensor(): " + SensorIndex.ToString() + " " + ex.Message); } Debug.WriteLine("End turn off sensor: " + SensorIndex.ToString()); }
public async Task DisableNotify() { Debug.WriteLine("Begin DisableNotify sensor: " + SensorIndex.ToString()); try { if (Notification != null) { if (Notification.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify)) { await Notification.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.None); this.NotificationState = NotificationStates.off; } } } catch (Exception ex) { Debug.WriteLine("Error: DisableNotify(): " + SensorIndex.ToString() + " " + ex.Message); } Debug.WriteLine("End DisableNotify sensor: " + SensorIndex.ToString()); }
private async Task <bool> WriteSensor(byte[] bytes, ServiceCharacteristicsEnum characteristicEnum) { bool ret = false; Debug.WriteLine("Begin WriteSensor: " + SensorIndex.ToString()); try { if (GattService != null) { GattCharacteristic characteristic = null; GattCharacteristicProperties flag = GattCharacteristicProperties.Write; switch (characteristicEnum) { case ServiceCharacteristicsEnum.Data: characteristic = this.Data; break; case ServiceCharacteristicsEnum.Notification: flag = GattCharacteristicProperties.Notify; characteristic = this.Notification; break; case ServiceCharacteristicsEnum.Configuration: characteristic = this.Configuration; break; case ServiceCharacteristicsEnum.Period: characteristic = this.GattCharacteristicPeriod; break; case ServiceCharacteristicsEnum.Address: characteristic = this.Address; break; case ServiceCharacteristicsEnum.Device_Id: characteristic = this.Device_Id; break; } if (characteristic != null) { if (characteristic.CharacteristicProperties.HasFlag(flag)) { var writer = new Windows.Storage.Streams.DataWriter(); writer.WriteBytes(bytes); var status = await characteristic.WriteValueAsync(writer.DetachBuffer()); if (status == GattCommunicationStatus.Success) { ret = true; } } } } } catch (Exception ex) { Debug.WriteLine("Error: WriteSensor(): " + SensorIndex.ToString() + " " + ex.Message); } Debug.WriteLine("End WriteSensor " + SensorIndex.ToString()); return(ret); }
/// <summary> /// Read a readable sensor characteristic, typically the Data characteristic /// </summary> /// <param name="character">The characteristic to read from.</param> /// <returns>Buffer for data. Is created in the call</returns> public async Task <byte[]> ReadSensorBase(ServiceCharacteristicsEnum character) { byte[] bytes = null; Debug.WriteLine("Begin ReadSensorBase: " + SensorIndex.ToString()); bool ret = false; try { if (GattService != null) { bytes = new byte[DataLength[(int)SensorIndex]]; GattCharacteristic characteristic = null; GattCharacteristicProperties flag = GattCharacteristicProperties.Read; switch (character) { case ServiceCharacteristicsEnum.Data: characteristic = this.Data; break; case ServiceCharacteristicsEnum.Notification: characteristic = this.Notification; break; case ServiceCharacteristicsEnum.Configuration: characteristic = this.Configuration; break; case ServiceCharacteristicsEnum.Period: characteristic = this.GattCharacteristicPeriod; break; case ServiceCharacteristicsEnum.Address: characteristic = this.Address; break; case ServiceCharacteristicsEnum.Device_Id: characteristic = this.Device_Id; break; } if (characteristic != null) { if (characteristic.CharacteristicProperties.HasFlag(flag)) { GattReadResult result = null; try { result = await characteristic.ReadValueAsync(Windows.Devices.Bluetooth.BluetoothCacheMode.Uncached); } catch (Exception ex) { Debug.WriteLine("Error-1: ReadSensorBase(): " + SensorIndex.ToString() + " " + ex.Message); result = await characteristic.ReadValueAsync(Windows.Devices.Bluetooth.BluetoothCacheMode.Cached); } if (result != null) { var status = result.Status; if (status == GattCommunicationStatus.Success) { ret = true; var dat = result.Value; var xx = dat.GetType(); var yy = dat.Capacity; var zz = dat.Length; bytes = new byte[result.Value.Length]; Windows.Storage.Streams.DataReader.FromBuffer(result.Value).ReadBytes(bytes); } } } } } } catch (Exception ex) { Debug.WriteLine("Error: ReadSensorBase(): " + SensorIndex.ToString() + " " + ex.Message); } Debug.WriteLine("End ReadSensorBase: " + SensorIndex.ToString()); if (!ret) { bytes = null; } return(bytes); }
/// <summary> /// Manually read sensor values from their Data Characteristic /// </summary> /// <param name="disableNotify">Notify needs to be off. Can optionally set this.</param> /// <param name="updateDisplay">Whether to callback to UI with data.</param> /// <param name="turnSensorOffOn">Whether to turn sensor on before data read and off afterwards. PS: Have found reads don't work reliably with this. Only need notifications turned off.</param> /// <returns>Buffer for data. Is created in the call.</returns> public async Task <SensorData> ReadSensor(bool disableNotify, bool updateDisplay, bool turnSensorOffOn) { byte[] bytes = null; SensorData sensorData = null; Debug.WriteLine("Begin ReadSensor: " + SensorIndex.ToString()); try { if (SensorIndex >= 0 && SensorIndex != SensorIndexes.IO_SENSOR && SensorIndex != SensorIndexes.REGISTERS) { if (GattService != null) { if (disableNotify) { await DisableNotify(); } //Enable Sensor if (turnSensorOffOn) { await TurnOnSensor(); } try { bytes = await ReadSensorBase(ServiceCharacteristicsEnum.Notification);//..Data); //Disable Sensor } catch (Exception ex) { Debug.WriteLine("Error-1: ReadSensor(): " + SensorIndex.ToString() + " " + ex.Message); } if (turnSensorOffOn) { await TurnOffSensor(); } } } if ((bytes != null) && (updateDisplay)) { switch (SensorIndex) { case SensorIndexes.KEYS: sensorData = await keyChangedProc(bytes, updateDisplay); break; case SensorIndexes.IR_SENSOR: sensorData = await tempChangedProc(bytes, updateDisplay); break; case SensorIndexes.HUMIDITY: sensorData = await humidChangedProc(bytes, updateDisplay); break; case SensorIndexes.OPTICAL: sensorData = await opticalChangedProc(bytes, updateDisplay); break; case SensorIndexes.MOVEMENT: sensorData = await movementChangedProc(bytes, updateDisplay); break; case SensorIndexes.BAROMETRIC_PRESSURE: sensorData = await pressureCC2650ChangedProc(bytes, updateDisplay); break; case SensorIndexes.IO_SENSOR: break; case SensorIndexes.REGISTERS: break; default: break; } } } catch (Exception ex) { Debug.WriteLine("Error: ReadSensor(): " + SensorIndex.ToString() + " " + ex.Message); } Debug.WriteLine("End ReadSensor: " + SensorIndex.ToString()); return(sensorData); }
internal TouchSensor(BrickPiRaw brickPiRaw, SensorIndex sensorIndex) : base(brickPiRaw, sensorIndex) { this.BrickPiRaw.SensorType[(int)this.SensorIndex] = SensorTypes.TYPE_SENSOR_TOUCH; }
public async Task SetChangedNotifactionHandler() { Debug.WriteLine("Begin SetChangedNotifactionHandler sensor: " + SensorIndex.ToString()); try { if (Notification != null) { if (!HasSetCallBacks) { switch (SensorIndex) { case SensorIndexes.KEYS: Notification.ValueChanged += keyChanged; break; case SensorIndexes.IR_SENSOR: Notification.ValueChanged += tempChanged; break; case SensorIndexes.HUMIDITY: Notification.ValueChanged += humidChanged; break; case SensorIndexes.OPTICAL: Notification.ValueChanged += opticalChanged; break; case SensorIndexes.MOVEMENT: Notification.ValueChanged += movementChanged; break; case SensorIndexes.BAROMETRIC_PRESSURE: Notification.ValueChanged += pressureCC2650Changed; break; case SensorIndexes.IO_SENSOR: break; case SensorIndexes.REGISTERS: break; default: break; } HasSetCallBacks = true; } if (Notification != null) { if (Notification.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify)) { Debug.WriteLine("Awaiting SetChangedNotifactionHandler sensor: " + SensorIndex.ToString()); await Notification.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); NotificationState = NotificationStates.on; Debug.WriteLine("Awaited SetChangedNotifactionHandler sensor: " + SensorIndex.ToString()); } } } IncProgressCounter(); } catch (Exception ex) { Debug.WriteLine("Error: SetChangedNotifactionHandler(): " + SensorIndex.ToString() + " " + ex.Message); } Debug.WriteLine("(End SetChangedNotifactionHandler sensor: " + SensorIndex.ToString()); }
/// <summary> /// Initializes a new instance of the <see cref="BrickSensor"/> class. /// </summary> /// <param name="brickPiRaw"> /// The brick pi raw. /// </param> /// <param name="sensorIndex"> /// The sensor index. /// </param> internal BrickSensor(IBrickPiRaw brickPiRaw, SensorIndex sensorIndex) { this.BrickPiRaw = brickPiRaw; this.sensorIndex = sensorIndex; }
private bool checkArray(byte[] bArray) { bool ret = false; if (bArray != null) { if (bArray.Length == DataLength[(int)this.SensorIndex]) { int count = 0; for (int i = 0; i < bArray.Length; i++) { count += (int)bArray[i]; } if ((count == 0) && (chkIgnoreZeros)) { //Only optical or keys can be all zeros if (this.SensorIndex == SensorIndexes.OPTICAL) { ret = true; } else if (this.SensorIndex == SensorIndexes.KEYS) { ret = true; } else { Debug.WriteLine("Invalid byte[] recvd: All zeros " + SensorIndex.ToString()); } } else if (DataLength[(int)this.SensorIndex] != DataLengthUsed[(int)this.SensorIndex]) { //Eg Humidity uses 2 out 4 bytes count = 0; for (int i = 0; i < DataLengthUsed[(int)this.SensorIndex]; i++) { count += (int)bArray[i]; } if (count == 0) { Debug.WriteLine("Invalid used byte[] recvd: All zeros " + SensorIndex.ToString()); } else { ret = true; } } else { ret = true; } } else { Debug.WriteLine("Invalid byte[] recvd: Num bytes " + SensorIndex.ToString()); } } else { Debug.WriteLine("Invalid byte[] recvd: Null " + SensorIndex.ToString()); } if (!ret) { string str = "Invalid byte[]: "; for (int i = 0; i < bArray.Length; i++) { str += "[" + bArray[i].ToString() + "] "; } Debug.WriteLine(str); } if (ret) { //If running in periodic mode, turn notifications off after 8th Update (we still start in Update mode) if ((this.SensorIndex != SensorIndexes.MOVEMENT) && (this.SensorIndex != SensorIndexes.OPTICAL)) { if ((SetSensorsManualMode) && (PeriodicUpdatesOnly) && (this.NotificationState == NotificationStates.on)) { Task.Run(() => this.DisableNotify()).Wait(); } } System.Threading.Interlocked.Increment(ref EventCount); } return(ret); }