Skip to content

Commit 7e83c5e

Browse files
committed
Added a new audio example, and tweaks to others
1 parent 1fa59dd commit 7e83c5e

13 files changed

Lines changed: 82 additions & 34 deletions

File tree

examples/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- [Read ADCs](#read-adcs)
55
- [Set GPIOs](#set-gpios)
66
- [Read GPIOs](#read-gpios)
7-
- [Read Encoders](#read-encoders)
7+
- [Read Encoders](#read-encoders)
88
- [Read Internals](#read-internals)
99
- [LED Rainbow](#led-rainbow)
1010
- [Reset Inventor](#reset-inventor)
@@ -30,6 +30,7 @@
3030
- [Servo Wave](#servo-wave)
3131
- [Calibration](#calibration)
3232
- [Audio Examples](#audio-examples)
33+
- [Play Sound](#play-sound)
3334
- [Motor Song](#motor-song)
3435
- [Random Droid](#random-droid)
3536
- [Extra Examples](#extra-examples)
@@ -201,6 +202,12 @@ Shows how to configure Inventor HAT Mini's servos with different common calibrat
201202

202203
## Audio Examples
203204

205+
### Play Sound
206+
[audio/play_sound.py](audio/play_sound.py)
207+
208+
Play a WAV audio file from your Inventor HAT Mini!
209+
210+
204211
### Motor Song
205212
[audio/motor_song.py](audio/motor_song.py)
206213

examples/audio/ahoy.wav

181 KB
Binary file not shown.

examples/audio/play_sound.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pygame
2+
from inventorhatmini import InventorHATMini
3+
4+
"""
5+
Play a WAV audio file from your Inventor HAT Mini!
6+
7+
Press "User" to play the sound.
8+
9+
Note: WAV files need to be signed 16-bit
10+
"""
11+
12+
# Create a new InventorHATMini
13+
board = InventorHATMini(init_leds=False)
14+
15+
# Initialise PyGame so we can play sounds with it
16+
pygame.init()
17+
18+
# Load the sound from file
19+
ahoy = pygame.mixer.Sound("./ahoy.wav")
20+
21+
# Loop forever
22+
while True:
23+
if board.switch_pressed():
24+
# Play the sound
25+
channel = ahoy.play()
26+
print("Playing ... ", end="")
27+
28+
# Wait for the sound to finish playing
29+
while channel.get_busy():
30+
pass
31+
32+
print("Done")

examples/audio/random_droid.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
Press "User" to play a random word out of the speaker.
1313
"""
1414

15-
# Constnats
15+
# Constants
1616
MIN_WORD_LENGTH = 3 # The smallest word size
1717
MAX_WORD_LENGTH = 10 # The largest word size
1818
WAIT_TIME = 0.25 # The time (in seconds) to wait after playing a word, before accepting user input again
@@ -24,14 +24,14 @@
2424

2525
# Generate the characters from `c1` to `c2`, inclusive.
2626
def char_range(c1, c2):
27-
for c in range(ord(c1), ord(c2)+1):
27+
for c in range(ord(c1), ord(c2) + 1):
2828
yield chr(c)
2929

3030

3131
# Generate a random word from the letters a to z.
3232
def random_string(word_length=5, no_repeat=True):
3333
chars = list(char_range("a", "z"))
34-
word =""
34+
word = ""
3535
char_count = 0
3636
while char_count != word_length:
3737
char = chars[random.randint(0, 25)]
@@ -40,7 +40,7 @@ def random_string(word_length=5, no_repeat=True):
4040
else:
4141
word += char
4242
char_count += 1
43-
print(word)
43+
print("Saying:", word)
4444
return word
4545

4646

examples/extras/gpio_encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
board = InventorHATMini(init_leds=False)
1616

1717
# Create an Encoder object using two GPIO pins
18-
enc = board.encoder_from_gpios(CHANNEL, GPIO_1, GPIO_2)
18+
enc = board.encoder_from_gpio_pins(CHANNEL, GPIO_1, GPIO_2)
1919

2020
# Uncomment the below line (and the top imports) to
2121
# reverse the counting direction of the encoder

examples/extras/read_servo_pins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import time
2-
from inventorhatmini import InventorHATMini, NUM_SERVOS
2+
from inventorhatmini import InventorHATMini, NUM_SERVOS, LED_SERVO_1
33
from ioexpander import IN # or IN_PU of a pull-up is wanted
44

55
"""
@@ -31,9 +31,9 @@
3131
# Set the neighbouring LED to a colour based on
3232
# the input, with Green for high and Blue for low
3333
if value:
34-
board.leds.set_hsv(i, 0.333, 1.0, BRIGHTNESS)
34+
board.leds.set_hsv(i + LED_SERVO_1, 0.333, 1.0, BRIGHTNESS)
3535
else:
36-
board.leds.set_hsv(i, 0.666, 1.0, BRIGHTNESS)
36+
board.leds.set_hsv(i + LED_SERVO_1, 0.666, 1.0, BRIGHTNESS)
3737

3838
# Print a new line
3939
print()

examples/extras/servo_pin_motor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
2+
import math
23
from inventorhatmini import InventorHATMini, SERVO_2, SERVO_3
3-
# from ioexpander.common import REVERSED_DIR
4+
from ioexpander.common import NORMAL_DIR # REVERSED_DIR
45

56
"""
67
Shows how to use Inventor HAT Mini's Servo headers to control an externally connected motor driver.
@@ -23,7 +24,7 @@
2324
board = InventorHATMini(init_servos=False, init_leds=False)
2425

2526
# Create an Encoder object using two GPIO pins
26-
motor = board.motor_from_servo_pins(CHANNEL, SERVO_2, SERVO_3, direction=NORMAL_DIR)
27+
motor = board.motor_from_servo_pins(SERVO_2, SERVO_3, direction=DIRECTION)
2728

2829

2930
offset = 0.0
@@ -46,7 +47,7 @@ def sleep_until(end_time):
4647
offset += SPEED / 1000.0
4748

4849
# Update the motor
49-
angle = (i + offset) * math.pi
50+
angle = offset * math.pi
5051
motor.speed(math.sin(angle) * SPEED_EXTENT)
5152

5253
# Sleep until the next update, accounting for how long the above operations took to perform

examples/extras/set_servo_pins.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import time
2-
from inventorhatmini import InventorHATMini, NUM_SERVOS
2+
from inventorhatmini import InventorHATMini, NUM_SERVOS, LED_SERVO_1
33
from ioexpander import OUT
44

55
"""
@@ -28,17 +28,17 @@
2828

2929
# Set each servos in turn and print its value
3030
for i in range(NUM_SERVOS):
31-
31+
# Set the pin to high if this is the current servo pin, otherwise low
3232
value = (i == current_servo)
33-
board.servo_pin_value(value)
33+
board.servo_pin_value(i, value)
3434
print(SERVO_NAMES[i], " = ", value, sep="", end=", ")
3535

3636
# Set the neighbouring LED to a colour based on
3737
# the output, with Green for high and Blue for low
3838
if value:
39-
board.leds.set_hsv(i, 0.333, 1.0, BRIGHTNESS)
39+
board.leds.set_hsv(i + LED_SERVO_1, 0.333, 1.0, BRIGHTNESS)
4040
else:
41-
board.leds.set_hsv(i, 0.666, 1.0, BRIGHTNESS)
41+
board.leds.set_hsv(i + LED_SERVO_1, 0.666, 1.0, BRIGHTNESS)
4242

4343
# Print a new line
4444
print()
@@ -50,5 +50,9 @@
5050

5151
time.sleep(0.5)
5252

53+
# Set all the servo pins back to low
54+
for i in range(NUM_SERVOS):
55+
board.servo_pin_value(i, False)
56+
5357
# Turn off the LED bars
5458
board.leds.clear()

examples/read_adcs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919

2020
# Setup each GPIO as an analog input
2121
for i in range(NUM_GPIOS):
22-
board.gpio_mode(i, ADC)
22+
board.gpio_pin_mode(i, ADC)
2323

2424
# Read the ADCs until the user button is pressed
2525
while not board.switch_pressed():
2626

2727
# Read each ADC in turn and print its voltage
2828
for i in range(NUM_GPIOS):
29-
voltage = board.gpio_value(i)
29+
voltage = board.gpio_pin_value(i)
3030
print(ADC_NAMES[i], " = ", round(voltage, 3), sep="", end=", ")
3131

3232
# Set the neighbouring LED to a colour based on the

examples/read_gpios.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import time
2-
from inventorhatmini import InventorHATMini, NUM_GPIOS
2+
from inventorhatmini import InventorHATMini, NUM_GPIOS, LED_GPIO_1
33
from ioexpander import IN # or IN_PU of a pull-up is wanted
44

55
"""
@@ -18,22 +18,22 @@
1818

1919
# Setup each GPIO as an input
2020
for i in range(NUM_GPIOS):
21-
board.gpio_mode(i, IN) # or IN_PU of a pull-up is wanted
21+
board.gpio_pin_mode(i, IN) # or IN_PU of a pull-up is wanted
2222

2323
# Read the GPIOs until the user button is pressed
2424
while not board.switch_pressed():
2525

2626
# Read each GPIO in turn and print its value
2727
for i in range(NUM_GPIOS):
28-
value = board.gpio_value(i)
28+
value = board.gpio_pin_value(i)
2929
print(GPIO_NAMES[i], " = ", value, sep="", end=", ")
3030

3131
# Set the neighbouring LED to a colour based on
3232
# the input, with Green for high and Blue for low
3333
if value:
34-
board.leds.set_hsv(i, 0.333, 1.0, BRIGHTNESS)
34+
board.leds.set_hsv(i + LED_GPIO_1, 0.333, 1.0, BRIGHTNESS)
3535
else:
36-
board.leds.set_hsv(i, 0.666, 1.0, BRIGHTNESS)
36+
board.leds.set_hsv(i + LED_GPIO_1, 0.666, 1.0, BRIGHTNESS)
3737

3838
# Print a new line
3939
print()

0 commit comments

Comments
 (0)