Skip to content

Instantly share code, notes, and snippets.

@nezuppo
Created October 7, 2018 08:57
Show Gist options
  • Save nezuppo/572d4724a48a23fe8812a1944358a195 to your computer and use it in GitHub Desktop.
Save nezuppo/572d4724a48a23fe8812a1944358a195 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import wiringpi
import time
LED = {
'pin': 13,
'min': 0,
'max': 1024,
}
SERVO = {
'pin': 18,
'min': 64,
'max': 96,
}
def init_pwm():
wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(LED['pin'], wiringpi.GPIO.PWM_OUTPUT)
wiringpi.pinMode(SERVO['pin'], wiringpi.GPIO.PWM_OUTPUT)
wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS)
wiringpi.pwmSetClock(375)
class Play:
def _write(self, target, val):
if val < target['min']:
return False
if target['max'] < val:
return False
wiringpi.pwmWrite(target['pin'], val)
return True
class Gradation(Play):
def __init__(self, target, start, end, step, sleep_time):
self.__max_loop = 2048
self.__target = target
self.__start = start
self.__end = end
self.__step = step
self.__sleep_time = sleep_time
def play(self):
val = self.__start
for i in range(self.__max_loop):
if self._write(self.__target, val) is not True:
break
val += self.__step
if val < min(self.__start, self.__end):
break
if max(self.__start, self.__end) < val:
break
time.sleep(self.__sleep_time)
class Sleep(Play):
def __init__(self, sleep_time, target=None, val=None):
self.__sleep_time = sleep_time
self.__target = target
self.__val = val
def play(self):
if self.__target is not None and self.__val is not None:
self._write(self.__target, self.__val)
time.sleep(self.__sleep_time)
def main():
init_pwm()
play_list = [
Gradation(LED, LED['min'], LED['max'], 32, 0.01),
Gradation(LED, LED['max'], 32, -32, 0.05),
Sleep(0.5),
Gradation(LED, 32, LED['max'], 32, 0.01),
Sleep(0.5),
Sleep(1.0, SERVO, SERVO['max']),
Sleep(1.0, SERVO, SERVO['min']),
Gradation(SERVO, SERVO['min'], SERVO['max'], 1, 0.05),
Gradation(SERVO, SERVO['max'], SERVO['min'], -1, 0.05),
Sleep(1.0),
Sleep(1.0, SERVO, 84),
Gradation(LED, LED['max'], 256, -32, 0.01),
Gradation(LED, 256, LED['max'], 32, 0.01),
]
for one in play_list:
one.play()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment