summaryrefslogtreecommitdiffstats
path: root/examples/scripts/macros
diff options
context:
space:
mode:
Diffstat (limited to 'examples/scripts/macros')
-rw-r--r--examples/scripts/macros/index.html120
-rw-r--r--examples/scripts/macros/script.py60
2 files changed, 180 insertions, 0 deletions
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