Skip to content

Commit e14152c

Browse files
committed
First commit convert_scale() API
1 parent ff13122 commit e14152c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

neopia/neosoco.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,37 @@ def get_value(self, port='in1'):
368368
else:
369369
raise TypeError
370370

371+
def convert_scale(self, port='in1', omin=0, omax=255, tmin=0, tmax=100):
372+
if isinstance(port, str):
373+
if port.lower() =='in1':
374+
value = self.read(Neosoco.INPUT_1)
375+
elif port.lower() =='in2':
376+
value = self.read(Neosoco.INPUT_2)
377+
elif port.lower() =='in3':
378+
value = self.read(Neosoco.INPUT_3)
379+
else:
380+
raise ValueError('Wrong value of port')
381+
if isinstance(omin, (int, float)) and isinstance(omax, (int, float)) and \
382+
isinstance(tmin, (int, float)) and isinstance(tmax, (int, float)):
383+
if omin > omax:
384+
temp = omin
385+
omin = omax
386+
omax = temp
387+
if tmin > tmax:
388+
temp = tmin
389+
tmin = tmax
390+
tmax = temp
391+
value -= omin
392+
value = value * ((tmax - tmin) / (omax - omin))
393+
value += tmin
394+
value = min(tmax, value)
395+
value = max(tmin, value)
396+
return Util.round(value)
397+
else:
398+
raise TypeError
399+
else:
400+
raise TypeError
401+
371402
def led_on(self, port='out1', brightness='100'):
372403
cvt_dic = {
373404
'100': 255,

test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,9 @@
5050
# n.buzzer('5', 'db', '16')
5151

5252
# case7) Play a sound by value from input port
53-
while True:
54-
n.buzzer_by_port('in1')
53+
# while True:
54+
# n.buzzer_by_port('in1')
55+
56+
# case8) Color LED on with variable color by input port
57+
# r = g = b = n.convert_scale('in1', 0, 255, 85, 170) # Limit to middle brightness
58+
# color_led_on('out1', r, g, b)

0 commit comments

Comments
 (0)