uun_iot_libledstrip package
Subpackages
LedStrip module
- class uun_iot_libledstrip.LedStrip.LedStrip(device: LedDev, segments: Union[List[LedStripSegment], Dict[Hashable, LedStripSegment]])
Bases:
LedStripInterfaceA collection of LedStripSegments forming a led strip.
- Parameters
device – an underlying LedDev instance
segments – dictionary with values being LedStripSegment objects. Specifies segments of the led strip based on dictionary keys. If list of LedStripSegment(s) is given instead, the segments in the list will be addressable by list indices (0, 1, …. n).
- Raises
TypeError – when segments is nor list nor dictionary
- activate(action: Optional[Action] = None, segment_id=None)
Activate an action for segment identified by
segment_id.- Parameters
action – Action to be applied to segment using
segment.activate(action). IfNone, activate segments stored action. Defaults toNone.segment_id – segment’s key in segment dictionary. If
None, apply to all segments. Defaults toNone.
- clear(segment_id: Optional[Hashable] = None)
Clear a segment, this includes clearing blinking. Set
segment_idtoNone(default) clear whole strip.
- clear_leds(leds: List[int])
Clear LEDs. This will not stop blinking nor .show() the strip.
Set color of specified LEDs to (0,0,0).
- Parameters
leds – list of LEDs to be cleared
- clear_strip()
Clear LED strip in one go. Prevent sending :meth`:.show` multiple times from each segment if
autoshow=Truefor some segments. This will not .show() the strip.
- set_color(col: tuple, segment_id=None)
Set color of a segment.
- Parameters
segment_id – segment’s key in segment dictionary. If
None, set color of all segments.col – RGB tuple
- set_color_blink(col, period, segment_id=None)
Set blinking of a segment.
- Parameters
col – RGB color tuple
period – period of blinking in seconds
segment_id – segment’s key in segment dictionary. If None, set color of all segments.
- show()
- class uun_iot_libledstrip.LedStrip.LedStripInterface
Bases:
ABC- abstract activate(*args, **kwargs)
- abstract clear()
- abstract set_color(*args, **kwargs)
- abstract set_color_blink(*args, **kwargs)
- abstract show()
- class uun_iot_libledstrip.LedStrip.LedStripSegment(device: LedDev, leds: List[int], autoshow: bool)
Bases:
LedStripInterfaceCreate an autonomous segment in a LED strip.
- Parameters
device – an underlying LedDev instance
leds – a list with pixel IDs. A segment will be created from these IDs. These does not have to be consecutive IDs.
autoshow – invoke
show()after everyset_color()call if set toFalse, blinking will still triggershow()(as expected)
- activate(action: Optional[Action] = None)
Invoke action.
- Parameters
action – Action instance. If
None, invoke stored action. Seestore_action().- Raises
ValueError –
actionisNoneand no action was stored earlier
- clear(clear_blink: bool = True)
Clear segment.
Set color of the segment to
(0,0,0).- Parameters
clear_blink – pass the same value to function :meth”.set_color, see documentation there. Defaults to
True.
- clear_blink()
Stop blinking.
- property leds
Read-only list of available leds.
- set_color(col: tuple, clear_blink: bool = True)
Set color of whole strip and display immediately.
- Parameters
col – RGB tuple
clear_blink – clear any blinking present before setting colors, defaults to
True
- set_color_blink(col: tuple, period: float, runonstart: bool = True)
Specify blinking.
Blink the specified color with period (s). Whole strip is
show()-n after each blink (as each segment can have different period, there is no other way to do this without checking for same period of all segments).- Parameters
col – RGB tuple color to blink with
period – period of blinking (seconds)
runonstart – if True, begin the cycle with setting color. Otherwise, wait first for
periods. Defaults to True
- show()
Refreshes WHOLE hardware strip, not only this segment.
- store_action(action: Action)
Store action.
Store action so that one does not have to specify colors (or blinking) every time. Invoke stored
actionwithactivate().- Parameters
action –
Actionobject to be stored
- class uun_iot_libledstrip.LedStrip.StripOverlayBundle(strips: Union[List[LedStrip], Dict[Hashable, LedStrip]])
Bases:
objectA class to pack different
LedStripinstances and set a single currently active strip among them.This can be used to add multiple different exclusive display modes (meaning exactly one of them will be active at a time) to the physical strip. Main reason for using this class is only logical with the advantage being confidence, that the various bundled strips will accidentally not be active at the same time.
Example
The
StripOverlayBundlewill act as a meter (MeterStrip) in normal circumstances but will be replaced by an errorLedStripin case of an error:error_action = Action(type=ActionType.BLINK, color=(255,0,0), period=.2) class State(Enum): NORMAL=1, ERROR=2 overlays = { State.NORMAL: MeterStrip(leddevice, segments), State.ERROR: LedStrip(leddevice, segments2) } overlay = StripOverlayBundle(overlays) if status=="OK": overlay.set_strip(State.NORMAL) overlay.strip.set_value(25) else: overlay.set_strip(State.ERROR) overlay.strip.activate(error_action) overlay.strip.show()
- Parameters
strips – dictionary with values being LedStrip objects. Specifies overlays of the real physical strip based on dictionary keys. If list of LedStrip(s) is given instead, the strips in the list will be addressable by list indices
(0, 1, ..., n).- Raises
TypeError – when
segmentsis nor a list nor a dictionary
- set_strip(index: Hashable)
Display/set active overlay given by
index.Clear currently active strip (if set and is different from a new one) and replace
stripwith a strip on positionindex. This does not affect the underlyingLedDevdevice immedieately (ie.self.strip.show()is needed to propagate changes from the new strip to underlying LedDev device).- Parameters
index – index of strip to be set as visible
MeterStrip module
- class uun_iot_libledstrip.MeterStrip.MeterStrip(device: LedDev, segments: Union[List[MeterStripSegment], Dict[Hashable, MeterStripSegment]])
Bases:
LedStripVisualize amount of some quantity on a
LedStrip.Multiple segments of the strip can be specified to indicate additional meaning by visual distinction of each segment.
The segments do not have to span an interval. In the case there is a “hole” (set value may not be in range of some segment) everything will still behave as expected, see definition of
MeterStripSegment.set_value().- set_value(value: float, action: Optional[Action] = None)
Set displayed value on the meter and invoke action on activated LEDs.
Sets value of segments and invokes
actionon them, or stored action if action is None. Value can be out of range of the segments.- Parameters
value – value to be displayed, see
set_value()for more informationaction – Action to invoke on activated LEDs in segments. If None, invoke stored action of the segment.
- class uun_iot_libledstrip.MeterStrip.MeterStripSegment(device: LedDev, autoshow: bool, leds: List[int], *, value_min: float, value_max: Optional[float], hook_set_value: Optional[Callable[[float, List[int], Action], Tuple[List[int], Action]]] = None)
Bases:
LedStripSegmentVisualize amount of some quantity on a LED strip (segment).
This is done by linear interpolation between minimum and maximum possible value - maximum meaning all LEDs are lit. Based on dynamically varying amount of leds in LedStripSegment - number of leds depends on actual value of the measured quantity. Quantity is visualized from left to right – left slots are allocated first. The Segment has to be comprised of succesive leds.
list
ledsand floatsvalue_minandvalue_maxare all inclusive – boundary points are achieved- Parameters
device – underlying LedDev device
autoshow – propagate every state change directly to hardware. Otherwise, a
show()is needed to propagate changes explicitly.leds – a list of succesive integers (IDs of LEDs) forming the LED segment. These IDs are inclusive - maximum ID is achieved for maximal value.
value_min – inclusive lower bound on value
value_max – inclusive upper bound on value.
value_maxcan be unbounded (None), indicating that the whole segment is fully active (all leds are activated) whenever value > value_min.hook_set_value – a function (float, List[int], Action) -> (List[int], Action) which, if specified, will be called at the end of
set_value()asself._leds, self._action = hook_set_value(value, self._leds, self._action),valuebeing currently set value,self._ledsbeing a int list of current LED IDs andself._actionbeing currently stored action.
- Raises
ValueError – when
ledsis not a list of succesive integersValueError – when
value_minis equal tovalue_max
Examples
Variable action based on set value:
Example using the
hook_set_valueargument for changing stored action depending on the value. If it is too large (in this example, bigger than 100), store action with red color, otherwise store green color:def hook(value, leds, action): if value > 100: action = Action(ActionType.SOLID, color=(255,0,0)) else: action = Action(ActionType.SOLID, color=(0,255,0)) return leds, action segment = MeterStripSegment( device, autoshow=True, leds=[0,1,2,3,4], value_min=5, value_max=200, hook_set_value=hook ) # definition of MeterStrip strip containing the segment strip = MeterStrip(device,[segment]) strip.set_value(200) strip.activate() strip.set_value(50) strip.activate()
>>> # the whole segment is lit red (255, 0, 0) >>> # some LEDs in the segment are lit green (0, 255, 0)
- set_value(value: float)
Display the value on the segment.
If the value is too large (larger or equal to
value_max, orvalue_maxisNone), light ALL leds in the segment. If the value is too low (strictly lower thanvalue_min), light NO leds. Otherwise, there will be some active leds.If
hook_set_value()was set during initialization, call the function at last with the newly modified values. See initialization for more information abouthook_set_value()- Parameters
value – value to be shown on the segment. Corresponding number of LEDs will be set active - the percentage of active leds will be the same as
value/(value_max-value_min). LEDs are set active from lowest to highest IDs.
Module contents
- class uun_iot_libledstrip.Action(type: ActionType, color: tuple, period: Optional[float] = None)
Bases:
objectObject representing possible visualization actions on
LedStrip.Light LED up with given color or blink with given color and specified period.
- color: tuple
- period: Optional[float] = None
- type: ActionType
- class uun_iot_libledstrip.ActionType(value)
Bases:
EnumAn enumeration.
- BLINK = 2
Blinking
- SOLID = 1
Static color
- uun_iot_libledstrip.hex2rgb(value: str) tuple
- uun_iot_libledstrip.rgb2hex(rgb: tuple) str