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
=== modified file 'uci_phone_masher/__init__.py'
--- uci_phone_masher/__init__.py 2016-04-21 19:16:40 +0000
+++ uci_phone_masher/__init__.py 2016-04-26 07:19:17 +0000
@@ -32,7 +32,7 @@
32from uci_phone_masher import errors32from uci_phone_masher import errors
3333
3434
35def create_flask_app(controller_type=''):35def create_flask_app(controller_type='', config_file=None):
36 """Create the flask web application.36 """Create the flask web application.
3737
38 Pass 'memory' as the controller_type if you want to force the use of a38 Pass 'memory' as the controller_type if you want to force the use of a
@@ -60,9 +60,11 @@
60 return response60 return response
6161
62 if controller_type == 'memory':62 if controller_type == 'memory':
63 app.extensions['BUTTONCONTROLLER'] = PhoneControllerMemory()63 app.extensions['BUTTONCONTROLLER'] = PhoneControllerMemory(
64 config_file=config_file)
64 else:65 else:
65 app.extensions['BUTTONCONTROLLER'] = PhoneControllerI2C()66 app.extensions['BUTTONCONTROLLER'] = PhoneControllerI2C(
67 config_file=config_file)
66 return app68 return app
6769
6870
@@ -108,7 +110,8 @@
108 return get_button(button_num)110 return get_button(button_num)
109111
110112
113config_file = os.environ.get('UCI_PHONE_MASHER_CONFIG')
111if os.environ.get('UCI_PHONE_MASHER_CONTROLLER', '').lower() == 'memory':114if os.environ.get('UCI_PHONE_MASHER_CONTROLLER', '').lower() == 'memory':
112 app = create_flask_app(controller_type='memory')115 app = create_flask_app(controller_type='memory', config_file=config_file)
113else:116else:
114 app = create_flask_app()117 app = create_flask_app(config_file=config_file)
115118
=== modified file 'uci_phone_masher/controller.py'
--- uci_phone_masher/controller.py 2016-04-26 07:19:17 +0000
+++ uci_phone_masher/controller.py 2016-04-26 07:19:17 +0000
@@ -16,6 +16,8 @@
16#16#
1717
1818
19import json
20
19from uci_phone_masher.model import ButtonModel21from uci_phone_masher.model import ButtonModel
2022
2123
@@ -35,8 +37,20 @@
3537
36 """38 """
3739
38 def __init__(self):40 def __init__(self, config_file=None):
39 self._model = ButtonModel()41 self.config_file = config_file
42 if config_file:
43 offset_config = self._load_config(config_file)
44 else:
45 offset_config = None
46 self._model = ButtonModel(initial_offset=offset_config)
47
48 def _load_config(self, config_file):
49 def str2int(x):
50 return {int(k): v for k, v in x.items()}
51
52 with open(config_file) as f:
53 return json.loads(f.read(), object_hook=str2int)
4054
41 def press(self, button_num):55 def press(self, button_num):
42 self._model = self._model.set_button_state(button_num, "on")56 self._model = self._model.set_button_state(button_num, "on")
@@ -59,8 +73,8 @@
5973
60 """A controller that knows how to manipulate the servos."""74 """A controller that knows how to manipulate the servos."""
6175
62 def __init__(self, debug=False):76 def __init__(self, config_file=None, debug=False):
63 super(PhoneControllerI2C, self).__init__()77 super(PhoneControllerI2C, self).__init__(config_file)
64 #78 #
65 # Note: The PWM class tries to open the i2c device when it's79 # Note: The PWM class tries to open the i2c device when it's
66 # declared (ugh), so we need the local import here:80 # declared (ugh), so we need the local import here:

Subscribers

People subscribed via source and target branches