Skip to content

Commit 6759a15

Browse files
author
Uros Petrevski
committed
P5 js library added with first example
1 parent 4227daa commit 6759a15

File tree

12 files changed

+11364
-57
lines changed

12 files changed

+11364
-57
lines changed

examples/p5/__init__.py

Whitespace-only changes.

examples/p5/driveLED/__init__.py

Whitespace-only changes.

examples/p5/driveLED/index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head >
5+
6+
<script src="www/libs/weio/weioApi.js"></script>
7+
<script src="www/libs/p5/p5.min.js"></script>
8+
9+
<script src="processing.js"></script>
10+
11+
<title>My first Web app</title>
12+
13+
<meta name="viewport" content="user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1,width=device-width">
14+
15+
</head>
16+
17+
<body>
18+
19+
<script>
20+
21+
function onWeioReady() {
22+
console.log("DOM is loaded, websocket is opened");
23+
}
24+
25+
</script>
26+
</body>
27+
</html>

examples/p5/driveLED/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from weioLib.weio import *
2+
3+
def setup():
4+
attach.process(myProcess)
5+
attach.event("getIn", fromWeb)
6+
7+
def fromWeb(data):
8+
print data
9+
10+
def myProcess():
11+
print("Hello world")

examples/p5/driveLED/processing.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var cnt = 0;
2+
var state = true;
3+
4+
function setup() {
5+
// canvas size in pixels
6+
createCanvas(800, 600);
7+
// always put some frame rate, don't make suffer cpu or websockets
8+
frameRate(12);
9+
}
10+
11+
function draw() {
12+
13+
background(0);
14+
fill(255);
15+
ellipse(width/2, height/2, cnt, cnt);
16+
17+
18+
if (state===true) {
19+
if (cnt<100) cnt+=5; else state = false;
20+
} else {
21+
if (cnt>0) cnt-=5; else state = true;
22+
}
23+
24+
// as LEDs has common V+ it's necessary to invert signal to get good colors
25+
// clors are expressed in percents, floating point is permited
26+
pwmWrite(18,100-cnt);
27+
pwmWrite(19,100-cnt);
28+
pwmWrite(20,100-cnt);
29+
30+
}

examples/p5/driveLED/www

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Users/ukicar/workNow/nodesign/WeIO/weio/weio/www/

handlers/weioJSPYHandler.py

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from weioLib.weioIO import *
5252
import json
5353

54-
from sockjs.tornado import SockJSConnection
54+
from tornado import websocket
5555

5656
from weioLib.weioParser import weioSpells
5757
from weioLib.weioParser import weioUserSpells
@@ -61,47 +61,36 @@
6161
import os
6262

6363

64-
class WeioHandler(SockJSConnection):
65-
def __init__(self, *args, **kwargs):
66-
SockJSConnection.__init__(self, *args, **kwargs)
67-
#self.connections = weioRunnerGlobals.weioConnections
64+
#class WeioHandler(SockJSConnection):
65+
class WeioHandler(websocket.WebSocketHandler):
6866

69-
def on_open(self, request):
67+
def check_origin(self, origin):
68+
return True
69+
70+
def open(self):
7071
# Add the connection to the connections dictionary
7172
# Do not forget the lock - this handler is called asynchronoisly,
7273
# and the weioRunnerGlobals.weioConnections[] is accessed in the same time by
7374
# weioRunner.py. This can lead to iteritems() in weioRunner while dictionary is modified,
7475
# which will lead to exception in weioRunner.py listener_thread()
7576
with weioRunnerGlobals.lockConn:
7677
connUuid = uuid.uuid4()
78+
self.uuid = connUuid
7779
weioRunnerGlobals.weioConnections[connUuid] = self
7880
weioRunnerGlobals.weioConnUuids.append(connUuid)
7981

8082
# collect client ip address and user machine info
8183
# print self.request to see all available info on user connection
8284
self.userAgent = None
83-
self.ip = request.ip
84-
85-
print "*SYSOUT* Client with IP address: " + self.ip + " connected to server"
86-
87-
#print "============="
88-
#print self.session.conn_info.get_header('x-client-ip')
89-
#print self.session.conn_info.ip
90-
#print self.session.conn_info.path
91-
#print self.session.conn_info.arguments
92-
93-
#print "++++++++++++++"
94-
#print request.headers['User-Agent']
95-
96-
# list all members of object
97-
#members = [attr for attr in dir(request) if not callable(attr) and not attr.startswith("__")]
98-
99-
#if ("User-Agent" in self.request.headers):
100-
# self.userAgent = self.request.headers["User-Agent"]
101-
# print "*SYSOUT* " + self.userAgent + " with IP address : " + self.ip + " connected to server!"
102-
#else :
103-
# print "*SYSOUT* Client with IP address : " + self.ip + " connected to server!"
104-
#print self.request.headers
85+
#self.ip = request.ip
86+
87+
#print "*SYSOUT* Client with IP address: " + self.ip + " connected to server"
88+
89+
try :
90+
print "*SYSOUT* Client with IP address: " + self.request.host + " connected to server with: " + self.request.headers["User-Agent"]
91+
except:
92+
print "*SYSOUT* Client with unknown IP address connected to server"
93+
10594
self.connection_closed = False
10695

10796
def emit(self, instruction, rq):
@@ -142,15 +131,18 @@ def serve(self, data) :
142131
def on_close(self):
143132
with weioRunnerGlobals.lockConn:
144133
self.connection_closed = True
145-
print "*SYSOUT* Client with IP address: " + self.ip + " disconnected from server"
134+
try :
135+
print "*SYSOUT* Client with IP address: " + self.request.host + " disconnected from server with: " + self.request.headers["User-Agent"]
136+
except:
137+
print "*SYSOUT* Client with unknown IP address disconnected from server"
138+
146139
# Remove client from the clients list and broadcast leave message
147-
#self.connections.remove(self)
148-
for connUuid, conn in weioRunnerGlobals.weioConnections.iteritems():
149-
if (conn == self):
150-
weioRunnerGlobals.weioConnections.pop(connUuid)
151-
weioRunnerGlobals.weioConnUuids.remove(connUuid)
152-
153-
140+
141+
if (self.uuid in weioRunnerGlobals.weioConnections):
142+
weioRunnerGlobals.weioConnections.pop(self.uuid)
143+
if (self.uuid in weioRunnerGlobals.weioConnUuids):
144+
weioRunnerGlobals.weioConnUuids.remove(self.uuid)
145+
154146
###
155147
# This is used for remote apps - Tornado User opens __client__ socket
156148
# and puths everything that comes from this socket to WeioHandlerRemote()

weioRunner.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,11 @@ def get(self):
108108
if (firstTimeSwitch=="YES") :
109109
path = "www/signin.html"
110110
else :
111+
# find index and launch
111112
path = "www/userIndex.html"
112113

113-
#if (weioFiles.checkIfFileExists(confFile['last_opened_project'] + "/index.html")):
114-
# path = "www/userIndex.html"
115-
116-
#else :
117-
# path = "www/error404.html"
118-
#path = confFile['last_opened_project'] + "index.html"
119-
120114
self.render(path, error="")
121115

122-
123116
###
124117
# WeIO User Event Handler
125118
###
@@ -432,11 +425,12 @@ def close(self):
432425

433426
options.define("addr", default=confFile['ip'])
434427

435-
apiRouter = SockJSRouter(WeioHandler, '/api')
428+
#apiRouter = SockJSRouter(WeioHandler, '/api')
436429

437430
# Instantiate all handlers for user Tornado
438-
app = web.Application(apiRouter.urls + [
439-
('/', WeioIndexHandler),
431+
app = web.Application([
432+
(r'/', WeioIndexHandler),
433+
(r'/api', WeioHandler),
440434
(r"/(.*)", web.StaticFileHandler, {"path": "www"})
441435
])
442436
#app.listen(options.options.port, "0.0.0.0")

0 commit comments

Comments
 (0)