ZZ-Cat / CRSFforArduino

An Arduino Library for communicating with ExpressLRS and TBS Crossfire receivers.
GNU Affero General Public License v3.0
162 stars 27 forks source link

refactor(library): Split CRSF for Arduino up into abstraction layers #34

Closed ZZ-Cat closed 1 year ago

ZZ-Cat commented 1 year ago

Overview

This Pull Request splits CRSF for Arduino up into the following layers:

By splitting the source code up like this, it only encapsulates code that is relevant to each layer & nothing else. This enhances readability, making the code appear less daunting, & making it easier to debug.

Problems with encapsulation

Previous iterations of CRSF for Arduino were encapsulated in only two files: CRSFforArduino.h & CRSFforArduino.cpp. As my project expanded, I found that I was spending the bulk of my time scrolling through 1,000+ lines of code, before I could get to where I needed to be.

With abstraction, I can easily get to any part of CRSF for Arduino without the need to feel like I am doomscrolling.

This benefits you too

Arduino IDE & PlatformIO users, this means you!

When you open up the sketch layer, this is all you see...

namespace sketchLayer
{
    class CRSFforArduino : private serialReceiver::SerialReceiver
    {
      public:
        CRSFforArduino();
        CRSFforArduino(uint8_t RxPin, uint8_t TxPin);
        ~CRSFforArduino();
        bool begin();
        void end();
        void update();
        uint16_t getChannel(uint8_t channel);
        uint16_t rcToUs(uint16_t rc);
        uint16_t readRcChannel(uint8_t channel, bool raw = false);

        void telemetryWriteGPS(float latitude, float longitude, float altitude, float speed, float groundCourse, uint8_t satellites);

      private:
        SerialReceiver *_serialReceiver;
    };
} // namespace sketchLayer

using namespace sketchLayer;

As for the examples, they are largely unchanged.

By abstracting away all of the stuff that intimidates newcomers to coding, I can dick around with stuff in the background without me even needing to touch the sketch layer.

Any changes I make in the future, unless I explicitly state otherwise, you don't need to make any changes to your existing code in order to accommodate newer iterations of CRSF for Arduino.

This brings me to my next point...

This breaks compatibility with previous versions

This is because I am using a different file-&-folder structure, & I have done my level best to help make the transition to the upcoming release of Version 0.4.0 as smooth & painless for you as possible.

But, keep in mind that not all things are dead-set perfect. If you are having issues, please don't hesitate with either submitting an Issue or opening a Discussion thread.