Add formatting proxy class.

This allows printing of customized angles using a function
angle.format().
This commit is contained in:
Chris--A 2015-08-03 21:42:59 +10:00
parent 8c0fe83507
commit 9a731891bc
2 changed files with 54 additions and 12 deletions

View File

@ -21,6 +21,14 @@
#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)
{
double a = alpha;
@ -45,21 +53,34 @@ Angle::Angle(const double alpha)
}
// 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;
n += p.print(d);
n += p.print('.');
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;
};

View File

@ -9,6 +9,8 @@
//
// Released to the public domain
//
// AngleFormat proxy added 03/03/15 by Christoper Andrews.
//
#include <math.h>
@ -22,6 +24,21 @@
#define ANGLE_LIB_VERSION "0.1.02"
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
{
public:
@ -38,7 +55,11 @@ public:
int second() { return s; };
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 toRadians() { return toDouble() * PI / 180.0; };