7.1. hardware — The Hardware module API

The module described in this chapter provides classes that handle several hardware peripherals.

hardware.info()

Return an info object that contains the following information regarding the platform where Wubby is running.

  • Platform name
  • The available heap size for the python garbage collector
  • The Wubby version
import hardware

info = hardware.info()
print (info)

7.1.1. Class GPIO

The gpio class controls the GPIO peripheral. It has methods to set the mode of the GPIO and methods to get and set the digital logic level.

class hardware.gpio(pin, mode)

Return a gpio object mapped to the given pin argument and initialize it according to the mode argument.

mode can be one of the following:

  • GPIO_MODE_INPUT_PULLUP : configure the pin as input, enable the pull-up resistor.
  • GPIO_MODE_INPUT_PULLDOWN: configure the pin as input, enable the pull-down resistor.
  • GPIO_MODE_OUTPUT_PULLUP: configure the pin as output, enable the pull-up resistor.
  • GPIO_MODE_OUTPUT_PULLDOWN: configure the pin as output, enable the pull-down resistor.

A ValueError exception is thrown if a GPIO with the given pin does not exist .

The gpio class provides the following methods. All of them raise a ValueError exception if the operation was not successfull.

gpio.write(level)

Set the selected GPIO to the value “0” or “1”. Logic 1 puts a 3.3V signal on the GPIO while Logic 0 puts a 0V signal on the GPIO.

gpio.read()

Return “0” or “1” depending on the state of the GPIO.

gpio.toggle()

Check the current status of the GPIO pin and toggle the output to the opposite state that is currently in. The return value is the current state of the GPIO pin.

Example usage:

import time
from hardware import gpio

print ("Wubby Project")
print ("GPIO Example")

## Create a new gpio object
led = gpio(2,gpio.GPIO_MODE_OUTPUT_PULLDOWN)

led.write(0)

## Read Value
while True:
        led.toggle()
        time.sleep(1000)

7.1.2. Class ADC

The adc class controls the ADC peripheral. More specifically it associates an adc object with a GPIO pin and provides the methods to read analog values on that pin.

class hardware.adc(pin)
Return an adc object mapped to the given pin.

The adc class provides the following methods.

adc.init(sampling_rate)

Initialize the adc object with the specified sampling rate. This is the first method that should be called after the creation of the object so that the ADC peripheral is correctly initialized. init() throws a ValueError exception if the adc object could not be initialized.

adc.read()

Read the value on the analog pin and return it.

Example usage:

import time
from hardware import adc

print ("Wubby Project")
print ("ADC Example")

## Create a new adc object
analog = adc(1)


## Initialize the new object
analog.init(1000)


## Read Value
while True:
        val = analog.read()
        print ("value : " + str(val))
        time.sleep(1000)

7.1.3. Class UART

The uart class implements the UART serial communication bus and provides methods to read and write through it.

class hardware.uart(bus)

Return a uart object mapped to the given bus.

uart.init(baudrate[, parity, twoStopBits, timeout, bufferLen])

Initialize the uart object with the given parameters.

  • baudrate is the baudrate of the UART.
  • parity can be 0 (no parity), 1 (odd) or 2 (even). If no parity is given, the object is initialized with parity = 0.
  • twoStopBits can be True or False. Default value is False.
  • timeout is the timeout in milliseconds to wait for the first character.
  • bufferLen is the character length of the read buffer.

This is the first method that should be called after the creation of the object so that the UART peripheral is correctly initialized. init() throws a ValueError exception if the uart object could not be initialized.

The uart class provides the following methods.

uart.write(buf)

Write the buffer of bytes buf to the bus and return the number of bytes written.

uart.read()

Read characters until a timeout happens or \n termination character is received.

7.1.4. Class SPI

The spi class implements the SPI serial interface.

class hardware.spi(id)

Return an spi object. id is the identifier of the selected SPI interface.

The spi class provides the following methods.

spi.init(clock, mode, ss)

Initialize the spi object with the given parameters. Where:

  • clock - the SCK clock rate in Hz (only sensible for a master).
  • mode - Configure the clock polarity (CPOL) and phase (CPHA), the use or not of DMA and the MSB/LSB first operation. Where:
  • SPI_CLOCK_RISING_EDGE ( 1 << 0 )
  • SPI_CLOCK_FALLING_EDGE ( 0 << 0 )
  • SPI_CLOCK_IDLE_HIGH ( 1 << 1 )
  • SPI_CLOCK_IDLE_LOW ( 0 << 1 )
  • SPI_USE_DMA ( 1 << 2 )
  • SPI_NO_DMA ( 0 << 2 )
  • SPI_MSB_FIRST ( 1 << 3 )
  • SPI_LSB_FIRST ( 0 << 3 )
  • ss - the GPIO pin used as the slave select signal.

This is the first method that should be called after the creation of the object so that the SPI peripheral is correctly initialized.

spi.deinit()

Deinitialize the spi object.

spi.read(recv, timeout=5000)

Receive data on the bus.

  • recv - can be an integer, which is the number of bytes to receive, or a mutable buffer, which will be filled with received bytes.
  • timeout - the timeout in milliseconds to wait for the receive.
spi.write(data, data_len)

Write data on the bus.

  • data is the data to send (an integer to send, or a buffer object).
  • data_len is the number of bytes to send.

Note

For an example of the SPI usage you can check the OLED_LCD example included in the Wubby IDE.

7.1.5. Class I2C

The i2c class implements the I2C peripheral.

class hardware.i2c(id)

Return a i2c object. id is the identifier of the selected I2C peripheral.

i2c.init(address, address_width, speed_mode)

Initialize the i2c object with the given parameters. Where:

  • address is the address of the device on the i2c bus

  • address_width can have one of the following values:
    • 0 for I2C_ADDRESS_WIDTH_7BIT
    • 1 for I2C_ADDRESS_WIDTH_10BIT
    • 2 for I2C_ADDRESS_WIDTH_16BIT
  • speed_mode is the speed mode the device operates in. It can have one of the following values:
    • 0 for I2C_LOW_SPEED_MODE ( 10Khz devices )
    • 1 for I2C_STANDARD_SPEED_MODE ( 100Khz devices )
    • 2 for I2C_HIGH_SPEED_MODE ( 400Khz devices )
i2c.deinit()

Deinitialize the i2c object.

i2c.read(recv)

Read recv bytes from the bus and return the result as a bytearray.

  • recv is an integer that corresponds to the number of bytes to read.
i2c.write(data, data_len)

Write data on the bus.

  • data is the data to send.
  • data_len is the number of bytes to send.

Note

For an example of the I2C usage you can check the I2C_PortExtender and I2C_RTC examples included in the Wubby IDE.

7.1.6. Class PWM

The pwm class implements the PWM peripheral.

class hardware.pwm(id)

Return a pwm object. id is the identifier of the selected PWM peripheral.

pwm.init(frequency, duty_cycle)

Initialize the pwm object with the given parameters. Where:

  • frequency is the PWM’s switching frequency.
  • duty_cycle is the proportion of ‘on’ time to the regular interval or ‘period’ of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.

This is the first method that should be called after the creation of the object so that the PWM peripheral is correctly initialized.

The init() method throws a ValueError exception if the peripheral with the given id is not found.

pwm.start()

Start the operation of the pwm .

pwm.stop()

Stop the operation of the pwm.

Example usage:

import time
from hardware import pwm

print ("Wubby Project")
print ("PWM Example")

## Create a new pwm object
fade = pwm(1)


## Initialize the new object

while True:
        fade.init(1000,1)
        fade.start()
        time.sleep(10000)
        fade.stop()
        fade.init(1000,30)
        fade.start()
        time.sleep(10000)
        fade.stop()
        time.sleep(10000)