-
Notifications
You must be signed in to change notification settings - Fork 0
/
thi2textlcd.py
executable file
·211 lines (169 loc) · 6.92 KB
/
thi2textlcd.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
#!/usr/bin/env python
"""Copyright 2010 Phidgets Inc.
This work is licensed under the Creative Commons Attribution 2.5 Canada License.
To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/
"""
__author__ = 'Adam Stelmack'
__version__ = '2.1.8'
__date__ = 'May 17 2010'
# Basic imports
from ctypes import *
import sys
import random
from time import sleep
# Phidget specific imports
from Phidgets.Phidget import PhidgetID
from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException
from Phidgets.Events.Events import AttachEventArgs, DetachEventArgs, ErrorEventArgs, InputChangeEventArgs, OutputChangeEventArgs, SensorChangeEventArgs
from Phidgets.Devices.InterfaceKit import InterfaceKit
from Phidgets.Devices.TextLCD import TextLCD, TextLCD_ScreenSize
# Create an interfacekit object
try:
interfaceKit = InterfaceKit()
textLCD = TextLCD()
except RuntimeError as e:
print("Runtime Exception: %s" % e.details)
print("Exiting....")
exit(1)
# Information Display Function
def dispalyifkinfo():
print("|------------|----------------------------------|--------------|------------|")
print("|- Attached -|- Type -|- Serial No. -|- Version -|")
print("|------------|----------------------------------|--------------|------------|")
print("|- %8s -|- %30s -|- %10d -|- %8d -|" %
(interfaceKit.isAttached(), interfaceKit.getDeviceName(), interfaceKit.getSerialNum(), interfaceKit.getDeviceVersion()))
print("|------------|----------------------------------|--------------|------------|")
print("Number of Digital Inputs: %i" % (interfaceKit.getInputCount()))
print("Number of Digital Outputs: %i" % (interfaceKit.getOutputCount()))
print("Number of Sensor Inputs: %i" % (interfaceKit.getSensorCount()))
# Information Display Function
def displaylcdinfo():
try:
isAttached = textLCD.isAttached()
name = textLCD.getDeviceName()
serialNo = textLCD.getSerialNum()
version = textLCD.getDeviceVersion()
rowCount = textLCD.getRowCount()
columnCount = textLCD.getColumnCount()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
return 1
print("|------------|----------------------------------|--------------|------------|")
print("|- Attached -|- Type -|- Serial No. -|- Version -|")
print("|------------|----------------------------------|--------------|------------|")
print("|- %8s -|- %30s -|- %10d -|- %8d -|" %
(isAttached, name, serialNo, version))
print("|------------|----------------------------------|--------------|------------|")
print("Number of Rows: %i -- Number of Columns: %i" %
(rowCount, columnCount))
# Event Handler Callback Functions
def TextLCDAttached(e):
attached = e.device
print("TextLCD %i Attached!" % (attached.getSerialNum()))
if textLCD.getDeviceID() == PhidgetID.PHIDID_TEXTLCD_ADAPTER:
textLCD.setScreenIndex(0)
textLCD.setScreenSize(TextLCD_ScreenSize.PHIDGET_TEXTLCD_SCREEN_2x8)
def TextLCDDetached(e):
detached = e.device
print("TextLCD %i Detached!" % (detached.getSerialNum()))
def TextLCDError(e):
try:
source = e.device
print("TextLCD %i: Phidget Error %i: %s" %
(source.getSerialNum(), e.eCode, e.description))
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
def interfaceKitAttached(e):
attached = e.device
print("InterfaceKit %i Attached!" % (attached.getSerialNum()))
def interfaceKitDetached(e):
detached = e.device
print("InterfaceKit %i Detached!" % (detached.getSerialNum()))
def interfaceKitError(e):
try:
source = e.device
print("InterfaceKit %i: Phidget Error %i: %s" %
(source.getSerialNum(), e.eCode, e.description))
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
def interfaceKitInputChanged(e):
source = e.device
print("InterfaceKit %i: Input %i: %s" %
(source.getSerialNum(), e.index, e.state))
def interfaceKitSensorChanged(e):
source = e.device
tmp = "TMP: " + \
str(round((source.getSensorValue(1) * 0.22222) - 61.11, 2)) + " c"
tmp = tmp.encode()
hmd = "HMD: " + \
str(round((source.getSensorValue(0) * 0.1906) - 40.2, 2)) + " %"
hmd = hmd.encode()
textLCD.setDisplayString(0, tmp)
textLCD.setDisplayString(1, hmd)
print("InterfaceKit %i: Sensor %i: %i" %
(source.getSerialNum(), e.index, e.value))
def interfaceKitOutputChanged(e):
source = e.device
print("InterfaceKit %i: Output %i: %s" %
(source.getSerialNum(), e.index, e.state))
if __name__ == '__main__':
# Main Program Code
try:
textLCD.setOnAttachHandler(TextLCDAttached)
textLCD.setOnDetachHandler(TextLCDDetached)
textLCD.setOnErrorhandler(TextLCDError)
interfaceKit.setOnAttachHandler(interfaceKitAttached)
interfaceKit.setOnDetachHandler(interfaceKitDetached)
interfaceKit.setOnErrorhandler(interfaceKitError)
interfaceKit.setOnInputChangeHandler(interfaceKitInputChanged)
interfaceKit.setOnOutputChangeHandler(interfaceKitOutputChanged)
interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged)
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)
try:
textLCD.openPhidget()
interfaceKit.openPhidget()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)
print("Waiting for attach....")
try:
textLCD.waitForAttach(10000)
interfaceKit.waitForAttach(10000)
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
try:
interfaceKit.closePhidget()
textLCD.closePhidget()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)
print("Exiting....")
exit(1)
else:
displaylcdinfo()
dispalyifkinfo()
print("Setting the data rate for each sensor index to 4ms....")
for i in range(interfaceKit.getSensorCount()):
try:
interfaceKit.setDataRate(i, 4)
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Press Enter to quit....")
sys.stdin.read(1)
print("Closing...")
try:
textLCD.setDisplayString(0, b"")
textLCD.setDisplayString(1, b"")
interfaceKit.closePhidget()
textLCD.closePhidget()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)
print("Done.")
exit(0)