diff options
Diffstat (limited to 'examples/scripts/macros/script.py')
| -rw-r--r-- | examples/scripts/macros/script.py | 60 |
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 | ||
| 2 | import webiopi | ||
| 3 | import time | ||
| 4 | |||
| 5 | # Enable debug output | ||
| 6 | webiopi.setDebug() | ||
| 7 | |||
| 8 | # Retrieve GPIO lib | ||
| 9 | GPIO = webiopi.GPIO | ||
| 10 | |||
| 11 | SWITCH = 21 | ||
| 12 | SERVO = 23 | ||
| 13 | LED0 = 24 | ||
| 14 | LED1 = 25 | ||
| 15 | |||
| 16 | # Called by WebIOPi at script loading | ||
| 17 | def 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 | ||
| 33 | def 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 | ||
| 40 | def 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 | ||
| 52 | def 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 | ||
| 58 | def PrintTime(): | ||
| 59 | webiopi.debug("PrintTime: " + time.asctime()) | ||
| 60 | |||
