Skip to content

Commit cf25945

Browse files
committed
Add value converter of percent-output_val to led_on()
1 parent 27f39d7 commit cf25945

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

neobot/neosoco.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -333,23 +333,6 @@ def _notify_sensory_device_data_changed(self):
333333
def _notify_motoring_device_data_changed(self):
334334
self._roboid._notify_motoring_device_data_changed()
335335

336-
def led_on(self, port: str, brightness: str):
337-
if isinstance(port, str) and isinstance(brightness, int):
338-
if port.lower() =='out1':
339-
self.write(Neosoco.OUTPUT_1, Util.round(brightness))
340-
elif port.lower() =='out2':
341-
self.write(Neosoco.OUTPUT_2, Util.round(brightness))
342-
elif port.lower() =='out3':
343-
self.write(Neosoco.OUTPUT_3, Util.round(brightness))
344-
elif port.lower() =='all':
345-
self.write(Neosoco.OUTPUT_1, Util.round(brightness))
346-
self.write(Neosoco.OUTPUT_2, Util.round(brightness))
347-
self.write(Neosoco.OUTPUT_3, Util.round(brightness))
348-
else:
349-
raise ValueError
350-
else:
351-
raise TypeError
352-
353336
def get_value(self, port: str):
354337
if isinstance(port, str):
355338
if port.lower() =='in1':
@@ -363,6 +346,42 @@ def get_value(self, port: str):
363346
elif port.lower() =='bat':
364347
return self.read(Neosoco.BATTERY)
365348
else:
349+
Util.print_error('Wrong value of port')
350+
raise ValueError
351+
else:
352+
raise TypeError
353+
354+
def led_on(self, port: str, brightness: str):
355+
try:
356+
cvt_dic = {
357+
'100': 255,
358+
'90': 230,
359+
'80': 204,
360+
'70': 179,
361+
'60': 153,
362+
'50': 128,
363+
'40': 102,
364+
'30': 77,
365+
'20': 51,
366+
'10': 26
367+
}
368+
cvt_val = cvt_dic[brightness]
369+
except KeyError:
370+
Util.print_error('Wrong value of percentage')
371+
raise ValueError
372+
if isinstance(port, str):
373+
if port.lower() =='out1':
374+
self.write(Neosoco.OUTPUT_1, cvt_val)
375+
elif port.lower() =='out2':
376+
self.write(Neosoco.OUTPUT_2, cvt_val)
377+
elif port.lower() =='out3':
378+
self.write(Neosoco.OUTPUT_3, cvt_val)
379+
elif port.lower() =='all':
380+
self.write(Neosoco.OUTPUT_1, cvt_val)
381+
self.write(Neosoco.OUTPUT_2, cvt_val)
382+
self.write(Neosoco.OUTPUT_3, cvt_val)
383+
else:
384+
Util.print_error('Wrong value of port')
366385
raise ValueError
367386
else:
368387
raise TypeError

neobot/util.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1717
# Boston, MA 02111-1307 USA
1818

19+
import sys
1920

2021
class Util(object):
2122
@staticmethod
@@ -26,4 +27,11 @@ def round(value):
2627
else:
2728
return int(0.5 + value)
2829
else:
29-
return 0
30+
return 0
31+
@staticmethod
32+
def print_message(message):
33+
sys.stdout.write("{}: {}\n".format('Debug', message))
34+
35+
@staticmethod
36+
def print_error(message):
37+
sys.stderr.write("{}: {}\n".format('Error', message))

test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
n = Neosoco()
44

5-
# case1) Turn on LED during 1s
6-
# n.led_on("out1", 255)
7-
# wait(1000)
5+
# case1) Turn on LED with 100% brightness during 1s
6+
n.led_on("out1", '100')
7+
wait(1000)
88

99
# case2) Turn on LED when the distance is under 10cm
10-
while True:
11-
if n.get_value('in1') < 10:
12-
n.led_on("out1", 255)
13-
else:
14-
n.led_on("out1", 0)
10+
# while True:
11+
# if n.get_value('in1') < 10:
12+
# n.led_on("out1", 255)
13+
# else:
14+
# n.led_on("out1", 0)

0 commit comments

Comments
 (0)