Running Hubo hardware using Python
Hubo hardware can not only be controlled by C++ but other languages too. Python is one of them. Please find below a few examples on how to go on with Python.
MCP23017 - Reading from and writing to Hubo's digital IO.
This demo illustrates how to read from and write to the digital IO.
#!/usr/bin/python
import smbus
import time
# Hubo MCP23017 Demo - simple reading and writing of the MCP23017 digital IO.
IODIRA = 0x00
IODIRB = 0x01
GPPUB = 0x0D
GPIOA = 0x12
GPIOB = 0x13
global g_ioaddress
g_ioaddress = 0x20 # I2C address - Hubo master modules use 0x20
i2cbus = smbus.SMBus(1) # Use 1 for the model
B variants, use 0 for Banana Pi.
i2cbus.write_byte_data (g_ioaddress, IODIRA, 0x00) # Configure port
A as output
i2cbus.write_byte_data (g_ioaddress, IODIRB, 0xFF) # Configure port
B as input
i2cbus.write_byte_data (g_ioaddress, GPPUB, 0xFF) # Activate port
B's internal pull up resistors
return i2cbus
def Get_DI_Channels (i2cbus):
return i2cbus.read_byte_data(g_ioaddress, GPIOB)
def Set_DI_Channels (i2cbus, value):
return i2cbus.write_byte_data(g_ioaddress, GPIOA, value)
# Read values from the digital input port and output the value on the digital output port.
i2cbus = Initialize_MCP23017()
while True:
value = Get_DI_Channels (i2cbus)
Set_DI_Channels(i2cbus, value)
print value
time.sleep(0.1)
MCP3208 - Reading analog input from Hubo hardware.
The following code reads analog values from the MCP3208's channel 0.
#!/usr/bin/python
import spidev
import time
# Hubo MCP3208 Demo - simple reading of the MCP3208 analog input.
def Open_SPI_ADC ():
spiADC = spidev.SpiDev()
spiADC.open(0, 0)
spiADC.max_speed_hz = (50000)
return spiADC
if ((channel > 7) or (channel < 0)):
print 'Illegal channel number (0 through 7)'
return 0.0
result = spiADC.xfer2([6 + (channel >> 2), channel << 6, 0])
count = ((result[1] & 0x0F) << 8) + (result[2])
# Use a reference voltage of 2.53V.
voltage = (2.53 * count) / 4096
return voltage
# Read 10 values from analog input 0.
spiADC = Open_SPI_ADC()
for i in range(0, 9):
time.sleep(1)
spiADC.close();