Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 14 Dec 2018 18:38:10 +0000 (UTC)
From:      Emmanuel Vadot <manu@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r342088 - head/usr.sbin/pwm
Message-ID:  <201812141838.wBEIcAaB023238@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: manu
Date: Fri Dec 14 18:38:10 2018
New Revision: 342088
URL: https://svnweb.freebsd.org/changeset/base/342088

Log:
  pwm(8): Add percentage value support for duty cycle

Modified:
  head/usr.sbin/pwm/pwm.8
  head/usr.sbin/pwm/pwm.c

Modified: head/usr.sbin/pwm/pwm.8
==============================================================================
--- head/usr.sbin/pwm/pwm.8	Fri Dec 14 18:37:26 2018	(r342087)
+++ head/usr.sbin/pwm/pwm.8	Fri Dec 14 18:38:10 2018	(r342088)
@@ -67,7 +67,7 @@ Show the configuration of the pwm channel
 .It Fl p Ar period
 Configure the period (in nanoseconds) of the pwm channel
 .It Fl d Ar duty
-Configure the duty (in nanoseconds) of the pwm channel
+Configure the duty (in nanoseconds or percentage) of the pwm channel
 .El
 .Sh EXAMPLES
 .Bl -bullet
@@ -76,9 +76,13 @@ Show the configuration of the pwm channel:
 .Pp
 pwm -f /dev/pwmc0 -C
 .It
-Configure a 50000 ns period and a 25000 duty cycles:
+Configure a 50000 ns period and a 25000 duty cycle:
 .Pp
 pwm -f /dev/pwmc0 -p 50000 -d 25000
+.It
+Configure a 50% duty cycle:
+.Pp
+pwm -f /dev/pwmc0 -d 50%
 .El
 .Sh SEE ALSO
 .Xr pwm 9 ,

Modified: head/usr.sbin/pwm/pwm.c
==============================================================================
--- head/usr.sbin/pwm/pwm.c	Fri Dec 14 18:37:26 2018	(r342087)
+++ head/usr.sbin/pwm/pwm.c	Fri Dec 14 18:38:10 2018	(r342088)
@@ -71,6 +71,7 @@ main(int argc, char *argv[])
 	int action, ch;
 	cap_rights_t right_ioctl;
 	const unsigned long pwm_ioctls[] = {PWMGETSTATE, PWMSETSTATE, PWMMAXCHANNEL};
+	char *percent;
 
 	action = 0;
 	fd = -1;
@@ -104,7 +105,9 @@ main(int argc, char *argv[])
 			if (action & ~(PWM_PERIOD | PWM_DUTY))
 				usage();
 			action = PWM_DUTY;
-			duty = strtol(optarg, NULL, 10);
+			duty = strtol(optarg, &percent, 10);
+			if (*percent != '\0' && *percent != '%')
+				usage();
 			break;
 		case 'c':
 			if (channel != -1)
@@ -199,8 +202,12 @@ main(int argc, char *argv[])
 	case PWM_DUTY:
 		if (period != -1)
 			state.period = period;
-		if (duty != -1)
-			state.duty = duty;
+		if (duty != -1) {
+			if (*percent != '\0')
+				state.duty = state.period * duty / 100;
+			else
+				state.duty = duty;
+		}
 		if (ioctl(fd, PWMSETSTATE, &state) == -1) {
 			fprintf(stderr,
 			  "Cannot configure the pwm controller\n");



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201812141838.wBEIcAaB023238>