Python Module Functions

pyMCU v1.0

mcuScan(baudrate)
scan for available pyMCUs.
returns a dictionary (portname, index)
primarily used during class initialization to find the first available pyMCU hardware module but this could also be called to get a list of all available hardware modules if you needed to find and initialize a specific one manually.
Usage:
baudrate - sets the baudrate to use when scanning for available pyMCU hardware modules. If not specified the default value is 115200.
Example:
pymcu.mcuScan()
{'COM23': 22}
Functions
mcuModule()
This is the main class for the pyMCU module it contains all of the related pyMCU hardware functions.
Usage:
import pymcu
myBoard = pymcu.mcuModule()
analogRead(pinNum)
Read the value of an analog pin.
Usage:
pinNum - specifies a valid analog pin number [1-6]
Example:
myBoard.analogRead(1)
Returns a value between 0 and 1023
close()
Close the pyMCU communication port.
Example:
myBaord.close()
digitalRead(pinNum)
Read the value of a digital pin that was set to an input state.
Usage:
pinNum - specifies a valid digital pin number [1-13]
Example:
myBoard.digitalRead(1)
Returns 0 or 1
digitalState(pinNum, io)
Sets the state of the digital pin as an input or an output.
All digital pins are configured as output by default.
Usage:
pinNum - specifies a valid digital pin number [1-13]
io - specifies whether you want to make the pin an input or an output, you can either use the string 'input' or 'output' or use and integer value 1 for input 0 for output.
Example:
myBoard.digitalState(1, 'input')
myBoard.digitalState(1, 'output')
myBoard.digitalState(1, 1)
myBoard.digitalState(1, 0)
dtmfOut(pinNum, dtmfNum)
Plays DTMF numbers on the specified pin.
[dtmfOut will halt your program until the DTMF tone is complete]
Usage:
pinNum - specifies a valid digital pin number [1-13]
dtmfNum - digit 0-9
Example:
myBoard.dtmfOut(1, 3)
eepromRead(address)
Read byte from EEPROM storage address.
Byte Values are Stored as Raw Byte Data if you are Retrieving ASCII Character Data use chr() on the Returned Read Data.
Usage:
address - specifies a valid EEPROM address to write data to [1-255].
Example:
myBoard.eepromRead(1)
Returns byte value from read.
eepromWrite(address, value)
Write a byte to EEPROM storage address.
Usage:
address - specifies a valid EEPROM address to write data to [1-255].
value - a byte sized value to write to the EEPROM, either an int in the range of 0-255 or an ASCII character.
Example:
myBoard.eepromWrite(1, 't')
myBoard.eepromWrite(2,100)
freqOut(pinNum, duration, freq1, freq2)
Plays frequency out on specified pin.
[freqOut will halt your program for the frequency duration]
Usage:
pinNum - specifies a valid digital pin number [1-13]
duration - specifies the duration in milliseconds
freq1 - frequency one range = 0 to 32767 hertz
freq2 - [optional frequency two] range = 0 to 32767 hertz
Example:
myBoard.freqOut(4, 2000, 1000)
Plays 1Khz tone on Digital Pin 4 for 2 Seconds.
i2cRead(control, {address,} numBytes)
i2c protocol function to read data from an i2c device.
i2c communications uses the hardware i2c pins of the PIC chip:
Data = Digital Pin 4
Clock = Digital Pin 11
Usage:
control - sets the control register for reading and writing.
address - optional address to read from.
numBytes - number of bytes to read from i2c device.
Example:
myBoard.i2cRead(0xEF,0xAA,2)
Set the control to hex value EF, read address at hex value AA, read 2 bytes of data.
Returns the data read as a python list.
i2cWrite(control, {address,} writeData)
i2c protocol function to write data to an i2c device.
i2c communications uses the hardware i2c pins of the PIC chip:
Data = Digital Pin 4
Clock = Digital Pin 11
Usage:
control - sets the control register for reading and writing.
address - optional address to write to.
writeData - data to write to i2c device. Data can be of type string, int, or a list of ints
Example:
myBoard.i2cWrite(0xEE,0xF4, 0x2E)
Set the control to hex value EE, write to address hex value F4, write data hex value 2E.
myBoard.i2cWrite(0xEE, 0xF4, [0x2E, 0xAC, 0x34])
Set the control to hex value EE, write to address hex value F4, write data of hex values contained in a list.
irDecode(pinNum)
Decode IR remote control code (sony 12-bit SIRC Format)
Usage:
pinNum - specifies a valid digital pin number [1-13]
Example:
myBoard.irDecode(4)
lcd(line, text)
Init or send text to the LCD
Supports up to 4 line LCD
To initialize the LCD call the function without setting any of the variables.
Usage:
line - specifies which line of the LCD to write to.
text - specifies the text string to write to the LCD.
Example:
myBoard.lcd()
myBoard.lcd(1,'Hello World')
myBoard.lcd(2,'This is a test')
mcuInfo()
Get additional info about the currently connected pyMCU hardware module.
Example:
myBoard.mcuInfo()
Version Info : pymcu for PIC v1.0.1
Available Digital Pins : 1 - 13
Available Analog Pins : 1 - 6
Analog Value Range : 0 - 1023
PWM Pins : 1 - 5
PWM Duty Cycle Range : 0 - 1023
COM Port: : COM23
Baudrate: : 115200
mcuSetBaudRate(baudrate)
Set the pyMCU hardware Baud Rate.
The setting is saved in the hardware EEPROM and the change will take effect next time you power cycle the device.
Usage:
The following values set the corresponding baudrate:
0 = 2400
1 = 4800
2 = 9600
3 = 19200
4 = 38400
5 = 57600
6 = 115200
7 = 230400
8 = 250000
9 = 460800
10 = 500000
11 = 750000
12 = 1000000
Example:
myBoard.mcuSetBaudRate(3)
Sets the pyMCU device to 19200 baud.
mcuVersion()
returns a string for the version info queried from the hardware chip.
Example:
myBoard.mcuVersion()
'pymcu for PIC v1.0.10'
owRead(pinNum, mode, numBytes)
1-Wire protocol function to read data from a 1-Wire device.
Usage:
pinNum - specifies a valid digital pin number [1-13]
mode - specifies whether a reset is sent before and/or after the operation and the size of the data items, either bit or byte.
Mode is a 3 bit number:
bit 0: 1 = Send reset pulse before data
bit 1: 1 = Send reset after data
bit 2: 0 = byte sized data, 1 = bit sized data
numBytes - number of bytes to read from 1-Wire device.
Example:
myBoard.owRead(1,1,20)
Read on digital pin 1, send reset pulse before data, read 20 bytes.
Returns read data as a python list.
owWrite(pinNum, mode, writeData)
1-Wire protocol function to write data to a 1-Wire device.
Usage:
pinNum - specifies a valid digital pin number [1-13]
mode - specifies whether a reset is sent before and/or after the operation and the size of the data items, either bit or byte.
Mode is a 3 bit number:
bit 0: 1 = Send reset pulse before data
bit 1: 1 = Send reset after data
bit 2: 0 = byte sized data, 1 = bit sized data
writeData - data to write to 1-Wire device. Data can be of type string, int, or a list of ints
Example:
myBoard.owWrite(1,1,100)
Write on digital pin 1 with mode set to only reset before pulse and byte sized data, write data value 100
pausems(value)
Pause for a given time in milliseconds
Usage:
value - specifies a value in milliseconds, 1000 milliseconds = 1 second.
Example:
myBoard.pausems(500)
pauseus(value)
Pause for a given time in microseconds
Usage:
value - specifies a value in microseconds, 1000 microseconds = 1 millisecond.
Example:
myBoard.pauseus(500)
pinHigh(pinNum)
Set a digital pin high
Usage:
pinNum - specifies a valid digital pin number [1-13]. pinNum can also be a list of pins if you need to change a bunch of pins high in one call.
Example:
myBoard.pinHigh(1)
myBoard.pinHigh([1,3,4,8,11])
pinLow(pinNum)
Set a digital pin low
Usage:
pinNum - specifies a valid digital pin number [1-13]. pinNum can also be a list of pins if you need to change a bunch of pins high in one call.
Example:
myBoard.pinLow(1)
myBoard.pinLow([1,3,4,8,11])
pinToggle(pinNum)
Toggle a digital pin high or low by inverting it's current state
Usage:
pinNum - specifies a valid digital pin number [1-13]. pinNum can also be a list of pins if you need to change a bunch of pins high in one call.
Example:
myBoard.pinToggle(1)
myBoard.pinToggle([1,3,4,8,11])
pulseCount(pinNum, duration)
Count the number of pulses that occur during the duration on digital pin
[PulseCount will halt your program until the pulse count is complete]
Usage:
pinNum - specifies a valid digital pin number [1-13]
duration - in milliseconds, the highest possible frequency that can be counted is 200KHz
Example:
pulseCount(4, 10)
pulseIn(pinNum, state)
Measure pulse width on a digital pin.
[PulseIn will halt your program until the pulse in is complete]
Usage:
pinNum - specifies a valid digital pin number [1-13]
state - specifies whether to measure a low or high pulse.
0 = measure the width of a low pulse.
1 = measure the width of a high pulse.
Example:
myBoard.pulseIn(1, 1)
pulseOut(pinNum, pulse, count)
Create a pulse out on a digital pin.
The pulse is generated by toggling the pin twice, thus the initial state of the pin determines the polarity of the pulse.
[PulseOut will halt your program until the pulse repeat cycle is complete]
Usage:
pinNum - specifies a valid digital pin number [1-13]
pulse - specifies the period for the pulse.
count - specifies how many times to repeat the pulse.
Example:
myBoard.pulseOut(4, 500, 20)
pwmDuty(pwmPin, duty)
Sets the PWM duty cycle for one of the PWM pins.
Usage:
pwmPin - specifies a valid PWM pin number [1-5]
duty - specifies a valid duty cycle value [0-1023]
Example:
myBoard.pwmDuty(1, 500)
pwmOff(pwmPin)
Turns off the hardware PWM function for one of the PWM pins.
Usage:
pwmPin - specifies a valid PWM pin number [1-5]
Example:
myBoard.pwmOff(1)
pwmOn(pwmPin)
Turns on the hardware PWM function for one of the PWM pins.
Usage:
pwmPin - specifies a valid PWM pin number [1-5]
Example:
myBoard.pwmOn(1)
pwmPeriod(preScaler, preScalerOffset)
Set PWM period
Usage: pwmPeriod(preScaler, preScalerOffset)
preScaler: 1, 4, 16, or 64
preScalerOffset: 0 to 255
Example: pwmPeriod(1, 255) - Sets preScaler to 1:1, preScalerOffset to 255, PWM Period will be 31.875 micro Seconds, at 50% duty cycle Freq. will be 31.25Khz
reset()
Performs a hardware reset.
Example:
myBoard.reset()
serialRead(pinNum, mode, timeout, numBytes)
Read serial data with digital pin.
Usage:
pinNum - specifies a valid digital pin number [1-13]
mode - sets baudrate and state:
0 = 2400 Driven True
1 = 4800 Driven True
2 = 9600 Driven True
3 = 19200 Driven True
4 = 2400 Driven Inverted
5 = 4800 Driven Inverted
6 = 9600 Driven Inverted
7 = 19200 Driven Inverted
8 = 2400 Open True
9 = 4800 Open True
10 = 9600 Open True
11 = 19200 Open True
12 = 2400 Open Inverted
13 = 4800 Open Inverted
14 = 9600 Open Inverted
15 = 19200 Open Inverted
timeout - specifies a timeout period to wait if no serial data is received.
numBytes - specifies the number of bytes to read from serial. Currently the max number of bytes is 64 per read.
Example:
myBoard.serialRead(1, 2, 1000, 10)
Read 10 Bytes of Serial Data at 9600 Baud Driven True with a timeout of 1 second.
Returns data as a string.
serialWrite(pinNum, mode, serialData)
Write serial data with digital pin.
Usage:
pinNum -
mode - sets baudrate and state:
0 = 2400 Driven True
1 = 4800 Driven True
2 = 9600 Driven True
3 = 19200 Driven True
4 = 2400 Driven Inverted
5 = 4800 Driven Inverted
6 = 9600 Driven Inverted
7 = 19200 Driven Inverted
8 = 2400 Open True
9 = 4800 Open True
10 = 9600 Open True
11 = 19200 Open True
12 = 2400 Open Inverted
13 = 4800 Open Inverted
14 = 9600 Open Inverted
15 = 19200 Open Inverted
serialData - Serial data to send. String data is currently limited to 64 Characters at a time, you can send lists or tuples which will get sent as individual serial writes per item.
Example:
Send a String, 9600 Driven Inverted on Pin 4:
myBoard.serialWrite(4, 6, 'Hello World')
Send an Int, 9600 Driven True on Pin 5:
myBoard.serialWrite(5,2,100)
Send multiple items at 9600 Driven True on pin 5:
myBoard.serialWrite(5,2,['Hello World\\r\\n','This is a test of Multiple Items', 'In a List\\r\\n'])
soundOut(pinNum, note, duration)
Generates tone and/or white noise on the specified pin.
[soundOut will halt your program until the sound playing is complete]
Usage:
note - 0 is silence. Notes 1-127 are tones. Notes 128-255 are white noise. tones and white noises are in ascending order (i.e. 1 and 128 are the lowest frequencies, 127 and 255 are the highest). Note 1 is about 78.74Hz and Note 127 is about 10,000Hz.
duration - is 0-255 and determines how long the Note is played in about 12 millisecond increments.
Example:
myBoard.soundOut(1, 120, 10)
spiDisable()
Disables SPI and changes pins back to general IO pins
Example:
myBoard.spiDisable()
spiEnable(clockPolarity, clockFreq, clockSelect, clockSample)
Enable SPI
SCK (Clock) = Digital Pin 11
SDI (MISO) = Digital Pin 4
SDO (MOSI) = Digital Pin 5
Usage:
clockPolarity: 0 - Idle state for clock is a low level
1 - Idle state for clock is a high level
clockFreq: Specify Frequency for SPI clock in Khz (16Khz - 2000Khz Range)
clockSelect: If clockPolarity = 0:
1 = Data transmitted on rising edge of Clock
0 = Data transmitted on falling edge of Clock
If clockPolarity = 1:
1 = Data transmitted on falling edge of Clock
0 = Data transmitted on rising edge of Clock
clockSample:
1 = Input data sampled at end of data output time
0 = Input data sampled at middle of data output time
Example:
myBoard.spiEnable(1,100,0,0)
Clock idle high, 100Khz clock speed, Transmit on idle to active clock state, Data sampled at middle of output clock
spiTransfer(writeData, delay)
SPI Data Transfer
SCK (Clock) = Digital Pin 11
SDI (MISO) = Digital Pin 4
SDO (MOSI) = Digital Pin 5
Usage:
writeData - Data to be sent to SPI device, can be int, string, or a list of int's max 64 bytes per transfer.
delay - Optional delay in microseconds default is 200 (1000 = 1 millisecond) between multibyte writes, max 64 bytes per transfer.
Example:
spiTransfer(160)
Write a value of 160 to the SPI device and return SPI Read Byte
spiTransfer([160,100,40,10,12,4], 400)
Write all values out to the SPI Device, delay 400us between each byte, return bytes for each byte read as a list.
mcuModule Class