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
=== modified file 'uci_phone_masher/__init__.py'
--- uci_phone_masher/__init__.py 2015-03-31 14:17:43 +0000
+++ uci_phone_masher/__init__.py 2016-04-20 17:40:53 +0000
@@ -77,10 +77,7 @@
77 button_states=controller.model.get_button_state(int(button_num)))77 button_states=controller.model.get_button_state(int(button_num)))
7878
7979
80def set_button(button_num):80def set_button_state(button_num, state):
81 if 'state' not in request.form:
82 raise errors.MissingParameter("state")
83 state = request.form['state'].lower()
84 controller = current_app.extensions['BUTTONCONTROLLER']81 controller = current_app.extensions['BUTTONCONTROLLER']
85 if state == "on":82 if state == "on":
86 controller.press(int(button_num))83 controller.press(int(button_num))
@@ -90,6 +87,22 @@
90 controller.calibrate(int(button_num))87 controller.calibrate(int(button_num))
91 else:88 else:
92 raise errors.BadStateName(state, ('on', 'off', 'calibrate'))89 raise errors.BadStateName(state, ('on', 'off', 'calibrate'))
90
91
92def set_button_offset(button_num, offset):
93 controller = current_app.extensions['BUTTONCONTROLLER']
94 controller.set_offset(int(button_num), int(offset))
95
96
97def set_button(button_num):
98 if 'state' not in request.form and 'offset' not in request.form:
99 raise errors.MissingParameter("state or offset")
100 if 'state' in request.form:
101 state = request.form['state'].lower()
102 set_button_state(button_num, state)
103 if 'offset' in request.form:
104 offset = request.form['offset'].lower()
105 set_button_offset(button_num, offset)
93 return get_button(button_num)106 return get_button(button_num)
94107
95108
96109
=== modified file 'uci_phone_masher/controller.py'
--- uci_phone_masher/controller.py 2015-04-01 13:32:32 +0000
+++ uci_phone_masher/controller.py 2016-04-20 17:40:53 +0000
@@ -44,6 +44,9 @@
44 def calibrate(self, button_num):44 def calibrate(self, button_num):
45 self._model = self._model.set_button_state(button_num, "calibrate")45 self._model = self._model.set_button_state(button_num, "calibrate")
4646
47 def set_offset(self, button_num, offset):
48 self._model = self._model.set_button_offset(button_num, offset)
49
47 @property50 @property
48 def model(self):51 def model(self):
49 return self._model52 return self._model
5053
=== modified file 'uci_phone_masher/model.py'
--- uci_phone_masher/model.py 2015-04-01 13:33:36 +0000
+++ uci_phone_masher/model.py 2016-04-20 17:40:53 +0000
@@ -33,12 +33,17 @@
3333
34 """34 """
3535
36 def __init__(self, initial_state=None):36 def __init__(self, initial_state=None, initial_offset=None):
37 if initial_state is None:37 if initial_state is None:
38 self._buttons = {num: "off" for num in range(16)}38 self._buttons = {num: "off" for num in range(16)}
39 else:39 else:
40 self._buttons = initial_state40 self._buttons = initial_state
4141
42 if initial_offset is None:
43 self._offset = {num: 0 for num in range(16)}
44 else:
45 self._offset = initial_offset
46
42 @property47 @property
43 def data(self):48 def data(self):
44 """Get a copy of the model's underlying data structure."""49 """Get a copy of the model's underlying data structure."""
@@ -56,8 +61,23 @@
56 raise errors.BadButtonNumber(button_num)61 raise errors.BadButtonNumber(button_num)
5762
58 state_copy = self._buttons.copy()63 state_copy = self._buttons.copy()
64 offset_copy = self._offset.copy()
59 state_copy[button_num] = state65 state_copy[button_num] = state
60 return ButtonModel(state_copy)66 return ButtonModel(state_copy, offset_copy)
67
68 def set_button_offset(self, button_num, offset):
69 """Modify the model by setting a button's offset.
70
71 Returns a copy of the modified model.
72
73 """
74 if not 0 <= button_num < len(self._buttons):
75 raise errors.BadButtonNumber(button_num)
76
77 state_copy = self._buttons.copy()
78 offset_copy = self._offset.copy()
79 offset_copy[button_num] = offset
80 return ButtonModel(state_copy, offset_copy)
6181
62 def get_button_state(self, button_num):82 def get_button_state(self, button_num):
63 """Get the state of a particular button."""83 """Get the state of a particular button."""
@@ -65,6 +85,12 @@
65 raise errors.BadButtonNumber(button_num)85 raise errors.BadButtonNumber(button_num)
66 return self._buttons[button_num]86 return self._buttons[button_num]
6787
88 def get_button_offset(self, button_num):
89 """Get the offset of a particular button."""
90 if not 0 <= button_num < len(self._buttons):
91 raise errors.BadButtonNumber(button_num)
92 return self._offset[button_num]
93
68 def __eq__(self, other):94 def __eq__(self, other):
69 return self._buttons == other._buttons95 return self._buttons == other._buttons
7096

Subscribers

People subscribed via source and target branches