/// <summary> /// Handle periodic sensor readings from the brick /// </summary> /// <param name="update"></param> private void NotificationHandler(brick.LegoSensorUpdate update) { nxtcmd.I2CResponseSonarSensor inputValues = new nxtcmd.I2CResponseSonarSensor(update.Body.CommandData); if (inputValues.Success) { bool firstTime = (_state.TimeStamp == DateTime.MinValue); _state.TimeStamp = inputValues.TimeStamp; if (_state.Distance != inputValues.UltraSonicVariable || firstTime) { _state.Distance = inputValues.UltraSonicVariable; SendNotification <SonarSensorUpdate>(_subMgrPort, _state); SendNotification <pxanalogsensor.Replace>(_genericSubMgrPort, SyncGenericState()); } } }
/// <summary> /// Handle sensor notifications from the brick /// </summary> /// <param name="update"></param> private void NotificationHandler(brick.LegoSensorUpdate update) { nxtcmd.LegoResponseGetInputValues inputValues = new nxtcmd.LegoResponseGetInputValues(update.Body.CommandData); if (inputValues != null && inputValues.Success) { bool firstNotification = (_state.TimeStamp == DateTime.MinValue); _state.TimeStamp = inputValues.TimeStamp; if (_state.Intensity != inputValues.ScaledValue || firstNotification) { _state.Intensity = inputValues.ScaledValue; SendNotification <SoundSensorUpdate>(_subMgrPort, _state); SendNotification <pxanalogsensor.Replace>(_genericSubMgrPort, SyncGenericState()); } } }
/// <summary> /// Handle sensor notifications from the brick /// </summary> /// <param name="update"></param> private void NotificationHandler(brick.LegoSensorUpdate update) { LegoResponseGetInputValues inputValues = new LegoResponseGetInputValues(update.Body.CommandData); if (inputValues != null && inputValues.Success) { bool firstTime = (_state.TimeStamp == DateTime.MinValue); _state.TimeStamp = inputValues.TimeStamp; if (_state.Intensity != inputValues.ScaledValue || firstTime) { _state.Intensity = inputValues.ScaledValue; // Send notifications to both the generic and native subscribers SendNotification <pxanalogsensor.Replace>(_genericSubMgrPort, SyncGenericState()); SendNotification <Replace>(_subMgrPort, _state); } } }
private void NotificationHandler(brick.LegoSensorUpdate update) { nxtcmd.LegoResponseGetInputValues inputValues = new nxtcmd.LegoResponseGetInputValues(update.Body.CommandData); if (inputValues != null && inputValues.Success) { bool touch = (inputValues.ScaledValue == 1); if (_state.TouchSensorOn != touch || _state.TimeStamp == DateTime.MinValue) { _state.TouchSensorOn = touch; _state.TimeStamp = inputValues.TimeStamp; // Send Touch Sensor notification SendNotification <TouchSensorUpdate>(_subMgrPort, _state); // Send contact sensor notification SyncGenericContactState(); SendNotification <contactsensor.Update>(_genericSubMgrPort, _genericSensor); } } }
/// <summary> /// Receive Battery Notifications /// </summary> /// <param name="update"></param> private void NotificationHandler(brick.LegoSensorUpdate update) { nxtcmd.LegoResponseGetBatteryLevel batteryLevel = new nxtcmd.LegoResponseGetBatteryLevel(update.Body.CommandData); if (batteryLevel != null && batteryLevel.Success) { if (_state.MaxVoltage < batteryLevel.Voltage) { _state.TimeStamp = update.Body.TimeStamp; _state.MaxVoltage = batteryLevel.Voltage; SaveState(_state); } if (batteryLevel.Voltage != 0 && _state.MinVoltage > batteryLevel.Voltage) { _state.TimeStamp = update.Body.TimeStamp; _state.MinVoltage = batteryLevel.Voltage; SaveState(_state); } double percentBatteryPower = batteryLevel.Voltage / _state.MaxVoltage; if (_state.PercentBatteryPower != percentBatteryPower || _state.CurrentBatteryVoltage != batteryLevel.Voltage) { _state.TimeStamp = update.Body.TimeStamp; _state.CurrentBatteryVoltage = batteryLevel.Voltage; _state.PercentBatteryPower = percentBatteryPower; SendNotification <pxbattery.Replace>(_subMgrPort, _state.SyncGenericState(ref _genericState)); } if (_state.CurrentBatteryVoltage < _state.CriticalBatteryVoltage) { pxbattery.UpdateCriticalBattery criticalBattery = new pxbattery.UpdateCriticalBattery(); criticalBattery.PercentCriticalBattery = _state.PercentBatteryPower; SendNotification <pxbattery.SetCriticalLevel>(_subMgrPort, criticalBattery); } } }
/// <summary> /// Receive Buttons Notifications /// </summary> /// <param name="update"></param> private void NotificationHandler(brick.LegoSensorUpdate update) { // Receive Button notifications from the NXT Brick LegoResponseGetButtonState buttonsUpdate = new LegoResponseGetButtonState(update.Body.CommandData); if (buttonsUpdate != null && buttonsUpdate.Success) { if (_state.Buttons.PressedLeft != buttonsUpdate.PressedLeft || _state.Buttons.PressedEnter != buttonsUpdate.PressedEnter || _state.Buttons.PressedRight != buttonsUpdate.PressedRight || _state.Buttons.PressedCancel != buttonsUpdate.PressedCancel || _state.Buttons.TimeStamp == DateTime.MinValue) { _state.Buttons.TimeStamp = update.Body.TimeStamp; _state.Buttons.PressedLeft = buttonsUpdate.PressedLeft; _state.Buttons.PressedEnter = buttonsUpdate.PressedEnter; _state.Buttons.PressedRight = buttonsUpdate.PressedRight; _state.Buttons.PressedCancel = buttonsUpdate.PressedCancel; SendNotification <ButtonsUpdate>(_subMgrPort, _state.Buttons); } } }