Library pwm

To use this library in your code:

import gpio.pwm
Pulse-Width Modulation (PWM) support.
Examples
A fading LED:

import gpio.pwm

LED-PIN ::= 5

main:
  // Create a PWM square wave generator with frequency 400Hz.
  generator := pwm.Pwm --frequency=400

  // Use it to drive the LED.
  // By default the duty factor is 0.
  channel := generator.start LED-PIN

  duty-percent := 0
  step := 1
  while true:
    // Update the duty factor.
    channel.set-duty-factor duty-percent/100.0
    duty-percent += step
    if duty-percent == 0 or duty-percent == 100:
      step = -step
    sleep --ms=10
Driving a servo:

import gpio.pwm

SERVO-PIN ::= 14

main:
  // Most servos need a 50Hz frequency. However, some models go up to
  // 400Hz. Consult the documentation for your servo.

  // Create a PWM square-wave generator with frequency 50.
  generator := pwm.Pwm --frequency=50

  // Generally, the acceptable duty-factor range of servos is 0.025 to 0.125.
  // Therefore start the pin with 0.075.
  channel := generator.start SERVO-PIN --duty-factor=0.075
  sleep --ms=1000

  // Max angle.
  print "max"
  channel.set-duty-factor 0.125
  sleep --ms=1500

  // Min angle.
  print "min"
  channel.set-duty-factor 0.025
  sleep --ms=1500

Classes

A Pulse-Width Modulation (PWM) instance, for managing multiple PwmChannel.

A PWM Channel for an already-opened PWM instance.