Merge lp:~pwlars/uci-phone-masher/set-offset into lp:uci-phone-masher

Proposed by Paul Larson
Status: Merged
Approved by: Paul Larson
Approved revision: 7
Merged at revision: 7
Proposed branch: lp:~pwlars/uci-phone-masher/set-offset
Merge into: lp:uci-phone-masher
Diff against target: 112 lines (+48/-6)
3 files modified
uci_phone_masher/__init__.py (+17/-4)
uci_phone_masher/controller.py (+3/-0)
uci_phone_masher/model.py (+28/-2)
To merge this branch: bzr merge lp:~pwlars/uci-phone-masher/set-offset
Reviewer Review Type Date Requested Status
Paul Larson Approve
Review via email: mp+292423@code.launchpad.net

Commit message

Add a way to set the offset

Description of the change

This allows setting an "offset" which will later be used for making small adjustments. More patches are coming to actually use it, and load settings from a file, and other useful features. Just trying to break them up to make it easier.

To post a comment you must log in.
Revision history for this message
Paul Larson (pwlars) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'uci_phone_masher/__init__.py'
2--- uci_phone_masher/__init__.py 2015-03-31 14:17:43 +0000
3+++ uci_phone_masher/__init__.py 2016-04-20 17:40:53 +0000
4@@ -77,10 +77,7 @@
5 button_states=controller.model.get_button_state(int(button_num)))
6
7
8-def set_button(button_num):
9- if 'state' not in request.form:
10- raise errors.MissingParameter("state")
11- state = request.form['state'].lower()
12+def set_button_state(button_num, state):
13 controller = current_app.extensions['BUTTONCONTROLLER']
14 if state == "on":
15 controller.press(int(button_num))
16@@ -90,6 +87,22 @@
17 controller.calibrate(int(button_num))
18 else:
19 raise errors.BadStateName(state, ('on', 'off', 'calibrate'))
20+
21+
22+def set_button_offset(button_num, offset):
23+ controller = current_app.extensions['BUTTONCONTROLLER']
24+ controller.set_offset(int(button_num), int(offset))
25+
26+
27+def set_button(button_num):
28+ if 'state' not in request.form and 'offset' not in request.form:
29+ raise errors.MissingParameter("state or offset")
30+ if 'state' in request.form:
31+ state = request.form['state'].lower()
32+ set_button_state(button_num, state)
33+ if 'offset' in request.form:
34+ offset = request.form['offset'].lower()
35+ set_button_offset(button_num, offset)
36 return get_button(button_num)
37
38
39
40=== modified file 'uci_phone_masher/controller.py'
41--- uci_phone_masher/controller.py 2015-04-01 13:32:32 +0000
42+++ uci_phone_masher/controller.py 2016-04-20 17:40:53 +0000
43@@ -44,6 +44,9 @@
44 def calibrate(self, button_num):
45 self._model = self._model.set_button_state(button_num, "calibrate")
46
47+ def set_offset(self, button_num, offset):
48+ self._model = self._model.set_button_offset(button_num, offset)
49+
50 @property
51 def model(self):
52 return self._model
53
54=== modified file 'uci_phone_masher/model.py'
55--- uci_phone_masher/model.py 2015-04-01 13:33:36 +0000
56+++ uci_phone_masher/model.py 2016-04-20 17:40:53 +0000
57@@ -33,12 +33,17 @@
58
59 """
60
61- def __init__(self, initial_state=None):
62+ def __init__(self, initial_state=None, initial_offset=None):
63 if initial_state is None:
64 self._buttons = {num: "off" for num in range(16)}
65 else:
66 self._buttons = initial_state
67
68+ if initial_offset is None:
69+ self._offset = {num: 0 for num in range(16)}
70+ else:
71+ self._offset = initial_offset
72+
73 @property
74 def data(self):
75 """Get a copy of the model's underlying data structure."""
76@@ -56,8 +61,23 @@
77 raise errors.BadButtonNumber(button_num)
78
79 state_copy = self._buttons.copy()
80+ offset_copy = self._offset.copy()
81 state_copy[button_num] = state
82- return ButtonModel(state_copy)
83+ return ButtonModel(state_copy, offset_copy)
84+
85+ def set_button_offset(self, button_num, offset):
86+ """Modify the model by setting a button's offset.
87+
88+ Returns a copy of the modified model.
89+
90+ """
91+ if not 0 <= button_num < len(self._buttons):
92+ raise errors.BadButtonNumber(button_num)
93+
94+ state_copy = self._buttons.copy()
95+ offset_copy = self._offset.copy()
96+ offset_copy[button_num] = offset
97+ return ButtonModel(state_copy, offset_copy)
98
99 def get_button_state(self, button_num):
100 """Get the state of a particular button."""
101@@ -65,6 +85,12 @@
102 raise errors.BadButtonNumber(button_num)
103 return self._buttons[button_num]
104
105+ def get_button_offset(self, button_num):
106+ """Get the offset of a particular button."""
107+ if not 0 <= button_num < len(self._buttons):
108+ raise errors.BadButtonNumber(button_num)
109+ return self._offset[button_num]
110+
111 def __eq__(self, other):
112 return self._buttons == other._buttons
113

Subscribers

People subscribed via source and target branches