How to periodically switch on and switch off flashlight with 1kHz rate on rooted Redmi Note 5 Oro?



  • My objective is to write a shell script to periodically turn off and on my phone's flashlight (about 1 kHz or 500Hz). My phone is rooted. So I gained superuser shell access (#)

    These are the files under my sys/class/leds/flashlight

    whyred:/sys/class/leds/flashlight # ls
    brightness device max_brightness power subsystem trigger uevent
    

    ( https://i.stack.imgur.com/aKwvu.png )

    I was able to modify the content of the brightness file and was able to manually switch off and on my flashlight. However, I want to periodically switch on and off with a delay and for that, I need a timer trigger. But within the trigger file above, there is no delay section indicated. There was just some contents within the file (no numbers)

    whyred:/sys/class/leds/flashlight # cat trigger
    [none] bkl-trigger switch0_trigger flash0_trigger flash1_trigger torch0_trigger torch1_trigger switch1_trigger flash2_trigger torch2_trigger dc-online usb-online main-online pc_port-online battery-charging-or-full battery-charging battery-full battery-charging-blink-full-solid rfkill0 mmc0 mmc1 bms-online parallel-online rfkill1
    

    ( https://i.stack.imgur.com/xK554.png )

    I was able to clock into a period of about 1-3ms by writing a bash script, that echoes brightness 100 followed by sleep and then echoes 0 brightness

    Script:

    while [ 1 le 100]
    do
    echo "100" > ~/sys/class/leds/brightness  
    sleep(0.001)  
    echo "100" > ~/sys/class/leds/brightness)
    sleep(0.001)
    done  # end
    

    But I want at least a 1 kHz frequency, so a timer is the one, sleep function is not advisable.

    How can I about attaining my objective?



  • There is Termux and Termux:API.

    Termux:

    Android terminal emulator and Linux environment application that works directly with no rooting or setup required. A minimal base system is installed automatically, additional packages are available using the package manager.

    Termux:API:

    This addon exposes device functionality as API to command line programs in Termux.

    Install both of them from the F-droid website.

    Then open up Termux and run pkg install termux-api nano

    Make a shell script with nano using nano torch.sh

    Use termux-torch on and termux-torch off to switch on and off the torch.

    Also make other edits to fulfill your needs. If you want to do it periodically, use a while loop.

    Save and exit the file with Ctrl + X, then type "Y" and enter.

    Run cat torch.sh to make sure the shell script is edited successfully.

    Make sure the shell script is executable by running chmod +x torch.sh

    Finally, run ./torch.sh

    P.S. You don't even need root to do that lol




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2