Created
January 29, 2021 21:52
-
-
Save lad1337/f52d344f78560c6760bb5340de461f74 to your computer and use it in GitHub Desktop.
octoprint physical spinner poller
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 python | |
from time import sleep | |
import requests | |
import pigpio | |
API_KEY = "put you octoprint api key here" | |
GPIO = 18 | |
PI = pigpio.pi() | |
STATE = None | |
def set_percentage(percent): | |
lower_limit = 500 # my servo limits | |
upper_limit = 2500 # my servo limits | |
pulse = (((upper_limit - lower_limit) / 100) * percent) + lower_limit | |
PI.set_servo_pulsewidth(GPIO, pulse) | |
return pulse | |
def set_completion(completion): | |
print completion | |
completion = completion or 0 | |
lower_limit = 10 # my move limit | |
upper_limit = 95 # my move limit | |
percent_limited = (((upper_limit - lower_limit) / 100.0) * completion) + lower_limit | |
percent_limited = 100 - percent_limited | |
set_percentage(percent_limited) | |
if 0 <= completion >= 100: | |
sleep(0.5) | |
PI.set_servo_pulsewidth(GPIO, 0) | |
def get_job(): | |
r = requests.get("http://octopi.local/api/job", headers={"x-api-key": API_KEY}) | |
return r.json() | |
def main(): | |
try: | |
while True: | |
job = get_job() | |
set_completion(job["progress"]["completion"]) | |
sleep(30) | |
finally: | |
print "bye, bye" | |
PI.stop() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment