Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a...

23
Wiring-x86 Documentation Release Emutex Ltd Apr 24, 2017

Transcript of Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a...

Page 1: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 DocumentationRelease

Emutex Ltd

Apr 24, 2017

Page 2: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable
Page 3: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Contents

1 Getting Started with Wiring-x86 3

2 The Wiring-x86 API 5

3 Examples 9

4 Learn More 17

i

Page 4: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

ii

Page 5: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable boards such as:

• Intel® Edison

• Intel® Galileo Gen2

• Intel® Galileo

It provides a simple and unified API (similar to the WiringPi module) to talk to the GPIO pins on the board.

At the moment the Wiring-x86 library provides support to:

• Writing to a GPIO pin configured as output.

• Reading from a GPIO pin configured as high impedance input.

• Reading from a GPIO pin configured as pullup input.

• Reading from a GPIO pin configured as pulldown input.

• Reading from a GPIO pin configured as analog input (ADC).

• Writing to a GPIO pin configured as analog output (PWM).

Here is a simple example:

# Import the time module enable sleeps between turning the led on and off.import time

Contents 1

Page 6: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

# Import the GPIOEdison class from the wiringx86 module.# In case you want to use a different board, simply the import right class# below. The API is unified for all supported boards.from wiringx86 import GPIOEdison as GPIO

# Create a new instance of the GPIOEdison class.# Setting debug=True gives information about the interaction with sysfs.gpio = GPIO(debug=False)pin = 13state = gpio.HIGH

# Set pin 13 to be used as an output GPIO pin.print 'Setting up pin %d' % pingpio.pinMode(pin, gpio.OUTPUT)

print 'Blinking pin %d now...' % pintry:

while(True):# Write a state to the pin. ON or OFF.gpio.digitalWrite(pin, state)

# Toggle the state.state = gpio.LOW if state == gpio.HIGH else gpio.HIGH

# Sleep for a while.time.sleep(0.5)

# When you get tired of seeing the led blinking kill the loop with Ctrl-C.except KeyboardInterrupt:

# Leave the led turned off.print '\nCleaning up...'gpio.digitalWrite(pin, gpio.LOW)

# Do a general cleanup. Calling this function is not mandatory.gpio.cleanup()

Contents:

2 Contents

Page 7: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

CHAPTER 1

Getting Started with Wiring-x86

Here are some easy instructions to get you up and running with the wiring-x86.py module.

Prerequisites

The wiring-x86.py module supports these platforms:

• Intel® Edison

• Intel® Galileo Gen2

• Intel® Galileo

The original YOCTO Linux OS provided by Intel® must be used. For more information on how to get this softwarego to Intel® Makers site. This module will only work with that combination of boards and OS since it uses specificIntel® GPIO driver sysfs interface.

The wiring-x86.py module also requires Python 2.7 which is installed as standard on.

Installing Wiring-x86

The first step is to install the Wiring-x86 module. There are several ways to do this.

Installing from a tarball

When using the original YOCTO Linux distribution provided with the board this is the easiest method to get themodule installed. A tarball of the latest code can be downloaded from GitHub as follows:

$ curl -O -L http://github.com/emutex/wiring-x86/archive/master.tar.gz

Install it as follows:

3

Page 8: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

$ tar zxvf master.tar.gz$ cd wiring-x86-master/$ sudo python setup.py install

Cloning from GitHub

The Wiring-x86 source code and bug tracker is in the wiring-x86 repository on GitHub.

You can clone the repository and install from it as follows:

$ git clone https://github.com/emutex/wiring-x86.git$ cd wiring-x86$ sudo python setup.py install

Using PIP

If available on your system, use pip installer to get the package from PyPI, the Python Package Index:

$ sudo pip install wiring-x86

Using Easy_Install

If pip isn’t available on your system then easy_install may be:

$ sudo easy_install wiring-x86

Running a sample program

The wiring-x86 repository and tarball includes several example programs. There are also shown in the Examplessection.

4 Chapter 1. Getting Started with Wiring-x86

Page 9: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

CHAPTER 2

The Wiring-x86 API

The wiring-x86 module provides an unified API for all the platforms it supports. In case of a platform-specificAPI was added it will be explicitly mentioned in the documentation.

Two classes are exposed my the module:

• GPIOEdison

• GPIOGalileoGen2

• GPIOGalileo

The module is generally imported and intialised as follows:

from wiringx86 import GPIOEdison as GPIO

gpio = GPIO()

Constructor

GPIO([debug])Create a new GPIO object.

Parameters debug (bool) – Optional debug parameter.

Return type A GPIO object.

The GPIO() constructor is used to create a new gpio object:

from wiringx86 import GPIOEdison as GPIO

gpio = GPIO()

The debug constructor options can be used to enable the debug mode showing the interaction with sysfs:

5

Page 10: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

gpio = GPIO(debug=False)

gpio.digitalWrite()

digitalWrite(pin, state)Configure a gpio pin for output.

Parameters

• pin (int) – Arduino pin number (0-19)

• state (string) – Pin state to be written (LOW-HIGH)

The digitalWrite() method is used to set the state for a given gpio pin. The available states are high and low.These are generally passed as GPIO properties such as gpio.HIGH for convenience:

gpio.digitalWrite(13, gpio.HIGH)

The GPIO pin is assumed to be configured as gpio.OUTPUT when writing.

gpio.digitalRead()

digitalRead(pin)Read the state of a GPIO pin.

Parameters pin (int) – Arduino pin number (0-19).

Returns Current value of the GPIO pin.

Return type int

Read the current state of a GPIO pin:

state = gpio.digitalRead(pin)

The GPIO pin is assumed to be configured as gpio.INPUT

gpio.analogWrite()

analogWrite(pin, value)Write analog output (PWM) to a pin.

Parameters

• pin (int) – Arduino PWM pin number (3, 5, 6, 9, 10, 11)

• value (int) – The duty cycle: between 0 (always off) and 255 (always on).

The gpio pin is assumed to be configured as gpio.PWM. Generates a PWM signal with the desired duty cycle. Thevalue must be in range 0-255.

6 Chapter 2. The Wiring-x86 API

Page 11: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

gpio.analogRead()

analogRead(pin)Read analog input from the pin

Param int pin: Arduino analog pin number (14-19).

Returns Digital representation with 10 bits resolution (range 0-1023) of voltage on the pin.

The GPIO pin is assumed to be configured as gpio.ANALOG_INPUT. Returns values in range 0-1023:

value = gpio.analogRead(analogpin)

gpio.setPWMPeriod()

setPWMPeriod(pin, period)Set the PWM period.

Param int pin: Arduino PWM pin number (3, 5, 6, 9, 10, 11).

Param int period: PWM period in nanoseconds.

On the Galileo Gen2 all PWM channels share the same period. When this is set all the PWM outputs are disabled forat least 1ms while the chip reconfigures itself. The PWM pin is then ignored.

gpio.pinMode()

pinMode(pin, mode)Set the mode of a GPIO pin.

Parameters

• pin (int) – Arduino pin number (0-19)

• mode (string) – Pin mode. See below.

This function must be called before doing any other operation on the pin. It sets up the muxing needed for the pin toput it in one of the following modes:

• OUTPUT: pin used as output. Use to write into it.

• INPUT: pin used as input (high impedance). Use to read from it.

• INPUT_PULLUP: pin used as input (pullup resistor). Use to read from it.

• INPUT_PULLDOWN: pin used as input (pulldown resistor). Use to read from it.

• ANALOG_INPUT: pin used as analog input (ADC).

• PWM: pin used as analog output (PWM).

For example:

gpio.pinMode(pin, gpio.OUTPUT)

2.5. gpio.analogRead() 7

Page 12: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

gpio.cleanup()

cleanup(self)Do a general cleanup.

Close all open handlers for reading and writing. Unexport all exported GPIO pins and unexport all exported PWMchannels:

gpio.cleanup()

Calling this function is not mandatory but it’s recommended once you are finished using the library and if it is beingused with a larger application that runs for a long period of time.

8 Chapter 2. The Wiring-x86 API

Page 13: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

CHAPTER 3

Examples

The following are some of the examples included in the examples directory of the Wiring-x86 distribution.

Example: Blink

A simple example of blinking an LED on and off using the wiring-x86 interface.

9

Page 14: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

# -*- coding: utf-8 -*-## Copyright © 2014, Emutex Ltd.# All rights reserved.# http://www.emutex.com## Author: Nicolás Pernas Maradei <[email protected]>## See license in LICENSE.txt file.## This example is inspired on Arduino Blink example.# http://arduino.cc/en/Tutorial/Blink## This example will work "out of the box" on an Intel® Edison board. If# you are using a different board such as an Intel® Galileo Gen2, just change the# import below. wiringx86 uses the same API for all the boards it supports.

# Import the time module enable sleeps between turning the led on and off.import time

# Import the GPIOEdison class from the wiringx86 module.from wiringx86 import GPIOEdison as GPIO

10 Chapter 3. Examples

Page 15: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

# Create a new instance of the GPIOEdison class.# Setting debug=True gives information about the interaction with sysfs.gpio = GPIO(debug=False)pin = 13state = gpio.HIGH

# Set pin 13 to be used as an output GPIO pin.print 'Setting up pin %d' % pingpio.pinMode(pin, gpio.OUTPUT)

print 'Blinking pin %d now...' % pintry:

while(True):# Write a state to the pin. ON or OFF.gpio.digitalWrite(pin, state)

# Toggle the state.state = gpio.LOW if state == gpio.HIGH else gpio.HIGH

# Sleep for a while.time.sleep(0.5)

# When you get tired of seeing the led blinking kill the loop with Ctrl-C.except KeyboardInterrupt:

# Leave the led turned off.print '\nCleaning up...'gpio.digitalWrite(pin, gpio.LOW)

# Do a general cleanup. Calling this function is not mandatory.gpio.cleanup()

Example: Blink All

A simple example of blinking all LED on and off using the wiring-x86 interface.

3.2. Example: Blink All 11

Page 16: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

# -*- coding: utf-8 -*-## Copyright © 2014, Emutex Ltd.# All rights reserved.# http://www.emutex.com## Author: Nicolás Pernas Maradei <[email protected]>## See license in LICENSE.txt file.## This example is inspired on Arduino Blink example.# http://arduino.cc/en/Tutorial/Blink## This example will work "out of the box" on an Intel® Edison board. If# you are using a different board such as an Intel® Galileo Gen2, just change the# import below. wiringx86 uses the same API for all the boards it supports.

# Import the time module enable sleeps between turning the led on and off.import time

# Import the GPIOEdison class from the wiringx86 module.from wiringx86 import GPIOEdison as GPIO

# Create a new instance of the GPIOEdison class.# Setting debug=True gives information about the interaction with sysfs.gpio = GPIO(debug=False)state = gpio.HIGHpins = 20

# Set all pins to be used as output GPIO pins.print 'Setting up all pins...'

12 Chapter 3. Examples

Page 17: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

for pin in range(0, pins):gpio.pinMode(pin, gpio.OUTPUT)

print 'Blinking all pins now...'try:

while(True):for pin in range(0, pins):

# Write a state to the pin. ON or OFF.gpio.digitalWrite(pin, state)

# Toggle the state.state = gpio.LOW if state == gpio.HIGH else gpio.HIGH

# Sleep for a while.time.sleep(0.5)

# When you get tired of seeing the led blinking kill the loop with Ctrl-C.except KeyboardInterrupt:

# Leave all leds turned off.print '\nCleaning up...'for pin in range(0, pins):

gpio.digitalWrite(pin, gpio.LOW)

# Do a general cleanup. Calling this function is not mandatory.gpio.cleanup()

Example: Button

A simple example of reading the status of a button using the wiring-x86 interface.

# -*- coding: utf-8 -*-## Copyright © 2014, Emutex Ltd.# All rights reserved.# http://www.emutex.com## Author: Nicolás Pernas Maradei <[email protected]>## See license in LICENSE.txt file.## This example is inspired on Arduino Button example.# http://arduino.cc/en/Tutorial/Button## This example will work "out of the box" on an Intel® Edison board. If# you are using a different board such as an Intel® Galileo Gen2, just change the# import below. wiringx86 uses the same API for all the boards it supports.

# Import the GPIOEdison class from the wiringx86 module.from wiringx86 import GPIOEdison as GPIO

# Create a new instance of the GPIOEdison class.# Setting debug=True gives information about the interaction with sysfs.gpio = GPIO(debug=False)pin = 13button = 2

3.3. Example: Button 13

Page 18: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

print 'Setting up pins %d and %d...' % (pin, button)

# Set pin 13 to be used as an output GPIO pin.gpio.pinMode(pin, gpio.OUTPUT)

# Set pin 2 to be used as an input GPIO pin.gpio.pinMode(button, gpio.INPUT)

print 'Reading from pin %d now...' % buttontry:

while(True):# Read the state of the buttonstate = gpio.digitalRead(button)

# If the button is pressed turn ON pin 13if state == 1:

gpio.digitalWrite(pin, gpio.HIGH)

# If the button is not pressed turn OFF pin 13else:

gpio.digitalWrite(pin, gpio.LOW)

# Kill the loop with Ctrl-C.except KeyboardInterrupt:

# Leave the led turned off.print '\nCleaning up...'gpio.digitalWrite(pin, gpio.LOW)

# Do a general cleanup. Calling this function is not mandatory.gpio.cleanup()

Example: Analog Input

A simple example of reading analong input using the wiring-x86 interface.

# -*- coding: utf-8 -*-## Copyright © 2014, Emutex Ltd.# All rights reserved.# http://www.emutex.com## Author: Nicolás Pernas Maradei <[email protected]>## See license in LICENSE.txt file.## This example is inspired on Arduino Analog Input example.# http://arduino.cc/en/Tutorial/AnalogInput## This example will work "out of the box" on an Intel® Edison board. If# you are using a different board such as an Intel® Galileo Gen2, just change the# import below. wiringx86 uses the same API for all the boards it supports.

# Import the time module enable sleeps between turning the led on and off.import time

14 Chapter 3. Examples

Page 19: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

# Import the GPIOEdison class from the wiringx86 module.from wiringx86 import GPIOEdison as GPIO

# Create a new instance of the GPIOEdison class.# Setting debug=True gives information about the interaction with sysfs.gpio = GPIO(debug=False)pin = 13analogpin = 14

print 'Setting up all pins...'

# Set pin 14 to be used as an analog input GPIO pin.gpio.pinMode(analogpin, gpio.ANALOG_INPUT)

# Set pin 13 to be used as an output GPIO pin.gpio.pinMode(pin, gpio.OUTPUT)

print 'Analog reading from pin %d now...' % analogpintry:

while(True):# Read the voltage on pin 14value = gpio.analogRead(analogpin)

# Turn ON pin 13gpio.digitalWrite(pin, gpio.HIGH)

# Sleep for a while depending on the voltage we just read. The higher# the voltage the more we sleep.time.sleep(value / 1023.0)

# Turn OFF pin 13gpio.digitalWrite(pin, gpio.LOW)

# Sleep for a while depending on the voltage we just read. The higher# the voltage the more we sleep.time.sleep(value / 1023.0)

# When you get tired of seeing the led blinking kill the loop with Ctrl-C.except KeyboardInterrupt:

# Leave the led turned off.print '\nCleaning up...'gpio.digitalWrite(pin, gpio.LOW)

# Do a general cleanup. Calling this function is not mandatory.gpio.cleanup()

3.4. Example: Analog Input 15

Page 20: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

16 Chapter 3. Examples

Page 21: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

CHAPTER 4

Learn More

The Wiring-x86 source code is available on GitHub: http://github.com/emutex/wiring-x86.

The latest version of this documentaion is available on Read The Docs: http://wiring-x86.readthedocs.org.

See Emutex Labs for other cool projects and Maker information.

See out main website to learn more about Emutex.

17

Page 22: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Wiring-x86 Documentation, Release

18 Chapter 4. Learn More

Page 23: Wiring-x86 Documentation - Read the Docs · Wiring-x86 Documentation, Release Wiring-x86 is a Python module that lets you use Arduino like functionality on Intel® Arduino capable

Index

AanalogRead() (built-in function), 7analogWrite() (built-in function), 6

Ccleanup() (built-in function), 8

DdigitalRead() (built-in function), 6digitalWrite() (built-in function), 6

GGPIO() (built-in function), 5

PpinMode() (built-in function), 7

SsetPWMPeriod() (built-in function), 7

19