Python
The Python DUE library allows for the use of full standard Python to access physical-computing.
Setup
This page assumes the user is already familiar with Python and there is a development machine that is already setup to build and run Python programs. No changes are needed there but we are using Microsoft Visual Studio Code as a personal preference.
Tip
If this is the first time you use your device, start by visiting the Hardware page and load your device with the appropriate firmware. The Console is also a great place to start.
Start a new project with a simple line of code to test out the project is running
print("Hello DUE!")
We now need to install the DUE Python library pip install DUELink
. The DUELink package will also install all required dependencies. The library is also available on the downloads page if needed.
Tip
The DUE python library requires pyserial, which requires an admin access to install.
Blinky!
Our first program will blink the on-board LED 20 times, where it comes on for 200ms and then it is off for 800ms.
Note
Change the serial COM port number to match your port on the system you are using.
from DUELink.DUELinkController import DUELinkController
print("Hello DUE!")
availablePort = DUELinkController.GetConnectionPort()
dev = DUELinkController(availablePort)
# Flash the LED 20 times (on for 200ms and off for 800ms)
dev.Led.Set(200,800,20)
print("Bye DUE!")
Python API
The provided API mirrors DUE Script's Core library. Referencing those APIs is a good place to learn about the available functionality and available arguments.
Python API | DUE Script Equivalent | Description |
---|---|---|
Analog.Read() | ARead() | Reads analog pin |
Analog.Write() | AWrite() | Reads analog pin |
Button.Enable() | BtnEnable() | Sets up a button to be used |
Button.JustPressed() | BtnDown() | Detects if button is was pressed |
Button.JustReleased() | BtnDown() | Detects if button is released |
DeviceConfig.IsEdge() | Version() | Checks for specific hardware |
DeviceConfig.IsFlea() | Version() | Checks for specific hardware |
DeviceConfig.IsPico() | Version() | Checks for specific hardware |
DeviceConfig.IsPulse() | Version() | Checks for specific hardware |
DeviceConfig.MaxPinIO() | NA | Returns # available GPIOs |
DeviceConfig.MaxPinAnalog() | NA | Returns # available Analog pins |
Digital.Read() | DRead() | Reads digital pin |
Digital.Write() | DWrite() | Writes to digital pin |
Display.Clear() | LcdClear() | Clears the display black or white |
Display.CreateImage() | LcdConfig() | Set display configuration |
Display.Configuration() | LcdConfig() | Set display configuration |
Display.DrawBuffer() | LcdStream() | Updates the entire display, using stream, with automatic Show() |
Display.DrawBufferBytes() | LcdStream() | Updates the entire display, using stream, with automatic Show() |
Display.DrawCircle() | LcdCircle() | Draws a circle on the display |
Display.DrawFillRect() | LcdFill() | Draws a filled rectangle on the display |
Display.DrawImage() | LcdImg() | Draws an image using an array |
Display.DrawImageBytes() | LcdImg() | Draws an image using bytes |
Display.DrawImageScale() | LcdImg() | Works same as DrawImage() adds scaling |
Display.DrawLine() | LcdLine() | Draws a line on the display |
Display.DrawRectangle() | LcdRect() | Draws a rectangle on the display |
Display.DrawText() | LcdText() | Draws a text on the display |
Display.DrawTextScale() | LcdTextS() | Draws scaled text on the display |
Display.SetPixel() | LcdPixel() | Draws pixel on the display |
Display.Show() | LcdShow() | Sends the display buffer |
Distance.Read() | Distance() | Used to read distance sensors |
Frequency.Write() | Freq() | Hardware generated PWM signal |
I2c.Write() | I2cStream() | I2C write, using stream |
I2c.Read() | I2cStream() | I2C read, using stream |
I2c.WriteRead() | I2cStream() | I2C write/read, using stream |
Infrared.Enable() | IrEnable() | Enables pin for IR signal capture |
Infrared.Read() | IrRead() | Reads value from IR enabled pin |
Led.Set() | LED() | Controls the on-board LED |
Neo.Clear() | NeoClear() | Clears all LED's in memory |
Neo.SetColor() | NeoSet() | Set's a specific LED to a color |
Neo.Show() | NeoShow() | Transfers the internal pattern to LEDs |
Neo.SetMultiple() | NeoStream() | Updates all LEDs, using streams, with automatic Show() |
Led.Set() | LED() | Controls the on-board LED |
Servo.Set() | ServoSet() | Sets servo motor connected to a pin |
Spi.Configuration() | SpiCfg() | Configures SPI bus |
Spi.Palette() | Palette() | Sets the desired color for a palette |
Spi.Read() | SpiByte() | Reads SPI byte |
Spi.Write() | SpiByte() | Sends SPI byte |
Spi.Write4bpp() | Spi4Bpp() | Streams and converts data from 4BPP to 16BPP |
Spi.WriteRead() | SpiStream() | Write & Read SPI bytes |
System.Beep() | Beep() | Uses any pin to generate a tone |
System.GetTickMicroseconds() | TickUs() | Returns system time in microseconds |
System.GetTickMilliseconds() | TickMs() | Returns system time in milliseconds |
System.Print() | Print() | Print to LCD display and Debug Output on the same line |
System.PrintLn() | PrintLn() | Print to LCD display and Debug Output then moves to next line |
System.Reset() | Reset() | Resets the board |
System.Wait() | Wait() | Pause the system in milliseconds |
Touch.Read() | TouchRead() | Initialize a pin for touch |
Uart.BytesToRead() | UartCount() | How many bytes buffered and ready to read |
Uart.Enable() | UartInit() | Initialize UART |
Uart.Read() | UartRead() | Read UART data |
Uart.Write() | UartWrite() | Write UART data |
Version() | Version() | Returns the current DUE firmware version |
Note
For convenience, the Pin Enum includes, ButtonA, ButtonB and Led. For example: dev.Digital.Write(dev.Pin.Led, True)
DUE Script Control
These methods allow developers to control DUE Scripts right from within Python
Method | Description |
---|---|
Script.Execute() | Executes the single line of code immediately |
Script.IsRunning() | Checks if DUE Script is running |
Script.Load() | Loads the line into internal buffer |
Script.New() | Clears the program stored in flash |
Script.Read() | Read the program stored in flash and return as string |
Script.Record() | Sends the internal buffer to the device, overwriting any previous programs |
Script.Run() | Runs the program stored in flash |
This example will load a simple program line by line and then record it.
dev.Script.Load("c = 10")
dev.Script.Load("@Blink");
dev.Script.Load("Led(100,100,c)")
dev.Script.Record()
This is an example to execute a single line(immediate mode). This does not modify the application stored in flash.
dev.Script.Execute("LED(200,200,10)")
You can also access a previously recorder program using goto (to label) or by calling a function that has a return. This example calls the recorded program above.
dev.Script.Execute("c=5:goto Blink")