PiJuiceConfig.cs 16.7 KB
Newer Older
D
Darren Fahy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Drawing;
using Iot.Device.PiJuiceDevice.Models;
using UnitsNet;

namespace Iot.Device.PiJuiceDevice
{
    /// <summary>
    /// PiJuiceConfig class to support status of the PiJuice
    /// </summary>
    public class PiJuiceConfig
    {
        private readonly PiJuice _piJuice;

20
        private readonly List<ElectricCurrent> _usbMicroCurrentLimits = new List<ElectricCurrent> { ElectricCurrent.FromAmperes(1.5), ElectricCurrent.FromAmperes(2.5) };
D
Darren Fahy 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
        private readonly List<ElectricPotential> _usbMicroDpm = new List<ElectricPotential>();

        private List<string> _batteryProfiles = new List<string>();

        /// <summary>
        /// PiJuiceConfig constructor
        /// </summary>
        /// <param name="piJuice">The PiJuice class</param>
        public PiJuiceConfig(PiJuice piJuice)
        {
            _piJuice = piJuice;

            for (int i = 0; i < 8; i++)
            {
35
                _usbMicroDpm.Add(ElectricPotential.FromVolts(4.2 + 0.8 * i));
D
Darren Fahy 已提交
36 37 38 39 40 41 42 43 44
            }
        }

        /// <summary>
        /// Get battery profile
        /// </summary>
        /// <returns>Battery profile</returns>
        public BatteryProfile GetBatteryProfile()
        {
R
Rich Lander 已提交
45 46
            byte[] response = _piJuice.ReadCommand(PiJuiceCommand.BatteryProfile, 14);
            return new BatteryProfile(
47 48 49 50 51 52 53 54 55
                ElectricCharge.FromMilliampereHours(BinaryPrimitives.ReadInt16LittleEndian(response)),
                ElectricCurrent.FromMilliamperes(response[2] * 75 + 550),
                ElectricCurrent.FromMilliamperes(response[3] * 50 + 50),
                ElectricPotential.FromMillivolts(response[4] * 20 + 3500),
                ElectricPotential.FromMillivolts(response[5] * 20),
                Temperature.FromDegreesCelsius(response[6]),
                Temperature.FromDegreesCelsius(response[7]),
                Temperature.FromDegreesCelsius(response[8]),
                Temperature.FromDegreesCelsius(response[9]),
R
Rich Lander 已提交
56
                (response[11] << 8) | response[10],
57
                ElectricResistance.FromOhms(((response[13] << 8) | response[12]) * 10));
D
Darren Fahy 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        }

        /// <summary>
        /// Set a custom battery profile
        /// </summary>
        /// <param name="batteryProfile">Custom battery profile</param>
        public void SetCustomBatteryProfile(BatteryProfile batteryProfile)
        {
            var data = new byte[14];

            data[0] = (byte)((int)(batteryProfile.Capacity.MilliampereHours) & 0xFF);
            data[1] = (byte)(((int)(batteryProfile.Capacity.MilliampereHours) >> 8) & 0xFF);
            data[2] = (byte)Math.Round((batteryProfile.ChargeCurrent.Milliamperes - 550) / 75, 0, MidpointRounding.AwayFromZero);
            data[3] = (byte)Math.Round((batteryProfile.TerminationCurrent.Milliamperes - 50) / 50, 0, MidpointRounding.AwayFromZero);
            data[4] = (byte)Math.Round((batteryProfile.RegulationVoltage.Millivolts - 3500) / 20, 0, MidpointRounding.AwayFromZero);
            data[5] = (byte)Math.Round(batteryProfile.CutOffVoltage.Millivolts / 20, 0, MidpointRounding.AwayFromZero);
            data[6] = (byte)batteryProfile.TemperatureCold.DegreesCelsius;
            data[7] = (byte)batteryProfile.TemperatureCool.DegreesCelsius;
            data[8] = (byte)batteryProfile.TemperatureWarm.DegreesCelsius;
            data[9] = (byte)batteryProfile.TemperatureHot.DegreesCelsius;
            data[10] = (byte)(batteryProfile.NegativeTemperatureCoefficientB & 0xFF);
            data[11] = (byte)((batteryProfile.NegativeTemperatureCoefficientB >> 8) & 0xFF);
            int ntcResistance = (int)batteryProfile.NegativeTemperatureCoefficientResistance.Ohms / 10;
            data[12] = (byte)(ntcResistance & 0xFF);
            data[13] = (byte)((ntcResistance >> 8) & 0xFF);

            _piJuice.WriteCommandVerify(PiJuiceCommand.BatteryProfile, data);
        }

        /// <summary>
        /// Get Battery extended profile
        /// </summary>
        /// <returns>Battery extended profile</returns>
        public BatteryExtendedProfile GetBatteryExtProfile()
        {
R
Rich Lander 已提交
93 94
            byte[] response = _piJuice.ReadCommand(PiJuiceCommand.BatteryExtendedProfile, 17);
            BatteryChemistry chemistry;
D
Darren Fahy 已提交
95 96
            if (response[0] < 2)
            {
R
Rich Lander 已提交
97
                chemistry = (BatteryChemistry)response[0];
D
Darren Fahy 已提交
98 99 100
            }
            else
            {
R
Rich Lander 已提交
101
                chemistry = BatteryChemistry.Unknown;
D
Darren Fahy 已提交
102 103
            }

R
Rich Lander 已提交
104 105
            return new BatteryExtendedProfile(
                chemistry,
106 107 108 109 110 111
                ElectricPotential.FromMillivolts((response[2] << 8) | response[1]),
                ElectricPotential.FromMillivolts((response[4] << 8) | response[3]),
                ElectricPotential.FromMillivolts((response[6] << 8) | response[5]),
                ElectricResistance.FromMilliohms(((response[8] << 8) | response[7]) / 100.0),
                ElectricResistance.FromMilliohms(((response[10] << 8) | response[9]) / 100.0),
                ElectricResistance.FromMilliohms(((response[12] << 8) | response[11]) / 100.0));
D
Darren Fahy 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        }

        /// <summary>
        /// Set custom battery extended profile
        /// </summary>
        /// <param name="batteryProfile">Custom battery extended profile</param>
        public void SetCustomBatteryExtProfile(BatteryExtendedProfile batteryProfile)
        {
            var data = new byte[17];

            data[0] = (byte)batteryProfile.BatteryChemistry;
            data[1] = (byte)((int)batteryProfile.OpenCircuitVoltage10Percent.Millivolts & 0xFF);
            data[2] = (byte)(((int)batteryProfile.OpenCircuitVoltage10Percent.Millivolts >> 8) & 0xFF);
            data[3] = (byte)((int)batteryProfile.OpenCircuitVoltage50Percent.Millivolts & 0xFF);
            data[4] = (byte)(((int)batteryProfile.OpenCircuitVoltage50Percent.Millivolts >> 8) & 0xFF);
            data[5] = (byte)((int)batteryProfile.OpenCircuitVoltage90Percent.Millivolts & 0xFF);
            data[6] = (byte)(((int)batteryProfile.OpenCircuitVoltage90Percent.Millivolts >> 8) & 0xFF);
            data[7] = (byte)((int)batteryProfile.InternalResistance10Percent.Milliohms & 0xFF);
            data[8] = (byte)(((int)batteryProfile.InternalResistance10Percent.Milliohms >> 8) & 0xFF);
            data[9] = (byte)((int)batteryProfile.InternalResistance50Percent.Milliohms & 0xFF);
            data[10] = (byte)(((int)batteryProfile.InternalResistance50Percent.Milliohms >> 8) & 0xFF);
            data[11] = (byte)((int)batteryProfile.InternalResistance90Percent.Milliohms & 0xFF);
            data[12] = (byte)(((int)batteryProfile.InternalResistance90Percent.Milliohms >> 8) & 0xFF);
            data[13] = 0xFF;
            data[14] = 0xFF;
            data[15] = 0xFF;
            data[16] = 0xFF;

            _piJuice.WriteCommandVerify(PiJuiceCommand.BatteryExtendedProfile, data);
        }

        /// <summary>
        /// Get battery profile status
        /// </summary>
        /// <returns>Battery profile status</returns>
        public BatteryProfileStatus GetBatteryProfileStatus()
        {
R
Rich Lander 已提交
149
            byte[] response = _piJuice.ReadCommand(PiJuiceCommand.BatteryProfileStatus, 1);
D
Darren Fahy 已提交
150 151
            SelectBatteryProfiles();
            int profileIndex = response[0] & 0x0F;
R
Rich Lander 已提交
152
            string batteryProfile;
D
Darren Fahy 已提交
153 154
            if (profileIndex < _batteryProfiles.Count)
            {
R
Rich Lander 已提交
155
                batteryProfile = _batteryProfiles[profileIndex];
D
Darren Fahy 已提交
156 157 158
            }
            else
            {
R
Rich Lander 已提交
159
                batteryProfile = "Unknown";
D
Darren Fahy 已提交
160 161
            }

R
Rich Lander 已提交
162 163 164 165 166
            return new BatteryProfileStatus(
                batteryProfile,
                (BatteryProfileSource)((response[0] >> 4) & 0x03),
                ((response[0] >> 6) & 0x01) == 0,
                (response[0] & 0x0F) == 0x0F ? BatteryOrigin.Custom : BatteryOrigin.Predefined);
D
Darren Fahy 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        }

        /// <summary>
        /// Get how the battery temperature is taken
        /// </summary>
        /// <returns>Battery temperature sensor configuration</returns>
        public BatteryTemperatureSense GetBatteryTemperatureSenseConfig()
        {
            var response = _piJuice.ReadCommand(PiJuiceCommand.BatteryTemperatureSensorConfig, 1);

            if ((response[0] & 0x07) < 0 || (response[0] & 0x07) > 3)
            {
                throw new ArgumentOutOfRangeException("Battery temperature sensor configuration out of range");
            }

            return (BatteryTemperatureSense)(response[0] & 0x07);
        }

        /// <summary>
        /// Set how the battery temperature is taken
        /// </summary>
        /// <param name="batteryTemperatureSense">Determine how the battery temperature is taken</param>
        public void SetBatteryTemperatureSenseConfig(BatteryTemperatureSense batteryTemperatureSense)
        {
            var response = _piJuice.ReadCommand(PiJuiceCommand.BatteryTemperatureSensorConfig, 1);

            _piJuice.WriteCommandVerify(PiJuiceCommand.BatteryTemperatureSensorConfig, new byte[] { (byte)((response[0] & (~0x07)) | (int)batteryTemperatureSense) });
        }

        /// <summary>
        /// Get battery relative state-of-health estimation type
        /// </summary>
        /// <returns>Battery relative state-of-health estimation type</returns>
        public RelativeStateOfChangeEstimationType GetRelativeStateOfChangeEstimation()
        {
            var response = _piJuice.ReadCommand(PiJuiceCommand.BatteryTemperatureSensorConfig, 1);

            return (RelativeStateOfChangeEstimationType)((response[0] & 0x30) >> 4);
        }

        /// <summary>
        /// Set battery relative state-of-health estimation type
        /// </summary>
        /// <param name="estimationType">Battery relative state-of-health estimation type</param>
        public void SetRelativeStateOfChangeEstimation(RelativeStateOfChangeEstimationType estimationType)
        {
            var response = _piJuice.ReadCommand(PiJuiceCommand.BatteryTemperatureSensorConfig, 1);

            _piJuice.WriteCommandVerify(PiJuiceCommand.BatteryTemperatureSensorConfig, new byte[] { (byte)((response[0] & (~0x30)) | ((int)estimationType) << 4) });
        }

        /// <summary>
        /// Get current battery charging state
        /// </summary>
        /// <returns>Battery charging configuration</returns>
        public ChargingConfig GetChargingConfig()
        {
R
Rich Lander 已提交
224
            byte[] response = _piJuice.ReadCommand(PiJuiceCommand.ChargingConfig, 1);
D
Darren Fahy 已提交
225

R
Rich Lander 已提交
226 227 228
            return new ChargingConfig(
                (response[0] & 0x01) == 0x01,
                (response[0] & 0x80) == 0x80);
D
Darren Fahy 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
        }

        /// <summary>
        /// Set current battery charging state
        /// </summary>
        /// <param name="config">Battery charging configuration</param>
        public void SetChargingConfig(ChargingConfig config)
        {
            var data = new byte[1];

            if (config.Enabled)
            {
                data[0] |= 0x01;
            }

            if (config.NonVolatile)
            {
                data[0] |= 0x80;
            }

            _piJuice.WriteCommandVerify(PiJuiceCommand.ChargingConfig, data);
        }

        /// <summary>
        /// Get power inputs
        /// </summary>
        /// <returns>Power inputs</returns>
        public PowerInput GetPowerInputs()
        {
R
Rich Lander 已提交
258 259 260 261 262 263 264 265
            byte[] response = _piJuice.ReadCommand(PiJuiceCommand.PowerInputsConfig, 1);
            return new PowerInput(
                (PowerInputType)(response[0] & 0x01),
                (response[0] & 0x02) == 0x02,
                (response[0] & 0x04) == 0x04,
                _usbMicroCurrentLimits[(response[0] >> 3) & 0x01],
                _usbMicroDpm[(response[0] >> 4) & 0x07],
                (response[0] & 0x80) == 0x80);
D
Darren Fahy 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        }

        /// <summary>
        /// Set power inputs
        /// </summary>
        /// <param name="powerInput">Power inputs</param>
        public void SetPowerInputs(PowerInput powerInput)
        {
            byte nonVolatile = powerInput.NonVolatile ? (byte)0x80 : (byte)0x00;
            byte precedence = powerInput.Precedence == PowerInputType.Gpio5Volt ? (byte)0x01 : (byte)0x00;
            byte gpioIn = powerInput.GpioIn ? (byte)0x02 : (byte)0x00;
            byte noBatteryTurnOn = powerInput.NoBatteryTurnOn ? (byte)0x04 : (byte)0x00;
            int index = _usbMicroCurrentLimits.IndexOf(powerInput.UsbMicroCurrentLimit);
            if (index == -1)
            {
                throw new ArgumentOutOfRangeException(nameof(powerInput.UsbMicroCurrentLimit));
            }

            byte usbMicroLimit = (byte)(index << 3);

            index = _usbMicroDpm.IndexOf(powerInput.UsbMicroDynamicPowerManagement);
            if (index == -1)
            {
                throw new ArgumentOutOfRangeException(nameof(powerInput.UsbMicroDynamicPowerManagement));
            }

            byte usbMicroDPM = (byte)((index & 0x07) << 3);

            var data = new byte[1];
            data[0] = (byte)(nonVolatile | precedence | gpioIn | noBatteryTurnOn | usbMicroLimit | usbMicroDPM);

            _piJuice.WriteCommandVerify(PiJuiceCommand.PowerInputsConfig, data);
        }

        /// <summary>
        /// Get the configuration for the specific Led
        /// </summary>
        /// <param name="led">Led to get configuration for</param>
        /// <returns>Led Configuration</returns>
        public LedConfig GetLedConfiguration(Led led)
        {
            var response = _piJuice.ReadCommand(PiJuiceCommand.LedConfig + (int)led, 4);

            if (response[0] < 0 || response[0] > 2)
            {
                throw new ArgumentOutOfRangeException("Led function type out of range");
            }

R
Rich Lander 已提交
314 315 316 317
            return new LedConfig(
                led,
                (LedFunction)response[0],
                Color.FromArgb(0, response[1], response[2], response[3]));
D
Darren Fahy 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
        }

        /// <summary>
        /// Set the configuration for the specific Led
        /// </summary>
        /// <param name="ledConfig">Led configuration</param>
        public void SetLedConfiguration(LedConfig ledConfig)
        {
            _piJuice.WriteCommandVerify(PiJuiceCommand.LedConfig + (int)ledConfig.Led, new byte[] { (byte)ledConfig.LedFunction, ledConfig.Color.R, ledConfig.Color.G, ledConfig.Color.B }, 200);
        }

        /// <summary>
        /// Get power regulator mode
        /// </summary>
        /// <returns>Power regulator mode</returns>
        public PowerRegulatorMode GetPowerRegulatorMode()
        {
            var response = _piJuice.ReadCommand(PiJuiceCommand.PowerRegulatorConfig, 1);

            return (PowerRegulatorMode)response[0];
        }

        /// <summary>
        /// Set power regulator mode
        /// </summary>
        /// <param name="powerMode">Power regulator mode</param>
        public void SetPowerRegulatorMode(PowerRegulatorMode powerMode)
        {
            _piJuice.WriteCommandVerify(PiJuiceCommand.PowerRegulatorConfig, new byte[] { (byte)powerMode });
        }

        /// <summary>
        /// Get run pin configuration
        /// </summary>
        /// <returns>Run pin configuration</returns>
        public RunPin GetRunPinConfig()
        {
            var response = _piJuice.ReadCommand(PiJuiceCommand.RunPinConfig, 1);

            if (response[0] < 0 || response[0] > 1)
            {
                throw new ArgumentOutOfRangeException("Run pin out of range.");
            }

            return (RunPin)response[0];
        }

        /// <summary>
        /// Set run pin configuration
        /// </summary>
        /// <param name="runPin">Run pin configuration</param>
        public void SetRunPinConfig(RunPin runPin)
        {
            _piJuice.WriteCommandVerify(PiJuiceCommand.RunPinConfig, new byte[] { (byte)runPin });
        }

        /// <summary>
        /// Selects which preset battery profiles to use based on the PiJuice firmware version
        /// </summary>
        private void SelectBatteryProfiles()
        {
J
jBarrineau 已提交
379
            var firmware = _piJuice.GetFirmwareVersion();
D
Darren Fahy 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
            var version = (firmware.Major << 4) + firmware.Minor;

            if (version >= 0x14)
            {
                _batteryProfiles = new List<string> { "PJZERO_1000", "BP7X_1820", "SNN5843_2300", "PJLIPO_12000", "PJLIPO_5000", "PJBP7X_1600", "PJSNN5843_1300", "PJZERO_1200", "BP6X_1400", "PJLIPO_600", "PJLIPO_500" };
            }
            else if (version == 0x13)
            {
                _batteryProfiles = new List<string> { "BP6X_1400", "BP7X_1820", "SNN5843_2300", "PJLIPO_12000", "PJLIPO_5000", "PJBP7X_1600", "PJSNN5843_1300", "PJZERO_1200", "PJZERO_1000", "PJLIPO_600", "PJLIPO_500" };
            }
            else
            {
                _batteryProfiles = new List<string> { "BP6X", "BP7X", "SNN5843", "LIPO8047109" };
            }
        }
    }
}