From 20882feeca881aa8cd1be366ab7e842b6bdb43ea Mon Sep 17 00:00:00 2001 From: Thomas Hooge Date: Sun, 14 Sep 2025 09:41:53 +0200 Subject: [PATCH] Flash LED programmierung --- led.py | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ obp60v.py | 11 ++++- 2 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 led.py diff --git a/led.py b/led.py new file mode 100644 index 0000000..14943e6 --- /dev/null +++ b/led.py @@ -0,0 +1,126 @@ +""" +Soft-LED + +Mögliche Basisfarben: + Rot, Grün, Blau, Orange, Gelb, Aqua, Violett, Weiß + +Funktionen + Farbe einstellen + Helligkeit einstellen + Blitzen + Einschalten + Ausschalten + Leuchten, limitiert auf eine Zeitdauer + +TODO Eigene Drawingarea im Frontend + +""" + +import math +from gi.repository import GLib, Gtk + +colordef = { + 'red': ((1.0, 0.0, 0.0), "0xff0000"), + 'green': ((0.0, 1.0, 0.0), "0x00ff00"), + 'blue': ((0.0, 0.0, 1.0), "0x0000ff"), + 'orange': ((1.0, 0.6, 0.0), "0xff9900"), + 'yellow': ((1.0, 1.0, 0.0), "0xffff00"), + 'aqua': ((0.2, 0.4, 1.0), "0x3366ff"), + 'violet': ((1.0, 0.0, 0.4), "0xff0066"), + 'white': ((1.0, 1.0, 1.0), "0xffffff") +} + +class LED(): + + def __init__(self, parent): + # parent muß ein Gtk.Fixed-Objekt sein + self._colorname = "" + self._color = (0, 0, 0) # Farbe schwarz ist aus + self._brightness = 0.0 + self._color_off = (0.3725, 0.4275, 0.6078) # RGB(95, 109, 155) + self._pos_x = 30 + self._pos_y = 16 + self._radius = 4 + self._flashcounter = 0 + + # Box oberhalb des Displays + self._da = Gtk.DrawingArea() + parent.put(self._da, 64, 56) + self._da.set_size_request(400, 32) + self._da.connect("draw", self.on_draw) + + def on_draw(self, widget, ctx): + #Debug + #ctx.set_source_rgb(1, 0, 0) + #ctx.rectangle(0, 0, 400, 32) + #ctx.stroke() + ctx.set_source_rgb(*self._color_off) + ctx.arc(self._pos_x, self._pos_y, self._radius, 0, 2*math.pi) + ctx.fill_preserve() + ctx.set_source_rgb(0, 0, 0) # Schwarzer Rand + ctx.stroke() + ctx.arc(self._pos_x, self._pos_y, self._radius, 0, 2*math.pi) + ctx.stroke() + + def on_timer(self): + self.switchOff() + return True + + def on_flash(self): + if self._flashcounter > 0: + self._flashcounter -=1 + return True + return False + + def switchOn(self, duration=None): + self._brightness = 1.0 + self._ctx.save() + self._ctx.set_source_rgb(*self._color) + self._ctx.arc(self._pos_x, self._pos_y, self._radius, 0, 2*math.pi) + self._ctx.fill_preserve() + self._ctx.set_source_rgb((0, 0, 0)) # Schwarzer Rand + self._ctx.stroke() + self._ctx.restore() + if self._duration: + GLib.timeout_add_seconds(duration, self.on_flash) + + + def switchOff(self): + self._brightness = 0.0 + self._ctx.save() + self._ctx.set_source_rgb(*self._color_off) + self._ctx.arc(self._pos_x, self._pos_y, self._radius, 0, 2*math.pi) + self._ctx.fill_preserve() + self._ctx.set_source_rgb((0, 0, 0)) # Schwarzer Rand + self._ctx.stroke() + self._ctx.restore() + + def doFlash(self, count=1): + if (self._brightness > 0) or (count < 0): + # Wenn aktiv oder falscher Parameter, dann passiert nichts + return + if count > 10: + # maximal 10 Blitze + self._flashcounter = 10 + else: + self._flashcounter = count + GLib.timeout_add_seconds(0.2, self.on_flash) + + def setColor(self, colorname, brightness=1.0): + #TODO Helligkeit einstellen + colorname = colorname.lower() + if not colorname in colordef: + return + self._colorname = colorname + self._color = colordef[colorname][0] + + def getColor(self): + return self._color + + def getColorAsHex(self): + # TODO + return None + + def setBrightness(self, newbrightness): + # 0% bis 100%, ausgehend von der aktuellen Basisfarbe + pass diff --git a/obp60v.py b/obp60v.py index e627dfd..2f48b1a 100755 --- a/obp60v.py +++ b/obp60v.py @@ -109,6 +109,7 @@ import uuid import json from appdata import AppData +from led import LED import nmea0183 import pages @@ -359,13 +360,21 @@ class Frontend(Gtk.Window): self.connect("draw", self.on_draw) + self.fixed = Gtk.Fixed() + self.add(self.fixed) + + # OBP GUI self.da = Gtk.DrawingArea() self.da.add_events(Gdk.EventMask.BUTTON_PRESS_MASK|Gdk.EventMask.BUTTON_RELEASE_MASK) - self.add(self.da) + self.fixed.put(self.da, 0, 0) + self.da.set_size_request(530, 555) # für fixed benötigt self.da.connect("draw", self.da_draw) self.da.connect('button-press-event', self.da_button_press) self.da.connect('button-release-event', self.da_button_release) + # Flash LED + self.flashled = LED(self.fixed) # Soft Flash-LED + self.button_clicked = 0 # Geklickter Button vor Loslassen self.keylock = False self.pages = profile