WiringPi-Python

This documentation will introduce how to use WiringPi Python API.

What is WiringPi-Python

WiringPi Python is a PIN based GPIO access library written in Python for the Raspberry Pi. Now we also migrate it to VIMs.

WiringPi-Python Function List

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//GPIO
class GPIO(object):
def __init__(self,pinmode=0):
self.MODE=pinmode
if pinmode==self.WPI_MODE_PINS:
wiringPiSetup()
if pinmode==self.WPI_MODE_GPIO:
wiringPiSetupGpio()
if pinmode==self.WPI_MODE_GPIO_SYS:
wiringPiSetupSys()
if pinmode==self.WPI_MODE_PHYS:
wiringPiSetupPhys()
if pinmode==self.WPI_MODE_PIFACE:
wiringPiSetupPiFace()

def delay(self,*args):
delay(*args)
def delayMicroseconds(self,*args):
delayMicroseconds(*args)

def piBoardRev(self):
return piBoardRev()
def wpiPinToGpio(self,*args):
return wpiPinToGpio(*args)
def setPadDrive(self,*args):
return setPadDrive(*args)
def getAlt(self,*args):
return getAlt(*args)
def digitalWriteByte(self,*args):
return digitalWriteByte(*args)

def pinMode(self,*args):
pinMode(*args)

def digitalWrite(self,*args):
digitalWrite(*args)
def digitalRead(self,*args):
return digitalRead(*args)

def analogWrite(self,*args):
analogWrite(*args)
def analogRead(self,*args):
return analogRead(*args)

def pullUpDnControl(self,*args):
return pullUpDnControl(*args)

def softPwmCreate(self,*args):
return softPwmCreate(*args)
def softPwmWrite(self,*args):
return softPwmWrite(*args)

//I2C:
class I2C(object):
def setupInterface(self,*args):
return wiringPiI2CSetupInterface(*args)
def setup(self,*args):
return wiringPiI2CSetup(*args)
def read(self,*args):
return wiringPiI2CRead(*args)
def readReg8(self,*args):
return wiringPiI2CReadReg8(*args)
def readReg16(self,*args):
return wiringPiI2CReadReg16(*args)
def write(self,*args):
return wiringPiI2CWrite(*args)
def writeReg8(self,*args):
return wiringPiI2CWriteReg8(*args)
def writeReg16(self,*args):
return wiringPiI2CWriteReg16(*args)

//Serial:
class Serial(object):
device = '/dev/ttyAMA0'
baud = 9600
serial_id = 0
def printf(self,*args):
return serialPrintf(self.serial_id,*args)
def dataAvail(self,*args):
return serialDataAvail(self.serial_id,*args)
def getchar(self,*args):
return serialGetchar(self.serial_id,*args)
def putchar(self,*args):
return serialPutchar(self.serial_id,*args)
def puts(self,*args):
return serialPuts(self.serial_id,*args)
def __init__(self,device,baud):
self.device = device
self.baud = baud
self.serial_id = serialOpen(self.device,self.baud)
def __del__(self):
serialClose(self.serial_id)

WiringPi-Python Demo

This is a simple program that demonstrates reading and writing to GPIO pins.

1
$ wget https://dl.khadas.com/development/code/docs_source/wiringpi-python.py

Notes

  • If you need to use the special pin functions of WiringPi-Python, you’ll need to confirm that the corresponding configuration is enabled in the dts file.
  • WiringPi-Python itself includes many functions, not limited to controlling the output of GPIO pins and reading pin levels. This is only a simple introduction, and you can explore more by yourself.