NS BASIC Tech Note                                     Dec 04, 1998

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
    4.3 Why can't my Newton read the codes?


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; 
statement 100 actually transmits the command. You'll be changing lines 20 
to 90 to suit your own device. 

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   addArraySlot(trans,1)
0060   if t[i]=zero then addArraySlot(trans,1) else addArraySlot(trans,3)
0070 next i
0080 addArraySlot(trans,1)
0090 addArraySlot(trans,1)
0100 sendIRremote(trans,1)


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 microseconds 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.



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 microseconds. There may also be a period of time that 
the signal is sent as a lead in or lead out to the command.

The Pioneer, like most consumer units, uses a 40 khz carrier, as does the 
Newton. For data communications, the standard is to use 500, which is what 
the Newton does in that mode.

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?
----------------------------------------------

There's a site in Sweden that documents many of the codes:
http://www.rhoads.nu/bjorn/hp48/remote/index.html
      
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
support© nsbasic.com, 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.

4.3 Why can't my Newton read the codes?
---------------------------------------------
This answered by Mike Martin, Apple Assistance Center / PowerBook Support 
Manager:
"The IR module that the Newton OMP, 100, 110, and 120 uses is based on an 
integrated sub module that SHARP makes .. it integrates transmission and 
receiving ciruitry in the same module.  The Transmissive IR LED is just 
straight out, no filtration on the outgoung bandwidth (the modulation of 
the outbound IR LED is done via software timing -- educated guess).  

The inbound IR Reciever is a different story. At the module level, there 
is a IR bandwith filter of 500Khz.  In an ASK modulated data stream, TVIR 
is about 40Khz, "SHARP PROTOCOL" IR (off of which the newton bases it's 
newton-newton communication or "beaming") is at 500Khz.  

So the only "incoming" IR data stream that the Newton can actually read 
is what is passed to it by the IR module made by Sharp.  If the Sharp 
module is put into recieve mode, it's only going to see signals in the 
500Khz range -- IE: it cant "see" the TVIR signals coming from a TV 
Remote control.

There is no hardware way around this from the standpoint of the Newton 
Hardware -- but there are other ways to sample and decode TVIR waveforms --

If you are interested in a really spiffy IR module that can do (send 
*and* receive) TVIR, SHARP ASK, and IRdA (low Speed), then look at the 
Crystal Semiconductor CS8130 Multi Standard IR Transciever .. It's a 
killer chip that does about 90% of the hard work for you."

For more detailed information on the Newton transmission protocol for 
data, see 
http://www.dstc.edu.au/AU/staff/david-arnold/newton/sharp-ask.html.

Now if only the Newton was backlit so we could see our remotes by the 
light of the TV!
[Now that we have the 130, let's go channel surfing!]

NS BASIC is BASIC for the Newton. For more information, please contact NS 
BASIC Corp. at 416 264-5999 or at info© nsbasic.com.