def reset_radio(self):
"""
Reset the Si4707 chip
"""
# Ref https://github.com/AIWIndustries/Pi_4707/blob/master/firmware/NWRSAME_v2.py
if self.gpio_started:
gpio.cleanup()
self.gpio_started = True
gpio.setmode(gpio.BCM) # Use board pin numbering
gpio.setup(17, gpio.OUT) # Setup the reset pin
gpio.output(17, gpio.LOW) # Reset the Si4707.
sleep(0.4)
gpio.output(17, gpio.HIGH)
gpio.setup(23, gpio.IN, pull_up_down=gpio.PUD_UP)
gpio.add_event_detect(23, gpio.FALLING)
# Initialize the onboard relays
for pin in self.relay_gpio_pins:
gpio.setup(pin, gpio.OUT) # setup gpio pin for relay
gpio.output(pin, gpio.LOW) # boot to disabled state
# set up the LED
# https://www.reddit.com/r/raspberry_pi/comments/3641ug/blinking_an_onboard_led_on_the_pi_2_model_b/
# http://raspberrypi.stackexchange.com/questions/697/how-do-i-control-the-system-leds-using-my-
# sudo echo none >/sys/class/leds/led0/trigger
# GPIO 16 LOW is on, HIGH is off
gpio.setup(16, gpio.OUT)
gpio.output(16, gpio.HIGH)
sleep(1.5)
python类PUD_UP的实例源码
def __init__(self):
GPIO.setmode(GPIO.BCM)
#Setup each hall effect pin for interrupts
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print ("Encoders Ready")
GPIO.add_event_detect(18, GPIO.FALLING, callback=self.add_right_pulse)
GPIO.add_event_detect(24, GPIO.FALLING, callback=self.add_left_pulse)
while 1:
pass
# Callback function for Hall Sensor interrupts
def __init__(self, gpio_pin):
self.gpio_pin = gpio_pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def __init__(self):
self.button_callback = None
self.button_held_callback = None
self.reset_callback = None
self.pressed_at = None
self.time_to_hold = 1
self.time_to_reset = 5
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(18, GPIO.BOTH, callback=self.button_state_changed,bouncetime=200)
def __init__(self, index, gpio_pin):
self.pressed = False
self._on_press_handler = None
self._on_release_handler = None
self._gpio_pin = gpio_pin
self._index = index
GPIO.setup(self._gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(self._gpio_pin, GPIO.BOTH, bouncetime=1, callback=self._handle_button)
def main():
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(LAMP_SX, GPIO.OUT)
GPIO.setup(LAMP_DX, GPIO.OUT)
GPIO.output(LAMP_SX, 0)
GPIO.output(LAMP_DX, 0)
GPIO.setup(INT_SX, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(INT_DX, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(INT_SX, GPIO.BOTH, callback=handler_int_sx, bouncetime=100)
GPIO.add_event_detect(INT_DX, GPIO.BOTH, callback=handler_int_dx, bouncetime=100)
try:
#Entro in un loop infinito dove conto i secondi che passano
while True:
time.sleep(1)
except KeyboardInterrupt:
print "Exit"
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print strftime('%d.%m.%Y: ', gmtime()) + 'rev counter started with an interval of ' + str(interval) + ' second(s)'
def __init__(self, pin):
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.BOTH, callback=self._gpio_event)
self._pin = pin
self._pressed = False
self._was_pressed = False
self._was_released = False
def initGpio(self):
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(23, GPIO.RISING, callback=self.tickFlow1, bouncetime=20)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(24, GPIO.RISING, callback=self.tickFlow2, bouncetime=20)
self.fm1 = FlowMeter('metric', ["beer"])
self.fm1.enabled = True
self.fm2 = FlowMeter('metric', ["beer"])
self.fm2.enabled = True
def __init__(self, clockPin, dataPin, switchPin, rotaryCallback, switchCallback):
#persist values
self.clockPin = clockPin
self.dataPin = dataPin
self.switchPin = switchPin
self.rotaryCallback = rotaryCallback
self.switchCallback = switchCallback
#setup pins
GPIO.setup(clockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(dataPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def __init__(self):
# Set the board.
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Setup outputs.
for pin in self.__output_pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, False)
# Setup inputs.
for pin in self.__input_pins:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Initialize the timers.
self.init_c1()
self.init_c2()
self.__mcp = Adafruit_MCP3008.MCP3008(clk=self.__CLK, cs=self.__CS, miso=self.__MISO, mosi=self.__MOSI)
self.__output_len = len(self.__output_pins)
self.__input_len = len(self.__input_pins)
## Destructor
# @param self The object pointer.
def Setup():
global logger
global p_R
global p_G
global p_B
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(Pins["FLOATSW_HIGH_WL"], GPIO.IN, pull_up_down=GPIO.PUD_UP, initial = GPIO.HIGH)
GPIO.setup(Pins["FLOATSW_LOW_WL"], GPIO.IN, pull_up_down=GPIO.PUD_UP, initial = GPIO.HIGH)
GPIO.setup(Pins["FLOATSW_LOW_AWC_WL"], GPIO.IN, pull_up_down=GPIO.PUD_UP, initial = GPIO.HIGH)
GPIO.setup(Pins["WATER_LEAK_DETECTOR_1"], GPIO.IN)
GPIO.setup(Pins["WATER_LEAK_DETECTOR_2"], GPIO.IN)
GPIO.setup(Pins["WATER_VALVE"], GPIO.OUT)
GPIO.setup(Pins["AWC_PUMP_IN"], GPIO.OUT)
GPIO.setup(Pins["AWC_PUMP_OUT"], GPIO.OUT)
GPIO.setup(Pins["LED_PIN_R"], GPIO.OUT, initial = GPIO.HIGH) #high = leds off
GPIO.setup(Pins["LED_PIN_G"], GPIO.OUT, initial = GPIO.HIGH)
GPIO.setup(Pins["LED_PIN_B"], GPIO.OUT, initial = GPIO.HIGH)
p_R = GPIO.PWM(Pins["LED_PIN_R"], 2000)
p_G = GPIO.PWM(Pins["LED_PIN_G"], 2000)
p_B = GPIO.PWM(Pins["LED_PIN_B"], 2000)
p_R.start(0)
p_G.start(0)
p_B.start(0)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.FileHandler('/var/log/aquamonitor.log')
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(message)s',"%Y-%m-%d %H:%M:%S")
handler.setFormatter(formatter)
logger.addHandler(handler)
if not sys.stdout.isatty(): # if we are not running from console, redirect the stdout & err to files
sys.stdout = open('/var/log/aquamonitor_stdout.log', 'a')
sys.stderr = open('/var/log/aquamonitor_stderr.log','a')
02_buttonControlLed.py 文件源码
项目:SunFounder_Super_Kit_V3.0_for_Raspberry_Pi
作者: sunfounder
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def setup():
# Set the GPIO modes to BCM Numbering
GPIO.setmode(GPIO.BCM)
# Set LedPin's mode to output,
# and initial level to high (3.3v)
GPIO.setup(LedPin, GPIO.OUT, initial=GPIO.HIGH)
# Set BtnPin's mode to input,
# and pull up to high (3.3V)
GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Set up a falling detect on BtnPin,
# and callback function to swLed
GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=swLed)
# Define a callback function for button callback
14_dice.py 文件源码
项目:SunFounder_Super_Kit_V3.0_for_Raspberry_Pi
作者: sunfounder
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(SDI, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(RCLK, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(SRCLK, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(TouchPin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(TouchPin, GPIO.RISING, callback = randomISR, bouncetime = 20)
# Shift the data to 74HC595
09_ne555.py 文件源码
项目:SunFounder_Super_Kit_V3.0_for_Raspberry_Pi
作者: sunfounder
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def setup():
GPIO.setmode(GPIO.BCM) # Numbers GPIOs by physical location
GPIO.setup(SigPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set Pin's mode is input, and pull up to high level(3.3V)
GPIO.add_event_detect(SigPin, GPIO.RISING, callback=count) # wait for rasing
def main(switch=18, green=8, red=7):
GPIO.setmode(GPIO.BCM)
#GPIO.setup(switch, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch, GPIO.IN)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(red, GPIO.OUT)
queue = Queue()
GPIO.add_event_detect(switch, GPIO.BOTH, callback=partial(interrupt_Event, queue), bouncetime=150)
start=False
try:
while True:
sleep(0.1)
if not queue.empty():
if not start:
start = queue.get()
GPIO.output(green, False)
GPIO.output(red, True)
else:
end = queue.get()
print_duration(start, end)
GPIO.output(green, True)
GPIO.output(red, False)
start=False
except (KeyboardInterrupt, SystemExit):
GPIO.cleanup()
print("\nQuit\n")
def main(switch=18, green=8, red=7):
GPIO.setmode(GPIO.BCM)
#GPIO.setup(switch, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch, GPIO.IN)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(red, GPIO.OUT)
queue = Queue()
GPIO.add_event_detect(switch, GPIO.BOTH, callback=partial(interrupt_Event, queue), bouncetime=150)
running=True
start=datetime.now()
end=datetime.now()
try:
while running:
sleep(0.1)
if not queue.empty():
pin, state = queue.get()
if state == GPIO.LOW:
start = datetime.now()
GPIO.output(green, False)
GPIO.output(red, True)
elif state == GPIO.HIGH:
print_duration(start, datetime.now())
GPIO.output(green, True)
GPIO.output(red, False)
except (KeyboardInterrupt, SystemExit):
running=False
GPIO.cleanup()
print("\nQuit\n")
def __init__(self):
''' initialize the pin for the anemometer sensor '''
self.SENSOR_PIN = 4
self.count = 0
# tell the GPIO module that we want to use the chip's pin numbering scheme
GPIO.setmode(GPIO.BCM)
# setup pin as an input with pullup
GPIO.setup(self.SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# threaded event, to detect voltage falling on anemometer
# bouncetime is in ms - edges within this time will be ignored
GPIO.add_event_detect(self.SENSOR_PIN, GPIO.FALLING, bouncetime=30)
self.starttime = time.time()
# deal with events by calling a function
GPIO.add_event_callback(self.SENSOR_PIN, self.inputEventHandler)
def main():
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.BOTH, callback = _hook_switch_changed, bouncetime = 200)
while True:
time.sleep(100)
def main():
GPIO.setmode(GPIO.BCM)
GPIO.setup(reset_switch, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(hook_switch, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(reset_switch, GPIO.BOTH, callback = _io_pulsed, bouncetime = 200)
GPIO.add_event_detect(hook_switch, GPIO.BOTH, callback = _io_pulsed, bouncetime = 200)
while True:
time.sleep(1000)