-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevknobs.py
More file actions
106 lines (86 loc) · 3.55 KB
/
devknobs.py
File metadata and controls
106 lines (86 loc) · 3.55 KB
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# devknobs.py
#
# (c) 2013 Konstantin Sering, Nora Umbach
# <colorlab[at]psycho.uni-tuebingen.de>
#
# GPL 3.0+ or (cc) by-sa (http://creativecommons.org/licenses/by-sa/3.0/)
#
# content:
#
# input: --
# output: --
#
# created 2013-04-16 KS
# last mod 2013-04-16 16:26 KS
"""
Defines a class that encapsulates the triggering and reading out for the
knobs from the user.
"""
from __future__ import print_function
import time
from achrolab.wasco import wasco
from achrolab.wasco.constants import (AD_ADCONT, AD_ADDAT, AD_ADRANGE,
AD_ADSTAT, AD_STARTCH, AD_SWTRIG, DAOUT1, RESETERRORFLAG,
RESETFIFO)
class DevKnobs(object):
"""
Encapsulates all functions getting input from the knobs.
Example:
>>> devknobs = DevKnobs(dummy=True)
>>> states = devknobs.getStates()
"""
def __init__(self, dummy=False):
"""
Setting attributes.
If dummy=True no wasco runtime libraries will be loaded.
"""
self.wasco_card = wasco.Wasco(dummy=dummy) # create wasco object
self.boardId = self.wasco_card.boardId
self.channels = (("red", 62), ("green", 61), ("blue", 60), ("all", 63))
self.reference_voltage = 0x800
# initialize wasco card
self.wasco_card.wasco_outportW(self.boardId, AD_ADCONT, 0x91) # A/D-Modus:
# Softwareauslösung
self.wasco_card.wasco_outportW(self.boardId, AD_ADCONT, 0xB1) # PGA-Ansteuerung über
# Register AD_ADRANGE
# MUX-Ansteuerung über Register AD_ADSTARTCH
self.wasco_card.wasco_outportW(self.boardId, AD_ADRANGE, 0x1) # VPGA = 2, single ended
# vgl Seite 34 im
# Manual
self.wasco_card.wasco_outportW(self.boardId, RESETERRORFLAG, 0x0)
self.wasco_card.wasco_outportW(self.boardId, RESETFIFO, 0x0)
self.wasco_card.wasco_outportW(self.boardId, DAOUT1, self.reference_voltage)
def getStates(self):
"""
Returns (red, green, blue, all) as integer values between 0 and 0xFFF.
Internally it triggers the measurement and reads out the values from
the wasco card.
"""
# trigger measurement
for name, channel in self.channels:
self.wasco_card.wasco_outportW(self.boardId, AD_STARTCH, channel)
time.sleep(0.001)
self.wasco_card.wasco_outportW(self.boardId, AD_SWTRIG, 0x0)
time.sleep(0.001)
values = list()
# read out values
for name, channel in self.channels:
status = self.wasco_card.wasco_inportW(self.boardId, AD_ADSTAT) # check A/D-Status-Register
if status:
value = self.wasco_card.wasco_inportW(self.boardId, AD_ADDAT) # read A/D-Wert
#print("Kanal: %02d %4x Hex %4.4f Volt\t" % (channel, value,
# -10 + value *
# 0.004882812),
# end="")
values.append(value)
time.sleep(0.001)
else:
print(("\nA/D-Statusregister: %x") % status)
values.append(None)
# transform from (0x800, 0xFFF) -> (0x000, 0xFFF)
return [(value - 0x800)*2 for value in values if value]
@property
def states(self):
return self.getStates()