-
Notifications
You must be signed in to change notification settings - Fork 4
/
RPi_GPIO_Output.py
executable file
·69 lines (51 loc) · 1.79 KB
/
RPi_GPIO_Output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python
#/***********************************************/
#/* Raspberry Pi GPIO Output Example in Python. */
#/* 2016-07-31 - Version 1.00 - Jason Birch. */
#/***********************************************/
import sys
import time
import datetime
import threading
import RPi.GPIO
#/****************************/
#/* USER DEFINABLE CONSTANTS */
#/****************************/
GPIO_OUTPUT_PIN = 14
# /*******************************************/
# /* Configure Raspberry Pi GPIO interfaces. */
#/*******************************************/
def InitGPIO():
RPi.GPIO.setmode(RPi.GPIO.BCM)
RPi.GPIO.setup(GPIO_OUTPUT_PIN, RPi.GPIO.OUT, initial=0)
# /****************************************/
# /* Procedure to execute every 1 second. */
#/****************************************/
def OutputTimer():
global OutValue
global TimerThread
OutValue = OutValue ^ 1
RPi.GPIO.output(GPIO_OUTPUT_PIN, OutValue)
Now = datetime.datetime.now()
print(Now.strftime("%Y-%m-%d %H:%M:%S") + " - GPIO: " + format(GPIO_OUTPUT_PIN, "00d") + " = " + format(OutValue, "0d"))
TimerThread = threading.Timer(1, OutputTimer)
TimerThread.start()
# /**************************/
# /* Main application loop. */
#/**************************/
Key = 0
OutValue = 0
TimerThread = None
InitGPIO()
OutputTimer()
while Key == 0:
time.sleep(1)
Key = sys.stdin.read(1)
# /*************************************************************************/
# /* Stop the timer so GPIO is not used by mistake after it is cleaned up. */
#/*************************************************************************/
TimerThread.cancel()
# /*************************************************/
# /* Close Raspberry Pi GPIO use before finishing. */
#/*************************************************/
RPi.GPIO.cleanup()