Skip to content

refactor: extract some logic classes into core#292

Closed
andre-stefanov wants to merge 54 commits into
developfrom
feature/phase-1
Closed

refactor: extract some logic classes into core#292
andre-stefanov wants to merge 54 commits into
developfrom
feature/phase-1

Conversation

@andre-stefanov
Copy link
Copy Markdown
Member

Phase 1: Extract pure domain logic into core

Summary

Extracts the pure (non-Arduino) arithmetic subsets of the firmware's core data types and algorithms into core, making them host-testable via the native PlatformIO environment. The original src files become thin overlays that inherit from the core versions and add only Arduino-dependent methods (ParseFromMeade, ToString). Zero behavior change — all 5 boards build green.

Architecture

src/core/
├── types/                     # Pure data containers (no Arduino)
│   ├── DayTime.hpp/.cpp       # 24h time arithmetic, formatString
│   ├── RightAscension.hpp/.cpp# 0–24h RA coordinate (wrapping)
│   ├── Declination.hpp/.cpp   # ±180° DEC coordinate (clamping)
│   ├── Latitude.hpp/.cpp      # ±90° latitude (clamping)
│   └── Longitude.hpp/.cpp     # ±180° longitude (wrapping)
├── SiderealClock.hpp/.cpp     # LST/HA calculation (theta, delta-JD)
├── CalendarMath.hpp/.cpp      # Date arithmetic with leap-year rules
├── CoordinateMath.hpp/.cpp    # Meridian-flip solution selector
├── CoordinateFormatter.hpp/.cpp# RA/DEC char-buffer formatting
└── EepromLayout.hpp           # Magic markers, flags, layout constants

Each src originals (DayTime, Declination, Latitude, Longitude) is now a thin overlay:

// src/DayTime.hpp
class DayTime : public core::DayTime {
    using core::DayTime::DayTime;  // inherit constructors
    virtual const char *ToString() const;
    static DayTime ParseFromMeade(String const &s);
};

New files

# File
6 src/core/types/{DayTime,RightAscension,Declination,Latitude,Longitude}.{hpp,cpp}
4 src/core/{SiderealClock,CalendarMath,CoordinateMath,CoordinateFormatter}.{hpp,cpp}
1 EepromLayout.hpp
2 src/{RightAscension}.{hpp,cpp} (new overlay)
10 unit_tests/test_core/{test_sidereal,test_calendar,test_coordinate_math,test_coordinate_formatter,test_eeprom_layout,types/test_daytime,types/test_declination,types/test_latitude,types/test_longitude,types/test_right_ascension}.cpp

Modified files

# File Change
8 src/{DayTime,Declination,Latitude,Longitude}.{hpp,cpp} Converted to thin overlays; pure methods removed
1 Sidereal.cpp Delegates to core::SiderealClock

Test results

258 test cases: 258 succeeded (188 existing + 70 new)

All 5 board matrix builds green (ramps, mksgenlv21, mksgenlv2, mksgenlv1, esp32, oaeboardv1).

Key decisions

  • Extract-before-test: src root files transitively include <Arduino.h> via inc/Globals.hpp and are untestable on native. Pure logic is extracted into core/ first, then tested.
  • Thin overlays preserve back-compat: Originals #include the core version and add only String-dependent methods. No flag day.
  • core namespace: All extracted types live in namespace core to avoid collisions with the overlay classes in the global namespace.
  • core::RightAscension was added as a first-class RA coordinate type alongside Declination, replacing DayTime in CoordinateFormatter::formatRA(). Inheritance from DayTime is acknowledged as a transitional convenience — a composition-based design is planned for Phase 2.

What's next (Phase 2)

Introduce HAL interfaces (IStepperMotor, ISystemClock, IEeprom, …), domain ports (IClock, IPersistentStore, IStepperAxis, …), and adapters that bind ports to HAL. Wire Mount to use injected port pointers instead of concrete types.

andre-stefanov and others added 30 commits May 15, 2026 22:19
- Introduced MeadeParser.hpp to define enums and structures for parsing Meade commands.
- Implemented parsing functions for various command types including Get, Set, Movement, and more.
- Created unit tests in test_MeadeParser.cpp to validate the parsing logic and ensure correct command classification.
- Added assertions for valid and invalid command inputs, covering all command families and their respective payloads.
- Moved MeadeParser related code into a new 'meade' namespace for better organization.
- Added comprehensive unit tests for MeadeParser functionality, covering various command parsing scenarios.
- Implemented assertions for valid and invalid command parsing, ensuring robustness of the parser.
- Included tests for all command families, including Get, Set, Sync, Movement, Home, Quit, Slew Rate, Focus, and Extra commands.
- Implemented MeadeResponse class for type-safe Meade replies with fixed capacity.
- Defined response shapes using zero-size tag types in the response namespace.
- Created makeResponse overloads for various response shapes.
- Added traits for mapping command kinds to response tags.
- Implemented entry points for responding to different command families.
- Developed unit tests to validate response shapes and command bindings, ensuring correct wire formatting.
- Consolidated response trait mappings into a single primary template `Response<K>`, replacing multiple family-specific templates.
- Updated entry point function from `respondGet<K>` and `respondSet<K>` to a unified `respond<K>`.
- Adjusted macro definitions for binding command kinds to response tags to accommodate the new structure.
- Modified unit tests to reflect changes in response function calls and ensure consistent behavior across all command kinds.
- Removed MeadeGet command parsing and related tests from MeadeParser unit tests.
- Introduced new unit tests for MeadeGet command handling in test_MeadeGetDispatcher.cpp.
- Added comprehensive wire-byte tests for Meade Get-family commands in test_MeadeGet.cpp.
- Updated MeadeResponse tests to remove redundant Get command assertions.
- Ensured all tests maintain coverage for command routing and response formatting.
- Removed the MeadeSetCommandKind enum and associated MeadeSetParseResult structure from MeadeParser.hpp.
- Introduced IMeadeSetHandlers interface for handling Meade Set commands, with methods for each command type.
- Implemented handleMeadeSet function to parse and dispatch Meade Set commands directly, returning a MeadeResponse.
- Updated MeadeResponse to remove bindings related to MeadeSetCommandKind.
- Added unit tests for the new handleMeadeSet functionality, covering various command scenarios and edge cases.
- Removed obsolete unit tests related to the old Meade Set command parsing.
- Implement tests for Meade Focus dispatcher covering commands such as continuous motion, MoveBy, speed settings, GetPosition, SetPosition, GetState, and Stop.
- Add tests for Meade GPS dispatcher to validate GPS acquisition commands and payload handling.
- Create tests for Meade Home dispatcher to verify park, slew-to-home, unpark, and setting Az/Alt home commands.
- Introduce tests for Meade Init dispatcher to ensure proper handling of serial control commands.
- Develop tests for Meade Movement dispatcher to cover slew, tracking, guide pulse, and stepper movement commands.
- Add tests for Meade SetSlewRate dispatcher to validate mapping of slew rates to commands.
- Implement tests for Meade SyncControl dispatcher to check synchronization commands and responses.
- Remove outdated MeadeResponse tests as they are no longer needed.
- Removed #include "core/MeadeResponse.hpp" from multiple unit test files.
- Updated includes to only reference "core/MeadeParser.hpp" where applicable.
andre-stefanov and others added 24 commits May 19, 2026 19:55
- Changed command handlers (e.g., handleMeadeGps, handleMeadeHome, etc.) to accept a reference to an existing MeadeResponse object instead of returning a new one.
- Updated response writing functions to fill the provided MeadeResponse object, improving memory efficiency.
- Adjusted unit tests to accommodate the new function signatures and ensure proper response handling.
- Renamed response creation functions to fill*Response to better reflect their purpose.
- Replaced Unity test framework with Google Test in all Meade unit test files.
- Updated assertions from Unity style to Google Test style (e.g., TEST_ASSERT_EQUAL_STRING to EXPECT_STREQ).
- Removed unnecessary test registration functions and streamlined test structure.
- Modified main test file to initialize and run all tests using Google Test.
- Introduced DayTime class for handling 24-hour time values with arithmetic operations.
- Created Declination, Latitude, Longitude, and RightAscension classes for astronomical coordinates.
- Implemented clamping and wrapping behavior for Declination and Longitude.
- Added unit tests for DayTime, Declination, Latitude, Longitude, and RightAscension to ensure correct functionality.
- Developed coordinate formatting functions for Right Ascension and Declination.
- Added calendar math functions and corresponding tests for date manipulation.
- Included EEPROM layout tests to verify flag values and magic marker constants.
- Implemented SiderealClock functionality with tests for delta JD calculations and hour angle computations.
@andre-stefanov andre-stefanov deleted the feature/phase-1 branch May 26, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants