summaryrefslogtreecommitdiffstats
path: root/examples/scripts
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2013-12-25 13:25:16 +0100
committermanuel <manuel@mausz.at>2013-12-25 13:25:16 +0100
commit0c8c9ad976879f7c90f9915a60845ccb0cdb337d (patch)
tree162951b4713f3836f4114958a423e2c90ecf9c6b /examples/scripts
downloadwebiopi-0c8c9ad976879f7c90f9915a60845ccb0cdb337d.tar.gz
webiopi-0c8c9ad976879f7c90f9915a60845ccb0cdb337d.tar.bz2
webiopi-0c8c9ad976879f7c90f9915a60845ccb0cdb337d.zip
initial commit
Diffstat (limited to 'examples/scripts')
-rw-r--r--examples/scripts/basic/script.py34
-rw-r--r--examples/scripts/blink/script.py41
-rw-r--r--examples/scripts/macros/index.html120
-rw-r--r--examples/scripts/macros/script.py60
-rw-r--r--examples/scripts/simple/index.html68
5 files changed, 323 insertions, 0 deletions
diff --git a/examples/scripts/basic/script.py b/examples/scripts/basic/script.py
new file mode 100644
index 0000000..5750a69
--- /dev/null
+++ b/examples/scripts/basic/script.py
@@ -0,0 +1,34 @@
1# Imports
2import webiopi
3
4# Enable debug output
5webiopi.setDebug()
6
7# Retrieve GPIO lib
8GPIO = webiopi.GPIO
9SWITCH = 21
10SERVO = 23
11LED0 = 24
12LED1 = 25
13
14# Called by WebIOPi at script loading
15def setup():
16 webiopi.debug("Basic script - Setup")
17 # Setup GPIOs
18 GPIO.setFunction(SWITCH, GPIO.IN)
19 GPIO.setFunction(SERVO, GPIO.PWM)
20 GPIO.setFunction(LED0, GPIO.PWM)
21 GPIO.setFunction(LED1, GPIO.OUT)
22
23 GPIO.pwmWrite(LED0, 0.5) # set to 50% ratio
24 GPIO.pwmWriteAngle(SERVO, 0) # set to 0 (neutral)
25 GPIO.digitalWrite(LED1, GPIO.HIGH)
26
27# Called by WebIOPi at server shutdown
28def destroy():
29 webiopi.debug("Basic script - Destroy")
30 # Reset GPIO functions
31 GPIO.setFunction(SWITCH, GPIO.IN)
32 GPIO.setFunction(SERVO, GPIO.IN)
33 GPIO.setFunction(LED0, GPIO.IN)
34 GPIO.setFunction(LED1, GPIO.IN)
diff --git a/examples/scripts/blink/script.py b/examples/scripts/blink/script.py
new file mode 100644
index 0000000..90c6c8c
--- /dev/null
+++ b/examples/scripts/blink/script.py
@@ -0,0 +1,41 @@
1# Imports
2import webiopi
3
4# Enable debug output
5webiopi.setDebug()
6
7# Retrieve GPIO lib
8GPIO = webiopi.GPIO
9SWITCH = 21
10SERVO = 23
11LED0 = 24
12LED1 = 25
13
14# Called by WebIOPi at script loading
15def setup():
16 webiopi.debug("Blink script - Setup")
17 # Setup GPIOs
18 GPIO.setFunction(SWITCH, GPIO.IN)
19 GPIO.setFunction(SERVO, GPIO.PWM)
20 GPIO.setFunction(LED0, GPIO.PWM)
21 GPIO.setFunction(LED1, GPIO.OUT)
22
23 GPIO.pwmWrite(LED0, 0.5) # set to 50% ratio
24 GPIO.pwmWriteAngle(SERVO, 0) # set to 0 (neutral)
25 GPIO.digitalWrite(LED1, GPIO.HIGH)
26
27# Looped by WebIOPi
28def loop():
29 # Toggle LED each 5 seconds
30 value = not GPIO.digitalRead(LED1)
31 GPIO.digitalWrite(LED1, value)
32 webiopi.sleep(5)
33
34# Called by WebIOPi at server shutdown
35def destroy():
36 webiopi.debug("Blink script - Destroy")
37 # Reset GPIO functions
38 GPIO.setFunction(SWITCH, GPIO.IN)
39 GPIO.setFunction(SERVO, GPIO.IN)
40 GPIO.setFunction(LED0, GPIO.IN)
41 GPIO.setFunction(LED1, GPIO.IN)
diff --git a/examples/scripts/macros/index.html b/examples/scripts/macros/index.html
new file mode 100644
index 0000000..2dc118e
--- /dev/null
+++ b/examples/scripts/macros/index.html
@@ -0,0 +1,120 @@
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />
6 <title>WebIOPi | Demo</title>
7 <script type="text/javascript" src="/webiopi.js"></script>
8 <script type="text/javascript">
9 webiopi().ready(function() {
10 var content, button;
11 content = $("#content");
12
13 // create a "SWITCH" labeled button for GPIO 21
14 button = webiopi().createGPIOButton(21, "SWITCH");
15 content.append(button); // append button to content div
16
17 // create a "LED" labeled button for GPIO 25
18 button = webiopi().createGPIOButton(25, "LED1");
19 content.append(button); // append button to content div
20
21 // create a button that output a single pulse
22 button = webiopi().createPulseButton("pulse", "Pulse", 25);
23 content.append(button); // append button to content div
24
25 // create a button which output a bit sequence on GPIO 25 with a 100ms period
26 button = webiopi().createSequenceButton("sos", "S.O.S 1", 25, 100, "01010100110011001100101010");
27 content.append(button); // append button to content div
28
29 // the previous button will always output the same sequence
30 // you can also create a simple button with your own function
31 button = webiopi().createButton("sos2", "S.O.S 2", outputSequence);
32 content.append(button); // append button to content div
33
34 // create a button which call PrintTime
35 button = webiopi().createMacroButton("macro", "Print Time", "PrintTime");
36 content.append(button); // append button to content div
37
38 // create a button which call HelloWorld with "User,Name" argument
39 button = webiopi().createMacroButton("macro", "Hello ?", "HelloWorld", ["User", "Name"]);
40 content.append(button); // append button to content div
41
42 // the previous button will always call HelloWorld with "User,Name" argument
43 // you can also create a simple button with your own function
44 button = webiopi().createButton("macro2", "Hello !", callMacro);
45 content.append(button); // append button to content div
46
47 // you can also create a button which calls a different function for mouse down and up events
48 button = webiopi().createButton("hold", "Hold", mousedown, mouseup);
49 content.append(button);
50
51 // Only for Chrome and Safari, create a slider that pulse out a -45 to +45° angle on GPIO 23
52 button = webiopi().createAngleSlider(23);
53 content.append(button);
54
55 // Only for Chrome and Safari, create a slider that pulse out a 0-100% duty cycle ratio on GPIO 24
56 button = webiopi().createRatioSlider(24);
57 content.append(button);
58
59 webiopi().refreshGPIO(true);
60 });
61
62 function mousedown() {
63 webiopi().digitalWrite(25, 1);
64 }
65
66 function mouseup() {
67 webiopi().digitalWrite(25, 0);
68 }
69
70 function outputSequence() {
71 var sequence = "01010100110011001100101010" // S.O.S. morse code or whatever you want
72 // output sequence on gpio 25 with a 100ms period
73 webiopi().outputSequence(25, 100, sequence, sequenceCallback);
74 }
75
76 function sequenceCallback(gpio, data) {
77 alert("sequence on " + gpio + " finished with " + data);
78 }
79
80 function callMacro() {
81 var args = ["User","Name"] // or whatever you want
82 // call HelloWorld(args)
83 webiopi().callMacro("HelloWorld", args, macroCallback);
84 }
85
86 function macroCallback(macro, args, data) {
87 alert(data);
88 }
89
90 </script>
91 <style type="text/css">
92 button {
93 display: block;
94 margin: 5px 5px 5px 5px;
95 width: 160px;
96 height: 45px;
97 font-size: 24pt;
98 font-weight: bold;
99 color: black;
100 }
101
102 input[type="range"] {
103 display: block;
104 width: 160px;
105 height: 45px;
106 }
107
108 .LOW {
109 background-color: White;
110 }
111
112 .HIGH {
113 background-color: Red;
114 }
115 </style>
116</head>
117<body>
118 <div id="content" align="center"></div>
119</body>
120</html>
diff --git a/examples/scripts/macros/script.py b/examples/scripts/macros/script.py
new file mode 100644
index 0000000..a469b1a
--- /dev/null
+++ b/examples/scripts/macros/script.py
@@ -0,0 +1,60 @@
1# Imports
2import webiopi
3import time
4
5# Enable debug output
6webiopi.setDebug()
7
8# Retrieve GPIO lib
9GPIO = webiopi.GPIO
10
11SWITCH = 21
12SERVO = 23
13LED0 = 24
14LED1 = 25
15
16# Called by WebIOPi at script loading
17def setup():
18 webiopi.debug("Script with macros - Setup")
19 # Setup GPIOs
20 GPIO.setFunction(SWITCH, GPIO.IN)
21 GPIO.setFunction(SERVO, GPIO.PWM)
22 GPIO.setFunction(LED0, GPIO.PWM)
23 GPIO.setFunction(LED1, GPIO.OUT)
24
25 GPIO.pwmWrite(LED0, 0.5) # set to 50% ratio
26 GPIO.pwmWriteAngle(SERVO, 0) # set to 0 (neutral)
27 GPIO.digitalWrite(LED1, GPIO.HIGH)
28
29 gpio0 = webiopi.deviceInstance("gpio0")
30 gpio0.digitalWrite(0, 0)
31
32# Looped by WebIOPi
33def loop():
34 # Toggle LED each 5 seconds
35 value = not GPIO.digitalRead(LED1)
36 GPIO.digitalWrite(LED1, value)
37 webiopi.sleep(5)
38
39# Called by WebIOPi at server shutdown
40def destroy():
41 webiopi.debug("Script with macros - Destroy")
42 # Reset GPIO functions
43 GPIO.setFunction(SWITCH, GPIO.IN)
44 GPIO.setFunction(SERVO, GPIO.IN)
45 GPIO.setFunction(LED0, GPIO.IN)
46 GPIO.setFunction(LED1, GPIO.IN)
47 gpio0 = webiopi.deviceInstance("gpio0")
48 gpio0.digitalWrite(0, 1)
49
50# A macro which says hello
51@webiopi.macro
52def HelloWorld(first, last):
53 webiopi.debug("HelloWorld(%s, %s)" % (first, last))
54 return "Hello %s %s !" % (first, last)
55
56# A macro without args which return nothing
57@webiopi.macro
58def PrintTime():
59 webiopi.debug("PrintTime: " + time.asctime())
60
diff --git a/examples/scripts/simple/index.html b/examples/scripts/simple/index.html
new file mode 100644
index 0000000..516ecbe
--- /dev/null
+++ b/examples/scripts/simple/index.html
@@ -0,0 +1,68 @@
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />
6 <title>WebIOPi | Demo</title>
7 <script type="text/javascript" src="/webiopi.js"></script>
8 <script type="text/javascript">
9 webiopi().ready(function() {
10 webiopi().setFunction(25, "out");
11
12 var content, button;
13 content = $("#content");
14
15 // create a "LED" labeled button for GPIO 25
16 button = webiopi().createGPIOButton(25, "LED1");
17 content.append(button); // append button to content div
18
19 // create a button that output a single pulse
20 button = webiopi().createPulseButton("pulse", "Pulse", 25);
21 content.append(button); // append button to content div
22
23 // you can also create a button which calls a different function for mouse down and up events
24 button = webiopi().createButton("hold", "Hold", mousedown, mouseup);
25 content.append(button);
26
27 webiopi().refreshGPIO(true);
28 });
29
30 function mousedown() {
31 webiopi().digitalWrite(25, 1);
32 }
33
34 function mouseup() {
35 webiopi().digitalWrite(25, 0);
36 }
37
38 </script>
39 <style type="text/css">
40 button {
41 display: block;
42 margin: 5px 5px 5px 5px;
43 width: 160px;
44 height: 45px;
45 font-size: 24pt;
46 font-weight: bold;
47 color: black;
48 }
49
50 input[type="range"] {
51 display: block;
52 width: 160px;
53 height: 45px;
54 }
55
56 .LOW {
57 background-color: White;
58 }
59
60 .HIGH {
61 background-color: Red;
62 }
63 </style>
64</head>
65<body>
66 <div id="content" align="center"></div>
67</body>
68</html>