$search
00001 # Software License Agreement (BSD License) 00002 # 00003 # Copyright (c) 2008, Willow Garage, Inc. 00004 # All rights reserved. 00005 # 00006 # Redistribution and use in source and binary forms, with or without 00007 # modification, are permitted provided that the following conditions 00008 # are met: 00009 # 00010 # * Redistributions of source code must retain the above copyright 00011 # notice, this list of conditions and the following disclaimer. 00012 # * Redistributions in binary form must reproduce the above 00013 # copyright notice, this list of conditions and the following 00014 # disclaimer in the documentation and/or other materials provided 00015 # with the distribution. 00016 # * Neither the name of the Willow Garage nor the names of its 00017 # contributors may be used to endorse or promote products derived 00018 # from this software without specific prior written permission. 00019 # 00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00031 # POSSIBILITY OF SUCH DAMAGE. 00032 00033 import roslib; roslib.load_manifest('generic_dashboard') 00034 import rospy 00035 import wx 00036 from os import path 00037 from generic_control import GenericControl 00038 class PowerControl(GenericControl): 00039 def __init__(self, parent, trigger=None, callback=None): 00040 GenericControl.__init__(self, parent, trigger=trigger, callback=callback) 00041 00042 self._pct = 0. 00043 self._time_remaining = rospy.Duration(0) 00044 self._ac_present = 0 00045 00046 icons_path = path.join(roslib.packages.get_pkg_dir('generic_dashboard'), "icons/") 00047 00048 self._left_bitmap = wx.Bitmap(path.join(icons_path, "battery-minus.png"), wx.BITMAP_TYPE_PNG) 00049 self._right_bitmap = wx.Bitmap(path.join(icons_path, "battery-plus.png"), wx.BITMAP_TYPE_PNG) 00050 self._plug_bitmap = wx.Bitmap(path.join(icons_path, "battery-charging.png"), wx.BITMAP_TYPE_PNG) 00051 self._background_bitmap = wx.Bitmap(path.join(icons_path, "battery-background.png"), wx.BITMAP_TYPE_PNG) 00052 self._green = wx.Bitmap(path.join(icons_path, "battery-green-bar.png"), wx.BITMAP_TYPE_PNG) 00053 self._yellow = wx.Bitmap(path.join(icons_path, "battery-yellow-bar.png"), wx.BITMAP_TYPE_PNG) 00054 self._red = wx.Bitmap(path.join(icons_path, "battery-red-bar.png"), wx.BITMAP_TYPE_PNG) 00055 00056 self.SetSize(wx.Size(self._left_bitmap.GetWidth() + self._right_bitmap.GetWidth() + self._background_bitmap.GetWidth(), 32)) 00057 00058 self._start_x = self._left_bitmap.GetWidth() 00059 self._end_x = self.GetSize().x - self._right_bitmap.GetWidth() 00060 self._width = self._end_x - self._start_x 00061 00062 self._plugged_in = False 00063 00064 self.Bind(wx.EVT_PAINT, self.on_paint) 00065 00066 def on_paint(self, evt): 00067 dc = wx.BufferedPaintDC(self) 00068 00069 dc.SetBackground(wx.Brush(self.GetBackgroundColour())) 00070 dc.Clear() 00071 00072 w = self.GetSize().GetWidth() 00073 h = self._left_bitmap.GetHeight() 00074 00075 color_bitmap = None 00076 if (self._pct > 0.5): 00077 color_bitmap = self._green 00078 elif (self._pct > 0.3): 00079 color_bitmap = self._yellow 00080 else: 00081 color_bitmap = self._red 00082 00083 dc.DrawBitmap(self._background_bitmap, self._start_x, 0, True) 00084 00085 color_image = color_bitmap.ConvertToImage() 00086 scaled_color_image = color_image.Rescale(round(self._width * self._pct), color_bitmap.GetHeight()) 00087 color_bitmap = wx.BitmapFromImage(scaled_color_image) 00088 dc.DrawBitmap(color_bitmap, self._start_x, 0, True) 00089 dc.DrawBitmap(self._left_bitmap, 0, 0, True) 00090 dc.DrawBitmap(self._right_bitmap, self._end_x, 0, True) 00091 00092 if (self._plugged_in): 00093 dc.DrawBitmap(self._plug_bitmap, 00094 (self._start_x + self._width) / 2.0 - (self._plug_bitmap.GetWidth() / 2.0), 00095 self.GetSize().GetHeight() / 2.0 - (self._plug_bitmap.GetHeight() / 2.0)) 00096 00097 00098 def set_data(self, data): 00099 (plugged_in, pct, time_remaining) = data 00100 last_pct = self._pct 00101 last_plugged_in = self._plugged_in 00102 last_time_remaining = self._time_remaining 00103 00104 self._pct = pct 00105 self._plugged_in = plugged_in 00106 self._time_remaining = time_remaining 00107 00108 if (last_pct != self._pct or last_plugged_in != self._plugged_in or last_time_remaining != self._time_remaining): 00109 drain_str = "remaining" 00110 if (self._plugged_in): 00111 drain_str = "to full charge" 00112 self.SetToolTip(wx.ToolTip("Battery: %.2f%% (%d minutes %s)"%(self._pct * 100.0, self._time_remaining.to_sec()/60.0, drain_str))) 00113 self.Refresh() 00114 00115 def set_status(self, status): 00116 if status=='stale': 00117 self._plugged_in = 0 00118 self._pct = 0 00119 self._time_remaining = rospy.Duration(0) 00120 self.SetToolTip(wx.ToolTip("Battery: Stale")) 00121 00122 self.Refresh() 00123