using System; namespace RoboidControl { /// /// A sensor which can detect touches /// public class DigitalSensor : Thing { /// /// Create a digital sensor without communication abilities /// /// Invoke a OnNewThing event when the thing has been created public DigitalSensor(bool invokeEvent = true) : base(Type.Switch, invokeEvent) { } /// /// Create a digital sensor for a participant /// /// The owning participant /// The ID of the thing, leave out or set to zero to generate an ID /// Invoke a OnNewThing event when the thing has been created public DigitalSensor(Participant owner, byte thingId = 0, bool invokeEvent = true) : base(owner, Type.Switch, thingId, invokeEvent) { } /// /// Create a new child digital sensor /// /// The parent thing /// The ID of the thing, leave out or set to zero to generate an ID /// Invoke a OnNewThing event when the thing has been created public DigitalSensor(Thing parent, byte thingId = 0, bool invokeEvent = true) : base(parent, Type.Switch, thingId, invokeEvent) { } /// /// Value which is true when the sensor is touching something, false otherwise /// private bool _state = false; public bool state { get { return _state; } set { if (_state != value) { _state = value; } stateUpdated = true; } } private bool stateUpdated = false; #if UNITY_5_3_OR_NEWER /// @copydoc Passer::RoboidControl::Thing::CreateComponent public override void CreateComponent() { this.component = Unity.DigitalSensor.Create(this); this.component.core = this; } #endif /// /// Function used to generate binary data for this digital sensor /// /// A byte array with the binary data /// The byte array will be empty when the digital status has not changed public override byte[] GenerateBinary() { if (!stateUpdated) return Array.Empty(); byte[] bytes = new byte[1]; bytes[0] = (byte)(state ? 1 : 0); stateUpdated = false; return bytes; } /// /// Function used to process binary data received for this digital sensor /// /// The binary data to process public override void ProcessBinary(byte[] bytes) { this.state |= (bytes[0] == 1); } } }