Skip to content

Commit 99d954d

Browse files
refactor: moved more logic components into core
1 parent 6b0edcb commit 99d954d

41 files changed

Lines changed: 2113 additions & 526 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

PLAN.md

Lines changed: 156 additions & 61 deletions
Large diffs are not rendered by default.

src/DayTime.cpp

Lines changed: 5 additions & 245 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
#include "DayTime.hpp"
55

66
///////////////////////////////////
7-
// DayTime (and Declination below)
8-
//
9-
// A class to handle hours, minutes, seconds in a unified manner, allowing
10-
// addition of hours, minutes, seconds, other times and conversion to string.
7+
// DayTime — Arduino-dependent overlay methods
8+
// Pure arithmetic is in core::DayTime (src/core/types/DayTime.hpp/.cpp)
119

1210
// Parses the RA or DEC from a string that has an optional sign, a two digit degree, a seperator, a two digit minute, a seperator and a two digit second.
1311
// Does not correct for hemisphere (derived class Declination takes care of that)
@@ -59,139 +57,6 @@ DayTime DayTime::ParseFromMeade(String const &s)
5957
return result;
6058
}
6159

62-
DayTime::DayTime()
63-
{
64-
totalSeconds = 0;
65-
}
66-
67-
DayTime::DayTime(const DayTime &other)
68-
{
69-
totalSeconds = other.totalSeconds;
70-
}
71-
72-
DayTime::DayTime(int h, int m, int s)
73-
{
74-
long sgn = sign(h);
75-
h = abs(h);
76-
totalSeconds = sgn * ((60L * h + m) * 60L + s);
77-
}
78-
79-
DayTime::DayTime(float timeInHours)
80-
{
81-
long sgn = fsign(timeInHours);
82-
timeInHours = fabsf(timeInHours);
83-
totalSeconds = sgn * static_cast<long>(roundf(timeInHours * 60.0f * 60.0f));
84-
}
85-
86-
int DayTime::getHours() const
87-
{
88-
int h, m, s;
89-
getTime(h, m, s);
90-
return h;
91-
}
92-
93-
int DayTime::getMinutes() const
94-
{
95-
int h, m, s;
96-
getTime(h, m, s);
97-
return m;
98-
}
99-
100-
int DayTime::getSeconds() const
101-
{
102-
int h, m, s;
103-
getTime(h, m, s);
104-
return s;
105-
}
106-
107-
float DayTime::getTotalHours() const
108-
{
109-
return 1.0f * totalSeconds / 3600.0f;
110-
}
111-
112-
float DayTime::getTotalMinutes() const
113-
{
114-
return 1.0f * totalSeconds / 60.0f;
115-
}
116-
117-
long DayTime::getTotalSeconds() const
118-
{
119-
return totalSeconds;
120-
}
121-
122-
void DayTime::getTime(int &h, int &m, int &s) const
123-
{
124-
long seconds = labs(totalSeconds);
125-
126-
h = (int) (seconds / 3600L);
127-
seconds = seconds - (h * 3600L);
128-
m = (int) (seconds / 60L);
129-
s = (int) (seconds - (m * 60L));
130-
131-
h *= sign(totalSeconds);
132-
}
133-
134-
void DayTime::set(int h, int m, int s)
135-
{
136-
DayTime dt(h, m, s);
137-
totalSeconds = dt.totalSeconds;
138-
checkHours();
139-
}
140-
141-
void DayTime::set(const DayTime &other)
142-
{
143-
totalSeconds = other.totalSeconds;
144-
checkHours();
145-
}
146-
147-
// Add hours, wrapping days (which are not tracked)
148-
void DayTime::addHours(float deltaHours)
149-
{
150-
totalSeconds += long(deltaHours * 3600L);
151-
checkHours();
152-
}
153-
154-
void DayTime::checkHours()
155-
{
156-
while (totalSeconds >= secondsPerDay)
157-
{
158-
totalSeconds -= secondsPerDay;
159-
}
160-
161-
while (totalSeconds < 0)
162-
{
163-
totalSeconds += secondsPerDay;
164-
}
165-
}
166-
167-
// Add minutes, wrapping hours if needed
168-
void DayTime::addMinutes(int deltaMins)
169-
{
170-
totalSeconds += deltaMins * 60;
171-
checkHours();
172-
}
173-
174-
// Add seconds, wrapping minutes and hours if needed
175-
void DayTime::addSeconds(long deltaSecs)
176-
{
177-
totalSeconds += deltaSecs;
178-
checkHours();
179-
}
180-
181-
// Add another time, wrapping seconds, minutes and hours if needed
182-
void DayTime::addTime(const DayTime &other)
183-
{
184-
totalSeconds += other.totalSeconds;
185-
checkHours();
186-
}
187-
188-
// Subtract another time, wrapping seconds, minutes and hours if needed
189-
void DayTime::subtractTime(const DayTime &other)
190-
{
191-
totalSeconds -= other.totalSeconds;
192-
checkHours();
193-
}
194-
19560
char achBuf[32];
19661

19762
// Convert to a standard string (like 14:45:06)
@@ -245,112 +110,7 @@ const char *DayTime::ToString() const
245110
snprintf(p, remaining, " (%s)", floatStr.c_str());
246111
return achBuf;
247112
}
248-
void DayTime::printTwoDigits(char *achDegs, int num) const
249-
{
250-
achDegs[0] = '0' + (num / 10);
251-
achDegs[1] = '0' + (num % 10);
252-
achDegs[2] = 0;
253-
}
254113

255-
const char *DayTime::formatStringImpl(char *targetBuffer, const char *format, char sgn, long degs, long mins, long secs) const
256-
{
257-
char achDegs[5];
258-
char achMins[3];
259-
char achSecs[3];
260-
const char *f = format;
261-
char *p = targetBuffer;
262-
263-
int i = 0;
264-
if (sgn != '\0')
265-
{
266-
achDegs[0] = sgn;
267-
i++;
268-
}
269-
270-
long absdegs = labs(degs);
271-
if (absdegs >= 100)
272-
{
273-
achDegs[i++] = '0' + min(9L, (absdegs / 100));
274-
absdegs = absdegs % 100;
275-
}
276-
277-
printTwoDigits(achDegs + i, absdegs);
278-
printTwoDigits(achMins, mins);
279-
printTwoDigits(achSecs, secs);
280-
281-
char macro = '\0';
282-
bool inMacro = false;
283-
while (*f)
284-
{
285-
switch (*f)
286-
{
287-
case '{':
288-
{
289-
inMacro = true;
290-
}
291-
break;
292-
case '}':
293-
{
294-
if (inMacro)
295-
{
296-
switch (macro)
297-
{
298-
case '+':
299-
{
300-
*p++ = (degs < 0 ? '-' : '+');
301-
}
302-
break;
303-
case 'd':
304-
{
305-
strcpy(p, achDegs);
306-
p += strlen(achDegs);
307-
}
308-
break;
309-
case 'm':
310-
{
311-
strcpy(p, achMins);
312-
p += 2;
313-
}
314-
break;
315-
case 's':
316-
{
317-
strcpy(p, achSecs);
318-
p += 2;
319-
}
320-
break;
321-
}
322-
inMacro = false;
323-
}
324-
}
325-
break;
326-
default:
327-
{
328-
if (inMacro)
329-
{
330-
macro = *f;
331-
}
332-
else
333-
{
334-
*p++ = *f;
335-
}
336-
}
337-
}
338-
f++;
339-
}
340-
341-
*p = '\0';
342-
return targetBuffer;
343-
}
344-
345-
const char *DayTime::formatString(char *targetBuffer, const char *format, long *pSecs) const
346-
{
347-
long secs = pSecs == nullptr ? totalSeconds : *pSecs;
348-
char sgn = secs < 0 ? '-' : '+';
349-
secs = labs(secs);
350-
long degs = secs / 3600;
351-
secs = secs - degs * 3600;
352-
long mins = secs / 60;
353-
secs = secs - mins * 60;
354-
355-
return formatStringImpl(targetBuffer, format, sgn, degs, mins, secs);
356-
}
114+
// ParseFromMeade, formatString, formatStringImpl, printTwoDigits
115+
// were previously here. formatString/formatStringImpl/printTwoDigits
116+
// are now in core::DayTime (pure).

src/DayTime.hpp

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,23 @@
11
#pragma once
22

3+
#include "core/types/DayTime.hpp"
4+
35
// A class to handle hours, minutes, seconds in a unified manner, allowing
46
// addition of hours, minutes, seconds, other times and conversion to string.
57

68
// Forward declarations
79
class String;
810

9-
// DayTime handles a 24-hour time.
10-
class DayTime
11+
/// Thin overlay over core::DayTime that adds Arduino-dependent methods
12+
/// (String-based parsing, formatting, logging).
13+
class DayTime : public core::DayTime
1114
{
12-
protected:
13-
long totalSeconds;
14-
1515
public:
16-
DayTime();
17-
18-
DayTime(const DayTime &other);
19-
DayTime(int h, int m, int s);
20-
21-
// From hours
22-
DayTime(float timeInHours);
23-
24-
int getHours() const;
25-
int getMinutes() const;
26-
int getSeconds() const;
27-
float getTotalHours() const;
28-
float getTotalMinutes() const;
29-
long getTotalSeconds() const;
30-
31-
void getTime(int &h, int &m, int &s) const;
32-
virtual void set(int h, int m, int s);
33-
void set(const DayTime &other);
34-
35-
// Add hours, wrapping days (which are not tracked). Negative or positive.
36-
virtual void addHours(float deltaHours);
37-
38-
// Add minutes, wrapping hours if needed
39-
void addMinutes(int deltaMins);
40-
41-
// Add seconds, wrapping minutes and hours if needed
42-
void addSeconds(long deltaSecs);
43-
44-
// Add time components, wrapping seconds, minutes and hours if needed
45-
void addTime(int deltaHours, int deltaMinutes, int deltaSeconds);
46-
47-
// Add another time, wrapping seconds, minutes and hours if needed
48-
void addTime(const DayTime &other);
49-
// Subtract another time, wrapping seconds, minutes and hours if needed
50-
51-
void subtractTime(const DayTime &other);
16+
// Inherit constructors from core
17+
using core::DayTime::DayTime;
5218

5319
// Convert to a standard string (like 14:45:06)
5420
virtual const char *ToString() const;
55-
virtual const char *formatString(char *targetBuffer, const char *format, long *pSeconds = nullptr) const;
56-
57-
//protected:
58-
virtual void checkHours();
5921

6022
static DayTime ParseFromMeade(String const &s);
61-
62-
protected:
63-
const char *formatStringImpl(char *targetBuffer, const char *format, char sgn, long degs, long mins, long secs) const;
64-
void printTwoDigits(char *achDegs, int num) const;
65-
66-
private:
67-
static long const secondsPerDay = 24L * 3600L; /// Real seconds (not sidereal)
6823
};

0 commit comments

Comments
 (0)