1
1
# cython: language_level=3
2
- # requires: Cython, wiringpi
3
2
4
3
from cpython.bytes cimport PyBytes_FromStringAndSize
4
+ cimport wiringpi as wp
5
+
6
+
7
+ # Wiring-PI Wrapper Functions
8
+ # The official Wiring-PI python library is abandoned and mixing it with a separate wiring pi library
9
+ # causes issues.
10
+ INPUT = wp.INPUT
11
+ OUTPUT = wp.OUTPUT
12
+ PWM_OUTPUT = wp.PWM_OUTPUT
13
+ def wiringPiSetup (): return wp.wiringPiSetup()
14
+ def wiringPiSetupGpio (): return wp.wiringPiSetupGpio()
15
+ def wiringPiSetupPhys (): return wp.wiringPiSetupPhys()
16
+ def wiringPiSetupSys (): return wp.wiringPiSetupSys()
17
+ def pinMode (int pin , int mode ): wp.pinMode(pin, mode)
18
+ def digitalWrite (int pin , int value ): wp.digitalWrite(pin, value)
19
+ def pwmWrite (int pin , int value ): wp.pwmWrite(pin, value)
20
+ def digitalRead (int pin ): return wp.digitalWrite(pin)
21
+ def millis (): return wp.millis()
22
+ def micros (): return wp.micros()
23
+ def delay (unsigned int howLong ): wp.delay(howLong)
24
+ def delayMicroseconds (unsigned int howLong ): wp.delayMicroseconds(howLong)
5
25
6
- cdef extern from " wiringPi.h" :
7
- cdef enum :
8
- INPUT = 0
9
- OUTPUT = 1
10
- PWM_OUTPUT = 2
11
- void pinMode(int pin, int mode) nogil
12
- void digitalWrite(int pin, int value) nogil
13
- void pwmWrite(int pin, int value) nogil
14
- int digitalRead(int pin) nogil
15
-
16
- unsigned int millis() nogil
17
- unsigned int micros() nogil
18
- void delay(unsigned int howLong) nogil
19
- void delayMicroseconds(unsigned int howLong) nogil
20
26
21
27
# Simple utility that should be in C code to be accurate and uses the above Wiring-PI definitions
22
28
def beep (int pin , double freq = 1000 , double dur = 0.1 ):
@@ -27,13 +33,13 @@ def beep(int pin, double freq=1000, double dur=0.1):
27
33
"""
28
34
cdef unsigned int delay, niter, i
29
35
with nogil:
30
- pinMode(pin, OUTPUT)
36
+ wp. pinMode(pin, OUTPUT)
31
37
delay = < unsigned int > (1000000 // (2 * freq)) # Hz -> microseconds (halfed)
32
38
niter = < unsigned int > (2 * dur* freq + 0.5 )
33
39
for i in xrange (niter):
34
- digitalWrite(pin, i & 1 )
35
- delayMicroseconds(delay)
36
- digitalWrite(pin, 0 )
40
+ wp. digitalWrite(pin, i & 1 )
41
+ wp. delayMicroseconds(delay)
42
+ wp. digitalWrite(pin, 0 )
37
43
38
44
cdef int [4 ] LCD_row_offs = [ 0x00 , 0x40 , 0x14 , 0x54 ]
39
45
@@ -66,25 +72,25 @@ cdef class LCD:
66
72
67
73
# We don't need the GIL from here to the end and there is a lot of waiting
68
74
# Function Set Command - 001(DL)NF00
69
- cdef int por = 40 - millis()
75
+ cdef int por = 40 - wp. millis()
70
76
with nogil:
71
77
72
78
# All pins start as outputs and low
73
- digitalWrite(EN, 0 ); pinMode(EN, OUTPUT)
74
- digitalWrite(RS, 0 ); pinMode(RS, OUTPUT)
75
- pinMode(RW, OUTPUT)
79
+ wp. digitalWrite(EN, 0 ); wp. pinMode(EN, wp. OUTPUT)
80
+ wp. digitalWrite(RS, 0 ); wp. pinMode(RS, wp. OUTPUT)
81
+ wp. pinMode(RW, wp. OUTPUT)
76
82
77
83
if bits == 4 :
78
84
self ._read = self .read4; self ._read_data = self .readData4
79
85
self ._write = self .write4; self ._write_data = self .writeData4
80
86
81
- # Need to wait 40 ms since the LCD recieved power
87
+ # Need to wait 40 ms since the LCD received power
82
88
self .writing4()
83
89
self .set4(0 )
84
- if por > 0 : delay(por)
85
- self .set4(0x3 ); self .clock(); delayMicroseconds(4100 )
86
- self .set4(0x3 ); self .clock(); delayMicroseconds(100 )
87
- self .set4(0x3 ); self .clock(); delayMicroseconds(100 )
90
+ if por > 0 : wp. delay(por)
91
+ self .set4(0x3 ); self .clock(); wp. delayMicroseconds(4100 )
92
+ self .set4(0x3 ); self .clock(); wp. delayMicroseconds(100 )
93
+ self .set4(0x3 ); self .clock(); wp. delayMicroseconds(100 )
88
94
self .set4(0x2 ); self .clock()
89
95
90
96
# Set default state to reading and send "Function Set Command"
@@ -105,9 +111,9 @@ cdef class LCD:
105
111
# Need to wait 40 ms since the LCD recieved power
106
112
self .writing8()
107
113
self .set8(0 )
108
- if por > 0 : delay(por)
109
- self .set8(0x30 ); self .clock(); delayMicroseconds(4100 )
110
- self .set8(0x30 ); self .clock(); delayMicroseconds(100 )
114
+ if por > 0 : wp. delay(por)
115
+ self .set8(0x30 ); self .clock(); wp. delayMicroseconds(4100 )
116
+ self .set8(0x30 ); self .clock(); wp. delayMicroseconds(100 )
111
117
self .set8(0x30 ); self .clock()
112
118
113
119
# Set default state to reading and send "Function Set Command"
@@ -134,68 +140,68 @@ cdef class LCD:
134
140
135
141
cdef inline void clock(self ) noexcept nogil:
136
142
""" Clocks a command in (EN pin high then low)"""
137
- digitalWrite(self .EN, 1 ); delayMicroseconds(1 ); digitalWrite(self .EN, 0 )
143
+ wp. digitalWrite(self .EN, 1 ); wp. delayMicroseconds(1 ); wp. digitalWrite(self .EN, 0 )
138
144
cdef inline void set8(self , unsigned char x) noexcept nogil:
139
145
""" Sets 8 bits to the DB pins"""
140
- digitalWrite(self .DB[7 ], x& 0x80 )
141
- digitalWrite(self .DB[6 ], x& 0x40 )
142
- digitalWrite(self .DB[5 ], x& 0x20 )
143
- digitalWrite(self .DB[4 ], x& 0x10 )
144
- digitalWrite(self .DB[3 ], x& 0x08 )
145
- digitalWrite(self .DB[2 ], x& 0x04 )
146
- digitalWrite(self .DB[1 ], x& 0x02 )
147
- digitalWrite(self .DB[0 ], x& 0x01 )
146
+ wp. digitalWrite(self .DB[7 ], x& 0x80 )
147
+ wp. digitalWrite(self .DB[6 ], x& 0x40 )
148
+ wp. digitalWrite(self .DB[5 ], x& 0x20 )
149
+ wp. digitalWrite(self .DB[4 ], x& 0x10 )
150
+ wp. digitalWrite(self .DB[3 ], x& 0x08 )
151
+ wp. digitalWrite(self .DB[2 ], x& 0x04 )
152
+ wp. digitalWrite(self .DB[1 ], x& 0x02 )
153
+ wp. digitalWrite(self .DB[0 ], x& 0x01 )
148
154
cdef inline void set4(self , unsigned char x) noexcept nogil:
149
155
""" Sets 4 bits to the DB pins"""
150
- digitalWrite(self .DB[3 ], x& 0x08 )
151
- digitalWrite(self .DB[2 ], x& 0x04 )
152
- digitalWrite(self .DB[1 ], x& 0x02 )
153
- digitalWrite(self .DB[0 ], x& 0x01 )
156
+ wp. digitalWrite(self .DB[3 ], x& 0x08 )
157
+ wp. digitalWrite(self .DB[2 ], x& 0x04 )
158
+ wp. digitalWrite(self .DB[1 ], x& 0x02 )
159
+ wp. digitalWrite(self .DB[0 ], x& 0x01 )
154
160
155
161
156
162
cdef inline void writing8(self ) noexcept nogil:
157
163
""" Set the interface into writing mode (RW=0 and all DBs as outputs) (8-bit interface)"""
158
- digitalWrite(self .RW, 0 )
159
- pinMode(self .DB[0 ], OUTPUT); pinMode(self .DB[1 ], OUTPUT)
160
- pinMode(self .DB[2 ], OUTPUT); pinMode(self .DB[3 ], OUTPUT)
161
- pinMode(self .DB[4 ], OUTPUT); pinMode(self .DB[5 ], OUTPUT)
162
- pinMode(self .DB[6 ], OUTPUT); pinMode(self .DB[7 ], OUTPUT)
164
+ wp. digitalWrite(self .RW, 0 )
165
+ wp. pinMode(self .DB[0 ], wp. OUTPUT); wp. pinMode(self .DB[1 ], wp. OUTPUT)
166
+ wp. pinMode(self .DB[2 ], wp. OUTPUT); wp. pinMode(self .DB[3 ], wp. OUTPUT)
167
+ wp. pinMode(self .DB[4 ], wp. OUTPUT); wp. pinMode(self .DB[5 ], wp. OUTPUT)
168
+ wp. pinMode(self .DB[6 ], wp. OUTPUT); wp. pinMode(self .DB[7 ], wp. OUTPUT)
163
169
cdef inline void writing4(self ) noexcept nogil:
164
170
""" Set the interface into writing mode (RW=0 and all DBs as outputs) (4-bit interface)"""
165
- digitalWrite(self .RW, 0 )
166
- pinMode(self .DB[0 ], OUTPUT); pinMode(self .DB[1 ], OUTPUT)
167
- pinMode(self .DB[2 ], OUTPUT); pinMode(self .DB[3 ], OUTPUT)
171
+ wp. digitalWrite(self .RW, 0 )
172
+ wp. pinMode(self .DB[0 ], wp. OUTPUT); wp. pinMode(self .DB[1 ], wp. OUTPUT)
173
+ wp. pinMode(self .DB[2 ], wp. OUTPUT); wp. pinMode(self .DB[3 ], wp. OUTPUT)
168
174
169
175
cdef inline void reading8(self ) noexcept nogil:
170
176
""" Set the interface into reading mode (RW=1 and all DBs as inputs) (8-bit interface)"""
171
- digitalWrite(self .RW, 1 )
172
- pinMode(self .DB[0 ], INPUT); pinMode(self .DB[1 ], INPUT)
173
- pinMode(self .DB[2 ], INPUT); pinMode(self .DB[3 ], INPUT)
174
- pinMode(self .DB[4 ], INPUT); pinMode(self .DB[5 ], INPUT)
175
- pinMode(self .DB[6 ], INPUT); pinMode(self .DB[7 ], INPUT)
177
+ wp. digitalWrite(self .RW, 1 )
178
+ wp. pinMode(self .DB[0 ], wp. INPUT); wp. pinMode(self .DB[1 ], wp. INPUT)
179
+ wp. pinMode(self .DB[2 ], wp. INPUT); wp. pinMode(self .DB[3 ], wp. INPUT)
180
+ wp. pinMode(self .DB[4 ], wp. INPUT); wp. pinMode(self .DB[5 ], wp. INPUT)
181
+ wp. pinMode(self .DB[6 ], wp. INPUT); wp. pinMode(self .DB[7 ], wp. INPUT)
176
182
cdef inline void reading4(self ) noexcept nogil:
177
183
""" Set the interface into reading mode (RW=1 and all DBs as inputs) (4-bit interface)"""
178
- digitalWrite(self .RW, 1 )
179
- pinMode(self .DB[0 ], INPUT); pinMode(self .DB[1 ], INPUT)
180
- pinMode(self .DB[2 ], INPUT); pinMode(self .DB[3 ], INPUT)
184
+ wp. digitalWrite(self .RW, 1 )
185
+ wp. pinMode(self .DB[0 ], wp. INPUT); wp. pinMode(self .DB[1 ], wp. INPUT)
186
+ wp. pinMode(self .DB[2 ], wp. INPUT); wp. pinMode(self .DB[3 ], wp. INPUT)
181
187
182
188
cdef inline bint busy8(self ) noexcept nogil:
183
189
""" Checks if the LCD is busy or not (8-bit interface)"""
184
190
# RS, RW = 0, 1
185
- digitalWrite(self .EN, 1 )
186
- delayMicroseconds(1 )
187
- cdef bint busy = digitalRead(self .DB[7 ])
188
- digitalWrite(self .EN, 0 )
191
+ wp. digitalWrite(self .EN, 1 )
192
+ wp. delayMicroseconds(1 )
193
+ cdef bint busy = wp. digitalRead(self .DB[7 ])
194
+ wp. digitalWrite(self .EN, 0 )
189
195
return busy
190
196
cdef inline bint busy4(self ) noexcept nogil:
191
197
""" Checks if the LCD is busy or not (4-bit interface)"""
192
198
# RS, RW = 0, 1
193
- digitalWrite(self .EN, 1 )
194
- delayMicroseconds(1 )
195
- cdef bint busy = digitalRead(self .DB[3 ])
196
- digitalWrite(self .EN, 0 )
197
- digitalWrite(self .EN, 1 )
198
- digitalWrite(self .EN, 0 )
199
+ wp. digitalWrite(self .EN, 1 )
200
+ wp. delayMicroseconds(1 )
201
+ cdef bint busy = wp. digitalRead(self .DB[3 ])
202
+ wp. digitalWrite(self .EN, 0 )
203
+ wp. digitalWrite(self .EN, 1 )
204
+ wp. digitalWrite(self .EN, 0 )
199
205
return busy
200
206
@property
201
207
def busy (self ): return self .busy8() if self .bits == 8 else self .busy4()
@@ -206,42 +212,42 @@ cdef class LCD:
206
212
cdef int EN = self .EN, DB = self .DB[7 ]
207
213
cdef bint busy = True
208
214
while busy:
209
- delayMicroseconds(1 ); digitalWrite(EN, 1 )
210
- delayMicroseconds(1 ); busy = digitalRead(DB); digitalWrite(EN, 0 )
215
+ wp. delayMicroseconds(1 ); wp. digitalWrite(EN, 1 )
216
+ wp. delayMicroseconds(1 ); busy = wp. digitalRead(DB); wp. digitalWrite(EN, 0 )
211
217
cdef inline void wait4(self ) noexcept nogil:
212
218
""" Waits for the LCD to not be busy (4-bit interface)"""
213
219
# RS, RW = 0, 1
214
220
cdef int EN = self .EN, DB = self .DB[3 ]
215
221
cdef bint busy = True
216
222
while busy:
217
- delayMicroseconds(1 ); digitalWrite(EN, 1 )
218
- delayMicroseconds(1 ); busy = digitalRead(DB); digitalWrite(EN, 0 )
219
- delayMicroseconds(1 ); digitalWrite(EN, 1 )
220
- delayMicroseconds(1 ); digitalWrite(EN, 0 )
223
+ wp. delayMicroseconds(1 ); wp. digitalWrite(EN, 1 )
224
+ wp. delayMicroseconds(1 ); busy = wp. digitalRead(DB); wp. digitalWrite(EN, 0 )
225
+ wp. delayMicroseconds(1 ); wp. digitalWrite(EN, 1 )
226
+ wp. delayMicroseconds(1 ); wp. digitalWrite(EN, 0 )
221
227
222
228
cdef inline int __read8(self ) noexcept nogil:
223
229
""" Read a single byte from the LCD, which must not be busy and RS set properly (8-bit interface)"""
224
230
# RW = 1
225
- delayMicroseconds(1 ); digitalWrite(self .EN, 1 ); delayMicroseconds(1 )
231
+ wp. delayMicroseconds(1 ); wp. digitalWrite(self .EN, 1 ); wp. delayMicroseconds(1 )
226
232
cdef int out = (
227
- digitalRead(self .DB[7 ]) << 7 | digitalRead(self .DB[6 ]) << 6 |
228
- digitalRead(self .DB[5 ]) << 5 | digitalRead(self .DB[4 ]) << 4 |
229
- digitalRead(self .DB[3 ]) << 3 | digitalRead(self .DB[2 ]) << 2 |
230
- digitalRead(self .DB[1 ]) << 1 | digitalRead(self .DB[0 ]) << 0 )
231
- digitalWrite(self .EN, 0 )
233
+ wp. digitalRead(self .DB[7 ]) << 7 | wp. digitalRead(self .DB[6 ]) << 6 |
234
+ wp. digitalRead(self .DB[5 ]) << 5 | wp. digitalRead(self .DB[4 ]) << 4 |
235
+ wp. digitalRead(self .DB[3 ]) << 3 | wp. digitalRead(self .DB[2 ]) << 2 |
236
+ wp. digitalRead(self .DB[1 ]) << 1 | wp. digitalRead(self .DB[0 ]) << 0 )
237
+ wp. digitalWrite(self .EN, 0 )
232
238
return out
233
239
cdef inline int __read4(self ) noexcept nogil:
234
240
""" Read a single byte from the LCD, which must not be busy and RS set properly (4-bit interface)"""
235
241
# RW = 1
236
- delayMicroseconds(1 ); digitalWrite(self .EN, 1 ); delayMicroseconds(1 )
242
+ wp. delayMicroseconds(1 ); wp. digitalWrite(self .EN, 1 ); wp. delayMicroseconds(1 )
237
243
cdef int out = (
238
- digitalRead(self .DB[3 ]) << 7 | digitalRead(self .DB[2 ]) << 6 |
239
- digitalRead(self .DB[1 ]) << 5 | digitalRead(self .DB[0 ]) << 4 )
240
- digitalWrite(self .EN, 0 )
241
- delayMicroseconds(1 ); digitalWrite(self .EN, 1 ); delayMicroseconds(1 )
242
- out |= (digitalRead(self .DB[3 ]) << 3 | digitalRead(self .DB[2 ]) << 2 |
243
- digitalRead(self .DB[1 ]) << 1 | digitalRead(self .DB[0 ]) << 0 )
244
- digitalWrite(self .EN, 0 )
244
+ wp. digitalRead(self .DB[3 ]) << 7 | wp. digitalRead(self .DB[2 ]) << 6 |
245
+ wp. digitalRead(self .DB[1 ]) << 5 | wp. digitalRead(self .DB[0 ]) << 4 )
246
+ wp. digitalWrite(self .EN, 0 )
247
+ wp. delayMicroseconds(1 ); wp. digitalWrite(self .EN, 1 ); wp. delayMicroseconds(1 )
248
+ out |= (digitalRead(self .DB[3 ]) << 3 | wp. digitalRead(self .DB[2 ]) << 2 |
249
+ wp. digitalRead(self .DB[1 ]) << 1 | wp. digitalRead(self .DB[0 ]) << 0 )
250
+ wp. digitalWrite(self .EN, 0 )
245
251
return out
246
252
cdef int read8(self ) noexcept nogil:
247
253
""" Read a single byte from the LCD, the character address (8-bit interface)"""
@@ -257,17 +263,17 @@ cdef class LCD:
257
263
""" Read a single byte from the LCD, the data (8-bit interface)"""
258
264
# RW = 1
259
265
self .wait8()
260
- digitalWrite(self .RS, 1 )
266
+ wp. digitalWrite(self .RS, 1 )
261
267
cdef int x = self .__read8()
262
- digitalWrite(self .RS, 0 )
268
+ wp. digitalWrite(self .RS, 0 )
263
269
return x
264
270
cdef int readData4(self ) noexcept nogil:
265
271
""" Read a single byte from the LCD, the data (4-bit interface)"""
266
272
# RW = 1
267
273
self .wait4()
268
- digitalWrite(self .RS, 1 )
274
+ wp. digitalWrite(self .RS, 1 )
269
275
cdef int x = self .__read4()
270
- digitalWrite(self .RS, 0 )
276
+ wp. digitalWrite(self .RS, 0 )
271
277
return x
272
278
273
279
cdef inline void __write8(self , unsigned char x) noexcept nogil:
@@ -301,16 +307,16 @@ cdef class LCD:
301
307
""" Writes a single byte to the LCD either as data (8-bit interface)"""
302
308
# assert(0 <= x <= 0xFF)
303
309
self .wait8()
304
- digitalWrite(self .RS, 1 )
310
+ wp. digitalWrite(self .RS, 1 )
305
311
self .__write8(x)
306
- digitalWrite(self .RS, 0 )
312
+ wp. digitalWrite(self .RS, 0 )
307
313
cdef void writeData4(self , unsigned char x) noexcept nogil:
308
314
""" Writes a single byte to the LCD either as data (4-bit interface)"""
309
315
# assert(0 <= x <= 0xFF)
310
316
self .wait4()
311
- digitalWrite(self .RS, 1 )
317
+ wp. digitalWrite(self .RS, 1 )
312
318
self .__write4(x)
313
- digitalWrite(self .RS, 0 )
319
+ wp. digitalWrite(self .RS, 0 )
314
320
315
321
# ######### COMMANDS ##########
316
322
def clear (self ):
0 commit comments