#!/usr/bin/python

"""
Demo program for Iowa grid laptop power saving
Copyright 2008, Troy Benjegerdes <hozer@hozed.org>
Licensed under the GNU GPLv3
"""

import wx
import Numeric
from wx.lib.plot import PlotCanvas, PolyLine, PolyMarker, PlotGraphics
import os
import time
import string

#----------------------------------------------------------------------
def _draw4Objects_old():
	# 25,000 point line
	data1 = Numeric.arange(5e5,1e6,10)
	data1.shape = (25000, 2)
	line1 = wx.lib.plot.PolyLine(data1, legend='Wide Line', colour='green', width=5)
	# A few more points...
	markers2 = wx.lib.plot.PolyMarker(data1, legend='Square', colour='blue',
			marker='square')

	return wx.lib.plot.PlotGraphics([line1, markers2], "25,000 Points", "Value X", "")


def drawtest(x):
	#data1 = Numeric.arange(5e5,1e6 * x,10)
	#data1.shape = (25000, 2)
	data1 = [[-0,1],[-2,25],[-3,x],[-4,50],[-5,100]] 
	#[1, 1, 3, 4, 5 ,6, 7, 8, 9, x]
	#data1 = Numeric.array(d)
	#data1.shape = (5,2)
	line1 = wx.lib.plot.PolyLine(data1, legend='Wide Line', colour='green', width=5)
	# A few more points...
	markers2 = wx.lib.plot.PolyMarker(data1, legend='Square', colour='blue',
			marker='square')

	return wx.lib.plot.PlotGraphics([line1, markers2], "Power", "time, sec", "")

def timeplot(d1,d2):
	m1 = wx.lib.plot.PolyMarker([30,30], colour='white', size=0)
	#line1 = wx.lib.plot.PolyLine(d1, legend='Square', colour='green', width=5)
	#m1 = wx.lib.plot.PolyMarker(d1, size=1, colour='green', marker='square')
	# A few more points...
	#markers2 = wx.lib.plot.PolyMarker(data, legend='Square', colour='blue',
	#		marker='square')
	m2 = wx.lib.plot.PolyMarker(d2, size=1, colour='blue', marker='square')

	return wx.lib.plot.PlotGraphics([m1,m2], "Power", "time, sec", "Watts")

def getPower():
	f = open("/proc/acpi/battery/BAT0/state")
	for l in f.readlines():
		if 'present rate' in l:
			x = l.split(':')[1]
			y = string.strip(x[0:-3])
			rate = string.atoi(y)
			break
	return rate/1000.0

	

def _draw4Objects():
	a = [[0,0],[1,1],[2,2]]
	return timeplot(a, a)


def runTest():
	class MyApp(wx.App):
		def OnInit(self):
			self.SetAssertMode(wx.PYAPP_ASSERT_DIALOG)
			frame = wx.Frame(None, -1, "powerDemo",
				style = wx.DEFAULT_FRAME_STYLE)
			self.SetTopWindow(frame)
			
			self.sizer = wx.BoxSizer(wx.HORIZONTAL)

			self.plot = PlotCanvas(parent=frame)
			#self.plot.resetDefaults()
			self.plot.Draw(_draw4Objects())
			self.slider = wx.Slider(parent=frame, value=0, minValue=0, maxValue=100,
				style = wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LABELS | wx.SL_INVERSE
				)

			self.sizer.Add(self.plot, proportion=1, flag=wx.EXPAND)
			self.sizer.Add(self.slider, proportion=0, flag=wx.EXPAND)
			frame.SetMinSize(wx.Size(500,300))
			frame.SetSizerAndFit(self.sizer)
			frame.Bind(wx.EVT_SLIDER, self.sliderUpdate)
			frame.Show(True)
			self.timer = wx.PyTimer(self.timerEvent)
			self.timer.Start(milliseconds=100)
			self.starttime = time.time()
			self.cost = [[0,1]]
			self.power = [[0,0]]
			return True

		def sliderUpdate(self, event):
			#x = self.slider.GetValue() 
			#self.plot.Draw(drawtest(x))
			pass

		def timerEvent(self):
			t = (time.time() - self.starttime)
			self.cost.append([t, self.slider.GetValue()])
			self.power.append([t, getPower()])
			#this should update, not draw new, very power inefficient ;)
			#print self.power
			self.plot.Draw(timeplot(self.cost, self.power))

	app = MyApp(0)
	app.MainLoop()

#----------------------------------------------------------------------

if __name__ == '__main__':
	runTest()
