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");
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to