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