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

Break up, refactor, and enforce style on the E-TKT firmware code #42

Closed
sabeechen opened this issue Apr 30, 2023 · 4 comments
Closed

Break up, refactor, and enforce style on the E-TKT firmware code #42

sabeechen opened this issue Apr 30, 2023 · 4 comments
Labels
firmware Arduino code flashed into the device web app Web app flashed into the device
Milestone

Comments

@sabeechen
Copy link
Contributor

I think it would be a good idea to refactor the E-TKT from its current (largely) single file structure into one that encapsulates its functionality into multiple classes. There are many advantages to this, but the biggest I see are:

  • Breaking functionality into smaller parts will make it easier to follow logic.
  • Smaller files are easier to read, and therefore more maintainable.
  • Breaking up functionality makes it easier to avoid conflicting changes if multiple people work on the project simultaneously.
  • Bugs are less likely if the state for the device isn't all tangled together everywhere.
  • Extending functionality is easier to do (ie for different hardware) when responsibilities are clearly defined by a class structure.
  • Adding tests will be easier if most classes depend on abstractions of the hardware rather than the arduino methods themselves.

These are the typical upsides of breaking up and encapsulating functionality. As usual it also comes with some downside, if things are broken up naively then it can often become more work to add functionality because the relationships between parts doesn't reflect what they actually need to do. My goal would be to avoid that, and I think I'm familiar enough with the project to do it well. I have a lot of professional experience doing this kind of thing, though not specifically with C++.

Here is how I'm initially thinking of breaking things up:

  • Printer.h - A "main" class created at the start which initializes everything and keeps track of global state like print progress, busy, etc
  • Helpers.h - Static utility methods, like the ones for handling utf-8 text.
  • Carousel.h - Manages everything having to do with the carousel geometry, its stepper motor, and homing.
  • Sound.h - Manages making sounds/music with the buzzer.
  • Display.h - Manages all the logic for displaying info on the oled.
  • Server.h - Sets up the webserver, its callbacks, and passing info from requests to where they need to go.
  • WiFi.h - Handles the logic for connecting to WiFi, AP Config mode, mdns, etc.
  • Button.h, Light.h, Motor.h - To handle smaller units of hardware. These classes will probably be very simple.

While actually untangling the relationships whats needed might change, but I think its likely things will look close to this.

From my personal experience its better to make big changes like this quickly all at once to minimize the time it impacts people with pending changes. Id want to break it all up quickly and coarsely in one PR, when go through and refactor/clean up individual classes by themselves at a more leisurely pace.

Alternative would be to check-in each new class one at a time, slowly carving out functionality from LabelMaker.cpp. This is less disruptive to concurrent contributors but can take a lot longer to complete.

As things are broken up I'd like to move toward enforcing a C++ style, the most obvious choice is probably Google C++ Style with clang, but any of the common alternatives would be fine so long as its adhered to, including something we make custom. Most projects choose one of the well-known styles (Google, Microsoft, Mozilla, etc) and then just override rules as they see fit.

@andreisperid You have your finger on the pulse of whats going on with this project currently. Do you think this is a good idea, and if so do you think now is a good time to do it?

@andreisperid
Copy link
Owner

andreisperid commented May 1, 2023

I think that's a great initiative, @sabeechen

From a roadmap point of view, these are the major things that might happen from now on:

  • improvements on hardware, that might affect the firmware somehow;
  • bugfixes that might appear with new browser versions;
  • integration with other systems such as the Home Assistant, Matter, etc, which will probably talk to the Server.h;
  • forking into other similar projects (lets say a braille embossing machine? I'm thinking of it ;)

So, right now I understand the E-TKT's archetype is pretty solid, which means the approach you brought makes sense and will not prevent any development. Instead, it might even improve as collaboration will surely take advantage of that clear structure.

Also, for me it will be surely a great opportunity to learn, I'm looking forward to it! I trust whatever you bring ;)

@andreisperid andreisperid added web app Web app flashed into the device firmware Arduino code flashed into the device labels May 1, 2023
@skrutt
Copy link

skrutt commented May 1, 2023

Great idea, i agree! I would probably go for one piece at a time, simpler to get an overview and there are a lot of moving pieces.
Today the code is quite interleaved and many things rely on each other, for example the print progress is updated in a display function. Sort of makes sense since that functionality is available there but not if you are looking to create functional modules with clean, well defined interfaces.
Will be interesting to see where it goes, i would probably go for a function based layout instead (display, print, music, web), but both have merit.

If you do a music module, would you make that non-blocking? Cuz that would be cool 😄

@sabeechen
Copy link
Contributor Author

Yeah, its got some oddities. But maybe it won't for much longer?
I made a proposal for my massive refactor in this PR.
Non-blocking execution is on my mind, but won't be in this change. Its a tricky thing to get working in the device's state model but possible down the line.

@andreisperid andreisperid added this to the v1.2.0 milestone May 3, 2023
@andreisperid
Copy link
Owner

Implemented on #46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
firmware Arduino code flashed into the device web app Web app flashed into the device
Projects
None yet
Development

No branches or pull requests

3 participants