DIO + PWM – Digital Input Output

A pin object is used to control I/O pins (also known as GPIO - general-purpose input/output).
Pin objects are commonly associated with a physical pin that can drive an output voltage and read input voltages.

In this section we will cover Din and Dout classes.

Note

  • The external hardware has inverse logic which is compensated by this module.
    E.g.: if you set DOUT1 to 1, you will see a high value output.

  • For details related with hardware (pinout, cables, schematics, …), please check this section.

Methods

class Din

Digital input pin manager that allows reading the digital input pins directly or via callback.
SBC supports digital input pins: DIN1 and DIN2.

read(pin_id)

Read Pin value.

Args:

pin_id: Pin to read, it can be an integer (0, 1) or a string (“DIN1”, “DIN2”).

Return:

Value readed.

Example to read DIN1 pin using an integer or using a string:
>>> import sbc
>>> din = sbc.Din()
>>> print( din.read( 0 ))
0
>>> print( din.read( "DIN1" ))
0
set_callback(pin_id, callback, edge=0)

Sets Pin callback function that will be called when the edge event is triggered.

Args:

pin_id: integer or string.
callback: function callback. It is expected one argument.
edge: configures the event which can generate an interrup.
Valid values are ¨EDGE_RISING¨ or ¨EDGE_FALLING¨. These values can be OR’ed: ¨EDGE_RISING | EDGE_FALLING¨.

Example that sets a callback function on pin DIN1 that prints its value when either it states goes from 0 to 1 or 1 to 0:
>>> import sbc
>>> def callback_din1( pin ):
>>>     print(pin.value())
>>> din = sbc.Din()
>>> din.set_callback( "DIN1", callback_din1, din.EDGE_RISING | din.EDGE_FALLING )
class Dout

Digital output pin manager that allows setting their output state (0 or 1) and a PWM signal output.
SBC supports digital output pins: DOUT1, DOUT2, DOUT3 and DOUT4.

write(pin_id, state)

Write Pin value.

Args:

pin_id: Pin to write, it can be an integer (0, 1, 2, 3) or a string (“DOUT1”, “DOUT2”, “DOUT3”, “DOUT4”). state: Pin state (0 or 1)

Example to write DOUT1 pin using an integer or using a string:
>>> import sbc
>>> dout = sbc.Dout()
>>> dout.write(0, 1)
>>> dout.write("DOUT1", 1)       
toggle(pin_id)

Toggle Pin value.
Toggle means to change the Pin state without knowing it (0 –> 1, 1 –> 0).

Args:

pin_id: Pin to toggle, it can be an integer (0, 1, 2, 3) or a string (“DOUT1”, “DOUT2”, “DOUT3”, “DOUT4”).

Example to toggle DOUT1 pin using an integer or using a string:
>>> import sbc
>>> dout = sbc.Dout()
>>> dout.toggle(0)
>>> dout.toggle("DOUT1")    
set_pwm(pin_id, freq_hz, pulse_percent_on)

Set PWM signal.

Args:

pin_id: Pin to set the PWM signal, it can be an integer (0, 1, 2, 3) or a string (“DOUT1”, “DOUT2”, “DOUT3”, “DOUT4”).
freq_hz: PWM frequency in Hz. Set to 0 to disable PWM.
pulse_percent_on: PWM duty cycle percent (ON) in range 0-100.

Example to set a 100Hz frequency PWM signal at pin DOUT3 with a 25% duty cycle:
>>> import sbc
>>> dout = sbc.Dout()
>>> dout.set_pwm( "DOUT3", 100, 25)