Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format proxy for custom printing. #24

Merged
merged 3 commits into from
Aug 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions libraries/Angle/Angle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 (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;
};

Expand Down Expand Up @@ -137,7 +158,7 @@ Angle Angle::subHelper(const Angle &a)

Angle Angle::operator * (const double dd)
{
return Angle(this->toDouble() * dd);
return Angle(this->toDouble() * dd);
}

Angle Angle::operator / (const double dd)
Expand Down
25 changes: 23 additions & 2 deletions libraries/Angle/Angle.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
//
// Released to the public domain
//
// AngleFormat proxy added 03/03/15 by Christoper Andrews.
//

#include <math.h>

Expand All @@ -20,7 +22,22 @@

#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
{
Expand All @@ -38,7 +55,11 @@ class Angle: public Printable
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; };

Expand Down
24 changes: 24 additions & 0 deletions libraries/Angle/examples/format_angle/format_angle.ino
Original file line number Diff line number Diff line change
@@ -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() {}