summaryrefslogtreecommitdiffstats
path: root/bin/bright
blob: ef6398905cc9bd52cfc120ab18c471cfc5cfc5a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh

set -eu

get_b()
{
	# VCP 10 C 100 100
	read -r vcp feat c val max <<-EOF
		$(ddcutil -t getvcp 10)
		EOF

	printf '%s' ${val}
	return 0
}

set_b()
{
	local b=${1}
	shift 1

	ddcutil setvcp 10 ${b}

	return 0
}

set_c()
{
	local c=${1}
	shift 1

	ddcutil setvcp 14 ${c}

	return 0
}

inc_b()
{
	local d=${1}
	shift 1
	local b=

	b=$(($(get_b) + ${d}))
	if [ ${b} -gt 100 ]; then
		b=100
	elif [ ${b} -lt 0 ]; then
		b=0
	fi

	set_b ${b}
}

tog_b()
{
	local b=

	b=$(get_b)
	case ${b} in
		[0-9] | [1-4][0-9])
			set_b 100; set_c 05;;
		[5-9][0-9] | 100)
			set_b 0;   set_c 06;;
	esac

	return 0
}

usage()
{
	printf 'Usage: %s {<direction>|<value>}\n' "${0}"
	printf '\n'
	printf 'Where <direction> is any word beginning with (in any case):\n'
	printf '  * "U" for up\n'
	printf '  * "D" for down\n'
	printf '  * "T" for toggle (between 0 and 100)\n'
	printf 'And <value> is any integer between 0 and 100 inclusive\n'

	return 0
}

main()
{
	local dir=

	if [ ${#} -ne 1 ]; then
		usage 1>&2
		return 1
	fi

	dir="${1}"
	shift 1

	case "${dir}" in
		[Uu]*) inc_b 20;;
		[Dd]*) inc_b -20;;
		[Tt]*) tog_b;;
		*[!0-9]* | '')
			usage 1>&2
			return 1
			;;
		*)
			set_b ${dir}
	esac

	return 0
}

main "${@}"