Skip to content

Instantly share code, notes, and snippets.

@indriApollo
Created December 16, 2023 16:46
Show Gist options
  • Save indriApollo/4b1327138334947fe04e79ca2fa58003 to your computer and use it in GitHub Desktop.
Save indriApollo/4b1327138334947fe04e79ca2fa58003 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import time
def export_gpio(num):
with open('/sys/class/gpio/export', 'w') as e:
e.write(str(num))
def unexport_gpio(num):
with open('/sys/class/gpio/unexport', 'w') as e:
e.write(str(num))
def gpio_dir(num):
return f'/sys/class/gpio/gpio{num}'
def set_direction_out(num):
with open(f'{gpio_dir(num)}/direction', 'w') as e:
e.write('out')
def init_gpio(num):
export_gpio(num)
set_direction_out(num)
def deinit_gpio(num):
set_off(num)
unexport_gpio(num)
def set_on(num):
with open(f'{gpio_dir(num)}/value', 'w') as e:
e.write('1')
def set_off(num):
with open(f'{gpio_dir(num)}/value', 'w') as e:
e.write('0')
def init_all(nums):
for n in nums:
init_gpio(n)
def deinit_all(nums):
for n in nums:
deinit_gpio(n)
leds = [ 17, 18, 27, 22, 23, 24, 14, 4 ]
init_all(leds)
for n in leds:
set_on(n)
time.sleep(0.5)
set_off(n)
deinit_all(leds)
@indriApollo
Copy link
Author

indriApollo commented Dec 16, 2023

With MAD effects !

#!/usr/bin/env python3

import time
import os

def gpio_dir(num):
    return f'/sys/class/gpio/gpio{num}'

def export_gpio(num):
    if os.path.exists(gpio_dir(num)):
        return
    with open('/sys/class/gpio/export', 'w') as e:
        e.write(str(num))

def unexport_gpio(num):
    with open('/sys/class/gpio/unexport', 'w') as e:
        e.write(str(num))

def set_direction_out(num):
    with open(f'{gpio_dir(num)}/direction', 'w') as e:
        e.write('out')

def init_gpio(num):
    export_gpio(num)
    set_direction_out(num)

def deinit_gpio(num):
    if not os.path.exists(gpio_dir(num)):
        return
    set_off(num)
    unexport_gpio(num)

def set_on(num):
    with open(f'{gpio_dir(num)}/value', 'w') as e:
        e.write('1')

def set_off(num):
    with open(f'{gpio_dir(num)}/value', 'w') as e:
        e.write('0')

def set_on_sleep_off(num, s):
    set_on(num)
    time.sleep(s)
    set_off(num)

def init_all(nums):
    for n in nums:
        init_gpio(n)

def deinit_all(nums):
    for n in nums:
        deinit_gpio(n)

def swipe(leds):
    for led in leds:
        set_on_sleep_off(led, 0.05)

def swipe_reverse(leds):
    swipe(list(reversed(leds)))

def outside_in(leds):
    offset = int(len(leds)/2)
    for i in range(offset):
        ledl = leds[i]
        ledr = leds[len(leds)-1-i]
        set_on(ledl)
        set_on(ledr)
        time.sleep(0.05)
        set_off(ledl)
        set_off(ledr)

def inside_out(leds):
    offset = int(len(leds)/2)
    for i in range(offset):
        ledl = leds[offset-1-i]
        ledr = leds[offset+i]
        set_on(ledl)
        set_on(ledr)
        time.sleep(0.05)
        set_off(ledl)
        set_off(ledr)
        
leds = [ 17, 18, 27, 22, 23, 24, 14, 4 ]

init_all(leds)

try:
    while True:
        swipe(leds)
        swipe_reverse(leds)
        outside_in(leds)
        inside_out(leds)
except KeyboardInterrupt:
    deinit_all(leds)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment