Home Contents | # Change GPIO State | Gpio | Core4 Lua Event System

## SYNOPSIS

#include <lua/gpio.lh>

1. gpio.out([group, ]nr, state)
2. gpio.pulse([group, ]nr, ms)
3. gpio.pulseOn([group, ]nr, ms)
4. gpio.pulseOff([group, ]nr, ms)
5. gpio.blink([group, ]nr, pattern)
6. gpio.pwm([group, ]nr, duty [, limit])
7. gpio.limit([group, ]nr[, timeout])

## DESCRIPTION

These commands modify the state of an output. In the simplest form, the parameter nr indicates the output number of an output on the controller itself.

gpio.out() | Sets output to state, interpreted as a boolean. This call will stop any pulse or blinking running on the output.
gpio.pulse() | Pulse the output to on for ms milliseconds, the turn it off. Pulse timer will stop when gpio.out() or gpio.blink() is called on that output.
gpio.pulseOn() | Same as above, aliased for clarity.
gpio.pulseOff() | Same as above, but output turns off at the start of the pulse and turns on at the end. This is currently only supported for outputs in the group GPGRP_GPIO.
gpio.blink() | Blink the output with pattern. The pattern is an unsigned 32bit value which is rotated right every 125ms. On each rotate, bit 0 is placed onto the output. Blinking will stop when gpio.out() or gpio.pulse() is called on that output. (Note: uBus nodes only support an 8bit blink pattern.)
gpio.pwm() | Output a PWM signal with the specified duty cycle. Only a few IO pins actually support PWM. See below for a list.
gpio.limit() | Turn the output on until the input with the same number turns on or the timeout has elapsed. (If the matching input is already on, the output will not turn on.) Used to control mechanical motion that stops when a limit switch is reached. Must be supported by the controlled device, otherwise the command has no effect. Timeout defaults to 10 seconds.

## NOTES

Please see Lua Reference Manual on how Lua interprets booleans: Only the explicit values nil and false flag as false, anything else, including the number 0, flag as true. Sorry for the inconvenience.

## EXAMPLE

Blinking with decreasing speeds:

>
gpio.blink(1, 0xAAAAAAAA)
>
gpio.blink(1, 0x33333333)
>
gpio.blink(1, 0x00FF00FF)

Flash of 0.5s every 3.5s.

>
gpio.blink(1, 0x0000000F)

## PWM

The V4 controller supports PWM signalling on two pins.

gpio.pwm(GPGRP_SIDEKICK, SKIO_CDR10, duty) -- J13, pin 6
gpio.pwm(GPGRP_SIDEKICK, SKIO_RCP1, duty) -- J13, pin 12
gpio.pwm(GPGRP_SIDEKICK, 255, top) -- Controls TOP value

The PWM duty value is an integer in the range [0...top]; top defaults to 65535.
Writing a zero duty cycle turns the output pin static L, writing the value of top turns it static H.
Any value between [1...TOP-1] outputs a PWM signal using the selected duty cycle.
The output is L for duty clocks, then H for top-duty clocks at 7.3728MHz.
The behaviour for values outside the allowed range is undefined.

By writing to PWM pin 255, the TOP value can be controlled between 2...65535. The PWM output frequency is 7.3728MHz / (TOP+1).

There is optional support for overcurrent detection. The PWM is stopped and locked off until released. This works by sensing the voltage on an analog input of the controller. External circuitry is required that converts the current into a voltage.

The input voltage range [0..5V] is converted to an 8-bit value in the range [0..255]. When the sample value goes above the passed limit value, the PWM output is turned off. A limit value of 0 disables overcurrent detection.

Once an overcurrent state is detected, the condition is latched into an error flag and the PWM output stays off. Any attempt to re-enable the PWM is blocked, until the error flag is cleared by software. The error flag is implemented as a virtual GPIO pin accessed via typical GPIO input/output commands.

The ADC implementation is not especially accurate. Reference voltage is the board 5V supply.

- For the first PWM output SKIO_CDR10:
  - ADC input is at J13, pin11 (SKIO_RDT1)
  - Virtual error flag is at SKIO_OCALERT0
- For the second PWM output SKIO_RCP1:
  - ADC input is at J13, pin24 (SKIO_RDT1)
  - Virtual error flag is at SKIO_OCALERT1

## SEE ALSO

gpio.dip, gpio.dips, gpio.edge, gpio.input, gpio.inputs, gpio.output, gpio.outputs