def _w_byte(self, byte):
"""
Write byte to the chip.
:param byte: byte value
:type byte: int
"""
# data pin is now output
GPIO.setup(self._data_pin, GPIO.OUT)
# clock the byte to chip
for _ in range(8):
GPIO.output(self._clk_pin, GPIO.LOW)
time.sleep(self.CLK_DELAY)
# chip read data on clk rising edge
GPIO.output(self._data_pin, byte & 0x01)
byte >>= 1
GPIO.output(self._clk_pin, GPIO.HIGH)
time.sleep(self.CLK_DELAY)
python类setup()的实例源码
def __init__(self, dev='/dev/spidev0.0', speed=1000000, pin_rst=22, pin_ce=0):
self.pin_rst = pin_rst
self.pin_ce = pin_ce
SPI.openSPI(device=dev, speed=speed)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_rst, GPIO.OUT)
GPIO.output(pin_rst, 1)
if pin_ce != 0:
GPIO.setup(pin_ce, GPIO.OUT)
GPIO.output(pin_ce, 1)
self.reset()
self.dev_write(0x2A, 0x8D)
self.dev_write(0x2B, 0x3E)
self.dev_write(0x2D, 30)
self.dev_write(0x2C, 0)
self.dev_write(0x15, 0x40)
self.dev_write(0x11, 0x3D)
self.set_antenna(True)
def read_soil_moist_digital(gpio_pin):
reading = False
soil_value = None
count = 0
while reading == False:
GPIO.setup(gpio_pin, GPIO.IN)
try:
soil_value = GPIO.input(gpio_pin)
except Exception as e:
print("!!! couldn't read sensor, error " + str(e))
time.sleep(1)
if not soil_value == None:
print(" The sensor " + str(gpio_pin) + " returned a value of; " + str(soil_value))
return soil_value
count = count + 1
if count >= 10:
print("Sensor failed to read ten times, giving up...")
return 'none'
def setPALevel(self, level):
setup = self.read_register(NRF24.RF_SETUP)
setup &= ~( _BV(NRF24.RF_PWR_LOW) | _BV(NRF24.RF_PWR_HIGH))
# switch uses RAM (evil!)
if level == NRF24.PA_MAX:
setup |= (_BV(NRF24.RF_PWR_LOW) | _BV(NRF24.RF_PWR_HIGH))
elif level == NRF24.PA_HIGH:
setup |= _BV(NRF24.RF_PWR_HIGH)
elif level == NRF24.PA_LOW:
setup |= _BV(NRF24.RF_PWR_LOW)
elif level == NRF24.PA_MIN:
nop = 0
elif level == NRF24.PA_ERROR:
# On error, go to maximum PA
setup |= (_BV(NRF24.RF_PWR_LOW) | _BV(NRF24.RF_PWR_HIGH))
self.write_register(NRF24.RF_SETUP, setup)
def turn_on(gpio_num, freq, dc):
global g_led_pwm
# print('turn_on: %d, %d, %d' % (gpio_num, freq, dc))
if not g_led_pwm.has_key(gpio_num):
g_led_pwm[gpio_num] = {}
GPIO.setup(gpio_num, GPIO.OUT)
g_led_pwm[gpio_num]['obj'] = GPIO.PWM(gpio_num, freq)
g_led_pwm[gpio_num]['obj'].start(0)
g_led_pwm[gpio_num]['obj'].ChangeFrequency(freq)
g_led_pwm[gpio_num]['obj'].ChangeDutyCycle(dc)
g_led_pwm[gpio_num]['freq'] = freq
g_led_pwm[gpio_num]['dc'] = dc
g_led_pwm[gpio_num]['status'] = 'on'
status_notify()
def __init__(self,pinA,pinB,callback):
self.pinA = pinA
self.pinB = pinB
#self.button = button
self.callback = callback
GPIO.setmode(GPIO.BCM)
# The following lines enable the internal pull-up resistors
# on version 2 (latest) boards
GPIO.setwarnings(False)
GPIO.setup(self.pinA, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.pinB, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(self.button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# For version 1 (old) boards comment out the above four lines
# and un-comment the following 3 lines
#GPIO.setup(self.pinA, GPIO.IN)
#GPIO.setup(self.pinB, GPIO.IN)
#GPIO.setup(self.button, GPIO.IN)
# Add event detection to the GPIO inputs
GPIO.add_event_detect(self.pinA, GPIO.FALLING, callback=self.switch_event)
GPIO.add_event_detect(self.pinB, GPIO.FALLING, callback=self.switch_event)
#GPIO.add_event_detect(self.button, GPIO.BOTH, callback=self.button_event, bouncetime=200)
return
# Call back routine called by switch events
def RCtime (PiPin):
measurement = 0
# Discharge capacitor
GPIO.setup(PiPin, GPIO.OUT)
GPIO.output(PiPin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(PiPin, GPIO.IN)
# Count loops until voltage across
# capacitor reads high on GPIO
start = time.time()
while (GPIO.input(PiPin) == GPIO.LOW):
measurement += 1
end = time.time()
# print end - start
# return measurement
return str(end - start)
# Connects the socket
def RCtime (PiPin):
measurement = 0
# Discharge capacitor
GPIO.setup(PiPin, GPIO.OUT)
GPIO.output(PiPin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(PiPin, GPIO.IN)
# Count loops until voltage across
# capacitor reads high on GPIO
start = time.time()
while (GPIO.input(PiPin) == GPIO.LOW):
measurement += 1
end = time.time()
# print end - start
# return measurement
return str(end - start)
# Main program loop
def ResetIOT_HW(cls, bMode):
"""Set Raspberry pi GPIO pins and reset RF Explorer device
Parameters:
bMode -- True if the baudrate is set to 500000bps, False to 2400bps
"""
try:
import RPi.GPIO as GPIO
#print("RPi info: " + str(GPIO.RPI_INFO)) #information about your RPi:
#print("RPi.GPio version: " + GPIO.VERSION) #version of RPi.GPIO:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) #refer to the pin numbers on the P1 header of the Raspberry Pi board
GPIO.setup(12, GPIO.OUT) #set /reset (pin 12) to output
GPIO.output(12, False) #set /reset (pin 12) to LOW
GPIO.setup(21, GPIO.OUT) #set GPIO2 (pin 21) to output
GPIO.output(21, bMode) #set GPIO2 (pin 21) to HIGH (for 500Kbps)
time.sleep(0.1) #wait 100ms
GPIO.output(12, True) #set /reset to HIGH
time.sleep(2.5) #wait 2.5sec
GPIO.setup(21, GPIO.IN) #set GPIO2 to input
GPIO.cleanup() #clean up GPIO channels
except RuntimeError:
print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")
def light_sense():
count = 0
#Output on the pin for
GPIO.setup(pin_to_circuit, GPIO.OUT)
GPIO.output(pin_to_circuit, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_to_circuit, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_to_circuit) == GPIO.LOW):
count += 1
if (count > 3000):
led5_on()
return count
else:
led5_off()
return count
def _setup_pin(self, pin):
self._logger.debug(u"_setup_pin(%s)" % (pin,))
if pin:
p = None
if self._pigpiod is None:
self._pigpiod = pigpio.pi()
if self._settings.get_boolean(['pigpiod']):
if not self._pigpiod.connected:
self._logger.error(u"Unable to communicate with PiGPIOd")
else:
p = PiGPIOpin(self._pigpiod, pin, self._logger)
else:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
p = GPIO.PWM(pin, 100)
p.start(100)
return p
def __init__(self, direction_channel, pwm=None, offset=True):
'''Init a motor on giving dir. channel and PWM channel.'''
if self._DEBUG:
print self._DEBUG_INFO, "Debug on"
self.direction_channel = direction_channel
self._pwm = pwm
self._offset = offset
self.forward_offset = self._offset
self.backward_offset = not self.forward_offset
self._speed = 0
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
if self._DEBUG:
print self._DEBUG_INFO, 'setup motor direction channel at', direction_channel
print self._DEBUG_INFO, 'setup motor pwm channel as', self._pwm.__name__
GPIO.setup(self.direction_channel, GPIO.OUT)
def __init__(self, no, dir=IN):
self.gpio_no = no
print("GPIO ########## self.gpio_no, dir={}, {}".format(self.gpio_no, dir))
GPIO.setmode(GPIO.BCM)
#GPIO.setmode(GPIO.BOARD)
if dir==Pin.IN:
GPIO.setup(self.gpio_no, GPIO.IN)
print("GPIO >>>>>>>>>>>>>>>>>>>>>>>>>>> IN")
else:
GPIO.setup(self.gpio_no, GPIO.OUT)
print("GPIO <<<<<<<<<<<<<<<<<<<<<<<<<< OUT")
def setup_GPIO():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
GPIO.setup(35, GPIO.IN)
def setup_GPIO():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
def switchOnLight(PIN):
GPIO.setup(PIN, GPIO.OUT)
GPIO.output(PIN, True)
def switchOffLight(PIN):
GPIO.setup(PIN, GPIO.OUT)
GPIO.output(PIN, False)
def startup(self): # funciton that starts th GPIO board and pin required
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.channel, GPIO.OUT)
self.pwm = GPIO.PWM(self.channel, self.frequency)
self.pwm.start(self.frequency)
def setupio(self, io_number, mode):
if self.importlib is not None:
GPIO.setup(io_number, mode)
def pull(self, io_number):
if self.importlib is not None:
# self.logger.debug('pull')
# Afin d'éviter de laisser flottante toute entrée, il est possible de connecter des résistances de pull-up ou de pull-down, au choix, en interne.
# Pour information, une résistance de pull-up ou de pull-down a pour but d'éviter de laisser une entrée ou une sortie dans un état incertain, en
# forçant une connexion à la # masse ou à un potentiel donné.
GPIO.setup(io_number, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(io_number, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)