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
2
2
#
3
3
# SPDX-License-Identifier: MIT
4
4
"""
5
5
`cedargrove_rangeslicer`
6
6
================================================================================
7
- cedargrove_rangeslicer.py 2022-09-14 v3.10 05:58PM
7
+ cedargrove_rangeslicer.py 2022-09-14 v3.10 07:29PM
8
8
RangeSlicer is a CircuitPython class for scaling a range of input values into
9
9
indexed/quantized output values. Output slice hysteresis is used to provide
10
10
dead-zone squelching.
24
24
25
25
# pylint: disable=too-many-instance-attributes
26
26
class Slicer :
27
- """The RangeSlicer class."""
27
+ """The primary RangeSlicer class."""
28
28
29
29
# pylint: disable=too-many-arguments
30
30
def __init__ (
@@ -69,9 +69,10 @@ def __init__(
69
69
self ._hyst_factor = hyst_factor # Hysteresis factor parameter
70
70
self ._out_integer = out_integer # Index output data type parameter
71
71
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
75
76
76
77
self ._update_param () # Establish the parameters for range_slicer helper
77
78
@@ -133,9 +134,7 @@ def slice(self):
133
134
@slice .setter
134
135
def slice (self , size = 1.0 ):
135
136
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" )
139
138
self ._slice = size
140
139
self ._update_param () # Update the parameters for range_slicer helper
141
140
@@ -170,37 +169,37 @@ def range_slicer(self, range_in=0):
170
169
- ((idx_mapped - self ._out_span_max ) % self ._slice )
171
170
) / self ._slice
172
171
slice_thresh = (slice_num * self ._slice ) + self ._out_span_max
172
+
173
173
upper_zone_limit = slice_thresh + (2 * self ._hyst_band )
174
174
175
- # if idx_mapped <= upper_zone_limit and idx_mapped >= slice_thresh:
176
175
if upper_zone_limit >= idx_mapped >= slice_thresh :
177
176
if self ._in_zone != slice_thresh :
178
177
self ._in_zone = slice_thresh
178
+
179
179
if idx_mapped > self ._old_idx_mapped :
180
180
self ._index = slice_thresh - self ._slice
181
181
if idx_mapped < self ._old_idx_mapped :
182
182
self ._index = slice_thresh
183
183
else :
184
184
self ._in_zone = None
185
185
self ._index = slice_thresh
186
+
186
187
if self ._out_span_min <= self ._out_span_max :
187
188
self ._index = max (min (self ._index , self ._out_span_max ), self ._out_span_min )
188
189
else :
189
190
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 )
195
191
192
+ change_flag = bool (self ._index != self ._old_idx_mapped )
196
193
self ._old_idx_mapped = idx_mapped
194
+
197
195
if self ._out_integer :
198
196
return int (self ._index ), change_flag
199
197
return self ._index , change_flag
200
198
201
199
def _mapper (self , map_in ):
202
200
"""_mapper: Determines the linear output value based on the input value
203
201
using the linear slope-intercept form y = mx + b ."""
202
+
204
203
if (self ._in_min == self ._in_max ) or (self ._out_span_min == self ._out_span_max ):
205
204
return self ._out_span_min
206
205
mapped = (
@@ -211,15 +210,9 @@ def _mapper(self, map_in):
211
210
return max (min (mapped , self ._out_span_max ), self ._out_span_min )
212
211
return min (max (mapped , self ._out_span_max ), self ._out_span_min )
213
212
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
-
221
213
def _update_param (self ):
222
214
"""Recalculate spans and hysteresis value when parameters change."""
215
+
223
216
# Update output span parameters
224
217
if self ._out_min > self ._out_max :
225
218
self ._out_span_min = self ._out_min + self ._slice
@@ -234,17 +227,14 @@ def _update_param(self):
234
227
else :
235
228
self ._out_span_dir = - 1
236
229
237
- # self._out_span_dir = self._sign(self._out_span)
238
-
239
230
# Update slice size parameter
240
231
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
246
235
self ._hyst_band = self ._hyst_factor * self ._slice
247
236
self ._in_zone = None
237
+
248
238
# Update index parameters
249
239
self ._index = 0
250
240
self ._old_idx_mapped = 0
0 commit comments