awonak / HagiwoModulove

Alternate firmware and scripts for Sync LFO & A-RYTH-MATIK eurorack modules.
https://awonak.github.io/HagiwoModulove/
MIT License
8 stars 2 forks source link

A-RYTH-MATIK compile time flag for encoder direction #16

Closed awonak closed 5 months ago

awonak commented 5 months ago

Some users will have their module built with an encoder that interprets turn clicks in the opposite direction, so the encoder responds backwards. It would be helpful to have a compile time flag to define encoder direction. This will also make it easier to dynamically build binaries for the web uploader.

modulove commented 5 months ago

Thanks Adam,

i was thinking about this too but didnt get to it yet. For our web-uploader we have two binarys that you can choose from with a select box.

Any recommendation how to implement this in an elegant way ?

Cheers Linus

awonak commented 5 months ago

Since the hardware is fixed, it definitely make sense to have two separate binaries for each version of the hardware encoder direction instead of some sort of software based configuration.

With the Arduino CLI we can pass build flags using --build-property to pass a flag that changes behavior based on a C/C++ DEFINE statement. Here's an example of how this can be done.

Once the code is updated to support a flag for reversing the encoder direction, I can then update my GitHub Action which compiles my binaries to add the flag here and provide the two options using the matrix feature for each value.

Lastly, I will need to update my website to include some way to select which encoder direction binary the user wants to flash. This part I don't have a solid plan in mind, but I'll figure it out when I get to it :)

awonak commented 5 months ago

I have updated the Encoder library code to introduce an optional DEFINE flag ENCODER_REVERSED which determines which encoder direction function will be called when reading encoder changes:

    /// @brief Get the rotary direction if it has turned.
    /// @return Direction of turn or unchanged.
    Direction Rotate() {
#ifdef ENCODER_REVERSED
        // Reversed (counter clockwise)
        return rotate_ccw();
#else
        // Default (clockwise)
        return rotate_cw();
#endif
    }

    Direction rotate_cw() {
        switch (encoder_.rotate()) {
            case 1:
                return DIRECTION_INCREMENT;
            case 2:
                return DIRECTION_DECREMENT;
            default:
                return DIRECTION_UNCHANGED;
        }
    }

    Direction rotate_ccw() {
        switch (encoder_.rotate()) {
            case 1:
                return DIRECTION_DECREMENT;
            case 2:
                return DIRECTION_INCREMENT;
            default:
                return DIRECTION_UNCHANGED;
        }
    }

Normal compile command:

arduino-cli compile -b arduino:avr:nano BitGarden.ino  -e

Reversed compile command:

arduino-cli compile -b arduino:avr:nano: --build-property "build.extra_flags=-DENCODER_REVERSED" BitGarden.ino -e --output-dir=./build_reversed
awonak commented 5 months ago

Fixed in:

modulove commented 5 months ago

Thanks for implementing and explaining this! Just stumbled over our latest encoder batch is the other way around again haha