Created
March 10, 2019 05:58
-
-
Save nezuppo/2cb26ff0ee19f6f40eaa109bade1bae9 to your computer and use it in GitHub Desktop.
3/9 にツイートしたザクヘッド ラズパイケースの制御スクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import wiringpi | |
import time | |
LED = { | |
'pin': 13, | |
'min': 0, | |
'max': 128, | |
} | |
SERVO = { | |
'pin': 18, | |
'min': 47, | |
'max': 108, | |
} | |
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 = [ | |
Sleep(0.01, SERVO, int((SERVO['max'] + SERVO['min']) / 2)), | |
Sleep(3, LED, LED['min']), | |
Gradation(LED, LED['min'], LED['max'], 4, 0.01), | |
Gradation(LED, LED['max'], 8, -4, 0.05), | |
Sleep(0.5), | |
Gradation(LED, 8, LED['max'], 4, 0.01), | |
Sleep(2), | |
Sleep(1.0, SERVO, SERVO['min']), | |
Sleep(1.0, SERVO, SERVO['max']), | |
Gradation(SERVO, SERVO['max'], SERVO['min'], -1, 0.05), | |
Gradation(SERVO, SERVO['min'], SERVO['max'], 1, 0.05), | |
Sleep(1.0), | |
Sleep(2, SERVO, 70), | |
Gradation(LED, LED['max'], 8, -4, 0.01), | |
Gradation(LED, 8, LED['max'], 4, 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