I am trying to code in Micropython for Microbit with Gravity UART MP3 Voice Module
[url]https://wiki.dfrobot.com/Voice_Module_S ... 4#target_5[/url]
[code]
from microbit import *
uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin12, rx=pin16)
buffer = {0xAA, 0x02, 0x00, 0xAC}
uart.write(buffer)
[/code]
Nothing happened and no sound output.
Please let me know what is the problem with my code. I have tried in Makecode and confirm that the pins and the module is working and it can play the file in the module.
UART MP3 VOICE MODULE
-
- Posts: 2
- Joined: Sun Jun 02, 2019 12:38 pm
UART MP3 VOICE MODULE
Last edited by terencebeh on Tue Jun 04, 2019 3:26 pm, edited 2 times in total.
Re: UART MP3 VOICE MODULE
Unlike C, In Python (and MicroPython), if you write
that creates a set containing the numbers 1, 9, 3. One important thing about sets is that the order doesn't matter.
In this case, you need an array of bytes to send to the device, in a specific order.
MicroPython happily converts your set of numbers to a bytearray of numbers (when you pass it to uart.write) but what you get isn't what you expect:
Also if you look at the example code that you linked to, you also need to specify the track number. Converting their play function for example:
Code: Select all
x = { 1, 9, 3 }
In this case, you need an array of bytes to send to the device, in a specific order.
MicroPython happily converts your set of numbers to a bytearray of numbers (when you pass it to uart.write) but what you get isn't what you expect:
Code: Select all
>>> buffer = {0xAA, 0x02, 0x00, 0xAC}
>>> bytearray(buffer)
bytearray(b'\x00\xac\xaa\x02')
Code: Select all
def play(track):
buffer = b'\xaa\x07\x02\x00' + bytes([track, track + 0xb3])
uart.write(buffer);
}
-
- Posts: 2
- Joined: Sun Jun 02, 2019 12:38 pm
Re: UART MP3 VOICE MODULE
Do you mean below? Sorry I am quite new to Microphython.
Code: Select all
from microbit import *
def play(track):
buffer = b'\xaa\x07\x02\x00' + bytes([track, track + 0xb3])
uart.write(buffer)
uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin12, rx=pin16)
play(1)