Files
detektor/src/channel.py
2025-03-10 17:10:04 +01:00

64 lines
1.9 KiB
Python

import logging
import uuid
from enum import Enum
from callbacks import CallbackDispatcher, CallbackType
class ChannelUnit(Enum):
VOLTS=1
PPM=2
AMPERS=3
CELSIUS=4
class Channel:
name: str = ""
_active: bool = True
number: int = 0
unit: ChannelUnit
data = []
color: str = ""
_slice = []
def __init__(self):
self.id = uuid.uuid1()
@property
def active(self):
return self._active
def toggle_active(self):
self._active = not self._active
if self._active:
logging.debug(f'Enabling channel {self.name} ({self.id})')
CallbackDispatcher().call(CallbackType.ENABLE_CHANNEL, self.id)
else:
logging.debug(f'Disabling channel {self.name} ({self.id})')
CallbackDispatcher().call(CallbackType.DISABLE_CHANNEL, self.id)
def calibrate_data(self, offset: float=0, multiple: float=1, start=None, end=None):
"""
'Calibrates' internal data by adding offset and multiplying all numbers
Updates the chart series
Marks that the data has been tainted and should be saved or discarded upon exit
"""
if start and end:
self.data[start:end] = [(x + offset) * multiple for x in self.data[start:end]]
else:
self.data = [(x + offset) * multiple for x in self.data]
logging.debug(f'Calibrating channel {self.name} ({self.id})')
CallbackDispatcher().call(CallbackType.UPDATE_CHANNEL, self.id, True)
CallbackDispatcher().call(CallbackType.DATA_TAINTED)
def copy(self, start: int, end: int):
self._slice = self.data[start:end + 1]
def cut(self, start: int, end: int):
# save the cut and remove it from active data
self.copy(start, end)
self.data[start:end + 1] = []
def paste(self, at: int):
self.data[at:at] = self._slice