GPIO API

When Kervi starts it scans for installed Kervi platform drivers and loads the GPIO driver. In your application you access GPIO via:

import kervi.hal.GPIO as GPIO

#define channel 23 as input
GPIO.define_as_input(23)
class kervi.hal.gpio.AnalogIOChannel(gpio_device, channel, is_input)
class kervi.hal.gpio.IGPIODeviceDriver(gpio_id)
define_as_input(channel, pullup=False)

Define a channel as input

define_as_output(channel)

Define a channel as output

define_as_pwm(channel, frequency, duty_cycle=None)

Defines a channel as pwm output.

Parameters:
  • channel (int) – The channel to define as a pwm output.
  • frequency – The pwn frequency.
  • duty_cycle – The duty_cycle to use, can be changed in call to pwm_start
get(channel)

Returns the state of a channel.

listen(channel, callback, bounce_time=0.2)

Listen on a channel for state change. The callback function is called when a channel is going high and low.

Parameters:
  • channel (float) – The channel to listen on.
  • callback (function or method) – The function or method to call when the state of the channel change.
  • bounce_time – To reduce switch noice a bounce_time > 0 filters small jitters when a switch or button is pressed
listen_falling(channel, callback)

calls the callback when a channel is going low.

Parameters:
  • channel (function or method) – The channel to listen on.
  • callback – The function or method to call when the state of the channel change.
listen_rising(channel, callback)

calls the callback when a channel is going high.

Parameters:
  • channel (function or method) – The channel to listen on.
  • callback – The function or method to call when the state of the channel change.
pwm_start(channel, duty_cycle=None, frequency=None)

Starts the pwm signal on a channel. The channel should be defined as pwm prior to this call. If no duty_cycle or frequency is passed in this call previous values from call to define_as_pwm or pwm_start is used.

Parameters:
  • channel (int) – The channel to start the pwm signal on.
  • duty_cycle (int) – The duty cycle use on the channel.
  • frequency (int) – The frequency to be used on the pwm channel.
pwm_stop(channel)

Stop pwm signal on channel.

set(channel, state)

Sets the state of a channel that is defined as output.

Parameters:
  • channel (bool) – The channel that should be changed.
  • state – The state of the channel.
set_channels(channels)

Sets the state of multiple channels in one operation.

Parameters:channels (dict) – A dictionary where keys are channels and values the value to set for each channel.
class kervi.hal.gpio.LogicIOChannel(gpio_device, channel)