Step 6Communications with the ASUS
Sending commands to the router
First we need to decide what we want the radio to do. We basically want it to:
- play
- pause
- stop
- change station
- change volume
If we look into the list of commands for mpc we see that:
- the command for play = mpc play
- the command for pause = mpc pause
- the command for stop = mpc stop
- the command for volume = mpc volume [+/-] num (numeric value)
The changing of the station is a bit more difficult. To be able to change to another station, mpc needs to know it's address. Easiest way to do so is to preload all our favorite stations into a playlist on the player. So at the beginning of our code we will add the command:
mpc add address for each station we want to preload into the router.
Before we start to add stations, we could issue a mpc clear command. This will clear all previous lists, just in case we made any changes to our list.
After all this is done, we can change the station with the mpc play num command, where num is the position of the station in our added playlist.
In Bascom, sending these commands to the router is very easy. Everything is done with PRINT:
PRINT "mpc play" or PRINT "mpc volume +5"
There is no need to add a Carridge Return (CR or ascii 013) to the command as the PRINT statement adds that by itself.
So as you can see, sending commands to the router isn't difficult at all!
Requesting and receiving info from the router
Requesting and receiving info from the router is a bit more difficult. Again, we'll need to decide what info we want to receive from the router. When you read part 6 on www.mightyohm.com, you'll see that there is lots of info that you can get, but we only want to know:
- the name of the station
- the title of the song
- the volume
The request for receiving the station and title is done with the following command:
echo "currentsong" | nc localhost 6600 | grep -e "^Title: " -e "^Name: " ( mind the capital T and N)
and for the volume:
echo "status" | nc localhost 6600 | grep -e "^volume: " (No capital here)
So in bascom that would give:
Print "echo " ; Chr(34) ; "currentsong" ; Chr(34) ; " | nc localhost 6600 | grep -e " ; Chr(34) ; "^Title: " ; Chr(34) ; " -e " ; Chr(34) ; "^Name: " ; Chr(34) ; " > /dev/tts/0"
and
Print "echo " ; Chr(34) ; "status" ; Chr(34) ; " | nc localhost 6600 | grep -e " ; Chr(34) ; "^volume: " ; Chr(34) ; " > /dev/tts/0"
Again there is no need to add a CR.
When we send one of these requests, we'll receive something into our buffer immediately. Now we need to arrange all the data in our buffer into useful stuff.
Luckily for us the router labels its info. Before the stationname it adds Name: , before the title title , and before volume volume. So we'll let our code search for the appropriate label and read the buffer from that point until it encounters a CR or until the buffer is empty. After all the info is extracted from the bufffer, we simply empty the buffer so that it can be used for another request.
To do so we start with reading the buffer into a string. After that we can simply clean the buffer so that there are no unwanted characters left. We will look in our string for one of the labels with the var = INSTR( string , substr ) statement. This will return the position of our label in our main string.
position = Instr( Uart_in_string, "Name: ")
This will give us the position of the first character of the label. If you don't want to add the label onto your screen, then just add the amount you need to start reading behind the label. In this case:
position = position + 6
From here we need to determine how long the data is. So from the point position we'll start to read one character at a time until we find a CR or until we reached the last character of the string. We use the var = MID(var1 ,st [, l] ) statement where var1 = the source, st = starting point and I = the amount of characters to read. So in our case:
Length = 0
Do
Incr Length
Position = Position + length
Temp = MID(Uart_in_string ,Position , 1 )
Loop Until Temp = Chr(13) or Temp = ""
So now we know the length of our data and the only thing that is left to do, is to extract the data from the source string. Again with the Mid statement:
Name = Mid(Uart_in_str, position length)
The same will work for the title and the volume.
Now we didn't discuss yet how to read from the buffer and how to empty it. Reading from the buffer is done with var = INKEY(). This will return the ascii value of the first byte in the buffer and then remove that character from the buffer. We just add every single received character together into a string containing the whole or part of the buffer.
Do
Uart_buffer = Ischarwaiting() Ischarwaiting() tells how many characters there are waiting in the buffer
If Uart_buffer > 0 Then
Uart_in = Inkey()
Uart_in_str = Uart_in_str + Chr(uart_in) Chr transforms the ascii value into the real character
End If
Loop Until Uart_buffer = 0 Or Len(uart_in_str) = 255 Len gives us the length of a string
To empty the buffer, just read everything into a string until Ischarwaiting() = 0
This was everything you needed know to communicate with your router. All the rest of the code are mostly cosmetic: graphic stuff and so on.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|
















































