-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd_piface_class.py
executable file
·227 lines (190 loc) · 4.98 KB
/
lcd_piface_class.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
# -*- coding: latin-1 -*-
#
# $Id: lcd_piface_class.py,v 1.2 2015/01/21 19:44:18 bob Exp $
# Raspberry Pi Internet Radio
# using a Piface backlit LCD plate
#
# Author : Patrick Zacharias
# Site : N/A
#
# From original LCD routines : piface.co.uk
# Site : http://piface.github.io/pifacecad
#
# Based on Bob Rathbone's LCD class
# Bob Rathbone's site : http://bobrathbone.com
#
# To use this program you need pifacecommon and pifacecad installed
# for their respective Python versions
#
# At the moment this program can only be executed with Python2.7
#
# This program uses Music Player Daemon 'mpd'and it's client 'mpc'
# See http://mpd.wikia.com/wiki/Music_Player_Daemon_Wiki
#
# License: GNU V3, See https://www.gnu.org/copyleft/gpl.html
#
# Disclaimer: Software is provided as is and absolutly no warranties are implied or given.
# The authors shall not be liable for any loss or damage however caused.
#
import os
import time
from translate_class import Translate
import subprocess
import pifacecommon
import pifacecad
from pifacecad.lcd import LCD_WIDTH
PLAY_SYMBOL = pifacecad.LCDBitmap(
[0x10, 0x18, 0x1c, 0x1e, 0x1c, 0x18, 0x10, 0x0])
PAUSE_SYMBOL = pifacecad.LCDBitmap(
[0x0, 0x1b, 0x1b, 0x1b, 0x1b, 0x1b, 0x0, 0x0])
INFO_SYMBOL = pifacecad.LCDBitmap(
[0x6, 0x6, 0x0, 0x1e, 0xe, 0xe, 0xe, 0x1f])
MUSIC_SYMBOL = pifacecad.LCDBitmap(
[0x2, 0x3, 0x2, 0x2, 0xe, 0x1e, 0xc, 0x0])
PLAY_SYMBOL_INDEX = 0
PAUSE_SYMBOL_INDEX = 1
INFO_SYMBOL_INDEX = 2
MUSIC_SYMBOL_INDEX = 3
#Symbols were taken from the pifacecad examples and aren't in use yet
# The wiring for the LCD is as follows:
# 1 : GND
# 2 : 5V
# 3 : Contrast (0-5V)*
# 4 : RS (Register Select)
# 5 : R/W (Read Write) - GROUND THIS PIN
# 6 : Enable or Strobe
# 7 : Data Bit 0 - NOT USED
# 8 : Data Bit 1 - NOT USED
# 9 : Data Bit 2 - NOT USED
# 10: Data Bit 3 - NOT USED
# 11: Data Bit 4
# 12: Data Bit 5
# 13: Data Bit 6
# 14: Data Bit 7
# 15: LCD Backlight +5V**
# 16: LCD Backlight GND
# Define LCD device constants
LCD_WIDTH = 16 # Default characters per line
LCD_CHR = True
LCD_CMD = False
# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005
translate = Translate()
# Lcd Class
class Lcd:
width = LCD_WIDTH
RawMode = False
ScrollSpeed = 0.2 # Default scroll speed
# Port expander input pin definitions
# MENU = 0
# RIGHT = 1
# DOWN = 2
# UP = 3
# LEFT = 4
LEFT = 0
DOWN = 1
UP = 2
RIGHT= 3
MENU = 4
def __init__(self):
# self.cad = pifacecad.pifacecad()
return
# self.cad = pifacecad.pifacecad()
# Initialise for either revision 1 or 2 boards
def init(self,revision=2):
# LED outputs
self.cad = pifacecad.PiFaceCAD()
# set up cad
#self.self.cad = cad
self.cad.lcd.blink_off()
self.cad.lcd.cursor_off()
self.cad.lcd.backlight_on()
self.cad.lcd.store_custom_bitmap(PLAY_SYMBOL_INDEX, PLAY_SYMBOL)
self.cad.lcd.store_custom_bitmap(PAUSE_SYMBOL_INDEX, PAUSE_SYMBOL)
self.cad.lcd.store_custom_bitmap(INFO_SYMBOL_INDEX, INFO_SYMBOL)
# self.self.cad = self.cad
return
# Set the display width
def setWidth(self,width):
self.width = width
return
# Send string to display
def _string(self,message):
s = message.ljust(self.width," ")
if not self.RawMode:
s = translate.toLCD(s)
self.cad.lcd.write(s)
return
# Display Line 1 on LED
def line1(self,text):
self.cad.lcd.set_cursor(0, 0)
self._string(text)
return
# Display Line 2 on LED
def line2(self,text):
self.cad.lcd.set_cursor(0, 1)
self._string(text)
return
# Scroll message on line 1
def scroll1(self,mytext,interrupt):
self._scroll(mytext,0,interrupt)
return
# Scroll message on line 2
def scroll2(self,mytext,interrupt):
self._scroll(mytext,1,interrupt)
return
# Scroll line - interrupt() breaks out routine if True
def _scroll(self,mytext,line,interrupt):
ilen = len(mytext)
skip = False
self.cad.lcd.set_cursor(0, line)
self._string(mytext[0:self.width + 1])
if (ilen <= self.width):
skip = True
if not skip:
for i in range(0, 5):
time.sleep(0.2)
if interrupt():
skip = True
break
if not skip:
for i in range(0, ilen - self.width + 1 ):
# self._byte_out(line, LCD_CMD)
self.cad.lcd.set_cursor(0, line)
self._string(mytext[i:i+self.width])
if interrupt():
skip = True
break
time.sleep(self.ScrollSpeed)
if not skip:
for i in range(0, 5):
time.sleep(0.2)
if interrupt():
break
return
# Set Scroll line speed - Best values are 0.2 and 0.3
# Limit to between 0.05 and 1.0
def setScrollSpeed(self,speed):
if speed < 0.05:
speed = 0.2
elif speed > 1.0:
speed = 0.3
self.ScrollSpeed = speed
return
# Set raw mode on (No translation)
def setRawMode(self,value):
self.RawMode = value
return
# Read state of single button
def buttonPressed(self, b):
button = self.cad.switches[b].value
return button
# Clear the display
def clear(self):
self.cad.lcd.clear()
# Sets cursor home
def home(self):
self.cad.lcd.home()
# End of Lcd class