1- # SPDX-FileCopyrightText: Copyright (c) 2020, 2021, 2022 JG for Cedar Grove Maker Studios
1+ # SPDX-FileCopyrightText: Copyright (c) 2022 JG for Cedar Grove Maker Studios
22#
33# SPDX-License-Identifier: MIT
44"""
55`cedargrove_rangeslicer`
66================================================================================
7- cedargrove_rangeslicer.py 2022-09-14 v3.10 05:58PM
7+ cedargrove_rangeslicer.py 2022-09-14 v3.10 07:29PM
88RangeSlicer is a CircuitPython class for scaling a range of input values into
99indexed/quantized output values. Output slice hysteresis is used to provide
1010dead-zone squelching.
2424
2525# pylint: disable=too-many-instance-attributes
2626class Slicer :
27- """The RangeSlicer class."""
27+ """The primary RangeSlicer class."""
2828
2929 # pylint: disable=too-many-arguments
3030 def __init__ (
@@ -69,9 +69,10 @@ def __init__(
6969 self ._hyst_factor = hyst_factor # Hysteresis factor parameter
7070 self ._out_integer = out_integer # Index output data type parameter
7171
72- self ._in_zone = None # Define variable for later use in the class
73- self ._index = None # Define variable for later use in the class
74- self ._old_idx_mapped = None # Define variable for later use in the class
72+ # Define variables for later us in the class
73+ self ._in_zone = None
74+ self ._index = None
75+ self ._old_idx_mapped = None
7576
7677 self ._update_param () # Establish the parameters for range_slicer helper
7778
@@ -133,9 +134,7 @@ def slice(self):
133134 @slice .setter
134135 def slice (self , size = 1.0 ):
135136 if size <= 0 :
136- raise RuntimeError (
137- "Setter: Invalid Slice setting; value must be greater than zero"
138- )
137+ raise RuntimeError ("Invalid Slice value; must be greater than zero" )
139138 self ._slice = size
140139 self ._update_param () # Update the parameters for range_slicer helper
141140
@@ -170,37 +169,37 @@ def range_slicer(self, range_in=0):
170169 - ((idx_mapped - self ._out_span_max ) % self ._slice )
171170 ) / self ._slice
172171 slice_thresh = (slice_num * self ._slice ) + self ._out_span_max
172+
173173 upper_zone_limit = slice_thresh + (2 * self ._hyst_band )
174174
175- # if idx_mapped <= upper_zone_limit and idx_mapped >= slice_thresh:
176175 if upper_zone_limit >= idx_mapped >= slice_thresh :
177176 if self ._in_zone != slice_thresh :
178177 self ._in_zone = slice_thresh
178+
179179 if idx_mapped > self ._old_idx_mapped :
180180 self ._index = slice_thresh - self ._slice
181181 if idx_mapped < self ._old_idx_mapped :
182182 self ._index = slice_thresh
183183 else :
184184 self ._in_zone = None
185185 self ._index = slice_thresh
186+
186187 if self ._out_span_min <= self ._out_span_max :
187188 self ._index = max (min (self ._index , self ._out_span_max ), self ._out_span_min )
188189 else :
189190 self ._index = min (max (self ._index , self ._out_span_max ), self ._out_span_min )
190- """if self._index != self._old_idx_mapped:
191- change_flag = True
192- else:
193- change_flag = False"""
194- change_flag = bool (self ._index != self ._old_idx_mapped )
195191
192+ change_flag = bool (self ._index != self ._old_idx_mapped )
196193 self ._old_idx_mapped = idx_mapped
194+
197195 if self ._out_integer :
198196 return int (self ._index ), change_flag
199197 return self ._index , change_flag
200198
201199 def _mapper (self , map_in ):
202200 """_mapper: Determines the linear output value based on the input value
203201 using the linear slope-intercept form y = mx + b ."""
202+
204203 if (self ._in_min == self ._in_max ) or (self ._out_span_min == self ._out_span_max ):
205204 return self ._out_span_min
206205 mapped = (
@@ -211,15 +210,9 @@ def _mapper(self, map_in):
211210 return max (min (mapped , self ._out_span_max ), self ._out_span_min )
212211 return min (max (mapped , self ._out_span_max ), self ._out_span_min )
213212
214- """def _sign(self, x):
215- "Determines the sign of a numeric value. Zero is evaluated as a
216- positive value."
217- if x >= 0:
218- return 1
219- return -1"""
220-
221213 def _update_param (self ):
222214 """Recalculate spans and hysteresis value when parameters change."""
215+
223216 # Update output span parameters
224217 if self ._out_min > self ._out_max :
225218 self ._out_span_min = self ._out_min + self ._slice
@@ -234,17 +227,14 @@ def _update_param(self):
234227 else :
235228 self ._out_span_dir = - 1
236229
237- # self._out_span_dir = self._sign(self._out_span)
238-
239230 # Update slice size parameter
240231 if self ._slice <= 0 :
241- raise RuntimeError (
242- "_update_param: Invalid Slice value; must be greater than zero"
243- )
244- # Update hysteresis parameters: calculate hysteresis band size,
245- # reset in-zone state
232+ raise RuntimeError ("Invalid Slice value; must be greater than zero" )
233+
234+ # Update hysteresis band size; reset in-zone state
246235 self ._hyst_band = self ._hyst_factor * self ._slice
247236 self ._in_zone = None
237+
248238 # Update index parameters
249239 self ._index = 0
250240 self ._old_idx_mapped = 0
0 commit comments