My favorites | Sign in
Logo
                
Search
for
Updated Feb 01, 2007 by naranjo.manuel
Interactive_PIO  

Tutorial 1 - Interactive Code, PIO Handling Introduction.

Introduction

Welcome back, in this example we will show you how to write interactive code, and will introduce to you the basis of Digital/Analogic lines handling.

Some times you might need that human users interact with your bluetooth devices. Even thought this task can be hard to achieve, with a series of tips and things to take into account this can be really easy. In this example we will write a piece of code that will give the user a shell interaction (like the old DOS, or BASH from Unix systems), this shell will be available on the Slave channel, and will be accesible to anyone.

The shell is quite basic and request user iteraction to be usefull, once the slave channel is opened the user will see the welcome message and the prompt:

Welcome, press h to see the list o f commands
COMMAND >

As you can see from the very first moment, the shell is quite intuitive to the human beeing. This shell can be used to change the state of PIOs (turn on/off), read PIOs, read the digital inputs and close the slave connection.

Here you have the code:

Code

@ERASE

@INIT 50
0 REM debugging information will be dumped to the SERIAL port
50 Z = 0
51 J = 20
52 A = pioset J
53 RETURN

@IDLE 100
100 A = slave 5
101 A = pioset J
102 A = pioclr J
103 RETURN

@SLAVE 150
150 A = pioset J
151 ALARM 1
152 PRINTS"Welcome, press h"
153 PRINTS" to see the list o 
154 PRINTS"f commands\n\r
155 PRINTS"COMMAND > "
156 RETURN

0 REM this function reads a character and stores it on E

@ALARM 200
200 TIMEOUTS 5
201 INPUTS $0
202 A = strlen $0
203 IF A > 0 THEN 210
204 ALARM 1
205 RETURN

207 ALARM 1
208 PRINTS"COMMAND > "
209 RETURN

0 REM this is the command dispatcher
0 REM help
210 IF $0[0] = 104 THEN 250
0 REM turn on a PIO
211 IF $0[0] = 111 THEN 270
0 REM turn off a PIO
212 IF $0[0] = 102 THEN 275
0 REM read a PIO
213 IF $0[0] = 114 THEN 280
0 REM read digital line
214 IF $0[0] = 100 THEN 290
0 REM close connection
215 IF $0[0] = 99 THEN 295
216 PRINTS"Invalid Command\n\r
217 GOTO 207

0 REM This is the help command
0 REM h help
0 REM o turn on a PIO
0 REM f turn off a PIO
0 REM r read a PIO
0 REM d read digital line
0 REM c close connection
250 PRINTS"h help\n\ro turn on
251 PRINTS" a PIO\n\rf turn of
252 PRINTS"f a PIO\n\rr read a
253 PRINTS" PIO\n\rd read digi
254 PRINTS"tal line\n\rc close
255 PRINTS" connection\n\r
256 GOTO 207

0 REM turn on a PIO
260 PRINTS"Enter the PIO "
261 PRINTS"Number: "
262 INPUTS $0
263 A = atoi $0
264 RETURN

270 GOSUB 260
271 B = pioset A
272 GOTO 207

275 GOSUB 260
276 B = pioclr A
277 GOTO 207

280 GOSUB 260
281 B = pioget A
282 PRINTS"VALUE: "
283 PRINTS B
284 PRINTS"\n\r
285 GOTO 207

290 A = nextsns 1
291 RETURN

295 PRINTS"Bye Bye\n\r"
296 A = disconnect 0
297 A = slave 1
298 RETURN

@SENSOR 300
300 PRINTS"\n\rSENSOR"
301 PRINTS" READING: "
302 A = sensor $0
303 PRINTS $0
304 PRINTS "\n\r
305 ALARM 3 
306 GOTO 208

Download File

Explanation

We will divide the explanations in two parts, on one side we will explain how you can handle PIOs and on the onther hand we will explain the interactive part itself.

Interactive Stuff

Basically an interactive program must have a data output to the user, and the user must be able to input data to the program. To output data we decide to use PRINTS this command sends the string given as argument to the slave channel. Output is not a hard stuff, but Input can be very sensitive, for input we had user INPUTS this command will read from the slave channel and will echo those characters to the slave channel. The input command by itself will wait for an infinite time until the user inputs something, this can be a problem sometimes that's why I added a timeout to the command.

There are some things you have to take into account before writting interactive code, one thing is time outs if you don't define a time out the bluetooth processor will be always "stucked" trying to read from the input. Another thing you have to take into account is processor overloading, take into account that the bluetooth processor is not a Desktop processor so overload will make it not only loose some interrupts but also will waste lots of energy.

PIOs Introduction

We had made some work with PIOs before, we had turn on/off leds, well basically a PIO is a digital line which is connected to the procesor, over which you have control. On the example you can see how to Turn On/Off (pioset, pioclr), read (pioget), and how to handle the analog line. The analog line needs some more work than the digital one, before reading the analog line you need to schedule the reading of it by using nextsns then you can get the value of it on the @SENSOR interrupt.


Comment by a00566769, Apr 01, 2008

When I am introducing the input from my cellphone... Shoud I write "h" "o" ? Or do I have to write something else like a comand indicating interacton with the basic program?

Comment by a00566769, Apr 01, 2008

Is there any software that I can use like to simulate my program? and one more thing... When you apply a tiemout o a program, if the timeout expires and I send a comand to the SMD, is it taking the instruction and proceeds with the programmed acctions or it does nothing? Sorry I am new on it.

Comment by naranjo.manuel, Apr 01, 2008

Q1: I have no idea what you mean sorry.

Q2: No sorry there's no simulation program. But you can see processor dump if you do Z = 1 or Z = 2 in a line of your code. Z = 1 will redirect dump to UART, Z = 2 will redirect to slave connection, if I'm not wrong there's also Z = 3 for master, but I have never used it. About the timeout thing, the SMD and any AIRcableOS has a 1KB buffer for the inputs (uart, Slave and Master), once each second this buffers are cleaned. Hope this answers your question. If you want to keep asking use the google code, it's better to keep track on questions.

Cheers, Manuel

Comment by a00566769, Apr 06, 2008

Hi. Thank you for your help. I have the smd microcontroller but I realiced that the little module that comes with it doesn't have outputs for the IO ports. How do I take the interaction with the ports, I mean how do I connect them to a device like a stepper motor or a led? Do you have a page where I can see the ensambling procedure?

Comment by naranjo.manuel, Apr 07, 2008

Have you taken a look at the datasheet ?

You have all the port numbers there. Just take into account that you can't drive much current from those ports, will not be enough for driving an stepper motor, might be enough for driving some LEDs. It can drive up to 20 mA higher currents might damage the hardware.

Thanks, Manuel

Comment by a00566769, Apr 10, 2008

Thank you for your comment. I have the datasheet and I have no problem with the microcontroller when identifying the pins. The real problem is the module AirCable? OEM V2.3 because I don't see any output for the PIOs. Do I have to insert some cable directly to the PIOs? Thank you

Comment by kienhoefer, Apr 12, 2008

The little sample board we ship with the order of AIRcable SMD samples does not have any PIO accessible. It is a sample to show how to do a simple Bluetooth to serial carrier board. If you need PIO, just spin your own board. There are many board houses that do samples for very low price. Cheers

Comment by a00566769, Apr 29, 2008

HI. I was configuring the bluetooth with the inicial settings but every time I'm charging the config.txt the bluetooth just do not recongnize the new settings and when I retreive the info from the device it appears just the same... No name and no device identifier. Is there a way to reset the settings to the original ones?

Comment by man...@aircable.net, Apr 29, 2008

Which Basic code are you using?

You can change this settings from BASIC code by calling name, and this runtime changes take over the static changes from the config.txt. Furthermore it's not recommended to modify the config.txt if you aren't really sure of what you're doing, you might block yourself and might need to reset the device through an special jumper.

Comment by a00566769, Apr 30, 2008

So basically I should not modify the txt file but I can change the name and the device type with the basic code right? In deed, the basic program that I'm using is the same on this page but the device appears like unknowed so I don't get any message or I can not send any instruction to my cellphone. About the last part... Can I send an instruction through a txt file from my cellphone? or how can I make an interactive program with my cellphone? Do you know what I mean?

Comment by man...@aircable.net, Apr 30, 2008

Yes, you should do something like:

A = name "AIRcable1"

About sending txt instructions, you can use Obex messages, and then wait for @FTP in the code. But you might need to make some J2ME, Symbian code or get some native application in your phone

Comment by a00566769, Apr 30, 2008

Ok. That's fine. I have two questions. If I get blocked what should I do to reset the SMD to the original state and if the aircable appears like a question mark what should I do to modify it to computer again. I tried with the code on the config.txt file by inserting @0000 = 00c2 0104 0014 but it simply erases the name and pin that I inserted. How do I get the original status? I downloaded the original files that comes with the device when it was shipped to me but it didn't work either after I uploaded it to the SMD. I'm sorry I have too many questions...

Comment by naranjo.manuel, May 03, 2008

If it appears like a question mark, then you need to modify the config.txt and define a correct Bluetooth Class of Device, check here for more information on how to do it: http://docs.google.com/View?docid=dcvjvpkp_30qzz3bf about which are valid codes, in the web there are lots of examples on which are valid values for this.

To get back to original state you need to make a hard reset on the device, this is done by shorting 2 pins, it depends on which hardware you have the way it needs to be done.

Let's keep this in the google group, not here, comments in a wiki page aren't for this.

Hosted by Google Code