2021-07-25 18:23:58 +01:00
|
|
|
#- LVGL lv_signal_bars and lv_wifi_bars
|
|
|
|
-
|
|
|
|
--#
|
|
|
|
|
|
|
|
class lv_signal_bars : lv_obj
|
2021-09-08 11:41:28 +01:00
|
|
|
var percentage # value to display, range 0..100
|
|
|
|
var p1, p2, area, line_dsc # instances of objects kept to avoid re-instanciating at each call
|
2021-07-25 18:23:58 +01:00
|
|
|
|
2021-10-02 08:33:59 +01:00
|
|
|
def init(parent)
|
|
|
|
# init custom widget (don't call super constructor)
|
|
|
|
_lvgl.create_custom_widget(self, parent)
|
2021-07-25 18:23:58 +01:00
|
|
|
# own values
|
|
|
|
self.percentage = 100
|
|
|
|
# pre-allocate buffers
|
2021-07-27 20:39:24 +01:00
|
|
|
self.p1 = lv_point()
|
|
|
|
self.p2 = lv_point()
|
2021-09-08 11:41:28 +01:00
|
|
|
self.area = lv_area()
|
2021-07-27 20:39:24 +01:00
|
|
|
self.line_dsc = lv_draw_line_dsc()
|
2021-07-25 18:23:58 +01:00
|
|
|
end
|
|
|
|
|
2021-10-02 08:33:59 +01:00
|
|
|
def widget_event(cl, event)
|
|
|
|
# Call the ancestor's event handler
|
|
|
|
if lv.obj_event_base(cl, event) != lv.RES_OK return end
|
|
|
|
var code = event.code
|
|
|
|
|
2021-07-25 20:20:14 +01:00
|
|
|
def atleast1(x) if x >= 1 return x else return 1 end end
|
2021-07-25 18:23:58 +01:00
|
|
|
# the model is that we have 4 bars and inter-bar (1/4 of width)
|
|
|
|
var height = self.get_height()
|
|
|
|
var width = self.get_width()
|
|
|
|
|
2021-07-25 20:20:14 +01:00
|
|
|
var inter_bar = atleast1(width / 15)
|
|
|
|
var bar = atleast1((width - inter_bar * 3) / 4)
|
2021-07-25 18:23:58 +01:00
|
|
|
var bar_offset = bar / 2
|
|
|
|
|
2021-10-02 08:33:59 +01:00
|
|
|
if code == lv.EVENT_DRAW_MAIN
|
|
|
|
var clip_area = lv_area(event.param)
|
2021-07-25 18:23:58 +01:00
|
|
|
|
2021-09-08 11:41:28 +01:00
|
|
|
# get coordinates of object
|
|
|
|
self.get_coords(self.area)
|
|
|
|
var x_ofs = self.area.x1
|
|
|
|
var y_ofs = self.area.y1
|
2021-07-25 18:23:58 +01:00
|
|
|
|
2021-10-02 08:33:59 +01:00
|
|
|
lv.draw_line_dsc_init(self.line_dsc) # initialize lv_draw_line_dsc structure
|
|
|
|
self.init_draw_line_dsc(lv.PART_MAIN, self.line_dsc) # copy the current values
|
2021-07-25 18:23:58 +01:00
|
|
|
|
|
|
|
self.line_dsc.round_start = 1
|
|
|
|
self.line_dsc.round_end = 1
|
|
|
|
self.line_dsc.width = bar
|
2021-10-02 08:33:59 +01:00
|
|
|
var on_color = self.get_style_line_color(lv.PART_MAIN | lv.STATE_DEFAULT)
|
|
|
|
var off_color = self.get_style_bg_color(lv.PART_MAIN | lv.STATE_DEFAULT)
|
2021-07-25 18:23:58 +01:00
|
|
|
|
2021-10-02 08:33:59 +01:00
|
|
|
lv.event_send(self, lv.EVENT_DRAW_PART_BEGIN, self.line_dsc)
|
2021-09-07 18:34:18 +01:00
|
|
|
for i:0..3 # 4 bars
|
2021-07-25 18:23:58 +01:00
|
|
|
self.line_dsc.color = self.percentage >= (i+1)*20 ? on_color : off_color
|
|
|
|
self.p1.y = y_ofs + height - 1 - bar_offset
|
|
|
|
self.p1.x = x_ofs + i * (bar + inter_bar) + bar_offset
|
|
|
|
self.p2.y = y_ofs + ((3 - i) * (height - bar)) / 4 + bar_offset
|
|
|
|
self.p2.x = self.p1.x
|
2021-09-08 11:41:28 +01:00
|
|
|
lv.draw_line(self.p1, self.p2, clip_area, self.line_dsc)
|
2021-07-25 18:23:58 +01:00
|
|
|
end
|
2021-10-02 08:33:59 +01:00
|
|
|
lv.event_send(self, lv.EVENT_DRAW_PART_END, self.line_dsc)
|
2021-07-25 18:23:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_percentage(v)
|
2021-10-02 08:33:59 +01:00
|
|
|
var old_bars = self.percentage / 20
|
2021-07-25 18:23:58 +01:00
|
|
|
if v > 100 v = 100 end
|
|
|
|
if v < 0 v = 0 end
|
|
|
|
self.percentage = v
|
2021-10-02 08:33:59 +01:00
|
|
|
if old_bars != v / 20
|
2021-07-25 18:23:58 +01:00
|
|
|
self.invalidate() # be frugal and avoid updating the widget if it's not needed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_percentage()
|
|
|
|
return self.percentage
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class lv_wifi_bars: lv_signal_bars
|
2021-10-02 08:33:59 +01:00
|
|
|
def init(parent)
|
|
|
|
super(self).init(parent)
|
2021-07-27 20:39:24 +01:00
|
|
|
tasmota.add_driver(self)
|
2021-09-07 18:34:18 +01:00
|
|
|
self.set_percentage(0) # we generally start with 0, meaning not connected
|
2021-07-25 18:23:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def every_second()
|
2021-07-27 20:39:24 +01:00
|
|
|
var wifi = tasmota.wifi()
|
2021-07-25 18:23:58 +01:00
|
|
|
var quality = wifi.find("quality")
|
2021-09-07 18:34:18 +01:00
|
|
|
var ip = wifi.find("ip")
|
|
|
|
if ip == nil
|
|
|
|
self.set_percentage(0)
|
|
|
|
elif quality != nil
|
2021-07-25 18:23:58 +01:00
|
|
|
self.set_percentage(quality)
|
|
|
|
end
|
|
|
|
end
|
2021-09-08 18:40:18 +01:00
|
|
|
|
|
|
|
def del()
|
|
|
|
super(self).del()
|
|
|
|
tasmota.remove_driver(self)
|
|
|
|
end
|
2021-09-07 18:34:18 +01:00
|
|
|
end
|
2021-09-08 18:40:18 +01:00
|
|
|
|
|
|
|
class lv_wifi_bars_icon: lv_wifi_bars
|
2021-10-02 08:33:59 +01:00
|
|
|
def init(parent)
|
|
|
|
super(self).init(parent)
|
|
|
|
self.set_style_line_color(lv_color(lv.COLOR_WHITE), lv.PART_MAIN | lv.STATE_DEFAULT)
|
|
|
|
self.set_style_bg_color(lv_color(lv.COLOR_BLACK), lv.PART_MAIN | lv.STATE_DEFAULT)
|
2021-09-08 18:40:18 +01:00
|
|
|
if parent != nil
|
|
|
|
var parent_height = parent.get_height()
|
2021-10-02 08:33:59 +01:00
|
|
|
var pad_right = parent.get_style_pad_right(lv.PART_MAIN | lv.STATE_DEFAULT)
|
2021-09-08 18:40:18 +01:00
|
|
|
self.set_height(parent_height)
|
|
|
|
self.set_width(parent_height)
|
|
|
|
self.set_x(parent.get_width() - parent_height - pad_right)
|
2021-10-02 08:33:59 +01:00
|
|
|
parent.set_style_pad_right(pad_right + parent_height + 1, lv.PART_MAIN | lv.STATE_DEFAULT)
|
2021-09-08 18:40:18 +01:00
|
|
|
end
|
|
|
|
end
|
2021-10-02 08:33:59 +01:00
|
|
|
end
|