From 86307cd8cb3a0c3af6edfc593e79e4ab8c0346e1 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Sat, 29 Oct 2022 12:45:36 +0200 Subject: [PATCH] 0.1.13 Angle --- libraries/Angle/.arduino-ci.yml | 21 +++++- libraries/Angle/Angle.cpp | 88 ++++++++++++-------------- libraries/Angle/Angle.h | 20 +++--- libraries/Angle/CHANGELOG.md | 61 ++++++++++++++++++ libraries/Angle/README.md | 5 ++ libraries/Angle/keywords.txt | 1 + libraries/Angle/library.json | 2 +- libraries/Angle/library.properties | 2 +- libraries/Angle/test/unit_test_001.cpp | 5 +- 9 files changed, 141 insertions(+), 64 deletions(-) create mode 100644 libraries/Angle/CHANGELOG.md diff --git a/libraries/Angle/.arduino-ci.yml b/libraries/Angle/.arduino-ci.yml index e7cb4633..77a333f9 100644 --- a/libraries/Angle/.arduino-ci.yml +++ b/libraries/Angle/.arduino-ci.yml @@ -1,3 +1,18 @@ +platforms: + rpipico: + board: rp2040:rp2040:rpipico + package: rp2040:rp2040 + gcc: + features: + defines: + - ARDUINO_ARCH_RP2040 + warnings: + flags: + +packages: + rp2040:rp2040: + url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json + compile: # Choosing to run compilation tests on 2 different Arduino platforms platforms: @@ -7,5 +22,7 @@ compile: # - leonardo - m4 - esp32 - # - esp8266 - # - mega2560 \ No newline at end of file + - esp8266 + # - mega2560 + - rpipico + diff --git a/libraries/Angle/Angle.cpp b/libraries/Angle/Angle.cpp index cac8d1f6..a821fd7f 100644 --- a/libraries/Angle/Angle.cpp +++ b/libraries/Angle/Angle.cpp @@ -1,26 +1,12 @@ // // FILE: Angle.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.1.12 +// VERSION: 0.1.13 // PURPOSE: library for Angle math for Arduino // URL: https://github.com/RobTillaart/Angle // http://forum.arduino.cc/index.php?topic=339402 // -// HISTORY -// 0.1.00 initial version -// 0.1.01 clean up a bit -// 0.1.02 added toRadians() + fix compare() -// 0.1.03 added URL, fromRadians [TEST] -// 0.1.04 changed thousands in tenthousands, string constructor -// 0.1.05 added AngleFormat proxy added 03/03/15 by Christoper Andrews. -// 0.1.06 fixed bug negative values. -// 0.1.7 2020-03-26 refactor #pragma once -// 0.1.8 2020-05-27 update library.json -// 0.1.9 2020-12-10 Arduino CI -// 0.1.10 2021-01-16 readme.md + minor refactor -// 0.1.11 2021-10-17 update build-CI, -// 0.1.12 2021-12-12 update library.json, license, minor edits - +// HISTORY: see changelog.md #include "Angle.h" @@ -38,23 +24,25 @@ size_t AngleFormat::printTo(Print& p) const } -Angle::Angle(int dd, int mm, int ss, int tt) // TODO optimize +Angle::Angle(int dd, int mm, int ss, int tt) { neg = false; d = dd; m = mm; s = ss; t = tt; - // TODO - // normalize(); - // assume only one (largest) parameter is negative at most... + // TODO + // normalize(); + // assume only one (largest) parameter is negative at most... if (d < 0) { d = -d; neg = true; } if (m < 0) { m = -m; neg = true; } if (s < 0) { s = -s; neg = true; } if (t < 0) { t = -t; neg = true; } + // modulo might be faster while (t >= 10000) { s++; t -= 10000; } while (s >= 60) { m++; s -= 60; } while (m >= 60) { d++; m -= 60; } + // check special case 0 if (d == 0 && m == 0 && s == 0 && t == 0) neg = false; } @@ -67,18 +55,20 @@ Angle::Angle(const double alpha) d = int(a); a = a - d; - // unsigned long p = a * 3600000L; - // 3600 000 = 2^7 • 3^2 • 5^5 = 128 * 28125 - // 2^7 = 128 will only affect exponent - no loss precision - // 28125 is less digits so less loss of significant digits. - // upgraded to 4 decimal seconds - // 36 000 000L = 256 * 140625 + // unsigned long p = a * 3600000L; + // 3600 000 = 2^7 • 3^2 • 5^5 = 128 * 28125 + // 2^7 = 128 will only affect exponent - no loss precision + // 28125 is less digits so less loss of significant digits. + // upgraded to 4 decimal seconds + // 36 000 000L = 256 * 140625 a = a * 256; unsigned long p = round(a * 140625.0); t = p % 10000UL; p = p / 10000UL; s = p % 60UL; m = p / 60UL; + // check special case 0 + if (d == 0 && m == 0 && s == 0 && t == 0) neg = false; } @@ -87,26 +77,26 @@ Angle::Angle(const char * str) uint32_t yy = 0; uint8_t d_cnt = 0; neg = false; - // parse whole degrees + // parse whole degrees char *p = (char *) str; d = 0; - // skip crap + // skip crap while (!isdigit(*p) && (*p != '-')) p++; - // process sign + // process sign if (*p == '-') { neg = true; p++; } if (*p == '+') p++; - // parse whole part into degrees; + // parse whole part into degrees; while (isdigit(*p)) { d *= 10; d += (*p - '0'); p++; } - // parse decimal part into an uint32_t + // parse decimal part into an uint32_t if (*p != '\0') { p++; // skip . @@ -118,29 +108,31 @@ Angle::Angle(const char * str) p++; } } - // make sure we have 9 decimal places. + // make sure we have 9 decimal places. while (d_cnt < 9) { d_cnt++; yy *= 10; } - // convert float to degrees. 1000000000 ~> 36000000 -> /250 * 9 - // yy = yy * 4 / 125 + yy / 250; // just keeps the maths within 32 bits + // convert float to degrees. 1000000000 ~> 36000000 -> /250 * 9 + // yy = yy * 4 / 125 + yy / 250; // just keeps the maths within 32 bits yy = yy * 4 / 125; - yy = yy + (yy + 4)/ 8; // just keeps the maths within 32 bits - // split yy in m, s, tt + yy = yy + (yy + 4)/ 8; // just keeps the maths within 32 bits + // split yy in m, s, tt t = yy % 10000UL; yy = yy / 10000UL; s = yy % 60; m = yy / 60; + // check special case 0 + if (d == 0 && m == 0 && s == 0 && t == 0) neg = false; } -// PRINTING +// PRINTING size_t Angle::printTo(Print& p, AngleFormatMode mode) const { unsigned char c = mode; - char separator[4] = ".\'\""; // "..."; // ALT-0176 = ° 179.59.59.9999 + char separator[4] = ".\'\""; // "..."; // ALT-0176 = ° 179.59.59.9999 size_t n = 0; if (neg) n += p.print('-'); @@ -178,7 +170,7 @@ double Angle::toDouble(void) } -// NEGATE +// NEGATE Angle Angle::operator - () { Angle temp = *this; @@ -194,7 +186,7 @@ Angle Angle::operator - () }; -// BASIC MATH +// BASIC MATH Angle Angle::operator + (const Angle &a) // TOCHECK { return addHelper(a); @@ -231,20 +223,20 @@ Angle Angle::addHelper(const Angle &a) // TOCHECK } -Angle Angle::operator - (const Angle &a) // TOCHECK +Angle Angle::operator - (const Angle &a) // TOCHECK { return subHelper(a); } -Angle& Angle::operator -= (const Angle &a) // TOCHECK +Angle& Angle::operator -= (const Angle &a) // TOCHECK { *this = subHelper(a); return *this; } -Angle Angle::subHelper(const Angle &a) // TOCHECK +Angle Angle::subHelper(const Angle &a) // TOCHECK { Angle temp = *this; if (temp.neg == a.neg) @@ -292,7 +284,7 @@ Angle& Angle::operator /= (const double dd) } -// !! can divide by zero +// !! can divide by zero double Angle::operator / (Angle& a) { double f = this->toDouble(); @@ -303,14 +295,14 @@ double Angle::operator / (Angle& a) /////////////////////////////////////////////////////////// // -// PRIVATE +// PRIVATE // int Angle::compare(const Angle &a, const Angle &b) { - // check sign first + // check sign first if (!a.neg && b.neg) return 1; if (a.neg && !b.neg) return -1; - // check abs value + // check abs value int rv = 0; if (a.d > b.d) rv = 1; else if (a.d < b.d) rv = -1; @@ -326,7 +318,7 @@ int Angle::compare(const Angle &a, const Angle &b) } -void Angle::normalize() // TODO CHECK +void Angle::normalize() // TODO CHECK { while (t < 0) { s--; t += 10000; } while (t >= 10000) { s++; t -= 10000; } diff --git a/libraries/Angle/Angle.h b/libraries/Angle/Angle.h index 5dc016db..8223eef3 100644 --- a/libraries/Angle/Angle.h +++ b/libraries/Angle/Angle.h @@ -2,7 +2,7 @@ // // FILE: Angle.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.12 +// VERSION: 0.1.13 // PURPOSE: angle library for Arduino // HISTORY: See angle.cpp // @@ -15,7 +15,7 @@ #include "Printable.h" -#define ANGLE_LIB_VERSION (F("0.1.12")) +#define ANGLE_LIB_VERSION (F("0.1.13")) class Angle; @@ -57,7 +57,7 @@ public: double toRadians() { return toDouble() * PI / 180.0; }; void fromRadians(double rad) { *this = rad * 180.0/PI; }; - // EQUALITIES + // EQUALITIES bool operator == (const Angle& a) { return compare(*this, a) == 0; }; bool operator != (const Angle& a) { return compare(*this, a) != 0; }; bool operator < (const Angle& a) { return compare(*this, a) < 0; }; @@ -65,7 +65,7 @@ public: bool operator > (const Angle& a) { return compare(*this, a) > 0; }; bool operator >= (const Angle& a) { return compare(*this, a) >= 0; }; - // NEGATE + // NEGATE Angle operator - (); Angle operator + (const Angle&); @@ -80,7 +80,7 @@ public: Angle operator / (const double); Angle& operator /= (const double); - double operator / (Angle&); // ratio + double operator / (Angle&); // ratio private: void normalize(); @@ -89,11 +89,11 @@ private: Angle addHelper(const Angle &a); Angle subHelper(const Angle &a); - bool neg; // angle is negative - int d; // whole degrees - int m; // minutes - int s; // seconds - int t; // ten thousands + bool neg; // angle is negative + int d; // whole degrees + int m; // minutes + int s; // seconds + int t; // ten thousands }; diff --git a/libraries/Angle/CHANGELOG.md b/libraries/Angle/CHANGELOG.md new file mode 100644 index 00000000..3a3eb072 --- /dev/null +++ b/libraries/Angle/CHANGELOG.md @@ -0,0 +1,61 @@ +# Change Log Angle + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + + +## [0.1.13] - 2022-10-12 +- Add RP2040 support to build-CI +- Add CHANGELOG.md +- minor change unit test +- minor edits (comments) +- Add zero tests to two constructors (zero is not negative) + +## [0.1.12] - 2021-12-12 +- update library.json +- update license +- minor edits + +## [0.1.11] - 2021-10-17 +- update build-CI + +## [0.1.10] - 2021-01-16 +- update readme.md +- minor refactor + +## [0.1.9] - 2020-12-10 +- Add build Arduino CI + +## [0.1.8] - 2020-05-27 +- update library.json + +## [0.1.7] - 2020-03-26 +- refactor #pragma once + +## [0.1.06] +- fixed bug negative values + +## [0.1.05] 2015-03-03 +- added AngleFormat +- proxy added 03/03/15 by Christoper Andrews + +## [0.1.04] +- changed thousands in tenthousands +- add string constructor + +## [0.1.03] +- added URL +- fromRadians (test) + +## [0.1.02] +- added toRadians() +- fix compare() + +## [0.1.01] +- clean up a bit + +## [0.1.00] +- initial version + diff --git a/libraries/Angle/README.md b/libraries/Angle/README.md index 1b085ba3..a8c585db 100644 --- a/libraries/Angle/README.md +++ b/libraries/Angle/README.md @@ -80,5 +80,10 @@ bugs. Especially the constructor does not check input so use it carefully. - improve documentation - test more - optimize code where possible + - performance sketch +- improve code quality + - fix TODO in code + - use better variable names in code + - move all code to .cpp diff --git a/libraries/Angle/keywords.txt b/libraries/Angle/keywords.txt index b9264348..552a2988 100644 --- a/libraries/Angle/keywords.txt +++ b/libraries/Angle/keywords.txt @@ -11,6 +11,7 @@ degree KEYWORD2 minute KEYWORD2 second KEYWORD2 tenthousand KEYWORD2 + toDouble KEYWORD2 toRadians KEYWORD2 fromRadians KEYWORD2 diff --git a/libraries/Angle/library.json b/libraries/Angle/library.json index a5f0dc41..77fb7597 100644 --- a/libraries/Angle/library.json +++ b/libraries/Angle/library.json @@ -20,7 +20,7 @@ "type": "git", "url": "https://github.com/RobTillaart/Angle.git" }, - "version": "0.1.12", + "version": "0.1.13", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/libraries/Angle/library.properties b/libraries/Angle/library.properties index a61e0691..a633ca93 100644 --- a/libraries/Angle/library.properties +++ b/libraries/Angle/library.properties @@ -1,5 +1,5 @@ name=Angle -version=0.1.12 +version=0.1.13 author=Rob Tillaart maintainer=Rob Tillaart sentence=Library to convert between floating point angle to minutes hours representation. diff --git a/libraries/Angle/test/unit_test_001.cpp b/libraries/Angle/test/unit_test_001.cpp index 06cb0a2d..961cd192 100644 --- a/libraries/Angle/test/unit_test_001.cpp +++ b/libraries/Angle/test/unit_test_001.cpp @@ -39,6 +39,7 @@ unittest_setup() { + fprintf(stderr, "ANGLE_LIB_VERSION: %s\n", (char *) ANGLE_LIB_VERSION); } unittest_teardown() @@ -48,8 +49,6 @@ unittest_teardown() unittest(test_constructors) { - fprintf(stderr, "ANGLE_LIB_VERSION: %s\n", (char *) ANGLE_LIB_VERSION); - Angle a(1, 2, 3, 4); Angle b(-45, 30); Angle n(0); @@ -87,6 +86,7 @@ unittest(test_constructors) assertEqual(0, s.tenthousand()); } + unittest(test_toDouble) { Angle a(1, 2, 3, 4); @@ -108,6 +108,7 @@ unittest(test_toDouble) assertMoreOrEqual(38, fs); } + unittest(test_Radians) { Angle a(1, 2, 3, 4);