Skip to content

Commit 80f6354

Browse files
committed
First commit of remote_button() API
1 parent 46a2b10 commit 80f6354

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

neopia/neosoco.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,17 @@ class Neosoco(Robot):
300300

301301
_SERVO_STOP = 254
302302
_SERVO_RESET_DEG = 186
303+
304+
_REMOTE_BTN_CVT = {
305+
'up': 1,
306+
'down': 2,
307+
'left': 3,
308+
'right': 4,
309+
'1': 10,
310+
'2': 11,
311+
'3': 12,
312+
'4': 13,
313+
}
303314

304315
_robots = {}
305316

@@ -472,7 +483,7 @@ def led_on(self, port='out1', brightness='100'):
472483
else:
473484
raise TypeError
474485

475-
def led_off(self, port='port1'):
486+
def led_off(self, port='out1'):
476487
if isinstance(port, str):
477488
if port.lower() =='out1':
478489
self.write(Neosoco.OUTPUT_1, 0)
@@ -692,19 +703,19 @@ def buzzer(self, pitch='3', note='c', beats='4'):
692703
else:
693704
pitch = Neosoco._NOTES[note.lower()] + (int(pitch) - 1) * 12
694705
if isinstance(pitch, (int, float)):
695-
cvt_dic = {
706+
bpm_cvt = {
696707
'2': 1,
697708
'4': 0.5,
698709
'8': 0.25,
699710
'16': 0.125
700711
}
701-
if beats in cvt_dic.keys():
712+
if beats in bpm_cvt.keys():
702713
bpm = self._bpm # default 60
703714
if note == 0:
704715
self.write(Neosoco.NOTE, Neosoco._NOTE_OFF)
705-
Runner.wait(cvt_dic[beats] * 60 * 1000.0 / bpm)
716+
Runner.wait(bpm_cvt[beats] * 60 * 1000.0 / bpm)
706717
else:
707-
timeout = cvt_dic[beats] * 60 * 1000.0 / bpm
718+
timeout = bpm_cvt[beats] * 60 * 1000.0 / bpm
708719
tail = 0
709720
if timeout > 100:
710721
tail = 100
@@ -723,3 +734,15 @@ def buzzer_by_port(self, port='in1'):
723734
self.write(Neosoco.NOTE, value)
724735
else:
725736
raise TypeError
737+
738+
def remote_button(self, button='1'):
739+
if isinstance(button, str):
740+
if button in self._REMOTE_BTN_CVT.keys():
741+
if self._REMOTE_BTN_CVT[button] == self.read(Neosoco.REMOCTL):
742+
return True
743+
else:
744+
return False
745+
else:
746+
raise ValueError('Wrong value of button')
747+
else:
748+
raise TypeError

test.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@
8989
# wait(1000)
9090

9191
# case 12) Rotate servo motor forward by 120 degrees at 50% speed within 3 seconds
92-
n.servo_reset_degree('out1')
93-
n.servo_rotate_by_degree('out1', 'forward', '50', '120')
94-
wait(3000)
92+
# n.servo_reset_degree('out1')
93+
# n.servo_rotate_by_degree('out1', 'forward', '50', '120')
94+
# wait(3000)
95+
96+
# case 13) When the 1 button of remote controller is pressed, turn on the LED
97+
while True:
98+
if n.remote_button('1'):
99+
n.led_on() # By default value
100+
else:
101+
n.led_off()

0 commit comments

Comments
 (0)