diff --git a/main.py b/main.py index 3cf747e..8ddeaef 100644 --- a/main.py +++ b/main.py @@ -14,19 +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 -global led_power -global led_power_toggler -led_power_toggler = True -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') - idle_mode = 1 - def check_internet(url='http://www.google.com/', timeout=5): try: _ = requests.head(url, timeout=timeout) @@ -35,7 +22,6 @@ def check_internet(url='http://www.google.com/', timeout=5): return False def wheel(pos): - """Generate rainbow colors across 0-255 positions.""" if pos < 85: return Color(pos * 3, 255 - pos * 3, 0) elif pos < 170: @@ -45,8 +31,7 @@ def wheel(pos): pos -= 170 return Color(0, pos * 3, 255 - pos * 3) -def rainbowCycle(strip, wait_ms=10, iterations=10): - """Draw rainbow that uniformly distributes itself across all pixels.""" +def rainbowCycle(strip, wait_ms=5, iterations=10): for j in range(256*iterations): for i in range(strip.numPixels()): strip.setPixelColor(i, wheel((int(i * 256 / strip.numPixels()) + j) & 255)) @@ -69,29 +54,20 @@ def blink(strip, color1, color2, iterations=10, wait_ms=10): strip.show() time.sleep(1) -def read_idle(): - with open('./idle.txt', 'r') as f: - idle_mode = f.readline() - def change_idle_mode(channel): global idle_mode - with open('./idle.txt', 'r') as f: - idle_mode = f.readline() - if int(idle_mode) > 7: - idle_mode = 0 - with open('./idle.txt', 'w') as f: - f.write(int(idle_mode) + 1) - with open('./idle.txt', 'r') as f: - idle_mode = f.readline() - print('Change Idle-Mode: '+idle_mode) + idle_mode += 1 + if idle_mode == 6: + idle_mode = 1 def power_toggle(channel): - if led_power == True: - led_power_toggler = False - else: - led_power_toggler = True + print('button pressed') + global power_button_pressed + power_button_pressed = 'True' if __name__ == '__main__': + led_power = 'True' + idle_mode = 1 strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL) strip.begin() GPIO.setmode(GPIO.BOARD) @@ -102,41 +78,40 @@ if __name__ == '__main__': try: while True: - led_power = led_power_toggler try: - with open('./idle.txt', 'r') as f: - idle_mode = f.readline() + print('Button pressed: '+str(power_button_pressed)) + if str(power_button_pressed) == 'True': + if led_power == 'True': + led_power = 'False' + power_button_pressed = 'False' + else: + led_power = 'True' + power_button_pressed = 'False' except: - with open('./idle.txt', 'w') as f: - f.write('1') - with open('./idle.txt', 'r') as f: - idle_mode = f.readline() + pass - if power_toggle == False: - print('Power: False') - setColor(strip, Color(0,0,0)) - led_power == False - - else: + if led_power == 'True': print('Power: True') - led_power == True if check_internet() == False: blink(strip, Color(255,0,0), Color(255,255,255)) else: - print(idle_mode) - if idle_mode == '1': + if idle_mode == 1: rainbowCycle(strip) - elif idle_mode == '2': + elif idle_mode == 2: setColor(strip, Color(255,0,0)) - elif idle_mode == '3': + elif idle_mode == 3: setColor(strip, Color(0,255,0)) - elif idle_mode == '4': + elif idle_mode == 4: setColor(strip, Color(0,0,255)) - elif idle_mode == '5': + elif idle_mode == 5: setColor(strip, Color(125,245,255)) else: blink(strip, Color(255,0,0), Color(0,0,0)) - + time.sleep(1) + else: + print('Power: False') + setColor(strip, Color(0,0,0)) + time.sleep(1) except KeyboardInterrupt: setColor(strip, Color(0,0,0)) GPIO.cleanup() \ No newline at end of file diff --git a/test.py b/test.py index 75ebd95..ce9ea7d 100644 --- a/test.py +++ b/test.py @@ -55,6 +55,6 @@ if __name__ == '__main__': while True: #blink(strip, Color(255,0,0), Color(255,255,255)) #setColor(strip, Color(255,0,0)) - #rainbowCycle(strip) + rainbowCycle(strip) except KeyboardInterrupt: setColor(strip, Color(0,0,0)) \ No newline at end of file