Merge pull request #24 from Chris--A/format-proxy

Format proxy for custom printing.
This commit is contained in:
Rob Tillaart 2015-08-23 17:22:14 +02:00
commit 295496ccbb
3 changed files with 79 additions and 13 deletions

View File

@ -21,6 +21,14 @@
#include "Angle.h" #include "Angle.h"
AngleFormat::AngleFormat( const Angle &ref, AngleFormatMode format )
: angle(ref), mode(format) {}
size_t AngleFormat::printTo(Print& p) const
{
return angle.printTo( p, mode );
}
Angle::Angle(const double alpha) Angle::Angle(const double alpha)
{ {
double a = alpha; double a = alpha;
@ -45,21 +53,34 @@ Angle::Angle(const double alpha)
} }
// PRINTING // PRINTING
size_t Angle::printTo(Print& p) const size_t Angle::printTo(Print& p, AngleFormatMode mode) const
{ {
unsigned char c = mode;
size_t n = 0; size_t n = 0;
n += p.print(d); n += p.print(d);
n += p.print('.'); n += p.print('.');
if (m < 10) n += p.print('0');
n += p.print(m);
n += p.print('\'');
if (s < 10) n += p.print('0');
n += p.print(s);
n += p.print('\"');
if (t < 100) n += p.print('0');
if (t < 10) n += p.print('0');
n += p.print(t);
if( --c )
{
if (m < 10) n += p.print('0');
n += p.print(m);
n += p.print('\'');
if( --c )
{
if (s < 10) n += p.print('0');
n += p.print(s);
n += p.print('\"');
if( c == 1 )
{
if (t < 100) n += p.print('0');
if (t < 10) n += p.print('0');
n += p.print(t);
}
}
}
return n; return n;
}; };
@ -137,7 +158,7 @@ Angle Angle::subHelper(const Angle &a)
Angle Angle::operator * (const double dd) Angle Angle::operator * (const double dd)
{ {
return Angle(this->toDouble() * dd); return Angle(this->toDouble() * dd);
} }
Angle Angle::operator / (const double dd) Angle Angle::operator / (const double dd)

View File

@ -9,6 +9,8 @@
// //
// Released to the public domain // Released to the public domain
// //
// AngleFormat proxy added 03/03/15 by Christoper Andrews.
//
#include <math.h> #include <math.h>
@ -20,7 +22,22 @@
#include "Printable.h" #include "Printable.h"
#define ANGLE_LIB_VERSION "0.1.02" #define ANGLE_LIB_VERSION "0.1.03"
class Angle;
enum AngleFormatMode{
D = 1, M, S, T
};
struct AngleFormat : Printable{
AngleFormat( const Angle &ref, AngleFormatMode format );
size_t printTo(Print& p) const;
const Angle &angle;
AngleFormatMode mode;
};
class Angle: public Printable class Angle: public Printable
{ {
@ -38,7 +55,11 @@ public:
int second() { return s; }; int second() { return s; };
int thousand() { return t; }; int thousand() { return t; };
size_t printTo(Print& p) const; size_t printTo(Print& p) const { return printTo( p, T ); }
size_t printTo(Print& p, AngleFormatMode mode) const;
AngleFormat format( AngleFormatMode format ) { return AngleFormat( *this, format ); }
double toDouble(); double toDouble();
double toRadians() { return toDouble() * PI / 180.0; }; double toRadians() { return toDouble() * PI / 180.0; };

View File

@ -0,0 +1,24 @@
/*
* Printing a formatted Angle example.
*
* Written by: Christopher Andrews (https://github.com/Chris--A)
*
* Licenced under MIT, free to use, blah blah...
*/
#include <Angle.h>
void setup() {
Serial.begin(9600);
Angle angle( 23, 12, 10, 123 );
Serial.println( angle ); // Print the most verbose version ( same as angle.format(T) )
Serial.println( "-------------------" );
Serial.println( angle.format(D) ); // Print only degree value.
Serial.println( angle.format(M) ); // Output includes minutes.
Serial.println( angle.format(S) ); // Output includes minutes, seconds.
Serial.println( angle.format(T) ); // Output includes minutes, seconds, thousands.
}
void loop() {}