0.2.0 weight

This commit is contained in:
rob tillaart 2022-12-05 20:23:09 +01:00
parent 4ed681cb0f
commit d3a8b65584
8 changed files with 380 additions and 12 deletions

View File

@ -6,12 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.0] - 2022-12-05
- add weightConverter class
- update readme.md
- update unit test
## [0.1.4] - 2022-11-26
- Add RP2040 support to build-CI.
- Add CHANGELOG.md
- update readme.md
## [0.1.3] - 2021-12-29
- update library.json
- update readme.md

View File

@ -15,7 +15,7 @@ Library of weight conversion functions.
Weight.h is a library containing weight conversion functions.
Open for additions, including obscure weight metrics or
Open for additions, including obscure weight metrics or
weight related math functions and constants.
@ -54,20 +54,86 @@ Functions are straightforward.
See examples
----
# weightConverter class
Since version 0.2.0 a weight convertor class is added to convert to and from
other (less known) weight scales.
The idea is to set a value in one scale and retrieve it in the other.
As the converter holds the last value set, multiple conversions of the same
weight are easy.
New conversions are possible by providing a setXYZ and getXYZ() function.
These two should convert to and from grams respectively.
Additions are welcome, please open an issue.
Internal representation is the gram as it the standard.
## Interface
#### Constructor
- **weightConverter()**
#### setters
- **void setKilogram(float value = 0)**
- **void setGram(float value = 0)**
- **void setLBS(float value = 0)**
- **void setStone(float value = 0)**
- **void setOunce(float value = 0)**
- **void setLongTonUK(float value = 0)**
- **void setShortTonUS(float value = 0)**
- **void setQuarterUK(float value = 0)**
- **void setQuarterUS(float value = 0)**
- **void setSlug(float value = 0)**
- **void setTroyPound(float value = 0)**
- **void setTroyOunce(float value = 0)**
- **void setRobie(float value = 0)**
- **void setDram(float value = 0)**
- **void setDrachme(float value = 0)**
- **void setPoint(float value = 0)**
- **void setGrain(float value = 0)**
- **void setCarat(float value = 0)**
#### getters
- **float getKilogram()**
- **float getGram()**
- **float getLBS()**
- **float getStone()**
- **float getOunce()**
- **float getLongTonUK()**
- **float getShortTonUS()**
- **float getQuarterUK()**
- **float getQuarterUS()**
- **float getSlug()**
- **float getTroyPound()**
- **float getTroyOunce()**
- **float getRobie()**
- **float getDram()**
- **float getDrachme()**
- **float getPoint()**
- **float getGrain()**
- **float getCarat()**
## Future
#### must
- improve documentation
- where are units used
#### should
- a class like the temperature convertor and pressure convertor
- internally grams?
- getters setters
#### could
- create data types of stone lbs kilo etc.?
#### won't (unless)
- large masses - sun planets ? (see relativity library)
- gravity constants of planets REL or ABS eg REL_GRAVITY_EARTH 1.0

View File

@ -0,0 +1,121 @@
//
// FILE: weightConverter_test.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// DATE: 2022-12-05
#include "weight.h"
const float accuracy = 0.0001;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.println();
weightConvertor WC;
WC.setKilogram(1);
Serial.print(WC.getKilogram(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setGram(1);
Serial.print(WC.getGram(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setLBS(1);
Serial.print(WC.getLBS(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setStone(1);
Serial.print(WC.getStone(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setOunce(1);
Serial.print(WC.getOunce(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setLongTonUK(1);
Serial.print(WC.getLongTonUK(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setShortTonUS(1);
Serial.print(WC.getShortTonUS(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setQuarterUK(1);
Serial.print(WC.getQuarterUK(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setQuarterUS(1);
Serial.print(WC.getQuarterUS(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setSlug(1);
Serial.print(WC.getSlug(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setTroyPound(1);
Serial.print(WC.getTroyPound(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setTroyOunce(1);
Serial.print(WC.getTroyOunce(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setRobie(1);
Serial.print(WC.getRobie(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setDram(1);
Serial.print(WC.getDram(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setDrachme(1);
Serial.print(WC.getDrachme(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setPoint(1);
Serial.print(WC.getPoint(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setGrain(1);
Serial.print(WC.getGrain(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
WC.setCarat(1);
Serial.print(WC.getCarat(), 4);
Serial.print("\t");
Serial.println(WC.getGram(), 4);
Serial.print("\ndone...");
}
void loop()
{
}
// -- END OF FILE --

View File

@ -1,6 +1,8 @@
# Syntax Colouring Map For weight
# Data types (KEYWORD1)
weightConvertor KEYWORD1
# Methods and Functions (KEYWORD2)
lbs2kilo KEYWORD2
@ -20,6 +22,48 @@ US2metric KEYWORD2
metric2US KEYWORD2
####################################
setKilogram KEYWORD2
setGram KEYWORD2
setLBS KEYWORD2
setStone KEYWORD2
setOunce KEYWORD2
setLongTonUK KEYWORD2
setShortTonUS KEYWORD2
setQuarterUK KEYWORD2
setQuarterUS KEYWORD2
setSlug KEYWORD2
setTroyPound KEYWORD2
setTroyOunce KEYWORD2
setRobie KEYWORD2
setDram KEYWORD2
setDrachme KEYWORD2
setPoint KEYWORD2
setGrain KEYWORD2
setCarat KEYWORD2
getKilogram KEYWORD2
getGram KEYWORD2
getLBS KEYWORD2
getStone KEYWORD2
getOunce KEYWORD2
getLongTonUK KEYWORD2
getShortTonUS KEYWORD2
getQuarterUK KEYWORD2
getQuarterUS KEYWORD2
getSlug KEYWORD2
getTroyPound KEYWORD2
getTroyOunce KEYWORD2
getRobie KEYWORD2
getDram KEYWORD2
getDrachme KEYWORD2
getPoint KEYWORD2
getGrain KEYWORD2
getCarat KEYWORD2
# Instances (KEYWORD2)

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/weight"
},
"version": "0.1.4",
"version": "0.2.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=weight
version=0.1.4
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library of weight conversion functions

View File

@ -126,6 +126,84 @@ unittest(test_US2metric)
}
unittest(test_weightConvertor)
{
weightConvertor WC;
fprintf(stderr, "setKilogram\n");
WC.setKilogram(1);
assertEqualFloat(1, WC.getKilogram(), 0.0001);
fprintf(stderr, "setGram\n");
WC.setGram(1);
assertEqualFloat(1, WC.getGram(), 0.0001);
fprintf(stderr, "setLBS\n");
WC.setLBS(1);
assertEqualFloat(1, WC.getLBS(), 0.0001);
fprintf(stderr, "setStone\n");
WC.setStone(1);
assertEqualFloat(1, WC.getStone(), 0.0001);
fprintf(stderr, "setOunce\n");
WC.setOunce(1);
assertEqualFloat(1, WC.getOunce(), 0.0001);
fprintf(stderr, "setLongTonUK\n");
WC.setLongTonUK(1);
assertEqualFloat(1, WC.getLongTonUK(), 0.0001);
fprintf(stderr, "setShortTonUS\n");
WC.setShortTonUS(1);
assertEqualFloat(1, WC.getShortTonUS(), 0.0001);
fprintf(stderr, "setQuarterUK\n");
WC.setQuarterUK(1);
assertEqualFloat(1, WC.getQuarterUK(), 0.0001);
fprintf(stderr, "setQuarterUS\n");
WC.setQuarterUS(1);
assertEqualFloat(1, WC.getQuarterUS(), 0.0001);
fprintf(stderr, "setSlug\n");
WC.setSlug(1);
assertEqualFloat(1, WC.getSlug(), 0.0001);
fprintf(stderr, "setTroyPound\n");
WC.setTroyPound(1);
assertEqualFloat(1, WC.getTroyPound(), 0.0001);
fprintf(stderr, "setTroyOunce\n");
WC.setTroyOunce(1);
assertEqualFloat(1, WC.getTroyOunce(), 0.0001);
fprintf(stderr, "setRobie\n");
WC.setRobie(1);
assertEqualFloat(1, WC.getRobie(), 0.0001);
fprintf(stderr, "setDram\n");
WC.setDram(1);
assertEqualFloat(1, WC.getDram(), 0.0001);
fprintf(stderr, "setDrachme\n");
WC.setDrachme(1);
assertEqualFloat(1, WC.getDrachme(), 0.0001);
fprintf(stderr, "setPoint\n");
WC.setPoint(1);
assertEqualFloat(1, WC.getPoint(), 0.0001);
fprintf(stderr, "setGrain\n");
WC.setGrain(1);
assertEqualFloat(1, WC.getGrain(), 0.0001);
fprintf(stderr, "setCarat\n");
WC.setCarat(1);
assertEqualFloat(1, WC.getCarat(), 0.0001);
}
unittest_main()
// --------

View File

@ -2,7 +2,7 @@
//
// FILE: weight.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// VERSION: 0.2.0
// PURPOSE: Collection weight conversion functions
// URL: https://github.com/RobTillaart/weight
@ -10,13 +10,13 @@
#include "Arduino.h"
#define WEIGHT_LIB_VERSION (F("0.1.4"))
#define WEIGHT_LIB_VERSION (F("0.2.0"))
float lbs2kilo(float lbs) { return lbs * 0.45359237; };
float kilo2lbs(float kilos) { return kilos * 2.20462262; };
float kilo2lbs(float kilos) { return kilos * 2.20462262185; };
float ounce2gram(float ounce) { return ounce * 28.3495231; };
float ounce2gram(float ounce) { return ounce * 28.349523125; };
float gram2ounce(float gram) { return gram * 0.03527396195; };
float gram2kilo(float gram) { return gram * 0.001; };
@ -45,7 +45,7 @@ float US2metric(float stone, float lbs, float ounce)
// returns lbs;
float metric2US(float kilo, float &stone, float &lbs, float &ounce)
{
float val = kilo * 2.20462262;
float val = kilo * 2.20462262185;
lbs = val;
stone = int(lbs * 0.0714285714);
lbs -= stone * 14;
@ -55,5 +55,59 @@ float metric2US(float kilo, float &stone, float &lbs, float &ounce)
}
/////////////////////////////////////////////////////////////
//
// WEIGHT CONVERTER CLASS
//
class weightConvertor
{
public:
weightConvertor() { _w = 0; };
// SETTERS
void setKilogram(float value = 0) { _w = value * 1000; };
void setGram(float value = 0) { _w = value; };
void setLBS(float value = 0) { _w = value * 453.59237; };
void setStone(float value = 0) { _w = value * 6350.29318; };
void setOunce(float value = 0) { _w = value * 28.349523125; };
void setLongTonUK(float value = 0) { _w = value * 1016046.9088; };
void setShortTonUS(float value = 0) { _w = value * 907184.74; };
void setQuarterUK(float value = 0) { _w = value * 12700.58636; };
void setQuarterUS(float value = 0) { _w = value * 11339.80925; };
void setSlug(float value = 0) { _w = value * 14593.903; };
void setTroyPound(float value = 0) { _w = value * 373.2417216; };
void setTroyOunce(float value = 0) { _w = value * 31.1034768; };
void setRobie(float value = 0) { _w = value * 10; };
void setDram(float value = 0) { _w = value * 1.77184519531; };
void setDrachme(float value = 0) { _w = value * 3.8; };
void setPoint(float value = 0) { _w = value * 0.002; };
void setGrain(float value = 0) { _w = value * 0.0647989; };
void setCarat(float value = 0) { _w = value * 0.2; };
// GETTERS
float getKilogram() { return _w * 0.001; }
float getGram() { return _w; }
float getLBS() { return _w * 0.00220462262185; }
float getStone() { return _w * 0.000157473044418;}
float getOunce() { return _w * 0.03527396195; }
float getLongTonUK() { return _w * 9.84206527611e-7;};
float getShortTonUS() { return _w * 0.00000110231131092;};
float getQuarterUK() { return _w * 0.0000787365222089;};
float getQuarterUS() { return _w * 0.000088184904874;};
float getSlug() { return _w * 0.000068521765562;};
float getTroyPound() { return _w * 0.00267922888072;};
float getTroyOunce() { return _w * 0.0321507465686;};
float getRobie() { return _w * 0.1;};
float getDram() { return _w * 0.564383391193;};
float getDrachme() { return _w * 0.263157894737;};
float getPoint() { return _w * 500;};
float getGrain() { return _w * 15.4323607345;};
float getCarat() { return _w * 5; };
private:
float _w; // grams
};
// -- END OF FILE --