Keyboard strings

:confused: I have a computer keyboard wired directly into the picaxe chip and it is all working fine

but...

i can only get the chip to detect single key presses and i would like to be able to type, for example "output 1 high" and it be able to detect that somehow but i am not sure how to do it in the program

any help appreciated !

alex ;)
 

hippy

Ex-Staff (retired)
There's quite a bit of work to that. Getting the single key presses is the first step.

You would have to build up a command line in memory ( probably using SFR with POKE ), detect the CR ( Return key ), determine what the command line meant ( use PEEK to read the data from SFR), then do what was required.

This attached code for example will store short command lines then send them to the Programming Editor Terminal when Return is pressed. The GetKey routine you'll have to modify, it returns an ASCII Key value in b0 for each key press. It currently fakes input characters so you or anyone can run this without a keyboard attached for testing. There's no error checking so don't backspace too far or type too many charcaters or SFR underflows/overflows.
 

Attachments

Last edited:

manuka

Senior Member
SFR= Special Function Register

In addition to the pre-defined variables, all PICAXEs but the 08 have access to the internal SFR (Special Function Registers) of the PICmicro they are based upon, allowing much of the unused SFR to be used as Random Access Memory (RAM) during execution. This can be used for temporary storage of variable values (using PEEK and POKE) and allows re-use of variables within subroutines and other code sections. Most parts have a 32 byte span free for use.
( Ref=> en.wikipedia.org/wiki/PICAXE )

For possible keyboard insights refer => www.picaxe.orcon.net.nz/keyb18xa.bas with schematic => www.picaxe.orcon.net.nz/keyb18xa.gif & layout => www.picaxe.orcon.net.nz/keyb18xa.jpg
 

hippy

Ex-Staff (retired)
it wont let me open the attachment !!! it just says invalid attachment please notify administrator if this is a valid link !!!
And have you ? :)

Firefox opens the link but not automatically, so not sure what's going on there. I'm sure it used to work. There have been two Firefox upgrades in the last couple of days so perhaps something broke with Firefox if you're also using that. Anyway, here's the code ...

Code:
Symbol char    = b0       ' Typed character
Symbol ptr     = b1       ' Put/Get pointer into SFR

Symbol BS      = 8        ' ASCII backspace character code

Main:
  Do
    Gosub ReadCommandLine ' Read a command line
    Gosub ShowCommandLine ' Send command line to Terminal window
    Pause 1000            ' Short delay
  Loop                    ' Repeat forever

ReadCommandLine:
  ptr = $50               ' Start of SFR, where to store command line
  Do
    Gosub GetKey          ' Read a keyboard character
    If char = BS Then     ' Backspace
      ptr = ptr-1         ' Delete last character
    Else
      Poke ptr,char       ' Save the character
      ptr = ptr+1         ' Where to save the next character
    End If
  Loop Until char = CR    ' Repeat until Return key pressed
  Return

ShowCommandLine:
  SerTxd("Typed : ")
  ptr = $50               ' Start of SFR, where command line is
  Do
    Peek ptr,char         ' Get the character
    SerTxd(char)          ' Send to Terminal
    ptr = ptr+1           ' Point to next character
  Loop Until char = CR    ' Repeat until Return found
  SerTxd(LF)              ' Add Linefeed to keep display formated
  Return

GetKey:
  Read b12,Char
  b12 = b12+1
  If char = CR Then
    b12 = 0
  End If
  Return

  Data 0,("I'm dead",BS,BS,BS,BS,"alive!",CR)
 

Wrenow

Senior Member
By the way, Hippy, the link worked for me as a simple text file and opened in notepad.

Cheers,

Wreno
 
confused

:confused:
GetKey:
Read b12,Char
b12 = b12+1
If char = CR Then
b12 = 0
End If
Return

Data 0,("I'm dead",BS,BS,BS,BS,"alive!",CR)

this is a piece of the program u have written....but i really dont under stand how it works with the rest of the program at all. b12 isnt even used and 1 more thing CR does not work or LF.

i have changed it so it works but there were a lot of changes so i think i have gone wrong somewhere !! :confused:
 
also now i need an answer to my original question:

i have been shown and realised now how to get strings of text to come to the screen BUT.....:

I now need the picaxe to be able to recognise certain character combinations and act when they are typed:

e.g. if i type "high all" then all the outputs come on

dont forget i am using a keyboard attached to the chip !

thanks guys !

alex :)
 

hippy

Ex-Staff (retired)
:confused:
GetKey:
Read b12,Char
b12 = b12+1
If char = CR Then
b12 = 0
End If
Return

Data 0,("I'm dead",BS,BS,BS,BS,"alive!",CR)

this is a piece of the program u have written....but i really dont under stand how it works with the rest of the program at all. b12 isnt even used and 1 more thing CR does not work or LF.

i have changed it so it works but there were a lot of changes so i think i have gone wrong somewhere !! :confused:
The code just cycles through the Data Eeprom getting a new character everytime. B12 is used only in that routine, it's initialised to zero at power-up so just runs round forever, or should.

Not sure about the changes you needed to make or why CR or LF are giving problems. Compiles (08M) unchanged for me and I've now tested it in the simulator. Check you have the Enhanced Compiler option selected.
 
is it acctually possible to do what i want ! :)

*type a string of letters on a keyboard connected to the pic then it recognises certain words and performs tasks accordingly*

??????
 

Mycroft2152

Senior Member
Hi Alex, since the PICAXE does not have any string handling functions, you will need to create them yourself. In fact,your project reminds me of some of the early programming languages. The programming languages had a very limited and structured set of commands. Definately not in the queen's english.

If I were doing this project I would create an APL (Alex Programming Language) with the following structure:

Let's use the "OUTPUT 1 HIGH" as an example

Each command consists of 4 characters"

First character "!" indicates this is a valid command
Second character "O" indicates the function Output
Third character "1" indicatesd which pin 1
Fourth character "H" indicates state HIGH

So the command characters are !O1H. The PICAXE can look for and receive the four indivisdual characters and if/then on each character. This can get very complicated but with a very limited set of commands it is do able.

As I am writing this, I remembered the PICAXE Laptop project that was posted. That project would have used something similar and would be a good reference/starting point.

Myc
 

hippy

Ex-Staff (retired)
is it acctually possible to do what i want ! :)

*type a string of letters on a keyboard connected to the pic then it recognises certain words and performs tasks accordingly*

??????
It is possible but it depends upon exactly what you want, how big the vocabulary will be, how it needs to parsed and you have to write the program to do that.

Do you need to handle what looks like English language or can you get by with a more terse language designed for the job like Mycroft2152 suggests ? That will be much easier to implement.

I'd simplify Mycroft2152's example even further; the language being a letter followed by a digit; "H" puts a pin high, "L" puts a pin low, "T" toggles a pin. Thus "H1L1" will pulse pin 1 high then low. Actually I'd do it so it was a number followed by letter "1H1L" as that opens the way to including pauses ; "1H 100P 1L" - pin 1 high, a 100mS pause, then pin1 low. Blank spaces, CR, LF etc would be simply ignored.

That could be implemented quite easily and it's equally easy to extend ...

Code:
ObeyCommand:
  Gosub GetCharFomBuffer 
  Do
    If char >= "0" And char <= "9" Then
      number = number * 10 + char - '0'
    Else
      Select Case char
        Case "H" : High   number
        Case "L" : Low    number
        Case "T" : Toggle number
        Case "P" : Pause  number
      End Select
      number = 0
    End If
  Loop Until char = CR
  Return
I've used precisely that to control hardware from a PC's serial line. It works well from a VB program or Terminal Emulator.

Parsing "plain English" is harder. Not necessarily that much harder, but you need the program space to do that.
 

Mycroft2152

Senior Member
One of the first steps is to write a list of all the commands that you want to use. From there, you can determind the syntax and grammar.

As Hippy says the amount of program space needed for parsing will increase significantly with more complicated (english type) commands.
 
hippy the program all makes sense ....except the "number" thing

at all the places where number is used i am confused...how does the number work??
 

lbenson

Senior Member
"number" in hippy's program is a variable which might be defined as a byte (symbol number = b13), or a word (symbol number = w6). That variable will contain the numeric portion of a command you enter. For instance, if all your program is doing is waiting for commands to be serially input (and then executing them), you would call ObeyCommand in an endless loop. For each successive numeral you input, the "if" statement would multiply "number" by 10 (it having been originally initialized to zero), and then add to it the numeric value of the ascii character you typed (which is obtained by subtracting "0" (with the quotes) from the ascii character which is between "0" and "9").

Thus in the instance of the command, 200P, when the parsing code came upon the character, P, the variable, number, would contain the value, 200. The 'Case P:' statement would then be 'Pause 200', giving you a 200 millisecond pause.
 
Last edited:

hippy

Ex-Staff (retired)
As said; 'number' is just another variable. Just as the variable 'char' holds a charater, 'number' holds a number. It is best defined as a 'Symbol number = wX" (where X is 0 to 6, or higher on the X1's ) so it can hold values greater than 255 for the Pause, but could be a byte variable.

It's always a good idea to give variables a name ( using 'Symbol' commands ) so it's clear what they hold or are used for but there's no tight and fast rules on what that naming should be. What's meaningful to the author isn't always meaningful to anyone else.

"High number" is probably a bit surprising if you haven't seen it before. Most commonly "High" is used with a numbered pin ( "High 7" ) but the same can also be done using ...

- b0 = 7
- High b0

Give b0 the name 'number' using 'Symbol" and that leads us to "High number". It then follows the same pattern for Low, Toggle and Pause commands.
 
Top