termux / termux-api

Termux add-on app which exposes device functionality as API to command line programs.
https://f-droid.org/en/packages/com.termux.api/
2.34k stars 459 forks source link

Feature request: termux-torch argument to actually toggle the LED #620

Open sudomain opened 1 year ago

sudomain commented 1 year ago

Feature description

termux-torch currently accepts on or off as arguments:

Usage: termux-torch [on | off]
Toggle LED Torch on device

It would be nice if we could have a 3rd option to actually toggle the LED: termux-torch toggle which would check the current LED state and change it to to the opposite (off -> on, and on -> off)

Reference implementation

Have you checked if the feature is accessible through the Android API? Yes Do you know of other open-source apps that has a similar feature as the one you want? (Provide links) N/A

drui9 commented 1 year ago

From my experience, termux-torch on twice turns the light first on, then off. That bug works like the toggle behavior you just described, as long as it is used with the on flag.

sudomain commented 1 year ago

That unfortunately doesn't seem to be the case for my device (pixel 2, android 11)

HusniMuhammad commented 1 year ago

For temporarily you Can use this

#!/bin/bash

# Set initial state
termux_torch_state="on"
termux-torch on

# Function to toggle the touch 
toggle_switch() {
    if [ "$termux_torch_state" == "on" ]; then
        echo "Switching off"
        termux-torch off
        termux_torch_state="off"

    else
        echo "Switching ON"
        termux-torch on
        termux_torch_state="on"
    fi
}

# Main script
echo "Toggle switch example"

while true; do
    echo "Current state: $termux_torch_state"
    echo "Press Enter to toggle the switch, or 'q' to quit"

    read -rsn1 input

    case $input in
        q)
            echo "Quitting script"
            break
            ;;
        *)
            toggle_switch
            ;;
    esac
done

You can save this script as torch.sh in termux widgets folder or your desired folder

alternatively you can set torch state in a file to remove the bash concepts like (bash functions,case, while loop) and without entering (temporarily struck in) into while loop,

using only if conditions

Simply saying about my idea above I mentioned

First time executing the script

Bash torch.sh => torch literal state on

Second time executing the file

Bash torch.sh => torch literal state off

In this way we can make toggle effect

(If I have free time, I would create script above I mentioned as a concept)

Btw, termux_torch_state is not literal device's physical torch's state, I'm just simulating for in this case, it's just a variable

If you optimize this script more, please let Me know,