Update main.py

master
Ebbe Baß 2021-11-09 17:06:13 +01:00
parent 4b581d846b
commit 40880128c9
1 changed files with 55 additions and 13 deletions

68
main.py
View File

@ -1,7 +1,7 @@
import time
import requests
from rpi_ws281x import *
import configparser
import RPi.GPIO as GPIO
import os
# LED strip configuration:
@ -14,8 +14,6 @@ LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
config = configparser.ConfigParser()
def check_internet(url='http://www.google.com/', timeout=5):
try:
_ = requests.head(url, timeout=timeout)
@ -58,27 +56,71 @@ def blink(strip, color1, color2, iterations=10, wait_ms=10):
strip.show()
time.sleep(1)
def write_file():
config.write(open('./config.ini', 'w'))
def led_power_toggle():
if led_power == True:
led_power = False
else:
led_power = True
def read_idle():
with open('./idle.txt', 'r') as f:
idle_mode = f.readline()
def change_idle_mode():
with open('./idle.txt', 'w') as f:
f.write(int(idle_mode) + 1)
if __name__ == '__main__':
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
strip.begin()
GPIO.setmode(GPIO.BCM)
if not os.path.exists('./config.ini'):
config['testing'] = {'led_power': True, 'timer': True, 'start_time': '06:00', 'stop_time': '22:00'}
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)
write_file()
GPIO.add_event_detect(13, GPIO.BOTH, callback=led_power_toggle(), bouncetime=300)
GPIO.add_event_detect(19, GPIO.BOTH, callback=change_idle_mode(), bouncetime=300)
led_power = config.get('testing', 'led_power')
else:
led_power = config.get('testing', 'led_power')
global led_power
global idle_mode
led_power = True
try:
with open('./idle.txt', 'r') as f:
idle_mode = f.readline()
except:
with open('./idle.txt', 'w') as f:
f.write('1')
with open('./idle.txt', 'r') as f:
idle_mode = f.readline()
try:
while True:
if led_power == True:
try:
with open('./idle.txt', 'r') as f:
idle_mode = f.readline()
except:
with open('./idle.txt', 'w') as f:
f.write('1')
with open('./idle.txt', 'r') as f:
idle_mode = f.readline()
if led_power == False:
setColor(strip, Color(0,0,0))
else:
setColor(strip, Color(255,0,0))
if idle_mode == '1':
rainbowCycle(strip)
elif idle_mode == '2':
setColor(strip, Color(255,0,0))
elif idle_mode == '3':
setColor(strip, Color(0,255,0))
elif idle_mode == '4':
setColor(strip, Color(0,0,255))
elif idle_mode == '5':
setColor(strip, Color(125,245,255))
else:
blink(strip, Color(255,0,0), Color(0,0,0))
except KeyboardInterrupt:
setColor(strip, Color(0,0,0))