Skip to content

Commit

Permalink
some error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
amrahm committed Oct 31, 2022
1 parent e824e27 commit ad246a8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 56 deletions.
63 changes: 20 additions & 43 deletions infrastructure.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import sys
import threading
import time, threading
import json
import win32api
import PySimpleGUI as sg
from os.path import exists
from typing import Dict, List
import monitorcontrol
from datetime import datetime
import contextlib

Expand Down Expand Up @@ -91,44 +92,6 @@ def _sort_times(settings):
settings[TIMES].sort(key=lambda x: datetime.strptime(x[TIME], "%H:%M").time())


# def load_settings() -> Settings:
# if exists(SETTINGS_PATH):
# with open(SETTINGS_PATH, "r") as jsonfile:
# settings = json.load(jsonfile)
# else:
# settings: Settings = {TIMES: [], UPDATE_FREQ: 10, SHOULD_HOLD: False, HOLD_TIME: 60}
# save_settings(settings)
# return settings


# def save_settings(settings: Settings):
# seen = set()
# to_del = []
# for i, time in enumerate(settings[TIMES]):
# time = time[TIME]
# hrs, mins = time.split(":") if time else ("", "")
# if not hrs and not mins:
# to_del.append(i)
# continue
# elif not hrs:
# settings[TIMES][i][TIME] = time = f"00:{mins}"
# elif not mins:
# settings[TIMES][i][TIME] = time = f"{hrs}:00"

# if time in seen:
# to_del.append(i)
# seen.add(time)

# for i in sorted(to_del, reverse=True):
# del settings[TIMES][i]

# sort_times(settings)
# with open(SETTINGS_PATH, "w") as jsonfile:
# json.dump(settings, jsonfile, indent=4)

# SettingsSingleton()._settings.update(settings)


def position_window_on_tray(window: sg.Window, keep_x=False):
window.hide()
mouse_pos = win32api.GetCursorPos()
Expand All @@ -147,7 +110,21 @@ def position_window_on_tray(window: sg.Window, keep_x=False):
window.TKroot.focus_force()


# def update_single_setting(value, setting):
# settings = _load_settings()
# settings[setting] = value
# save_settings(settings)
def set_monitor_brightness(new_brightness):
for _ in range(3):
try:
for monitor in monitorcontrol.get_monitors():
with monitor:
old = monitor.get_luminance()
if old == new_brightness:
continue
monitor.set_luminance(new_brightness)
break
except (ValueError, monitorcontrol.VCPError) as e:
print(e)
time.sleep(0.2)


def get_monitor_brightness():
with monitorcontrol.get_monitors()[0] as monitor:
return monitor.get_luminance()
2 changes: 1 addition & 1 deletion settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}
],
"UPDATE_FREQ": 10,
"SHOULD_HOLD": true,
"SHOULD_HOLD": false,
"HOLD_TIME": 1,
"HOLD_START_TIME": "2022-10-30T23:33:23.667384"
}
11 changes: 3 additions & 8 deletions sliderwindow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import monitorcontrol
import PySimpleGUI as sg
from infrastructure import *

Expand Down Expand Up @@ -46,19 +45,15 @@ def show_slider_window(window: sg.Window):


def _init_slider_value(window: sg.Window):
with monitorcontrol.get_monitors()[0] as monitor:
slider = window[BRIGHTNESS_SLIDER]
slider.Update(value=monitor.get_luminance())
slider = window[BRIGHTNESS_SLIDER]
slider.Update(value=get_monitor_brightness())


def handle_slider_window_events(window: sg.Window, event, values):
if event == FOCUS_OUT:
window.hide()
elif event == f"{BRIGHTNESS_SLIDER}_RELEASE":
val = int(values[BRIGHTNESS_SLIDER])
for monitor in monitorcontrol.get_monitors():
with monitor:
monitor.set_luminance(val)
set_monitor_brightness(int(values[BRIGHTNESS_SLIDER]))

window[CHECKBOX_HOLD].update(value=True)
SettingsSingleton().update_single_setting(HOLD_START_TIME, datetime.now().isoformat())
Expand Down
7 changes: 3 additions & 4 deletions timeloop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import time, threading
from datetime import datetime, date, timedelta
import monitorcontrol
from infrastructure import *


Expand Down Expand Up @@ -53,6 +52,6 @@ def run(self):
ratio = 1 if next_time == prev_time else (curr_time - prev_time) / (next_time - prev_time)
new_brightness = int(next_time_val.value * ratio + prev_time_val.value * (1 - ratio))

for monitor in monitorcontrol.get_monitors():
with monitor:
monitor.set_luminance(new_brightness)
# print(prev_time_val, curr_time.time(), next_time_val, f"{ratio:0.5}", new_brightness)

set_monitor_brightness(new_brightness)

0 comments on commit ad246a8

Please sign in to comment.