[tuxdroid-svn] r534 - in software/gadgets: . light_monitor light_monitor/trunk
Status: Beta
Brought to you by:
ks156
From: Paul_R <c2m...@c2...> - 2007-09-20 07:48:54
|
Author: Paul_R Date: 2007-09-20 09:48:54 +0200 (Thu, 20 Sep 2007) New Revision: 534 Added: software/gadgets/light_monitor/ software/gadgets/light_monitor/branches/ software/gadgets/light_monitor/tags/ software/gadgets/light_monitor/trunk/ software/gadgets/light_monitor/trunk/lightGraph_class.py Log: * Added a new gadget : light_monitor Added: software/gadgets/light_monitor/trunk/lightGraph_class.py =================================================================== --- software/gadgets/light_monitor/trunk/lightGraph_class.py (rev 0) +++ software/gadgets/light_monitor/trunk/lightGraph_class.py 2007-09-20 07:48:54 UTC (rev 534) @@ -0,0 +1,104 @@ +class lightGraph(gtk.DrawingArea): + def __init__(self, spl, rate): + super(lightGraph, self).__init__() + self.connect("expose_event", self.expose) + self.context = None + self.sample = float(spl) + self.val_table = [0] * spl + self.rate = rate + self.tick = 0 + + def expose(self, widget, event): + #when an event occur, update the context + self.context = widget.window.cairo_create() + self.context.rectangle(event.area.x, event.area.y, + event.area.width, event.area.height) + self.context.clip() + self.draw(self.context) + return False + + def refresh(self): + if self.window: + width,height = self.window.get_size() + self.window.invalidate_rect(gtk.gdk.Rectangle(0,0,width,height),False) + + def draw(self, context): + rect = self.get_allocation() + #draw axes + #draw a rectangle in the window area + context.rectangle(rect.x, rect.y, rect.width, rect.height) + #set a white background + context.set_source_rgb(1, 1, 1) + context.fill_preserve() + #draw a black border + context.set_source_rgb(0, 0, 0) + context.set_line_width(1) + context.stroke() + + #draw horizontal grid with 10 div. and 10 sub-div + for i in range(20): + y = (rect.height / 20) * i + #place correctly the vector + context.move_to(rect.width, y) + if i % 2 == 1: + #Minor div: no text + context.set_source_rgb(0.2, 0.2, 0.2) + context.set_line_width(0.4) + context.line_to(rect.x, y) + else: + #Major div: place text + context.set_source_rgb(0, 0, 0) + context.set_line_width(0.6) + context.line_to(rect.x, y) + context.show_text(str((10-(i/2))*10)+"%") + context.stroke() + + #draw vertical division + #create a tab with the wanted divisions + temp_tab = [0.25, 0.5, 0.75] + for i in range(3): + context.set_source_rgb(0.2, 0.2, 0.2) + context.set_line_width(0.6) + #place correctly the vector + context.move_to(rect.width * temp_tab[i], 0) + #trace a line + context.line_to(rect.width * temp_tab[i], rect.height) + #replace the vector to draw text + context.move_to(rect.width * temp_tab[i], rect.height - 5) + #determine the time labels + context.show_text(str((self.rate * self.sample) - ((self.rate) * + self.sample * temp_tab[i])) + " sec.") + context.stroke() + + #display sample and rate + #move the vector in the bottom + context.move_to(10, rect.height - 10) + #display the rate + context.show_text("rate = "+str(self.rate)+" sec.") + context.move_to(10, rect.height - 20) + #and the samples + context.show_text("samples = "+str(self.sample)) + context.stroke() + + #determine the coefficients to adjust the curve in the window space + coeff_y = float(rect.height / 1124.00) + coeff_x = float(rect.width / self.sample) + + #trace the curve + context.move_to(rect.x, rect.height) + for i, val in enumerate(self.val_table): + context.set_source_rgb(1.0, 0, 0) + context.line_to(rect.x + (i * coeff_x), rect.height - (val * coeff_y)) + context.stroke() + + def on_light_level(self, args): + if self.context != None: + if self.tick == 0: + val = (args[0] * 256) + args[1] + self.val_table.append(val) + self.val_table.pop(0) + self.refresh() + self.tick = int(self.rate * 10) + else: + self.tick -= 1 + |