Skip to content

Commit ff13122

Browse files
committed
First commit buzzer_by_port() of API
1 parent bf38a69 commit ff13122

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

neopia/neosoco.py

Lines changed: 21 additions & 0 deletions
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 math
1920

2021
from neopia.runner import Runner
2122
from neopia.util import Util
@@ -473,6 +474,26 @@ def buzzer(self, pitch='3', note='c', beats='4'):
473474
else:
474475
raise ValueError('Wrong value of beats')
475476

477+
def buzzer_by_port(self, port='in1'):
478+
if isinstance(port, str):
479+
if port.lower() =='in1':
480+
value = self.read(Neosoco.INPUT_1)
481+
elif port.lower() =='in2':
482+
value = self.read(Neosoco.INPUT_2)
483+
elif port.lower() =='in3':
484+
value = self.read(Neosoco.INPUT_3)
485+
else:
486+
raise ValueError('Wrong value of port')
487+
488+
if value:
489+
# Map to 0~65 from 0~100, it's same as Entry
490+
value = max(value, 0)
491+
value = min(value, 100)
492+
value = math.ceil(value / 100 * 65)
493+
self.write(Neosoco.NOTE, value)
494+
else:
495+
raise TypeError
496+
476497
def wheels(self, left_velocity, right_velocity=None):
477498
self.write(Neosoco.LINE_TRACER_MODE, Neosoco.LINE_TRACER_MODE_OFF)
478499
if isinstance(left_velocity, (int, float)):

test.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@
4040
# n.motor_move('stop')
4141

4242
# case6) Play same note by pitch, sharp and flat, and a length of note
43-
n.buzzer('3', n.NOTE_NAME_C)
44-
n.buzzer('3', 'c')
43+
# n.buzzer('3', n.NOTE_NAME_C)
44+
# n.buzzer('3', 'c')
4545

46-
n.buzzer('4', n.NOTE_NAME_C_SHARP, '8')
47-
n.buzzer('4', 'c#', '8')
46+
# n.buzzer('4', n.NOTE_NAME_C_SHARP, '8')
47+
# n.buzzer('4', 'c#', '8')
4848

49-
n.buzzer('5', n.NOTE_NAME_D_FLAT, '16')
50-
n.buzzer('5', 'db', '16')
49+
# n.buzzer('5', n.NOTE_NAME_D_FLAT, '16')
50+
# n.buzzer('5', 'db', '16')
51+
52+
# case7) Play a sound by value from input port
53+
while True:
54+
n.buzzer_by_port('in1')

0 commit comments

Comments
 (0)