Example #1
0
 def _set_state(self, value):
     if self._pwm:
         freq, duty = self._pwm
         self._pwm = (freq, min(100, max(0, int(value * 100))))
         lgpio.tx_pwm(self.factory._handle, self.number, *self._pwm)
     elif self.function == 'input':
         raise PinSetInput('cannot set state of pin %r' % self)
     else:
         lgpio.gpio_write(self.factory._handle, self.number, bool(value))
Example #2
0
 def _set_state(self, value):
     if self._pwm:
         freq, duty = self._pwm
         self._pwm = (freq, int(value * 100))
         try:
             lgpio.tx_pwm(self.factory._handle, self.number, *self._pwm)
         except lgpio.error:
             raise PinInvalidState(
                 'invalid state "{value}" for pin {self!r}'.format(
                     self=self, value=value))
     elif self.function == 'input':
         raise PinSetInput(
             'cannot set state of pin {self!r}'.format(self=self))
     else:
         lgpio.gpio_write(self.factory._handle, self.number, bool(value))
Example #3
0
import time
import lgpio

#17,27,22

LED = 17

# open the gpio chip and set the LED pin as output
h = lgpio.gpiochip_open(0)
lgpio.gpio_claim_output(h, LED)

try:
    while True:
        # Turn the GPIO pin high
        lgpio.gpio_write(h, LED, 0)
        print('GIO', LED, 'is On')
        time.sleep(3)

        # Turn the GPIO pin low
        lgpio.gpio_write(h, LED, 1)
        print('GIO', LED, 'is Off')
        time.sleep(1)

except KeyboardInterrupt:
    lgpio.gpio_write(h, LED, 1)
    lgpio.gpiochip_close(h)
Example #4
0
status = sbc.gpio_set_watchdog_micros(9999, 8888, 4000)
check("gpio_set_watchdog_micros 2", sbc.BAD_HANDLE, status)

status = sbc.gpio_claim_alert(9999, 8888, 7777, 6666, 5555)
check("gpio_claim_alert 1", sbc.BAD_HANDLE, status)

status = sbc.gpio_claim_input(9999, 8888)
check("gpio_claim_input 1", sbc.BAD_HANDLE, status)

status = sbc.group_claim_input(9999, [8888, 7777, 6666])
check("group_claim_input 1", sbc.BAD_HANDLE, status)

status = sbc.gpio_claim_output(9999, 8888, 7777)
check("gpio_claim_output 1", sbc.BAD_HANDLE, status)

status = sbc.group_claim_output(9999, [8888, 7777, 6666])
check("group_claim_output 1", sbc.BAD_HANDLE, status)

status = sbc.gpio_write(9999, 8888, 7777)
check("gpio_write 1", sbc.BAD_HANDLE, status)

err = sbc.error_text(0)
check("error_text 1", "No error", err)

err = sbc.error_text(-1)
check("error_text 2", "initialisation failed", err)

err = sbc.error_text(1)
check("error_text 3", "unknown error", err)
Example #5
0
          ' (note) 0 is good working')
    #lgpio.gpio_claim_output(h, LED1)
    #lgpio.gpio_claim_output(h, LED2)

    try:
        # Turn the GPIO pin low
        lgpio.group_write(handle, 17, 0x6, 0x7)  # 3개 on, blue, green, red
        #lgpio.group_write(handle, 17, 0x4, 0x7) # 2개 on,       green, red
        #lgpio.group_write(handle, 17, 0x2, 0x7) # 2개on, blue, red, A
        #lgpio.group_write(handle, 17, 0x0, 0x7) # 1개on, red 0,8
        #lgpio.group_write(handle, 17, 0xa, 0x7) # no on 5,1,3,9
        print('GIO', 'is On')
        time.sleep(5)

        # Turn the GPIO pin high
        lgpio.group_write(handle, 17, 0x7, 0x7)
        #lgpio.group_write(handle, LEDS, 0x000008)
        #lgpio.gpio_write(h, LED0, 1)
        #lgpio.gpio_write(h, LED1, 1)
        #lgpio.gpio_write(h, LED2, 1)
        print('GIO', 'is Off')  #LED 보드 구현에 따라, H->On, L->On 이 다를수 있음
        time.sleep(2)

        lgpio.gpiochip_close(handle)

    except KeyboardInterrupt:
        lgpio.gpio_write(handle, LED0, 0)
        lgpio.gpio_write(handle, LED1, 0)
        lgpio.gpio_write(handle, LED2, 0)
        lgpio.gpiochip_close(h)
Example #6
0
"""
bench.py
2020-11-18
Public Domain

http://abyz.me.uk/lg/py_lgpio.html

./bench.py
"""
import time
import lgpio

OUT = 21
LOOPS = 100000

h = lgpio.gpiochip_open(0)

lgpio.gpio_claim_output(h, OUT)

t0 = time.time()

for i in range(LOOPS):
    lgpio.gpio_write(h, OUT, 0)
    lgpio.gpio_write(h, OUT, 1)

t1 = time.time()

lgpio.gpiochip_close(h)

print("{:.0f} toggles per second".format(LOOPS / (t1 - t0)))
if __name__ == "__main__":

    print("...Start...")

    # open the gpio chip and set the LED pin as output
    h = lgpio.gpiochip_open(0)
    print(lgpio.gpio_claim_output(h, LED0))
    lgpio.gpio_claim_output(h, LED1)
    lgpio.gpio_claim_output(h, LED2)

    try:

        while True:
            # Turn the GPIO pin low
            lgpio.gpio_write(h, LED0, 0)
            lgpio.gpio_write(h, LED1, 0)
            lgpio.gpio_write(h, LED2, 0)
            print('GIO', 'is On')
            time.sleep(90)

            # Turn the GPIO pin high
            lgpio.gpio_write(h, LED0, 1)
            lgpio.gpio_write(h, LED1, 1)
            lgpio.gpio_write(h, LED2, 1)
            print('GIO', 'is Off')  #LED 보드 구현에 따라, H->On, L->On 이 다를수 있음
            time.sleep(1)

    except KeyboardInterrupt:
        lgpio.gpio_write(h, LED0, 1)
        lgpio.gpio_write(h, LED1, 1)
Example #8
0
nano_i2c = sbc.i2c_open(1, NANO)

nano_serial = sbc.serial_open("serial0", 115200)

inc = True

potpos = 0

while True:

    while True:

        sbc.spi_write(dac, [0, potpos])

        for i in range(8):
            sbc.gpio_write(chip, ADC_CS, 0)
            (b, d) = sbc.spi_xfer(others, [1, 0x80 + (i << 4), 0])
            sbc.gpio_write(chip, ADC_CS, 1)

            if b == 3:
                c1 = d[1] & 0x03
                c2 = d[2]
                ch0 = (c1 << 8) + c2
            else:
                ch0 = -1

            print("ADC{}={:4d} pot={}".format(i, ch0, potpos))

        potpos += 1
        if potpos > 129:
            potpos = 0