From 602f34a340016f5aebefd08f31f149f1047083ca Mon Sep 17 00:00:00 2001 From: ahmetkotan Date: Thu, 31 Jan 2019 01:50:27 +0300 Subject: [PATCH] added change_value function --- picontrol/control.py | 9 ++++++++- pins/views.py | 1 + statics/static_dirs/js/pintable.js | 16 ++++++++++++++++ templates/pins/index.html | 2 ++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/picontrol/control.py b/picontrol/control.py index c76e05e..411e5e0 100644 --- a/picontrol/control.py +++ b/picontrol/control.py @@ -11,6 +11,9 @@ def pin_physical_control(physical): if physical > 40 and physical < 1: raise ValidationError({"physical": "Wrong pin number."}) + if physical not in settings.BOARD_PORTS: + raise ValidationError({"physical": "This pin isn't a board pin."}) + def get_pin(physical): pins = settings.PINS @@ -60,7 +63,11 @@ def write_pin_mode(physical, mode): raise ValidationError({"mode": "Wrong mode."}) gpio.setup(physical, new_mode) - return read_pin(physical) + pin = read_pin(physical) + if mode == gpio.IN: + pin["value"] = 0 + pin["hr_value"] = settings.PORT_VALUES[0] + return pin def write_pin_value(physical, value): diff --git a/pins/views.py b/pins/views.py index 0298749..5082713 100644 --- a/pins/views.py +++ b/pins/views.py @@ -46,6 +46,7 @@ class PinView(PaginationAPIView): if not pin: return Response({"physical": "No pin number."}) + serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) diff --git a/statics/static_dirs/js/pintable.js b/statics/static_dirs/js/pintable.js index 1e7b596..b11d2d6 100644 --- a/statics/static_dirs/js/pintable.js +++ b/statics/static_dirs/js/pintable.js @@ -24,6 +24,22 @@ app.controller('pinTableController', function($scope, $http) { $scope.change_mode = function (physical, mode_code) { var url = "/pins/api/" + physical; $http.post(url, {mode: mode_code}, {headers: {'Content-Type': 'application/json'}}) + .then(function (response) { + if(response.status == 200 && response.data.operation){ + console.log(response.data.pin); + $scope.pinlist[physical-1] = response.data.pin; + $scope.pins = $scope.refresh_list($scope.pinlist); + } + }) + .catch((err) => { + $scope.pins = $scope.refresh_list($scope.pinlist); + }) + }; + + $scope.change_value = function (physical) { + var url = "/pins/api/" + physical; + var new_value = ($scope.pinlist[physical-1].value) ? 0 : 1 + $http.post(url, {value: new_value}, {headers: {'Content-Type': 'application/json'}}) .then(function (response) { if(response.status == 200 && response.data.operation){ $scope.pinlist[physical-1] = response.data.pin; diff --git a/templates/pins/index.html b/templates/pins/index.html index fa2674d..32c2497 100644 --- a/templates/pins/index.html +++ b/templates/pins/index.html @@ -49,6 +49,7 @@ ng-checked="pin[0].value" ng-disabled="pin[0].mode != 0" ng-show="pin[0].value != null" + ng-click="change_value(pin[0].physical)" > {{ pin[0].physical }} @@ -59,6 +60,7 @@ ng-checked="pin[1].value" ng-disabled="pin[1].mode != 0" ng-show="pin[1].value != null" + ng-click="change_value(pin[1].physical)" >