def reverse(t=wheel_pulse/2):
gpio.output(reverse_left, gpio.HIGH)
gpio.output(reverse_right, gpio.HIGH)
sleep(t)
gpio.output(reverse_left, gpio.LOW)
gpio.output(reverse_right, gpio.LOW)
##########################################################################
# cleanup
##########################################################################
python类HIGH的实例源码
testWheels_Individual_4WD.py 文件源码
项目:RaspberryPi-Robot
作者: timestocome
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def reverse_left(t):
gpio.output(15, gpio.HIGH)
gpio.output(36, gpio.HIGH)
sleep(t)
gpio.output(15, gpio.LOW)
gpio.output(36, gpio.LOW)
# test
def _GPIO_Power_Set(pins, on):
if on==1:
GPIO.output( pins, GPIO.HIGH)
else :
GPIO.output( pins, GPIO.LOW )
return
#=====================================
def startwalking():
# Make the buzzer buzz on and off, half a second of
# sound followed by half a second of silence
# GPIO.output(PinRedPedestrian, GPIO.LOW)
# GPIO.output(PinGreenPedestrian, GPIO.HIGH)
iCount = 1
while iCount <= 4:
GPIO.output(PinBuzzer, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(PinBuzzer, GPIO.LOW)
time.sleep(0.5)
iCount += 1
# Turn the buzzer off and wait for 2 seconds
# (If you have a second green 'pedestrian' LED, make it flash on and
# off for the two seconds)
def send_gpio_order(param):
"""
GPIO????
type in(GPIO.IN) out(GPIO.OUT)
value 1 GPIO.HIGH 0 GPIO.LOW
:param param:
:return:
"""
print param
channel, type, value = param
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("????")
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
if type == 'in':
GPIO.setup(channel, GPIO.IN)
GPIO.input(channel, value)
else:
GPIO.setup(channel, GPIO.OUT)
GPIO.output(channel, value)
def start():
last = GPIO.input(button)
while True:
val = GPIO.input(button)
GPIO.wait_for_edge(button, GPIO.FALLING) # we wait for the button to be pressed
GPIO.output(lights[1], GPIO.HIGH)
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, device)
inp.setchannels(1)
inp.setrate(16000)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(500)
audio = ""
while(GPIO.input(button)==0): # we keep recording while the button is pressed
l, data = inp.read()
if l:
audio += data
rf = open(path+'recording.wav', 'w')
rf.write(audio)
rf.close()
inp = None
alexa()
def init(self,bitrate,SDAPIN,SCLPIN):
if(SDAPIN != SCLPIN):
self.SCL = SCLPIN
self.SDA = SDAPIN
else:
print "SDA = GPIO"+str(self.SDA)+" SCL = GPIO"+str(self.SCL)
#configer SCL as output
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(self.SCL, GPIO.OUT)
GPIO.setup(self.SDA, GPIO.OUT)
GPIO.output(self.SDA, GPIO.HIGH)
GPIO.output(self.SCL, GPIO.HIGH)
if bitrate == 100:
self.int_clk = 0.0000025
elif bitrate == 400:
self.int_clk = 0.000000625
elif bitrate == 1000:
self.int_clk = 1
elif bitrate == 3200:
self.int_clk = 1
def Start(self):
#SCL
# ______
# | |______
#SDA
# ___
# | |_________
GPIO.setup(self.SDA, GPIO.OUT) #cnfigure SDA as output
GPIO.output(self.SDA, GPIO.HIGH)
GPIO.output(self.SCL, GPIO.HIGH)
self.tick(1)
GPIO.output(self.SDA, GPIO.LOW)
self.tick(1)
GPIO.output(self.SCL, GPIO.LOW)
self.tick(2)
def ReadAck(self):
GPIO.setup(self.SDA, GPIO.IN)
readbuffer =0
for i in range(8):
GPIO.output(self.SCL, GPIO.HIGH)
self.tick(2)
readbuffer |= (GPIO.input(self.SDA)<< 7) >> i
GPIO.output(self.SCL, GPIO.LOW)
self.tick(2)
GPIO.setup(self.SDA, GPIO.OUT)
GPIO.output(self.SDA, GPIO.LOW)
GPIO.output(self.SCL, GPIO.HIGH)
self.tick(2)
GPIO.output(self.SCL, GPIO.LOW)
GPIO.output(self.SDA, GPIO.LOW)
self.tick(2)
return readbuffer
def ReadNack(self):
GPIO.setup(self.SDA, GPIO.IN)
readbuffer =0
for i in range(8):
GPIO.output(self.SCL, GPIO.HIGH)
self.tick(2)
readbuffer |= (GPIO.input(self.SDA)<< 7) >> i
GPIO.output(self.SCL, GPIO.LOW)
self.tick(2)
GPIO.setup(self.SDA, GPIO.OUT)
GPIO.output(self.SDA, GPIO.HIGH)
GPIO.output(self.SCL, GPIO.HIGH)
self.tick(2)
GPIO.output(self.SCL, GPIO.LOW)
GPIO.output(self.SDA, GPIO.LOW)
self.tick(2)
return readbuffer
def start():
last = GPIO.input(button)
while True:
val = GPIO.input(button)
GPIO.wait_for_edge(button, GPIO.FALLING) # we wait for the button to be pressed
GPIO.output(lights[1], GPIO.HIGH)
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, device)
inp.setchannels(1)
inp.setrate(16000)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(500)
audio = ""
while(GPIO.input(button)==0): # we keep recording while the button is pressed
l, data = inp.read()
if l:
audio += data
rf = open(path+'recording.wav', 'w')
rf.write(audio)
rf.close()
inp = None
alexa()
def _int_to_bcd(value):
# type: (Optional[int]) -> Tuple[bool, bool, bool, bool]
"""Converts an integer to a tuple representing the input bits to a BCD.
If the input value is None, an all high output will be produced. This will
typically make the BCD to turn its corresponding output off.
Args:
value: The value to be converted.
Returns:
tuple of bool corresponding to the BCD representation of the
inputted value.
"""
if value is None:
output = (GPIO.HIGH,) * 4
elif 0 <= value <= 9:
output = tuple(int(digit, 2) for digit in "{:04b}".format(value))
assert len(output) == 4
else:
raise ValueError("Specified input must be either None or between "
"0 and 9. Input was: {!r}.".format(value))
logger.debug("Converted %s to %s", value, output)
return output
def beep(self):
if self.state is False:
cbpi.app.logger.error("BUZZER not working")
return
def play(sound):
try:
for i in sound:
if (isinstance(i, str)):
if i == "H" and self.beep_level == "HIGH":
GPIO.output(int(self.gpio), GPIO.HIGH)
elif i == "H" and self.beep_level != "HIGH":
GPIO.output(int(self.gpio), GPIO.LOW)
elif i == "L" and self.beep_level == "HIGH":
GPIO.output(int(self.gpio), GPIO.LOW)
else:
GPIO.output(int(self.gpio), GPIO.HIGH)
else:
time.sleep(i)
except Exception as e:
pass
start_new_thread(play, (self.sound,))
def on_message(mosq, obj, msg):
"""
Handle incoming messages
"""
if msg.topic == MONITOR_REFRESH:
refresh()
return
topicparts = msg.topic.split("/")
pin = int(topicparts[len(topicparts) - 1])
try:
value = int(float(msg.payload))
except ValueError:
value = 0
if pin not in GPIO_OUTPUT_PINS:
GPIO.setup(pin, GPIO.OUT, initial=GPIO.HIGH)
GPIO_OUTPUT_PINS.append(pin)
if topicparts[2] == 'in':
if value == 1:
GPIO.output(pin, GPIO.LOW)
else:
GPIO.output(pin, GPIO.HIGH)
# End of MQTT callbacks
def read(self):
'''Reads 32 bits of the SPI bus & stores as an integer in self.data.'''
bytesin = 0
# Select the chip
GPIO.output(self.cs_pin, GPIO.LOW)
# Read in 32 bits
for i in range(32):
GPIO.output(self.clock_pin, GPIO.LOW)
bytesin = bytesin << 1
if (GPIO.input(self.data_pin)):
bytesin = bytesin | 1
GPIO.output(self.clock_pin, GPIO.HIGH)
# Unselect the chip
GPIO.output(self.cs_pin, GPIO.HIGH)
# Save data
self.data = bytesin
def read(self):
'''Reads 16 bits of the SPI bus & stores as an integer in self.data.'''
bytesin = 0
# Select the chip
GPIO.output(self.cs_pin, GPIO.LOW)
# Read in 16 bits
for i in range(16):
GPIO.output(self.clock_pin, GPIO.LOW)
time.sleep(0.001)
bytesin = bytesin << 1
if (GPIO.input(self.data_pin)):
bytesin = bytesin | 1
GPIO.output(self.clock_pin, GPIO.HIGH)
time.sleep(0.001)
# Unselect the chip
GPIO.output(self.cs_pin, GPIO.HIGH)
# Save data
self.data = bytesin
def switch_kalliope_mute_led(self, event):
"""
Switch the state of the MUTE LED
:param event: not used
"""
logger.debug("[RpiUtils] Event button caught. Switching mute led")
# get led status
led_mute_kalliope = GPIO.input(self.rpi_settings.pin_led_muted)
# switch state
if led_mute_kalliope == GPIO.HIGH:
logger.debug("[RpiUtils] Switching pin_led_muted to OFF")
self.switch_pin_to_off(self.rpi_settings.pin_led_muted)
self.callback(muted=False)
else:
logger.debug("[RpiUtils] Switching pin_led_muted to ON")
self.switch_pin_to_on(self.rpi_settings.pin_led_muted)
self.callback(muted=True)
def _r_byte(self):
"""
Read byte from the chip.
:return: byte value
:rtype: int
"""
# data pin is now input (pull-down resistor embedded in chip)
GPIO.setup(self._data_pin, GPIO.IN)
# clock the byte from chip
byte = 0
for i in range(8):
# make a high pulse on CLK pin
GPIO.output(self._clk_pin, GPIO.HIGH)
time.sleep(self.CLK_DELAY)
GPIO.output(self._clk_pin, GPIO.LOW)
time.sleep(self.CLK_DELAY)
# chip out data on clk falling edge: store current bit into byte
bit = GPIO.input(self._data_pin)
byte |= ((2 ** i) * bit)
# return byte value
return byte
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)
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 ADC_Read(channel):
value = 0;
for i in range(0,4):
if((channel >> (3 - i)) & 0x01):
GPIO.output(Address,GPIO.HIGH)
else:
GPIO.output(Address,GPIO.LOW)
GPIO.output(Clock,GPIO.HIGH)
GPIO.output(Clock,GPIO.LOW)
for i in range(0,6):
GPIO.output(Clock,GPIO.HIGH)
GPIO.output(Clock,GPIO.LOW)
time.sleep(0.001)
for i in range(0,10):
GPIO.output(Clock,GPIO.HIGH)
value <<= 1
if(GPIO.input(DataOut)):
value |= 0x01
GPIO.output(Clock,GPIO.LOW)
return value
def detect_distance():
# ??????
GPIO.output(TRIG_CHANNEL, GPIO.HIGH)
# ??10us??
time.sleep(0.000015)
GPIO.output(TRIG_CHANNEL, GPIO.LOW)
while GPIO.input(ECHO_CHANNEL) == GPIO.LOW:
pass
# ???????????
t1 = time.time()
while GPIO.input(ECHO_CHANNEL) == GPIO.HIGH:
pass
# ??????????
t2 = time.time()
# ?????????
return (t2-t1)*340/2
def beepOff():
GPIO.output(BeepPin, GPIO.HIGH)
def ledOff():
GPIO.output(LedPin, GPIO.HIGH)
# Event Listener
def __init__(self, name = ''):
self.importlib = GPIO
self.logger = com_logger.Logger(name)
# self.setwarnings(False)
self.IN = GPIO.IN if GPIO is not None else None
self.OUT = GPIO.OUT if GPIO is not None else None
self.LOW = GPIO.LOW if GPIO is not None else None
self.HIGH = GPIO.HIGH if GPIO is not None else None
self.PUD_UP = GPIO.PUD_UP if GPIO is not None else None
self.PUD_DOWN = GPIO.PUD_DOWN if GPIO is not None else None
self.RISING = GPIO.RISING if GPIO is not None else None
def reset_led(self):
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
for gpio in self.gpios:
gpio = int(gpio)
GPIO.setup(gpio, GPIO.OUT)
GPIO.output(gpio, GPIO.HIGH)
def enable_tally(self, enable):
if enable:
GPIO.output(self.gpio_red, GPIO.LOW)
else:
GPIO.output(self.gpio_red, GPIO.HIGH)
def forward(t=wheel_pulse):
gpio.output(forward_right, gpio.HIGH)
gpio.output(forward_left, gpio.HIGH)
sleep(t)
gpio.output(forward_right, gpio.LOW)
gpio.output(forward_left, gpio.LOW)
def turn_left(t=wheel_pulse):
gpio.output(forward_right, gpio.HIGH)
sleep(t)
gpio.output(forward_right, gpio.LOW)
def turn_right(t=wheel_pulse):
gpio.output(forward_left, gpio.HIGH)
sleep(t)
gpio.output(forward_left, gpio.LOW)