Edison上程序开发——Python API 一览
本帖最后由 孙毅 于 2015-10-31 00:35 编辑前面我们学了如何使用Python来编程,也做了几个例子了。这次教程,我们一块看下Edison Python编程中所有的API。
不需要背过,至少我们也得混个脸熟啊,这样在需要的时候,我们得知道去哪里找他们啊。
下面基本上就是Edison提供给我们所有的Python API了。恕我懒惰,没有翻译成汉语啊,不过这样可以培养大家看英文文档的能力啊。嘻嘻嘻。
有任何问题,直接回复,跟大家一块看一下啊。
总的来说,一共7类API,就涵盖了几乎所有的功能了:
Aio
I2c
Gpio
Pwm
Spi
Uart
Common
来下面我们一个类一个类的帮大家列出来,以下部分可粗略过一下,用到的时候,再来查询好了:p
Aio
class mraa.Aio(pin)
API to Analog IO.
This file defines the aio interface for libmraa
getBit() → int
Gets the bit value mraa is shifting the analog read to.
bit value mraa is set return from the read function
read() → int
Read a value from the AIO pin. By default mraa will shift the raw value up or down to a 10 bit value.
The current input voltage. By default, a 10bit value
readFloat() → float
Read a value from the AIO pin and return it as a normalized float.
The current input voltage as a normalized float (0.0f-1.0f)
setBit(int bits) → mraa::Result
Set the bit value which mraa will shift the raw reading from the ADC to. I.e. 10bits
bits: the bits the return from read should be i.e 10
I2c
class mraa.I2c(bus, raw=False)
API to Inter-Integrated Circuit.
An I2c object represents an i2c master and can talk multiple i2c slaves by selecting the correct addressIt is considered best practice to make sure the address is correct before doing any calls on i2c, in case another application or even thread changed the addres on that bus. Multiple instances of the same bus can exist.
address( uint8_t address) → mraa::Result
Set the slave to talk to, typically called before every read/write operation
address: Communicate to the i2c slave on this address
frequency( mraa::I2cMode mode) → mraa::Result
Sets the i2c Frequency for communication. Your board may not support the set frequency. Anyone can change this at any time and this will affect every slave on the bus
mode: Frequency to set the bus to
read( uint8_t * data) → int
Read length bytes from the bus into *data pointer
data: Data to read into
length: Size of read in bytes to make
length of read, should match length
readByte() → uint8_t
Read exactly one byte from the bus
char read from the bus
readBytesReg( uint8_t reg, uint8_t * data) → int
Read length bytes from the bus into *data pointer starting from an i2c register
reg: Register to read from
data: pointer to the byte array to read data in to
length: max number of bytes to read
length passed to the function or -1
readReg( uint8_t reg) → uint8_t
Read byte from an i2c register
reg: Register to read from
char read from register
readWordReg( uint8_t reg) → uint16_t
Read word from an i2c register
reg: Register to read from
char read from register
write( uint8_t const * data) → mraa::Result
Write length bytes to the bus, the first byte in the array is the command/register to write
data: Buffer to send on the bus, first byte is i2c command
length: Size of buffer to send
writeByte( uint8_t data) → mraa::Result
Write a byte on the bus
data: The byte to send on the bus
writeReg( uint8_t reg, uint8_t data) → mraa::Result
Write a byte to an i2c register
reg: Register to write to
data: Value to write to register
writeWordReg( uint8_t reg, uint16_t data) → mraa::Result
Write a word to an i2c register
reg: Register to write to
data: Value to write to register
Gpio
class mraa.Gpio(pin, owner=True, raw=False)
API to General Purpose IO.
This file defines the gpio interface for libmraa
dir( mraa:: Dir dir) → mraa::Result
Change Gpio direction
dir: The direction to change the gpio into
edge( mraa::Edge mode) → mraa::Result
Set the edge mode for ISR
mode: The edge mode to set
getPin( bool raw=False) → int
getPin() -> int
Get pin number of Gpio. If raw param is True will return the number as used within sysfs. Invalid will return -1.
raw: (optional) get the raw gpio number.
isr( mraa::Edge mode, PyObject * pyfunc, PyObject * args) → mraa::Result
Sets a callback to be called when pin value changes
mode: The edge mode to set
fptr: Function pointer to function to be called when interupt is triggered
args: Arguments passed to the interrupt handler (fptr)
isrExit() → mraa::Result
Exits callback - this call will not kill the isr thread immediatly but only when it is out of it’s critical section
mode( mraa::Mode mode) → mraa::Result
Change Gpio mode
mode: The mode to change the gpio into
read() → int
Read value from Gpio
useMmap( bool enable) → mraa::Result
Enable use of mmap i/o if available.
enable: true to use mmap
write( int value) → mraa::Result
Write value to Gpio
value: Value to write to Gpio
Pwm
class mraa.Pwm(pin, owner=True, chipid=-1)
API to Pulse Width Modulation.
This file defines the PWM interface for libmraa
config_ms( int period, float duty) → mraa::Result
Set the period and duty of a PWM object.
period: represented in ms.
duty: represnted in ms as float.
config_percent( int period, float duty) → mraa::Result
Set the period and duty (percent) of a PWM object.
period: as represented in ms.
duty: percentage i.e. 50% = 0.5f
enable( bool enable) → mraa::Result
Set the enable status of the PWM pin. None zero will assume on with output being driven and 0 will disable the output
enable: enable status of pin
max_period() → int
Get the maximum pwm period in us
max pwm in us
min_period() → int
Get the minimum pwm period in us
min pwm in us
period( float period) → mraa::Result
Set the PWM period as seconds represented in a float
period: Period represented as a float in seconds
period_ms( int ms) → mraa::Result
Set period, milliseconds
ms: milliseconds for period
period_us( int us) → mraa::Result
Set period, microseconds
us: microseconds as period
pulsewidth( float seconds) → mraa::Result
Set pulsewidth, As represnted by seconds in a (float)
seconds: The duration of a pulse
pulsewidth_ms( int ms) → mraa::Result
Set pulsewidth, milliseconds
pulsewidth_us( int us) → mraa::Result
The pulsewidth, microseconds
us: microseconds for pulsewidth
read() → float
Read the ouput duty-cycle percentage, as a float
A floating-point value representing percentage of output. The value should lie between 0.0f (representing on 0%) and 1.0f Values above or below this range will be set at either 0.0f or 1.0f
write( float percentage) → mraa::Result
Set the output duty-cycle percentage, as a float
percentage: A floating-point value representing percentage of output. The value should lie between 0.0f (representing on 0%) and 1.0f Values above or below this range will be set at either 0.0f or 1.0f
Spi
class mraa.Spi(bus)
API to Serial Peripheral Interface.
This file defines the SPI interface for libmraa
bitPerWord( unsigned int bits) → mraa::Result
Set bits per mode on transaction, default is 8
bits: bits per word
frequency( int hz) → mraa::Result
Set the SPI device operating clock frequency
hz: the frequency to set in hz
lsbmode( bool lsb) → mraa::Result
Change the SPI lsb mode
lsb: bool
lsb: Use least significant bit transmission - 0 for msbi
mode( mraa::Spi_Mode mode) → mraa::Result
Set the SPI device mode. see spidev0-3
mode: the mode. See Linux spidev doc
write( uint8_t * txBuf) → uint8_t *
Write buffer of bytes to SPI device The pointer return has to be free’d by the caller. It will return a NULL pointer in cases of error
txBuf: buffer to send
length: size of buffer to send
uint8_t* data received on the miso line. Same length as passed in
writeByte( uint8_t data) → int
Write single byte to the SPI device
data: the byte to send
data received on the miso line or -1 in case of error
write_word( uint16_t data) → uint16_t
Write buffer of bytes to SPI device The pointer return has to be free’d by the caller. It will return a NULL pointer in cases of error
txBuf: buffer to send
length: size of buffer (in bytes) to send
uint8_t* data received on the miso line. Same length as passed in
Uart
class mraa.Uart(*args)
API to UART (enabling only)
This file defines the UART interface for libmraa
dataAvailable( unsigned int millis=0) → bool
dataAvailable() -> bool
Check to see if data is available on the device for reading
millis: number of milliseconds to wait, or 0 to return immediately
true if there is data available to read, false otherwise
flush() → mraa::Result
Flush the outbound data. Blocks until complete.
getDevicePath() → std::string
Get string with tty device path within Linux For example. Could point to “/dev/ttyS0”
char pointer of device path
read( char * data) → int
Read bytes from the device into char* buffer
data: buffer pointer
length: maximum size of buffer
numbers of bytes read
readStr( int length) → std::string
Read bytes from the device into a String object
length: to read
string of data
setBaudRate( unsigned int baud) → mraa::Result
Set the baudrate. Takes an int and will attempt to decide what baudrate is to be used on the UART hardware.
baud: unsigned int of baudrate i.e. 9600
setFlowcontrol( bool xonxoff, bool rtscts) → mraa::Result
Set the flowcontrol
xonxoff: XON/XOFF Software flow control.
rtscts: RTS/CTS out of band hardware flow control
setMode( int bytesize, mraa::UartParity parity, int stopbits) → mraa::Result
Set the transfer mode For example setting the mode to 8N1 would be “dev.setMode(8,UART_PARITY_NONE , 1)”
bytesize: data bits
parity: Parity bit setting
stopbits: stop bits
setTimeout( int read, int write, int interchar) → mraa::Result
Set the timeout for read and write operations <= 0 will disable that timeout
read: read timeout
write: write timeout
interchar: inbetween char timeout
write( char const * data) → int
Write bytes in String object to a device
data: buffer pointer
length: maximum size of buffer
the number of bytes written, or -1 if an error occurred
writeStr( std::string data) → int
Write bytes in String object to a device
the number of bytes written, or -1 if an error occurred
Common
Python interface to libmraa
mraa.adcRawBits() → unsigned int
mraa.adcSupportedBits() → unsigned int
mraa.getI2cBusCount() → int
mraa.getI2cBusId(unsigned int i2c_bus) → int
mraa.getPinCount() → unsigned int
mraa.getPinName(int pin) → std::string
mraa.getPlatformName() → std::string
mraa.getPlatformType() → mraa:: Platform
mraa.getSubPlatformId(int pin_or_bus_index) → int
mraa.getSubPlatformIndex(int pin_or_bus_id) → int
mraa.getVersion() → std::string
mraa.hasSubPlatform() → bool
mraa.init() → mraa::Result
mraa.isSubPlatformId(int pin_or_bus_id) → bool
mraa.pinModeTest(int pin, mraa:: Pinmodes mode) → bool
mraa.printError(mraa::Result result)
mraa.setLogLevel(int level) → mraa::Result
mraa.setPriority(unsigned int const priority) → int
顶{:5_171:} 求翻译 求翻译 大圣你这样搞好像不地道,搞点例如好不好 大连林海 发表于 2015-10-31 12:59
求翻译 求翻译
翻译也是然并卵啊。。。。 hnyzcj 发表于 2015-10-31 19:47
大圣你这样搞好像不地道,搞点例如好不好
好说,例子马上就到位!吼吼吼 孙毅 发表于 2015-10-31 20:00
好说,例子马上就到位!吼吼吼
感谢大圣 孙毅 发表于 2015-10-31 20:00
好说,例子马上就到位!吼吼吼
这对你来说都是小孩游戏 hnyzcj 发表于 2015-10-31 20:27
这对你来说都是小孩游戏
等到花儿都谢了 大连林海 发表于 2015-10-31 20:29
等到花儿都谢了
去看吧,不知道 花儿明天 是否依然会开{:5_168:} hnyzcj 发表于 2015-10-31 20:27
这对你来说都是小孩游戏
别整没用的!赶紧学起来! 孙毅 发表于 2015-10-31 23:54
去看吧,不知道 花儿明天 是否依然会开
你这不错哦 孙毅 发表于 2015-10-31 23:54
别整没用的!赶紧学起来!
你就笑话我们 不过也是鞭策 大神好,请问这篇英文文档的源地址在哪呀?
页:
[1]