Git Product home page Git Product logo

meadow.units's Introduction

Meadow.Units

This repo contains a strong unitization into the entire stack of Meadow. No more ambiguous float returns for temperature, pressure, distance, voltage, or other units. Instead sensors return strongly-typed units such as Temperature and Voltage, with properties that do automatic conversions to the various representations, such as Celsius or Fahrenheit.

Contents

Using unitization

Consider the following AnalogTemperature sensor sample:

analogTemperature.TemperatureUpdated += (object sender, IChangeResult<Temperature> result) => 
{
    Console.WriteLine($"Temp Changed, temp: {result.New.Celsius:N2}C, old: {result.Old?.Celsius:N2}C");
};

When the TemperatureUpdated event is raised, it passes an IChangeResult<Temperature> parameter. That result has a New and Old property of type Temperature, whose value can be accessed via a direct conversion to Celsius, Fahrenheit, or Kelvin on properties of the same name.

Nullable Everywhere

Not also that Meadow fully embraces C# 8’s nullable features, so the result’s Old property is nullable, and in fact, on the first reading, it will be null because there isn’t a previous reading to populate it with.

Unit types

We've added a number of unit types that describe a measure of something, including:

  • AbsoluteHumidity
  • Acceleration
  • Acceleration3D
  • Angle
  • AngularAcceleration
  • AngularAcceleration3D
  • AngularVelocity
  • Azimuth
  • Concentration
  • Current
  • Density
  • Energy
  • Frequency
  • Illuminance
  • Length
  • MagneticField
  • MagneticField3D
  • Mass
  • Power
  • Pressure
  • RelativeHumidity
  • Speed
  • Temperature
  • Torque
  • Voltage
  • Volume

Unit Conversions

Each of these units have an enum of UnitTypes that they can be described as, as well as accessed as, via properties.

For instance the Temperature type has properties such as Fahrenheit, Celsius, Kelvin, etc. that allow you to access the unit value, converted to that particular UnitType. Consider the following code:

var temp = new Temperature(32, UnitType.Fahrenheit);
Console.WriteLine($"{temp.Celsius:N2}C"); // outputs `0C`
Console.WriteLine($"{temp.Fahrenheit:N2}F"); // outputs `32F`

The units are all lightweight struct types which help to reduce heap allocations (when not boxed by Nullable), and have built in math operator and comparison support so you can perform math operations and comparison such as:

Temperature t1 = new Temperature(1);
Temperature t2 = new Temperature(10);
Assert.That(t1 != t2);
Assert.That((t1 + t2) == new Temperature(11));
Assert.That((t2 - t1) == new Temperature(9));
Assert.That(t1 < t2);

meadow.units's People

Contributors

adrianstevens avatar ctacke avatar jorgedevs avatar lamebrain avatar patridge avatar

Watchers

 avatar  avatar

meadow.units's Issues

Failing tests: adding or subtracting angles/azimuths doesn't reset when passing zero degrees

Running test from Meadow.Units.Tests results in two failing tests around math when adding angles or azimuths that cross zero degrees. Here's an example CI run and a copy of the failure output.

[xUnit.net 00:00:00.45] Meadow.Units.Tests.AzimuthTests.MathTests [FAIL]
[xUnit.net 00:00:00.45] Meadow.Units.Tests.AngleTests.ZeroCrossTests [FAIL]
Failed Meadow.Units.Tests.AzimuthTests.MathTests [7 ms]
Error Message:
Assert.Equal() Failure
Expected: 90
Actual: 450
Stack Trace:
at Meadow.Units.Tests.AzimuthTests.MathTests() in C:...\Meadow.Units\Source\Meadow.Units.Tests\AzimuthTests.cs:line 58
Failed Meadow.Units.Tests.AngleTests.ZeroCrossTests [< 1 ms]
Error Message:
Assert.Equal() Failure
Expected: 90
Actual: 450
Stack Trace:
at Meadow.Units.Tests.AngleTests.ZeroCrossTests() in C:...\Meadow.Units\Source\Meadow.Units.Tests\AngleTests.cs:line 28

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.