diff --git a/README.md b/README.md index a44e363..8a13147 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Switch between presets 1 to 17 or list available presets: `$ wled -f|-w|-a [1-17|l(ist)]` -Change brightness: +Display or change brightness: `$ wled -f|-w|-a -b [1-255]` diff --git a/wled b/wled index 775914e..bb15ae2 100755 --- a/wled +++ b/wled @@ -33,7 +33,7 @@ commands: on|off switch on/off [1-17] switch preset l, list list presets - -b,--brightness [1-255] set brightness + -b,--brightness [1-255] get or set brightness exit status: 0 no failure @@ -61,6 +61,12 @@ active_preset() { sed -n 's/.*"ps"[[:space:]]*:[[:space:]]*\(1\?[0-9]\).*/\1/p' } +current_brightness() { + # Find current brightness. + curl_fetch "http://$1/json" | \ + sed -n 's/.*"state"[[:space:]]*:[[:space:]]*{[^}]*"bri"[[:space:]]*:[[:space:]]*\([0-9]\{1,3\}\).*/\1/p' +} + arg_inst="$1" arg_cmnd="$2" arg_param="$3" @@ -107,19 +113,21 @@ for wled_host; do ;; -b|--brightness) # Set brightness. if test -z "$arg_param"; then - echo "Error: missing parameter for brightness." >&2 - exit 1 + # No parameter given. Print current brightness. + echo "Current brightness of $wled_host:" + current_brightness "$wled_host" + else + # Try to convert 3rd parameter to an integer between 1 an 255. + bright=$(printf '%u' "$arg_param" 2>/dev/null) + if test $? -gt 0; then # Conversion failed. + echo "Error: unable to parse parameter for brightness." >&2 + exit 1 + elif test "$bright" -lt 1 -o "$bright" -gt 255; then + echo "Error: parameter for brightness is out of range." >&2 + exit 1 + fi + api_resp=$(curl_send "{'bri':$bright}" "http://$wled_host/json") fi - # Try to convert 3rd parameter to an integer between 1 an 255. - bright=$(printf '%u' "$arg_param" 2>/dev/null) - if test $? -gt 0; then # Conversion failed. - echo "Error: unable to parse parameter for brightness." >&2 - exit 1 - elif test "$bright" -lt 1 -o "$bright" -gt 255; then - echo "Error: parameter for brightness is out of range." >&2 - exit 1 - fi - api_resp=$(curl_send "{'bri':$bright}" "http://$wled_host/json") ;; [1-9]|1[0-7]) # Switch to preset. api_resp=$(curl_send "{'ps':$arg_cmnd}" "http://$wled_host/json")