Python API

Port

class modulo.Port(serialPortPath=None)[source]

Bases: object

The Port class represents a physical connection to Modulo devices through a usb or i2c port. Once a port has been opened, create a Module object for each device that’s connected to the port.

loop(noWait=False)[source]

Call loop as often as possible to handle events and execute callbacks

runForever()[source]

Continue to process events forever

Knob

class modulo.Knob(port, deviceID=None)[source]

Bases: modulo.modulos.ModuloBase

Connect to the module with the specified deviceID on the given port. If deviceID isn’t specified, finds the first unused KnobModule.

buttonPressCallback = None

A function that will be called when the knob is pressed.

The first argument to the function is the knob object that’s receiving the event. ie:

def onKnobPressed(knob) :
   ...
buttonReleaseCallback = None

A function that will be called when the knob is released.

The first argument to the function is the knob object that’s receiving the event. ie:

def onKnobReleased(knob) :
   ...
close()

Disconnect this object from its associated Port

getAddress()

Return the I2C address or None if no modulo was found

getAngle()[source]

Return the angle of the knob in degrees between 0 and 360.

getButton()[source]

Return whether the knob is currently being pressed

getDeviceID()

Return the device ID or None if no modulo was found

getPosition()[source]

Return the position of the knob in clicks (24 per revolution).

positionChangeCallback = None

A function that will be called when the knob’s position changes.

The first argument to the function is the knob object that’s receiving the event. ie:

def onKnobTurned(knob) :
   ...
setColor(red, green, blue)[source]

Set the color of the knob’s LED. red, green, and blue should be between 0 and 1

setDeviceID(deviceID)

Set the ID of the modulo that this object should connect to

setHSV(hue, saturation, value)[source]

Set the color of the knob’s LED. hue, saturation, and value should be between 0 and 1

transfer(command, sendData, receiveLen)

Joystick

class modulo.Joystick(port, deviceID=None)[source]

Bases: modulo.modulos.ModuloBase

Connect to the module with the specified deviceID on the given port. If deviceID isn’t specified, finds the first unused KnobModule.

buttonPressCallback = None

A function that will be called when the joystick is pressed.

The first argument to the function is the joystick object that’s receiving the event. ie:

def onJoystickPressed(joystick) :
   ...
buttonReleaseCallback = None

A function that will be called when the joystick is released.

The first argument to the function is the joystick object that’s receiving the event. ie:

def onJoystickReleased(joystick) :
   ...
close()

Disconnect this object from its associated Port

getAddress()

Return the I2C address or None if no modulo was found

getButton()[source]

Return wehther the joystick is currently pressed

getDeviceID()

Return the device ID or None if no modulo was found

getHPos()[source]

Return the horizontal position of the joystick. (between -1 and 1)

getVPos()[source]

Return the vertical position of the joystick. (between -1 and 1)

positionChangeCallback = None

A function that will be called when the joystick position changes.

The first argument to the function is the joystick object that’s receiving the event. ie:

def onJoystickMoved(joystick) :
   ...
setDeviceID(deviceID)

Set the ID of the modulo that this object should connect to

transfer(command, sendData, receiveLen)

Display

class modulo.Display(port, deviceID=None)[source]

Bases: modulo.modulos.ModuloBase

Connect to the module with the specified deviceID on the given port. If deviceID isn’t specified, finds the first unused MiniDisplayModule.

buttonPressCallback = None

A function that will be called when a button is pressed.

The first argument to the function is the display object that’s receiving the event. The second argument is the button number (0, 1, or 2) ie:

def onButtonPressed(display, button) :
   ...
buttonReleaseCallback = None

A function that will be called when a button is released.

The first argument to the function is the display object that’s receiving the event. The second argument is the button number (0, 1, or 2) ie:

def onButtonReleased(display, button) :
   ...
clear()[source]

Fill the screen with black, set the line, fill, and text colors to white, and return the cursor to (0,0)

close()

Disconnect this object from its associated Port

drawCircle(x, y, radius)[source]

Draw a circle centered at (x,y) with the specified radius.

x and y must be between -128 and 127. radius must be between 0 and 255

drawLine(x0, y0, x1, y1)[source]

Draw a line segment from (x0,y0) to (x1,y1)

All values must be between -127 and 128.

Draw the Modulo logo

drawRect(x, y, w, h, r=0)[source]

Draw a rectangle with the upper left corner at (x,y) and the specified width, height, and corner radius.

drawSplashScreen()[source]

Draw the Modulo logo and the word ‘MODULO’ on a purple background

drawTriangle(x0, y0, x1, y1, x2, y2)[source]

Draw a triangle.

Values must be between -128 and 127.

fillScreen(r, g, b)[source]

Fill the screen

getAddress()

Return the I2C address or None if no modulo was found

getButton(button)[source]

Return whether the specified button is currently pressed

getButtons()[source]

Return the state of all three buttons, one in each bit.

getDeviceID()

Return the device ID or None if no modulo was found

height = None

The height of the display in pixels

isComplete()[source]

Return whether all previous drawing operations have been completed.

isEmpty()[source]

Return whether the queue of drawing operations is empty. If the display is still refreshing, it may be empty but not complete.

refresh(flip=False)[source]

Display the results of all previous drawing commands. Note that after calling refresh, the next drawing operation will block until the frame has been drawn.

setContrast(r, g, b)[source]

Set the per channel contrast values, which affect image brightness and color balance. The default is (.93, 0.555, 1.0).

setCurrent(current)[source]

Set the display’s master current. Higher current values produce a brighter, more vivid image but may increase image burn-in and audible noise from the OLED driver. The default is .75.

setCursor(x, y)[source]

Set the cursor position, which is where the next text will be drawn.

setDeviceID(deviceID)

Set the ID of the modulo that this object should connect to

setFillColor(r, g, b, a=1)[source]

Set the current fill color

setLineColor(r, g, b, a=1)[source]

Set the current line color.

setTextColor(r, g, b, a=1)[source]

Set the current text color

setTextSize(size)[source]

Set the text size. This is a multiplier of the base text size, which is 8px high.

transfer(command, sendData, receiveLen)
width = None

The width of the display in pixels

write(s)[source]

Write a string s. You can also print to the display with print >>display,”Hello Modulo” (Python 2) or print(“Hello Modulo”, file=display) (Python 3)

MotorDriver

class modulo.MotorDriver(port, deviceID=None)[source]

Bases: modulo.modulos.ModuloBase

ModeDC = 1
ModeDisabled = 0
ModeStepper = 2
close()

Disconnect this object from its associated Port

faultChangedCallback = None

A function that will be called when the fault status changes.

The first argument to the function is the MotorDriver object that’s receiving the event. ie:

def onFaultChanged(motorDriver) :
   ...
getAddress()

Return the I2C address or None if no modulo was found

getDeviceID()

Return the device ID or None if no modulo was found

getStepperPosition()[source]

Return the current position of the stepper motor in 1/256 increments of wholes steps.

hasFault()[source]

Return whether a fault condition (such as a short between motor terminals, over current shutdown, or over temperature shutdown) is currently present.

positionReachedCallback = None

A function that will be called when the stepper target position is reached.

The first argument to the function is the MotorDriver object that’s receiving the event. ie:

def onPositionReached(motorDriver) :
   ...
setChannel(channel, amount)[source]

Set a single channel (0-3) to the specified amount, between 0 and 1. Changes the mode to ModeDC if it’s not already.

setCurrentLimit(limit)[source]

Set the driver current limit (between 0 and 1).

setDeviceID(deviceID)

Set the ID of the modulo that this object should connect to

setMode(mode)[source]

Set the driver mode to Disabled, DC, or Stepper

setMotorA(value)[source]

Set the motor output A to the specified amount, between -1 and 1. Changes the mode to ModeDC if it’s not already.

setMotorB(value)[source]

Set the motor output B to the specified amount, between -1 and 1. Changes the mode to ModeDC if it’s not already.

setPWMFrequency(freq)[source]

Set the motor driver PWM frequency

setStepperRate(usPerStep)[source]

Set the number of microseconds to take between each whole step.

setStepperResolution(microsteps, minMicrostepDuration=1000)[source]

Set the number of microsteps to take between each whole step. It can be 1, 2, 4, 8, 16, 32, 64, 128, or 256.

If the duration of a microstep (in microseconds) would be less than minMicrostepDuration, then the number of microsteps is decreased automatically. This helps to avoid skipping steps when the rate is higher than the motor or driver can handle.

setStepperSpeed(stepsPerSecond)[source]

Set the stepper speed in whole steps per second.

setStepperTarget(targetPos)[source]

Set the stepper target position. The target position is in 1/256 of a whole step. (So setting the target to 256 will take as many steps/ microsteps as are necessary to move to the position that is 1 whole step from the starting position.

transfer(command, sendData, receiveLen)

TemperatureProbe

class modulo.TemperatureProbe(port, deviceID=None)[source]

Bases: modulo.modulos.ModuloBase

close()

Disconnect this object from its associated Port

getAddress()

Return the I2C address or None if no modulo was found

getDeviceID()

Return the device ID or None if no modulo was found

getTemperatureC()[source]

Return the temperature of the probe in celsius

getTemperatureF()[source]

Return the temperature of the probe in fahrenheit

isValid = None

A function that will be called when the probe’s temperature changes.

The first argument to the function is the temperature probe that’s receiving the event. ie:

def onTemperatureChanged(temperatureProbe) :
   ...
setDeviceID(deviceID)

Set the ID of the modulo that this object should connect to

temperatureChangeCallback = None

Function to be called when the temperature changes

transfer(command, sendData, receiveLen)

IRRemote

class modulo.IRRemote(port, deviceID=None)[source]

Bases: modulo.modulos.ModuloBase

Infrared remote control transmitter and receiver

Note: this class can send and receive raw IR data as a sequence of pulse lengths, but support for encoding and decoding those pulse lengths into useful codes is not quite complete. Full encoding/decoding will be implemented soon. Please check community.modulo.co for more information on the status of this feature.

close()

Disconnect this object from its associated Port

getAddress()

Return the I2C address or None if no modulo was found

getDeviceID()

Return the device ID or None if no modulo was found

send(data)[source]

Send raw data. Each byte is the number of 50us ticks that the output should be on or off. The first byte is an off period.

setBreakLength(l)[source]

Set the no signal time that’s required before the receiver considers a transmission complete.

setDeviceID(deviceID)

Set the ID of the modulo that this object should connect to

transfer(command, sendData, receiveLen)

BlankSlate

class modulo.BlankSlate(port, deviceID=None)[source]

Bases: modulo.modulos.ModuloBase

close()

Disconnect this object from its associated Port

getAddress()

Return the I2C address or None if no modulo was found

getAnalogInput(pin)[source]

Disables the output on the specified pin and performs an analog read.

getDeviceID()

Return the device ID or None if no modulo was found

getDigitalInput(pin)[source]

Disables the output on the specified pin and returns the pin’s value

getDigitalInputs()[source]

Reads the digital inputs from all 8 pins. Does not enable/disable outputs on any pins.

setDeviceID(deviceID)

Set the ID of the modulo that this object should connect to

setDigitalOutput(pin, value)[source]

Enables the output and sets the output value on the specified pin.

setDigitalOutputs(values)[source]

Set the digital outputs on all 8 pins. Does not enable or disable outputs on any pins.

setDirection(pin, output)[source]

Sets the pin direction to either output or input

setDirections(outputs)[source]

Sets the pin directions for all 8 pins simultaneously

setPWMFrequency(pin, value)[source]

Set the frequency for PWM signals on the specified pin.

setPWMValue(pin, value)[source]

Enable the output and set the PWM duty cycle on the specified pin. Pins 0-4 have hardware PWM support. Pins 5-7 only have software PWM which has more jitter, especially at high frequencies.

setPullup(pin, enable)[source]

Sets whether a pullup is enabled on the specified pin.

setPullups(values)[source]

Set whether the pullup is enabled on all 8 pins.

transfer(command, sendData, receiveLen)