Example #1
0
        private void ProcessBatteryState(PiTopMessage message)
        {
            var newState = BatteryState.FromMessage(message);

            if (!BatteryState.BatteryStateComparer.Equals(BatteryState, newState))
            {
                BatteryState = newState;
                BatteryStateChanged?.Invoke(this, newState);
            }
        }
Example #2
0
        public static BatteryState FromMessage(PiTopMessage message)
        {
            var args = message.Parameters.ToArray();

            var chargingState = (BatteryChargingState)int.Parse(args[0]);
            var capacity      = double.Parse(args[1]);
            var timeRemaining = TimeSpan.FromMinutes(int.Parse(args[2]));
            var wattage       = double.Parse(args[3]);
            var state         = new BatteryState(chargingState, capacity, timeRemaining, wattage);

            return(state);
        }
Example #3
0
        private PiTop4Board(IGpioController controller, Func <SpiConnectionSettings, SpiDevice> spiDeviceFactory, Func <I2cConnectionSettings, I2cDevice> i2cDeviceFactory)
        {
            BatteryState        = BatteryState.Empty;
            _controller         = controller ?? throw new ArgumentNullException(nameof(controller));
            _spiDeviceFactory   = spiDeviceFactory ?? throw new ArgumentNullException(nameof(spiDeviceFactory));
            _i2CDeviceFactory   = i2cDeviceFactory ?? throw new ArgumentNullException(nameof(i2cDeviceFactory));
            _moduleDriverClient = new ModuleDriverClient();
            _moduleDriverClient.MessageReceived += ModuleDriverClientMessageReceived;
            _disposables.Add(Disposable.Create(() =>
            {
                using var operation = Log.OnEnterAndExit();

                operation.Info("disposing devices.");
                var plates = _plates.Values.ToList();
                foreach (var piTopPlate in plates)
                {
                    piTopPlate.Dispose();
                }

                var busses = _i2cDevices.Values.ToList();
                foreach (var i2CDevice in busses)
                {
                    i2CDevice.Dispose();
                }

                var spiDevices = _spiDevices.Values.ToList();
                foreach (var spiDevice in spiDevices)
                {
                    spiDevice.Dispose();
                }
            }));

            _disposables.Add(Disposable.Create(() => _moduleDriverClient.ReleaseDisplay()));
            _moduleDriverClient.Start();
            _disposables.Add(_moduleDriverClient);
            _disposables.Add(_controller);

            _moduleDriverClient.RequestBatteryState();
        }