Search code examples
architectureembedded8-bit

How can I plan my software to avoid excessive rewriting and interdependencies


I'm writing a motor controller that has a couple of interfaces (buttons, Bluetooth, haptic knobs) which is a task that is steadily growing to be a larger than I figured. I've tried to just go at it by starting with low-level modules (e.g. write code to talk on the I2C bus), then ones above that (code to talk to a particular device on the I2C bus...), but all too often I have to dive back down to my lower modules to handle quirks I didn't accommodate. This either takes a long time or I get really hack-ish code.

My target is an 8-bit MCU, so bottom-up seems like I can exploit the hardware better. If I go top-down I don't have any structure to build on or test/debug.

I've tried drawing up some overall diagrams and ones for particular levels/drivers, but I'm not sure how to structure them so I can be very systematic about it and avoid missing the odd signal that needs to go up through 2-3 layers.

I guess this is the reason for a CS degree? I'm an electrical engineer :P


Solution

  • It sounds like you are on the right track. Sometimes no amount of planning is going to prevent you from having to redesign or refactor parts of a system at a later date. Try some of the following tips:

    • Keep your code in modules separated by logical functions.
    • Do not duplicate code, instead design reusable methods for shared functionality.
    • Try to avoid the temptation to add hacks for special cases. Eventually this will become unmaintainable. Instead, adjust and refactor small sections as soon as possible. Trying to do a large re-design at the end will be more difficult.
    • Don't try to over-design the system from the start, as you might just be wasting your time when you get to the actual implementation.
    • Keep the lower levels as simple as possible, then build more advanced capabilities on top.
    • Document your functions and write a some unit tests, especially after adding complex conditional statements.
    • Try to catch errors as high up the stack as possible. For example doing input validation and checking return values. This will make debugging easier.