iPod Simple Remote Example
Thursday, 04 March 2010
Today we’ll look at one of the example sketches that comes with my iPod Serial library. This one is for the Simple Remote mode, aka iPod Remote mode, aka Mode 2. Simple Remote mode provides serial commands that are the equivalents of the commands available through the physical buttons on the iPod. For the nitty gritty see the protocol wiki page on ipodlinux.org.
The SimpleRemote_with_Bounce example sketch sends the Play/Pause message when it sees a button is down and sends the “button released” message when it sees the button is up. This is the sketch I used in my own remote.
This sketch uses the Arduino Bounce library to debounce the button. There’s a version that does the debouncing on its own, called SimpleRemote, but it’s more complicated and wouldn’t scale if you were to use it for more buttons.
Let’s take the example sketch line by line (mostly):
Line 7 pulls in the Simple Remote mode header file from the library. You need to have placed a copy of the iPodSerial library in your Arduino IDE’s libraries folder for this to work.
Line 8 pulls in the Bounce library. You need to have placed a copy of the Bounce library in your Arduino IDEs libraries folder for this to work.
Line 10 says we’re going to have our button connected to the Arduino’s digital I/O pin 5.
Line 11 sets the debounce interval to 20 milliseconds, meaning that the button has to stay up or down for 20 milliseconds before the Bounce library will decide that is really up or down.
Line 13 creates our Bounce object, which is what’s going to do the debouncing for us.
Line 14 creates our SimpleRemote object, which is what’s going to talk to the iPod for us.
Line 18 makes digital I/O pin 5 an input, so we can read from it to see if the button is up or down.
Line 21 enables digital I/O pin 5’s internal pull-up resistor. This sounds scary, but all it means to us is that we can connect a button between pin 5 and ground without any other circuitry, which is referred to as an active-low configuration. The means that the pin will read as LOW when the button is down (pressed) and HIGH when the button is up (released). That probably seem backwards but as long as we’re expecting it to work that way it’s not a problem.
Line 23 takes care of setting up the serial connection to the iPod.
Line 28 lets the SimpleRemote object take care of anything it needs to do with the iPod. This needs to be in our loop() function.
Line 30 asks our Bounce object if there has been a change in the state of the button. That is, has the button just been pressed or just been released.
Line 32 checks the new state of the button, now that line 30 has told us the button has just changed state. If the new state is LOW that means the button has just been pressed (since we’re using an active-low button configuration).
Line 34 will get executed if the button has just been pressed. It tells our SimpleRemote object to send the Play command, which toggles the iPod between Play and Pause, just like the physical Play / Pause button would.
Line 37 will get executed if the button has just been released. The Simple Remote protocol requires this so that the iPod knows when the button has been released. Because of this, press-and-hold behaviours of the physical buttons also work through Simple Remote mode. So, for example, if you press and hold the button using this sketch the iPod screen will fade to black after a few seconds, just like it would if you pressed and held the physical Play button.
And that’s it!
If you look in the library at the file SimpleRemote.h you can see the full list of commands that can be sent:
Expanding the example to work with more buttons is pretty simple. Let’s see what it would look like if we added volume plus and minus button support:
The SimpleRemote_with_Bounce example sketch sends the Play/Pause message when it sees a button is down and sends the “button released” message when it sees the button is up. This is the sketch I used in my own remote.
This sketch uses the Arduino Bounce library to debounce the button. There’s a version that does the debouncing on its own, called SimpleRemote, but it’s more complicated and wouldn’t scale if you were to use it for more buttons.
Let’s take the example sketch line by line (mostly):
Line 7 pulls in the Simple Remote mode header file from the library. You need to have placed a copy of the iPodSerial library in your Arduino IDE’s libraries folder for this to work.
Line 8 pulls in the Bounce library. You need to have placed a copy of the Bounce library in your Arduino IDEs libraries folder for this to work.
Line 10 says we’re going to have our button connected to the Arduino’s digital I/O pin 5.
Line 11 sets the debounce interval to 20 milliseconds, meaning that the button has to stay up or down for 20 milliseconds before the Bounce library will decide that is really up or down.
Line 13 creates our Bounce object, which is what’s going to do the debouncing for us.
Line 14 creates our SimpleRemote object, which is what’s going to talk to the iPod for us.
Line 18 makes digital I/O pin 5 an input, so we can read from it to see if the button is up or down.
Line 21 enables digital I/O pin 5’s internal pull-up resistor. This sounds scary, but all it means to us is that we can connect a button between pin 5 and ground without any other circuitry, which is referred to as an active-low configuration. The means that the pin will read as LOW when the button is down (pressed) and HIGH when the button is up (released). That probably seem backwards but as long as we’re expecting it to work that way it’s not a problem.
Line 23 takes care of setting up the serial connection to the iPod.
Line 28 lets the SimpleRemote object take care of anything it needs to do with the iPod. This needs to be in our loop() function.
Line 30 asks our Bounce object if there has been a change in the state of the button. That is, has the button just been pressed or just been released.
Line 32 checks the new state of the button, now that line 30 has told us the button has just changed state. If the new state is LOW that means the button has just been pressed (since we’re using an active-low button configuration).
Line 34 will get executed if the button has just been pressed. It tells our SimpleRemote object to send the Play command, which toggles the iPod between Play and Pause, just like the physical Play / Pause button would.
Line 37 will get executed if the button has just been released. The Simple Remote protocol requires this so that the iPod knows when the button has been released. Because of this, press-and-hold behaviours of the physical buttons also work through Simple Remote mode. So, for example, if you press and hold the button using this sketch the iPod screen will fade to black after a few seconds, just like it would if you pressed and held the physical Play button.
And that’s it!
If you look in the library at the file SimpleRemote.h you can see the full list of commands that can be sent:
- Play (the one we used in the example, that toggles between Play and Pause)
- Volume Plus
- Volume Minus
- Skip Forward
- Skip Backward
- Next Album
- Previous Album
- Stop
- Just Play (rather than toggling between Play and Pause)
- Just Pause (rather than toggling between Play and Pause)
- Toggle Mute
- Next Playlist
- Previous Playlist
- Toggle Shuffle
- Toggle Repeat
- iPod Off
- iPod On
- Menu
- Ok / Select
- Scroll Up
- Scroll Down
Expanding the example to work with more buttons is pretty simple. Let’s see what it would look like if we added volume plus and minus button support: