private void InitializeCommands() { try { this.EnabledCommand = new RelayCommand(OnEnabledCommand, OnCanEnabledCommand); this.EnableThresholdsCommand = new RelayCommand(OnEnableThresholdsCommand, OnCanEnableThresholdsCommand); this.TriggerReadingCommand = new RelayCommand(OnTriggerReadingCommand, OnCanTriggerReadingCommand); this.Reserved1Command = new RelayCommand(OnReserved1Command, OnCanReserved1Command); this.Reserved2Command = new RelayCommand(OnReserved2Command, OnCanReserved2Command); this.Reserved3Command = new RelayCommand(OnReserved3Command, OnCanReserved3Command); this.WriteConfigurationCommand = new RelayCommand(OnWriteConfigurationCommand, OnCanWriteConfigurationCommand); this.ResetConfigurationCommand = new RelayCommand(OnResetConfigurationCommand, OnCanResetConfigurationCommand); this.SettingsCommand = new RelayCommand(OnSettingsCommand, OnCanSettingsCommand); } catch (Exception ex) { ExceptionEvent.Publish(ex); } }
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { try { if (_dhtTiny != null) { _dhtTiny.Dispose(); _dhtTiny = null; } _timer.Stop(); } catch (Exception ex) { ExceptionEvent.Publish(ex); } base.OnNavigatingFrom(e); }
private async Task FlipConfigurationBit(DhtTiny.ConfigBit bit) { try { // *** // *** Stop the timer. // *** _timer.Stop(); // *** // *** Wait for the timer event to finish. // *** _busy.WaitOne(); // *** // *** Get the current configuration bits. // *** byte configurationBits = await _dhtTiny.GetConfigurationAsync(); // *** // *** Determine the value of the bit. // *** bool isEnabled = _dhtTiny.GetConfigurationBit(configurationBits, bit); // *** // *** Set the new configuration bit. // *** await _dhtTiny.SetConfigurationAsync((byte)bit, !isEnabled); } catch (Exception ex) { ExceptionEvent.Publish(ex); } finally { // *** // *** Start the timer. // *** _timer.Start(); } }
private async void OnSettingsCommand() { try { // *** // *** Stop the timer. // *** _timer.Stop(); // *** // *** Wait for the timer event to finish. // *** _busy.WaitOne(); // *** // *** Create and open the settings dialog. // *** Settings settings = new Settings(_dhtTiny); await settings.ShowAsync(); } catch (Exception ex) { ExceptionEvent.Publish(ex); } finally { // *** // *** Update the status bar items. // *** await UpdateStatusBarItems(); // *** // *** Start the timer. // *** _timer.Start(); } }
protected async override void OnNavigatedTo(NavigationEventArgs e) { try { // *** // *** Find all devices on the bus. // *** IEnumerable <byte> address = await DhtTiny.FindAllDhtTinyAsync(this.FindAllDhtTinyCallback); if (address.Count() > 0) { this.Status = string.Format("{0} device(s) found.", address.Count()); // *** // *** Setup the DhtTiny instance. // *** _dhtTiny = new DhtTiny(address.First()); var result = await _dhtTiny.InitializeAsync(); if (result == InitializationResult.Successful) { await UpdateStatusBarItems(); // *** // *** Start the timer. // *** _timer.Start(); this.Status = "Connected/Monitoring."; } else { string message = string.Empty; switch (result) { case InitializationResult.None: message = "Initialization has not been performed."; break; case InitializationResult.NoI2cController: message = "Initialization failed due to lack of an I2C controller."; break; case InitializationResult.DeviceInUse: message = "Initialization failed due to device already in use."; break; case InitializationResult.DeviceNotFound: message = "Initialization failed because a device was not found on the I2C bus."; break; } this.Status = string.Format("Initialization Error: {0}", message); } } else { this.Status = "No DHT Tiny devices were found. Check connections and try again."; } } catch (Exception ex) { ExceptionEvent.Publish(ex); } base.OnNavigatedTo(e); }
private async void Timer_Tick(object sender, object e) { try { _busy.Reset(); this.Interval = await _dhtTiny.GetIntervalAsync(); this.ReadingId = await _dhtTiny.GetReadingIdAsync(); this.Temperature = string.Format("{0:0.0}°C", await _dhtTiny.GetTemperatureAsync()); this.Humidity = string.Format("{0:0.0}%", await _dhtTiny.GetHumidityAsync()); this.UpperThreshold = await _dhtTiny.GetUpperThresholdAsync(); this.LowerThreshold = await _dhtTiny.GetLowerThresholdAsync(); this.StartDelay = await _dhtTiny.GetStartDelayAsync(); byte statusBits = await _dhtTiny.GetStatusAsync(); byte configurationBits = await _dhtTiny.GetConfigurationAsync(); this.StatusEnabled = _dhtTiny.GetStatusBit(statusBits, DhtTiny.StatusBit.IsEnabled) ? "1" : "0"; this.StatusUpperThresholdExceeded = _dhtTiny.GetStatusBit(statusBits, DhtTiny.StatusBit.UpperThresholdExceeded) ? "1" : "0"; this.StatusLowerThresholdExceeded = _dhtTiny.GetStatusBit(statusBits, DhtTiny.StatusBit.LowerThresholdExceeded) ? "1" : "0"; this.StatusDhtReadError = _dhtTiny.GetStatusBit(statusBits, DhtTiny.StatusBit.DhtReadError) ? "1" : "0"; this.StatusReserved2 = _dhtTiny.GetStatusBit(statusBits, DhtTiny.StatusBit.Reserved2) ? "1" : "0"; this.StatusConfigurationSaved = _dhtTiny.GetStatusBit(statusBits, DhtTiny.StatusBit.ConfigSaved) ? "1" : "0"; this.StatusReadError = _dhtTiny.GetStatusBit(statusBits, DhtTiny.StatusBit.ReadError) ? "1" : "0"; this.StatusWriteError = _dhtTiny.GetStatusBit(statusBits, DhtTiny.StatusBit.WriteError) ? "1" : "0"; this.configEnabled.IsChecked = _dhtTiny.GetConfigurationBit(configurationBits, DhtTiny.ConfigBit.SensorEnabled) ? true : false; this.configThresholdEnabled.IsChecked = _dhtTiny.GetConfigurationBit(configurationBits, DhtTiny.ConfigBit.ThresholdEnabled) ? true : false; this.configTriggerReading.IsChecked = _dhtTiny.GetConfigurationBit(configurationBits, DhtTiny.ConfigBit.TriggerReading) ? true : false; this.reserved1.IsChecked = _dhtTiny.GetConfigurationBit(configurationBits, DhtTiny.ConfigBit.Reserved1) ? true : false; this.reserved2.IsChecked = _dhtTiny.GetConfigurationBit(configurationBits, DhtTiny.ConfigBit.Reserved2) ? true : false; this.reserved3.IsChecked = _dhtTiny.GetConfigurationBit(configurationBits, DhtTiny.ConfigBit.Reserved3) ? true : false; this.writeConfiguration.IsChecked = _dhtTiny.GetConfigurationBit(configurationBits, DhtTiny.ConfigBit.WriteConfig) ? true : false; this.resetConfiguration.IsChecked = _dhtTiny.GetConfigurationBit(configurationBits, DhtTiny.ConfigBit.ResetConfig) ? true : false; this.EnabledCommand.RaiseCanExecuteChanged(); this.EnableThresholdsCommand.RaiseCanExecuteChanged(); this.TriggerReadingCommand.RaiseCanExecuteChanged(); this.Reserved1Command.RaiseCanExecuteChanged(); this.Reserved2Command.RaiseCanExecuteChanged(); this.Reserved3Command.RaiseCanExecuteChanged(); this.WriteConfigurationCommand.RaiseCanExecuteChanged(); this.ResetConfigurationCommand.RaiseCanExecuteChanged(); this.SettingsCommand.RaiseCanExecuteChanged(); } catch (Exception ex) { ExceptionEvent.Publish(ex); } finally { this.EnabledCommand.RaiseCanExecuteChanged(); this.EnableThresholdsCommand.RaiseCanExecuteChanged(); this.TriggerReadingCommand.RaiseCanExecuteChanged(); this.Reserved1Command.RaiseCanExecuteChanged(); this.Reserved2Command.RaiseCanExecuteChanged(); this.Reserved3Command.RaiseCanExecuteChanged(); this.WriteConfigurationCommand.RaiseCanExecuteChanged(); this.ResetConfigurationCommand.RaiseCanExecuteChanged(); this.SettingsCommand.RaiseCanExecuteChanged(); _busy.Set(); } }