diff options
Diffstat (limited to 'client/ur_fuzzy_alarm_clock.py')
-rwxr-xr-x | client/ur_fuzzy_alarm_clock.py | 143 |
1 files changed, 80 insertions, 63 deletions
diff --git a/client/ur_fuzzy_alarm_clock.py b/client/ur_fuzzy_alarm_clock.py index 4fbe3e5..d6e5a1b 100755 --- a/client/ur_fuzzy_alarm_clock.py +++ b/client/ur_fuzzy_alarm_clock.py @@ -18,11 +18,11 @@ except ImportError: logging.error('This program needs pySerial') sys.exit(1) -class FuzzyAlarmClock: - def __init__(self,device): +class FuzzyAlarmClock: + def __init__(self, device): try: - self.serial = serial.Serial(device,9600,timeout=5) + self.serial = serial.Serial(device, 9600, timeout=5) except serial.serialutil.SerialException: logging.warning('No such serial device {device}; mocking'.format( device=device @@ -36,7 +36,7 @@ class FuzzyAlarmClock: now = 's'+datetime.datetime.now().strftime("%y%m%d%u%H%M%S") self.serial.write(now) - def read_alarms(self,n): + def read_alarms(self, n): data = [] self.serial.flushInput() self.serial.write('p') @@ -51,32 +51,33 @@ class FuzzyAlarmClock: data[i] = days+data[i][1:] return data - def set_alarm(self,alarm,values): + def set_alarm(self, alarm, values): s = 'a'+str(alarm) b0 = 0 - for i,bit in enumerate(values[1:8] + values[0:1]): + for i, bit in enumerate(values[1:8] + values[0:1]): b0 += bit << i - s += "%02x"%(b0) + s += "%02x" % (b0) for val in values[8:]: - s += "%02x"%(val) + s += "%02x" % (val) self.serial.write(s) -class ModEdit(urwid.IntEdit): - def __init__(self,mod,caption="",default=None,offset=0): +class ModEdit(urwid.IntEdit): + def __init__(self, mod, caption="", default=None, offset=0): self.mod = mod self.offset = offset - super(ModEdit,self).__init__(caption,default) + super(ModEdit, self).__init__(caption, default) - def insert_text(self,text): - super(ModEdit,self).insert_text(text) - self.set_edit_text(str((self.value() - self.offset)% self.mod+self.offset)) + def insert_text(self, text): + super(ModEdit, self).insert_text(text) + self.set_edit_text(str( + (self.value() - self.offset) % self.mod + self.offset + )) class Alarm: - - def __init__(self,aid,fac): + def __init__(self, aid, fac): self.w_aid = urwid.Text(str(aid)) self.w_repeat = urwid.CheckBox("") self.w_mon = urwid.CheckBox("") @@ -86,28 +87,30 @@ class Alarm: self.w_fri = urwid.CheckBox("") self.w_sat = urwid.CheckBox("") self.w_sun = urwid.CheckBox("") - self.w_month = ModEdit(12,offset=1) - self.w_day = ModEdit(31,offset=1) - self.w_hour = ModEdit(24,default=0) - self.w_minute = ModEdit(60,default=0) - - self.widgets = urwid.Columns([self.w_aid, - self.w_repeat, - self.w_mon, - self.w_tue, - self.w_wed, - self.w_thu, - self.w_fri, - self.w_sat, - self.w_sun, - self.w_month, - self.w_day, - self.w_hour, - self.w_minute, - ], - dividechars=1) - - def set_values(self,values): + self.w_month = ModEdit(12, offset=1) + self.w_day = ModEdit(31, offset=1) + self.w_hour = ModEdit(24, default=0) + self.w_minute = ModEdit(60, default=0) + + self.widgets = urwid.Columns( + [ + self.w_aid, + self.w_repeat, + self.w_mon, + self.w_tue, + self.w_wed, + self.w_thu, + self.w_fri, + self.w_sat, + self.w_sun, + self.w_month, + self.w_day, + self.w_hour, + self.w_minute, + ], + dividechars=1) + + def set_values(self, values): self.w_repeat.set_state(values[0]) self.w_mon.set_state(values[1]) self.w_tue.set_state(values[2]) @@ -139,14 +142,17 @@ class Alarm: class UI: - - def __init__(self,fac): + def __init__(self, fac): self.fac = fac - self.alarm_number=4 + self.alarm_number = 4 self.frame = self.build_ui() - self.loop = urwid.MainLoop(self.frame,self.palette,unhandled_input=self.read_commands) + self.loop = urwid.MainLoop( + self.frame, + self.palette, + unhandled_input=self.read_commands + ) def build_ui(self): self.palette = [ @@ -156,8 +162,8 @@ class UI: self.alarms = [] for i in range(self.alarm_number): - self.alarms.append(Alarm(i,self.fac)) - + self.alarms.append(Alarm(i, self.fac)) + alarm_headers = urwid.Columns([ urwid.Text('#'), urwid.Text('rep'), @@ -172,11 +178,15 @@ class UI: urwid.Text('dd'), urwid.Text('HH'), urwid.Text('MM'), - ],dividechars=1) - self.main_body = urwid.ListBox([alarm_headers]+[al.widgets for al in self.alarms]) + ], + dividechars=1) + self.main_body = urwid.ListBox( + [alarm_headers] + + [al.widgets for al in self.alarms] + ) self.footer = urwid.Text('') - footer_map = urwid.AttrMap(self.footer,'footer') + footer_map = urwid.AttrMap(self.footer, 'footer') header = urwid.Text( 'q: Quit ' + @@ -184,20 +194,25 @@ class UI: 'f: to File l: Load ' + '?:Help' ) - header_map = urwid.AttrMap(header,'header') + header_map = urwid.AttrMap(header, 'header') - ui = urwid.Frame(self.main_body,footer=footer_map,header=header_map) + ui = urwid.Frame( + self.main_body, + footer=footer_map, + header=header_map + ) return ui - def read_commands(self,cmd): - {'n': self.set_time_to_now, - 'r': self.read_alarms, - 's': self.set_alarms, - '?': self.print_help, - 'q': self.quit, - 'f': self.save_alarms, - 'l': self.load_alarms, - }.get(cmd,self.do_nothing)() + def read_commands(self, cmd): + { + 'n': self.set_time_to_now, + 'r': self.read_alarms, + 's': self.set_alarms, + '?': self.print_help, + 'q': self.quit, + 'f': self.save_alarms, + 'l': self.load_alarms, + }.get(cmd, self.do_nothing)() def do_nothing(self): pass @@ -211,8 +226,8 @@ class UI: self.footer.set_text('Alarms read from device') def set_alarms(self): - for i,alarm in enumerate(self.alarms): - self.fac.set_alarm(i,alarm.get_values()) + for i, alarm in enumerate(self.alarms): + self.fac.set_alarm(i, alarm.get_values()) self.footer.set_text('Alarms set on device') @@ -224,7 +239,7 @@ class UI: def save_alarms(self, fname='saved_alarms.json'): alarms = [a.get_values() for a in self.alarms] - with open(fname,'w') as fp: + with open(fname, 'w') as fp: json.dump(alarms, fp) self.footer.set_text('Alarms saved to {fname}'.format(fname=fname)) @@ -248,7 +263,7 @@ class UI: def quit(self): raise urwid.ExitMainLoop() - + def run(self): self.loop.run() @@ -258,4 +273,6 @@ def main(): ui = UI(fac) ui.run() -if __name__ == '__main__': main() + +if __name__ == '__main__': + main() |