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

Proposed by Paul Larson
Status: Merged
Approved by: Paul Larson
Approved revision: 11
Merged at revision: 11
Proposed branch: lp:~pwlars/uci-phone-masher/load-offset-config
Merge into: lp:uci-phone-masher
Prerequisite: lp:~pwlars/uci-phone-masher/debug-memory-controller
Diff against target: 84 lines (+26/-9)
2 files modified
uci_phone_masher/__init__.py (+8/-5)
uci_phone_masher/controller.py (+18/-4)
To merge this branch: bzr merge lp:~pwlars/uci-phone-masher/load-offset-config
Reviewer Review Type Date Requested Status
Paul Larson Approve
Review via email: mp+292881@code.launchpad.net

Description of the change

If you specify the path to a json file in UCI_PHONE_MASHER_CONFIG, it will load those config values and use them for offsets. You can export the dict you should use in the file using something like:
$ curl localhost:8000/button

The file should contain something like this:
{
    "0": 0,
    "1": 0,
    "2": 0,
    "3": 3,
    "4": 0,
    "5": 0,
    "6": 0,
    "7": 0,
    "8": 0,
    "9": 0,
    "10": 88,
    "11": 0,
    "12": 0,
    "13": 0,
    "14": 0,
    "15": 0
}

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 2016-04-21 19:16:40 +0000
3+++ uci_phone_masher/__init__.py 2016-04-26 07:19:17 +0000
4@@ -32,7 +32,7 @@
5 from uci_phone_masher import errors
6
7
8-def create_flask_app(controller_type=''):
9+def create_flask_app(controller_type='', config_file=None):
10 """Create the flask web application.
11
12 Pass 'memory' as the controller_type if you want to force the use of a
13@@ -60,9 +60,11 @@
14 return response
15
16 if controller_type == 'memory':
17- app.extensions['BUTTONCONTROLLER'] = PhoneControllerMemory()
18+ app.extensions['BUTTONCONTROLLER'] = PhoneControllerMemory(
19+ config_file=config_file)
20 else:
21- app.extensions['BUTTONCONTROLLER'] = PhoneControllerI2C()
22+ app.extensions['BUTTONCONTROLLER'] = PhoneControllerI2C(
23+ config_file=config_file)
24 return app
25
26
27@@ -108,7 +110,8 @@
28 return get_button(button_num)
29
30
31+config_file = os.environ.get('UCI_PHONE_MASHER_CONFIG')
32 if os.environ.get('UCI_PHONE_MASHER_CONTROLLER', '').lower() == 'memory':
33- app = create_flask_app(controller_type='memory')
34+ app = create_flask_app(controller_type='memory', config_file=config_file)
35 else:
36- app = create_flask_app()
37+ app = create_flask_app(config_file=config_file)
38
39=== modified file 'uci_phone_masher/controller.py'
40--- uci_phone_masher/controller.py 2016-04-26 07:19:17 +0000
41+++ uci_phone_masher/controller.py 2016-04-26 07:19:17 +0000
42@@ -16,6 +16,8 @@
43 #
44
45
46+import json
47+
48 from uci_phone_masher.model import ButtonModel
49
50
51@@ -35,8 +37,20 @@
52
53 """
54
55- def __init__(self):
56- self._model = ButtonModel()
57+ def __init__(self, config_file=None):
58+ self.config_file = config_file
59+ if config_file:
60+ offset_config = self._load_config(config_file)
61+ else:
62+ offset_config = None
63+ self._model = ButtonModel(initial_offset=offset_config)
64+
65+ def _load_config(self, config_file):
66+ def str2int(x):
67+ return {int(k): v for k, v in x.items()}
68+
69+ with open(config_file) as f:
70+ return json.loads(f.read(), object_hook=str2int)
71
72 def press(self, button_num):
73 self._model = self._model.set_button_state(button_num, "on")
74@@ -59,8 +73,8 @@
75
76 """A controller that knows how to manipulate the servos."""
77
78- def __init__(self, debug=False):
79- super(PhoneControllerI2C, self).__init__()
80+ def __init__(self, config_file=None, debug=False):
81+ super(PhoneControllerI2C, self).__init__(config_file)
82 #
83 # Note: The PWM class tries to open the i2c device when it's
84 # declared (ugh), so we need the local import here:

Subscribers

People subscribed via source and target branches