if [ "((CURRENT_BRIGHTNESS + STEP))
if [ MAX_BRIGHTNESS ]; then
NEW_BRIGHTNESS=$MAX_BRIGHTNESS
fi
Assistant
User
add "setp" command to set brightness in percent of max value. For example
$ ddccontrol.sh setp 70
Max value is 200
Brightness adjusted to 140 points
Assistant
OPERATION"="plus"];thenNEWBRIGHTNESS=
NEWBRIGHTNESS−gt
elif [ "OPERATION"="minus"];thenNEWBRIGHTNESS=((CURRENT_BRIGHTNESS - STEP))
if [ $NEW_BRIGHTNESS -lt 0 ]; then
NEW_BRIGHTNESS=0
fi
else
echo "Invalid operation. Please use 'plus' or 'minus'."
exit 1
fi
Add ability to set specific brightness from min to max range. For example:
ddccontrol.shgetmaxMaxvalueis255 ddccontrol.sh set 128
You can extend your existing Bash script to add the ability to set a specific brightness value directly, while retaining the existing functionality. Below is an updated version of the script that includes the ability to set a brightness level based on your examples:
bash
#!/bin/bash
# Check if ddcutil is installed
if ! command -v ddcutil &> /dev/null; then
echo "Error: ddcutil is not installed. Please install it before running this script."
exit 1
fi
# Get max brightness value using ddcutil
MAX_BRIGHTNESS=$(ddcutil --sleep-multiplier 0.2 --noverify getvcp 10 | awk '{print $13}')
MIN_BRIGHTNESS=0 # Typically the minimum brightness value is 0
# Check if arguments are provided
if [ $# -lt 1 ]; then
echo "Usage: $0 <command> [args]"
echo "Commands:"
echo " plus <step> : Increase brightness by step"
echo " minus <step> : Decrease brightness by step"
echo " getmax : Get the maximum brightness value"
echo " set <value> : Set brightness to a specific value"
exit 1
fi
# Get current brightness value
CURRENT_BRIGHTNESS=$(ddcutil --sleep-multiplier 0.2 --noverify getvcp 10 | awk '{print $9}' | grep -Eo '[0-9]{1,4}')
# Parse command
COMMAND=$1
case $COMMAND in
plus)
if [ $# -ne 2 ]; then
echo "Usage: $0 plus <step>"
exit 1
fi
STEP=$2
NEW_BRIGHTNESS=$((CURRENT_BRIGHTNESS + STEP))
if [ $NEW_BRIGHTNESS -gt $MAX_BRIGHTNESS ]; then
NEW_BRIGHTNESS=$MAX_BRIGHTNESS
fi
;;
minus)
if [ $# -ne 2 ]; then
echo "Usage: $0 minus <step>"
exit 1
fi
STEP=$2
NEW_BRIGHTNESS=$((CURRENT_BRIGHTNESS - STEP))
if [ $NEW_BRIGHTNESS -lt $MIN_BRIGHTNESS ]; then
NEW_BRIGHTNESS=$MIN_BRIGHTNESS
fi
;;
getmax)
echo "Max brightness value is $MAX_BRIGHTNESS"
exit 0
;;
set)
if [ $# -ne 2 ]; then
echo "Usage: $0 set <value>"
exit 1
fi
NEW_BRIGHTNESS=$2
if [ "$NEW_BRIGHTNESS" -gt "$MAX_BRIGHTNESS" ] || [ "$NEW_BRIGHTNESS" -lt "$MIN_BRIGHTNESS" ]; then
echo "Error: Value must be between $MIN_BRIGHTNESS and $MAX_BRIGHTNESS."
exit 1
fi
;;
*)
echo "Invalid command. Please use 'plus', 'minus', 'getmax', or 'set'."
exit 1
;;
esac
# Set new brightness value
ddcutil --sleep-multiplier 0.2 --noverify setvcp 10 $NEW_BRIGHTNESS
# Print debug info
echo "Max brightness: $MAX_BRIGHTNESS"
echo "Current brightness: $CURRENT_BRIGHTNESS"
echo "New brightness: $NEW_BRIGHTNESS"
echo "Command: $COMMAND"
Changes Made:
Updated Usage Information: Expanded usage help to include commands for getting max brightness and setting a specific brightness value.
Command Parsing: Used a case statement to define different commands (plus, minus, getmax, set).
Get Max Brightness: The getmax command now allows you to output just the maximum brightness value.
Set Specific Brightness: The set command allows you to directly set the brightness to a specified value (with validation against the maximum and minimum values).
Example Usage:
To increase brightness: ./ddccontrol.sh plus 10
To decrease brightness: ./ddccontrol.sh minus 10
To get the max brightness value: ./ddccontrol.sh getmax
To set a specific brightness value: ./ddccontrol.sh set 128
To add a "setp" command that allows setting the brightness as a percentage of the maximum value, you can modify the script accordingly. Below is the updated version of the script incorporating the "setp" command:
bash
#!/bin/bash
# Check if ddcutil is installed
if ! command -v ddcutil &> /dev/null; then
echo "Error: ddcutil is not installed. Please install it before running this script."
exit 1
fi
# Get max brightness value using ddcutil
MAX_BRIGHTNESS=$(ddcutil --sleep-multiplier 0.2 --noverify getvcp 10 | awk '{print $13}')
MIN_BRIGHTNESS=0 # Typically the minimum brightness value is 0
# Check if arguments are provided
if [ $# -lt 1 ]; then
echo "Usage: $0 <command> [args]"
echo "Commands:"
echo " plus <step> : Increase brightness by step"
echo " minus <step> : Decrease brightness by step"
echo " getmax : Get the maximum brightness value"
echo " set <value> : Set brightness to a specific value"
echo " setp <percent> : Set brightness as a percentage of max value"
exit 1
fi
# Get current brightness value
CURRENT_BRIGHTNESS=$(ddcutil --sleep-multiplier 0.2 --noverify getvcp 10 | awk '{print $9}' | grep -Eo '[0-9]{1,4}')
# Parse command
COMMAND=$1
case $COMMAND in
plus)
if [ $# -ne 2 ]; then
echo "Usage: $0 plus <step>"
exit 1
fi
STEP=$2
NEW_BRIGHTNESS=$((CURRENT_BRIGHTNESS + STEP))
if [ $NEW_BRIGHTNESS -gt $MAX_BRIGHTNESS ]; then
NEW_BRIGHTNESS=$MAX_BRIGHTNESS
fi
;;
minus)
if [ $# -ne 2 ]; then
echo "Usage: $0 minus <step>"
exit 1
fi
STEP=$2
NEW_BRIGHTNESS=$((CURRENT_BRIGHTNESS - STEP))
if [ $NEW_BRIGHTNESS -lt $MIN_BRIGHTNESS ]; then
NEW_BRIGHTNESS=$MIN_BRIGHTNESS
fi
;;
getmax)
echo "Max brightness value is $MAX_BRIGHTNESS"
exit 0
;;
set)
if [ $# -ne 2 ]; then
echo "Usage: $0 set <value>"
exit 1
fi
NEW_BRIGHTNESS=$2
if [ "$NEW_BRIGHTNESS" -gt "$MAX_BRIGHTNESS" ] || [ "$NEW_BRIGHTNESS" -lt "$MIN_BRIGHTNESS" ]; then
echo "Error: Value must be between $MIN_BRIGHTNESS and $MAX_BRIGHTNESS."
exit 1
fi
;;
setp)
if [ $# -ne 2 ]; then
echo "Usage: $0 setp <percent>"
exit 1
fi
PERCENT=$2
if ! [[ "$PERCENT" =~ ^[0-9]+$ ]] || [ "$PERCENT" -lt 0 ] || [ "$PERCENT" -gt 100 ]; then
echo "Error: Percent must be an integer between 0 and 100."
exit 1
fi
NEW_BRIGHTNESS=$((MAX_BRIGHTNESS * PERCENT / 100))
;;
*)
echo "Invalid command. Please use 'plus', 'minus', 'getmax', 'set', or 'setp'."
exit 1
;;
esac
# Set new brightness value
ddcutil --sleep-multiplier 0.2 --noverify setvcp 10 $NEW_BRIGHTNESS
# Print debug info
echo "Max brightness: $MAX_BRIGHTNESS"
echo "Current brightness: $CURRENT_BRIGHTNESS"
echo "New brightness: $NEW_BRIGHTNESS"
echo "Command: $COMMAND"
New Command Addition:
setp: This command allows you to set the brightness as a percentage of the maximum brightness. It takes a single argument representing the desired percentage. The script calculates the new brightness value based on this percentage and sets it.
Example Usage:
To set the brightness to 70% of the maximum value: ./ddccontrol.sh setp 70
Output:
When using the setp command, if the maximum brightness is, for example, 200, and you run ./ddccontrol.sh setp 70, the output would be:
code
Max brightness: 200
Current brightness: <current_value>
New brightness: 140
Command: setp
Make sure to test this functionality on your setup to ensure that the brightness levels are adjusted as expected.