Commits

Ashley Felton committed cf449e7

LED controller now saves state to a config file.

Comments (0)

Files changed (5)

 \.pyc$
 \.swp$
 \.org$
+\gpio.cfg$

temper/__init__.py

 CACHE_THRESHOLD = 20
 TEMP_LOGS = '/var/log/temps'
 GPIO_PINS = [17, 18, 21]
+GPIO_CFG = 'gpio.cfg'
 
 app = Flask(__name__)
 cache = Cache(app)

temper/templates/leds.html

             {% for g in gpio_config %}
             <tr>
                 <td>
-                    GPIO pin {{ g.id }}:
+                    GPIO pin {{ g.gpio }}:
                 </td>
                 <td>
-                    <div class="gpio-toggle" id="toggle-button-gpio{{ g.id }}" data-gpio="{{ g.id }}">
+                    <div class="gpio-toggle" id="toggle-button-gpio{{ g.gpio }}" data-gpio="{{ g.gpio }}">
                         <input type="checkbox"{% if g.mode %} checked="checked"{% endif %}>
                     </div>
                 </td>
 <script src="/static/js/jquery.toggle.buttons.js"></script>
 <script type="text/javascript">
 {% for g in gpio_config %}
-    $('#toggle-button-gpio' + {{ g.id }}).toggleButtons({
+    $('#toggle-button-gpio' + {{ g.gpio }}).toggleButtons({
         onChange: function ($el, status, e) {
             //console.log(status);
             //console.log($($el).parent().data("gpio"));
             else {
                 pinmode = 0
             };
-            $.post("/gpio_mode", {gpio: {{ g.id }}, mode: pinmode},
+            $.post("/gpio_mode", {gpio: {{ g.gpio }}, mode: pinmode},
                 function(data) {
                     console.log(data);
                 }
 #!/usr/bin/python
 import os
+import json
 import subprocess
 import time
 from datetime import datetime, date
 from temper import app
 
+script_path = os.path.dirname(os.path.abspath(__file__))
 
 def read_onewire_temp():
     '''
     return sorted(temps)
 
 
-import json
-def gpio_config():
-    print os.path.dirname(os.path.abspath(__file__))
+def read_gpio_config():
+    '''
+    Read the GPIO pin config from the config file.
+    '''
+    cfg = open(os.path.join(script_path, app.config['GPIO_CFG']), 'r')
+    gpio = json.loads(cfg.readline())
+    cfg.close()
+    return gpio
+
+
+def write_gpio_config(cfg):
+    '''
+    Write the GPIO pin config to the config file.
+    '''
+    if not isinstance(cfg, list):
+        return
+    cfg_file = open(os.path.join(script_path, app.config['GPIO_CFG']), 'w')
+    cfg_file.write(json.dumps(cfg))
+    cfg_file.close()
+    return
 
 
 if __name__ == "__main__":
 #!/usr/bin/python
 import json
 import subprocess
+from datetime import datetime
 from flask import request, render_template, jsonify
+from operator import itemgetter
 from temper import app, utils, cache
-from datetime import datetime
 
 
 @app.route('/favicon.ico')
 @app.route('/leds')
 def led_controller():
     # Read in the current GPIO pin config and pass it to the template.
-    # Temporarily just make something up:
-    gpio_config = [{'id': 17, 'mode': 0}, {'id': 18, 'mode': 0}, {'id': 21, 'mode': 0}]
-    return render_template('leds.html', gpio_config=gpio_config)
+    cfg = utils.read_gpio_config()
+    return render_template('leds.html', gpio_config=cfg)
 
 
 @app.route('/gpio_mode', methods=['POST'])
     # Read the GPIO and mode arguments from the POST request.
     gpio = int(request.form['gpio'])
     mode = int(request.form['mode'])
-    if gpio in [17, 18, 21] and mode in [0, 1]:  # NOTE: mode == 0 evaluates to False. Duh!
+    if gpio in app.config['GPIO_PINS'] and mode in [0, 1]:  # NOTE: mode == 0 evaluates to False. Duh!
+        cfg = utils.read_gpio_config()
+        i = map(itemgetter('gpio'), cfg).index(gpio)  # Index of the GPIO dict in the list.
+        cfg[i]['mode'] = mode
+        utils.write_gpio_config(cfg)
         subprocess.call('gpio -g write {0} {1}'.format(gpio, mode), shell=True)
         return jsonify({'gpio': gpio, 'mode': mode})
     else:
-        return '{}'
+        return 'None'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.