Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 import roslib
00038 roslib.load_manifest('nao_dashboard')
00039
00040 import wx
00041
00042 from os import path
00043
00044 class PowerStateControl(wx.Window):
00045 def __init__(self, parent, id, icons_path):
00046 wx.Window.__init__(self, parent, id, wx.DefaultPosition, wx.Size(60, 32))
00047
00048 self._power_consumption = 0.0
00049 self._pct = 0
00050
00051 self._left_bitmap = wx.Bitmap(path.join(icons_path, "battery-minus.png"), wx.BITMAP_TYPE_PNG)
00052 self._right_bitmap = wx.Bitmap(path.join(icons_path, "battery-plus.png"), wx.BITMAP_TYPE_PNG)
00053 self._plug_bitmap = wx.Bitmap(path.join(icons_path, "battery-charging.png"), wx.BITMAP_TYPE_PNG)
00054 self._background_bitmap = wx.Bitmap(path.join(icons_path, "battery-background.png"), wx.BITMAP_TYPE_PNG)
00055 self._green = wx.Bitmap(path.join(icons_path, "battery-green-bar.png"), wx.BITMAP_TYPE_PNG)
00056 self._yellow = wx.Bitmap(path.join(icons_path, "battery-yellow-bar.png"), wx.BITMAP_TYPE_PNG)
00057 self._red = wx.Bitmap(path.join(icons_path, "battery-red-bar.png"), wx.BITMAP_TYPE_PNG)
00058
00059 self.SetSize(wx.Size(self._left_bitmap.GetWidth() + self._right_bitmap.GetWidth() + self._background_bitmap.GetWidth(), 32))
00060
00061 self._start_x = self._left_bitmap.GetWidth()
00062 self._end_x = self.GetSize().x - self._right_bitmap.GetWidth()
00063 self._width = self._end_x - self._start_x
00064
00065 self._discharging = False
00066 self._charging = False
00067 self._full = False
00068 self._plugged_in = False
00069 self.isStale = True
00070
00071 self.Bind(wx.EVT_PAINT, self.on_paint)
00072
00073 def on_paint(self, evt):
00074 dc = wx.BufferedPaintDC(self)
00075
00076 dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
00077 dc.Clear()
00078
00079 w = self.GetSize().GetWidth()
00080 h = self._left_bitmap.GetHeight()
00081
00082 color_bitmap = None
00083 if (self._pct > 0.5):
00084 color_bitmap = self._green
00085 elif (self._pct > 0.3):
00086 color_bitmap = self._yellow
00087 else:
00088 color_bitmap = self._red
00089
00090 dc.DrawBitmap(self._background_bitmap, self._start_x, 0, True)
00091
00092 color_image = color_bitmap.ConvertToImage()
00093 scaled_color_image = color_image.Rescale(round(self._width * self._pct), color_bitmap.GetHeight())
00094 color_bitmap = wx.BitmapFromImage(scaled_color_image)
00095 dc.DrawBitmap(color_bitmap, self._start_x, 0, True)
00096 dc.DrawBitmap(self._left_bitmap, 0, 0, True)
00097 dc.DrawBitmap(self._right_bitmap, self._end_x, 0, True)
00098
00099 if (self._plugged_in):
00100 dc.DrawBitmap(self._plug_bitmap,
00101 (self._start_x + self._width) / 2.0 - (self._plug_bitmap.GetWidth() / 2.0),
00102 self.GetSize().GetHeight() / 2.0 - (self._plug_bitmap.GetHeight() / 2.0))
00103
00104
00105 def set_power_state(self, msg):
00106 last_pct = self._pct
00107 last_discharging = self._discharging
00108 last_charging = self._charging
00109 last_full = self._full
00110 last_plugged_in = self._plugged_in
00111
00112 self.isStale = False
00113 for kv in msg:
00114 if kv.key == "Current":
00115 self._power_consumption = float(kv.value)
00116 elif kv.key == "Percentage":
00117 if kv.value == "unknown":
00118 isStale = True
00119 self.set_stale()
00120 else:
00121 self._pct = float(kv.value)/100.
00122 elif kv.key == "Discharging flag":
00123 self._discharging = (kv.value == "True")
00124 elif kv.key == "Charge Flag":
00125 self._charging = (kv.value == "True")
00126 elif kv.key == "Full Charge Flag":
00127 self._full = (kv.value == "True")
00128
00129 self._plugged_in = ((self._charging or not self._discharging) and not self.isStale)
00130 if (last_pct != self._pct or last_discharging != self._discharging or last_charging != self._charging or last_full != self._full):
00131 if(self._full):
00132 self.SetToolTip(wx.ToolTip("Battery fully charged"))
00133 else:
00134 drain_str = "discharging"
00135 if (self._charging):
00136 drain_str = "charging"
00137 self.SetToolTip(wx.ToolTip("Battery: %.0f%% (%s)"%(self._pct * 100., drain_str)))
00138 self.Refresh()
00139
00140 def set_stale(self):
00141 self._plugged_in = False
00142 self._discharging = 0
00143 self._pct = 0
00144 self._power_consumption = 0
00145 self.SetToolTip(wx.ToolTip("Battery: Stale"))
00146 self.isStale = True
00147
00148 self.Refresh()