NS BASIC Tech Note Nov 30, 1994 How to control your TV, CD player, etc from NS BASIC on your Newton ------------------------------------------------------------------- 1.0 Introduction 2.0 Listing 3.0 Notes on Listing 4.0 IR Remote Control 4.1 Pioneer CD player codes 4.2 Finding codes for your own player 1.0 Introduction ---------------- It's pretty easy to write a program to control infrared remote controlled devices from NS BASIC. In this tech note, we'll show you how to do the basic commands to control the remote: it will be up to you to set up the proper codes for your own devices and make your own controlling program. The following code sample shows how to issue a PLAY command to a Pioneer CD player. The first part of the program sets up the calling sequence; statements 1000 to 1060 actually transmit the command. You'll be changing lines 20 to 90 to suit your own device; feel free to copy lines 1000 to 1060 into your program. In the next rev of NS BASIC, look for a built in function to take over most of the work of the subroutine: this should work fine for now, however. 2.0 Listing ----------- 0010 rem test ir -- ir 0020 LET t="01000101101110101110100000010111" 0030 LET trans=[0,500,14,50,14,0,8] 0035 LET zero="0"[0] // char 0 (not string 0) 0040 for i=0 to strLen(t)-1 0050 LET x:=addArraySlot(trans,1) 0060 if t[i]=zero then x:=addArraySlot(trans,1) else x:=addArraySlot(trans,3) 0070 next i 0080 LET x:=addArraySlot(trans,1) 0090 LET x:=addArraySlot(trans,1) 0100 GOSUB 1000 //transmit to IR 0110 stop 1000 rem transmit to IR 1010 function makeCode(x) begin c:=setLength(clone(""),24+length(x)*4);for i:=0 to length(x)-1 do stuffLong(c,i*4,x[i]); c end 1020 LET trans[5]=length(trans)-6 1030 LET ir=openRemoteControl() 1040 LET x:=sendRemoteControlCode(ir,u:makecode(trans),1) 1050 LET x:=closeRemoteControl(ir) 1060 return 3.0 Notes on Listing -------------------- In the above example, trans is an array which contains the command string to be sent out the IR port. The following elements make up the array: trans[0] Command name code; put anything you like in here. trans[1] TimeBase: number of milliseconds in each time unit trans[2] Lead In: number of time units to transmit before starting command trans[3] Repeat: number of time units to wait before repeating command trans[4] Lead Out: number of time units after the command trans[5] always zero. trans[6...] A sequence of numbers, showing the number of time units to spend in each state, starting with off. Some notes about the subroutine: The third argument in line 1040 is the number of times to repeat the command. This is usually just one, but for certain commands (and certain manufacturer's remote units) you may want to make this a parameter. If you open the IR (as in line 1030), make sure you do the close (as in 1050). The variable IR returned by the openRemoteControl() command is a magic number: if you lose it, the only way to close the IR remote control will be to reset your Newton. 4.0 How IR Remote Controls work ------------------------------- IR remote controls are really quite simple: A sequence of flashes, much like signalling with a flashlight, is transmitted. Each command has a different pattern of lengths of time the signal is on or off. Each manufacturer has his own pattern; sometimes different devices from the same manufacturer will even have different patterns. Commands are usually expressed as hex numbers. By converting these to binary, you get the sequence of ONs and OFFs to be sent. Along with the codes, the manufacturer will specify how long the ON and OFF periods have to be to define each of the characters. These periods are usually expressed as a number of time units. Each time unit is a number of milliseconds. There may also be a period of time that the signal is sent as a lead in or lead out to the command. 4.1 How the Pioneer CD control works ------------------------------------ The Pioneer does everything in multiples of 500 milliseconds, so trans[1] is set to 500. For a command, the first thing that is expected is a lead in of 14 time units, set in trans[2], followed by no signal for 8 time units, which is in trans[6]. The command itself is made up of two parts: a lead in code, which is fixed, and the command code itself. For the the Pioneer CD player, the lead in code is 45BA (base 16) and the command for PLAY is E817. Converting these to binary gives the string "01000101101110101110100000010111" used in the above example. Each binary digit in the Pioneer is transmitted as single time unit ON, followed by a single time unit OFF for 0 and three time units for 1. The example program above is a quick and dirty method for building up the array of ones and threes that is needed: I'm sure more elegant code can be written. In particular, building up the trans array takes too long to be done each time. A nice application could store the completed trans arrays in a file. The end result is the trans[7] and on are a sequence of time units that the signal is to be alternately ON and OFF, starting with ON. For the record (or CD that is), the other codes used on Pioneer CD players are STOP (6897), REW (8877), FF (08F7), PAUSE (18E7), PGM (B04F), TRACK (02FD) and DISC (B847, also 827D) 4.2 How do I find the codes for my own player? ---------------------------------------------- The easiest way is through the internet: ftp to nada.kth.se:home/d89-bga/hp/remote/remotes Many of the common units are documented at this site. If you make a sample bit of code for a new type of remote, email it to us at gh@hookup.net, and we'll put it in our code samples library for all to use. If all else fails, you can always hook up a diode to an oscilliscope and capture the codes from another remote. It doesn't seem to be possible to collect the code directly from another remote to the Newton: the incoming IR filters out frequencies other than those needed for Sharp-type data transmission. This rules out the frequency used by IR remote devices. Now if only the Newton was backlit so we could see our remotes by the light of the TV! NS BASIC is BASIC for the Newton. For more information, please contact NS BASIC Corp. at 416 264-5999 or at gh@hookup.net.