Skip to content

Commit

Permalink
Added support for interrupts from JS
Browse files Browse the repository at this point in the history
  • Loading branch information
Uros Petrevski committed Mar 16, 2015
1 parent bded96b commit 6c310db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
19 changes: 10 additions & 9 deletions weioLib/weioParser.py
Expand Up @@ -48,6 +48,8 @@
###

from weioLib.weioIO import *
from weioUserApi import serverPush

from weioLib import weioRunnerGlobals
import platform, sys

Expand Down Expand Up @@ -160,7 +162,8 @@ def callProportion(data) :

def callAttachInterrupt(data) :
if (weioRunnerGlobals.WEIO_SERIAL_LINKED is True):
attachInterrupt(data[0], data[1], data[2], data[3])
iObj = {"pin" : data[0], "jsCallbackString" : data[2]}
attachInterrupt(data[0], data[1], genericInterrupt, iObj)
else:
print "attachInterrupt ON PC", data
return None
Expand All @@ -172,6 +175,12 @@ def callDetachInterrupt(data) :
print "detachInterrupt ON PC", data
return None

def genericInterrupt(event, obj):
bck = {}
bck["data"] = obj["pin"]
bck["eventType"] = getInterruptType(event["type"])
serverPush(obj["jsCallbackString"], bck)

def callDelay(data) :
if (weioRunnerGlobals.WEIO_SERIAL_LINKED is True):
delay(data[0])
Expand Down Expand Up @@ -228,14 +237,6 @@ def callGetTemperature(data):
bck["data"] = 0 # faked value
return bck

def genericInterrupt(data):
#type = data["type"]
#data = {}
#data["requested"] = 'analogRead'
#data["data"] = value
#self.write_message(json.dumps(data))
pass

def callUserMesage(data):
print "USER TALKS", data
#weioRunnerGlobals.userMain
Expand Down
41 changes: 19 additions & 22 deletions www/libs/weio/weioApi.js
Expand Up @@ -58,8 +58,7 @@ var _weio = null;
* Each key is binded to coresponding function
*/
var weioCallbacks = {
"inbox":callInbox,
"attachInterrupt":weioExecuteInterrupt
"inbox":callInbox
};

/**
Expand Down Expand Up @@ -139,18 +138,19 @@ $(document).ready(function() {
data = JSON.parse(e.data);
console.log(data);

if ("requested" in data) {
if ("requested" in data) {
// this is instruction that was echoed from server + data as response
instruction = data.requested;
if (instruction in weioCallbacks)
weioCallbacks[instruction](data);
} else if ("serverPush" in data) {
// this is instruction that was echoed from server + data as response
instruction = data.requested;
if (instruction in weioCallbacks)
weioCallbacks[instruction](data);
} else if ("serverPush" in data) {
// this is instruction that was echoed from server + data as response

instruction = data.serverPush;
if (instruction in weioCallbacks)
weioCallbacks[instruction](data.data);

instruction = data.serverPush;
if (instruction in weioCallbacks) {
weioCallbacks[instruction](data.data);

}

}
};

Expand Down Expand Up @@ -317,14 +317,14 @@ function noTone(pin) {
function constrain(x, a, b) {
if(x > a){
if(x < b){
return a
return a;
}
}
if(x < a){
return a
return a;
}
if(x > b){
return b
return b;
}
};

Expand Down Expand Up @@ -362,14 +362,11 @@ function callInbox(data) {
}
};

function attachInterrupt(pin, mode, callback, obj) {
function attachInterrupt(pin, mode, callback) {
var fName = callback.name;
weioCallbacks[fName] = callback;
weioInterrupts[pin] = fName;
genericMessage("attachInterrupt", [pin, mode,fName,obj], null);
}

function weioExecuteInterrupt(data) {
console.log("INTERRUPR",data);
genericMessage("attachInterrupt", [pin, mode,fName], null);
}

function detachInterrupt(pin) {
Expand Down

0 comments on commit 6c310db

Please sign in to comment.