Shop OBEX P1 Docs P2 Docs Learn Events
Keyboard Demo — Parallax Forums

Keyboard Demo

NewzedNewzed Posts: 2,503
edited 2006-06-27 00:43 in Propeller 1
In Keyboard Demo, I changed:

'echo keystrokes in hex
· repeat
··· term.hex(kb.getkey,3)
··· term.out(" ")

to read

'echo keystrokes in ASCII
· repeat
··· term.out(kb.getkey)

Now the TV displays:

"Come visit Port Richey"

Also, in TV Terminal, under DATA/colors, I changed:

long··· $BC_6C_05_02

to

long··· $BC_6C_05_0A

Now the TV background is blue instead of black.

Sid

Comments

  • NewzedNewzed Posts: 2,503
    edited 2006-06-23 17:17
    In Keyboard Demo you can enter a letter or a number - one byte - and the TV will display.

    I would like to enter four numbers - for example, 5500 - and have the four numbers become an entity equal to 5500.· Then I want to equate this four-digit value to a variable called "cycle".· I think I can handle the equate part, but how do I enter the four numbers and how do I tell the program I have finished entering digits for this particular entity?· What if I wanted to enter just 550?

    Is this even close:

    term.getstr(string(ser\5\"*")

    I don't think there is a "term.getstr" routine in keyboard.spin

    Questions, questions, questions

    Sid.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-06-23 17:41
    No there isn't a getstr function in the keyboard object. This exact situation is whats presently holding me up with the logic analyser Im working on, its entirely possible to do, I just haven't had enough free time for quite a while now to bang out the code.

    My situation is a little more complicated in that there is a pre-existing 32 bit hexidecimal value that is displayed on the screen (there are 4 such numbers displayed) and keyboard entries are taken and processed accordingly, 0-9, a-f, A-F, the arrows, Tab, Shift-Tab, Enter and Esc must all be handled.

    Taking 4 numbers straight up should be a bit easier, just use a case statement to catch the numbers (which would be case "$30..$39:", have a working variable and a place variable, start the place variable at 1000 and the working variable at 0, multiple the place variable by the number entered and add it to the working variable, if the place variable is 1 leave the loop otherwise divide it by 10 and loop. Have Enter be another entry in the case statement where the working variable is divided by 10 times the place variable to achieve the properly shifted final value and leave the loop.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Life is one giant teacup ride.
  • SSteveSSteve Posts: 808
    edited 2006-06-23 17:59
    The attached program will allow you to enter a decimal value consisting of any number of digits. Just press enter when you're finished entering the number. It will then be displayed on the screen.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-06-23 18:14
    SSTeve.
    That's almost what I have.
    I'm working on including functionality to print each digit as it goes and support for the backspace / delete keys.
    I'm currently at work otherwise I'd post the code I completed...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Just tossing my two bits worth into the bit bucket


    KK
    ·
  • SSteveSSteve Posts: 808
    edited 2006-06-23 18:27
    Backspace/delete could be implemented by adding the lines
    elseif (theKey == $C8) or (theKey == $C9)
      enteredValue /= 10
    


    before until theKey == 13

    Echoing each digit would add a fair amount of complexity. I'd probably make a separate method for displaying keys and add a call to that method right after theKey := kb.getkey to avoid cluttering up the code that's putting together the value. I can't work on it right now, but I'll be interested to see what you have come up with.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-06-23 19:41
    SSTeve:
    As soon as I can, I'll post it...
    The laptop's batteries are currently dead, and as well as at home and have some time.
    Prolly the latter part of the weekend...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Just tossing my two bits worth into the bit bucket


    KK
    ·
  • NewzedNewzed Posts: 2,503
    edited 2006-06-23 20:30
    SSteve, thank you very much.· Excellent program.

    I have attached two files, Frequency Counter Internal and Frequency Counter Internal A, hereafter referred to as Internal and Internal A.

    Internal A is your program with some changes in the variable names.

    Internal is a program that outputs·a frequency dependent on the "cycle' time.· As you can see near the end of the program, cycle value is entered in the program and then run.

    Both programs work perfectly when run independently.

    What I would like to do is to merge the two programs so that the cycle value can be entered from the keyboard.· Internal has no keyboard object and I tried several approaches but nothing would work.· I do not understand the PUB configuration very well - that is where my problem is.· I tried copying Internal into Internal A but that didn't work either.

    If you have time to help I would appreciate it.

    Sid
  • SSteveSSteve Posts: 808
    edited 2006-06-24 02:41
    Hi, Sid:

    I merged them into a program that does the following:
    1) Accept a "cycle" value from the keyboard
    2) Display the entered cycle
    3) Start another cog to measure frequency
    4) Toggle a pin, wait the given number of cycles, repeat

    While the pin is being toggled, the other cog measures and displays the frequency on the input pin. (Of course you'll need to connect the output pin to the input pin.)

    Note the simplified toggle routine:
    Repeat
        !OutA[noparse][[/noparse]OutPin]
        WaitCNT(cycle + CNT)
    


    The "!" operator toggles the value to its right.

    I didn't add anything to allow entering a different cycle value once the program starts. That's what Reset buttons are for. smile.gif

    I hope this is instructive. Please feel free to ask about anything that's not clear.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • NewzedNewzed Posts: 2,503
    edited 2006-06-24 14:06
    SSteve, the program works beautifully.· I made a few cosmetic changes just because of personal preferences.· You are a tremendous help.

    Also, your explanation of the "· !· " operator helped me solve a problem with another program.

    One question -· how come you could do it with one cog when the original program had two cogs?

    You wrote:· PRI MeasureFrequency | Frequency

    I noticed frequency was not defined in VAR.· Does your PRI statement declare frequency as a local variable?··If I wrote:

    PRI MeasureFrequency
    and defined "frequency" in VAR, would it still work?

    Does the "· |· " operator declare one or more variables?· Could I write

    PRI MeasureFrequency |frequency, period

    and then write:· Period == 1/frequency*1_000_000 to express the period in us?

    Thanks again.

    Sid
    ·
  • SawmillerSawmiller Posts: 276
    edited 2006-06-24 14:16
    | or's the two values together for ex. %0001 | %1000 would equal %1001
    dan
  • SSteveSSteve Posts: 808
    edited 2006-06-24 16:44
    Newzed said...
    One question - how come you could do it with one cog when the original program had two cogs?
    I think the original program was just a contrived example to show how two cogs could communicate. It actually used one cog to launch two others:
    PUB Start
    '
      CogNew(FrequencyOutPinA6, @Stack0)
      Cognew(MeasureFrequency, @Stack1)
    


    I believe that once those two cogs are launched, the cog running Start shuts down. In my code, there are still two cogs running. One is executing these lines at the end of the Start method:
    Repeat
        !OutA[noparse][[/noparse]OutPin]
        WaitCNT(cycle + CNT)
    

    and one is executing MeasureFrequency. The original example could have done the same thing like this:
    PUB Start
    '
      CogNew(FrequencyOutPinA6, @Stack0)
      MeasureFrequency
    


    The only difference being that instead of launching a new cog to run MeasureFrequency it would run it in the cog running Start. (I hope that makes sense. I'm pressed for time so I have to be short.)
    Newzed said...
    You wrote: PRI MeasureFrequency | Frequency

    I noticed frequency was not defined in VAR. Does your PRI statement declare frequency as a local variable?
    Exactly.
    Newzed said...
    If I wrote:

    PRI MeasureFrequency
    and defined "frequency" in VAR, would it still work?
    Yes. The difference would be that frequency in that case would be global to every method in the object. A change to frequency in any method would be seen by all, as happens with cycle. If you define a variable as a local variable, only that method would see it and if you launched multiple copies of the method in different cogs, each would have its own instance of the variable. So there's no right or wrong, it's a matter of what you need in your application. There are some situations (like this one) where either works.
    Newzed said...
    Does the " | " operator declare one or more variables? Could I write

    PRI MeasureFrequency |frequency, period

    and then write: Period == 1/frequency*1_000_000 to express the period in us?
    Yes. Except you'd use ":=", not "==". ":=" is the assignment operator and "==" is the comparison operator.

    I have to run. Let me know if I've raised more questions than I answered. smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • NewzedNewzed Posts: 2,503
    edited 2006-06-25 19:24
    SSteve, I added the following lines at the very end of internal_b:

    repeat
    ···· WaitCNT( clkfreq + CNT )
    ··· until kb.getkey···········
    ··· start

    If load the program· I get a good answer for Hz.· I press any key, the screen clears, then display the starting menu.· If I enter another number, the screen turns black for a moment then back to a blank blue screen, which is the color of the screen when I press Game on the remote.

    What am I overlooking?··I know I could use Reset but I would like to do the same thing from the keyboard.

    Thanks

    Sid
  • NewzedNewzed Posts: 2,503
    edited 2006-06-25 21:06
    SSteve, I think I solved my own problem, at least partially.· I deleted the three lines mentioned bove and wrote:

    PUB Start
    ·repeat············ 'inserted this command
    · 'start the tv terminal
    · term.start(12)
    · term.out(3)

    and then wrote:

    cognew(MeasureFrequency, @stack1)
    · cycle := 500
    · DirA[noparse][[/noparse]OutPin] := Out
    · Repeat travel
    ·· repeat 102
    ··· !OutA[noparse][[/noparse]OutPin]·········· 'Toggles at cycle rate
    ··· WaitCNT(cycle + CNT)
    · waitcnt(clkfreq*10 + cnt)······ 'inserted this command

    Now the scrren displays for 10 seconds, clears the screen, then displays the starting menu.· Everything works fine - I would still like to accomplish the same thing by entering any key from the keyboard, but I can live with this.· Now the only problem is getting a precise number of pulses generated on Pin 6.

    Sid
    ·
  • SSteveSSteve Posts: 808
    edited 2006-06-26 16:00
    If you want to generate the pulse on pin 6 and wait for a keyboard press at the same time, you'll need to do that with two separate cogs. Something like the attached program should work. Please note that I haven't tried compiling or running this so there might be a mistake, but hopefully it will give you a good idea.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-06-26 18:15
    As an idea, have it generate the pulses for XXX seconds, then check the keyboard, if nothing, then cycle back OR... just do the pulses for xxx seconds then drop back to the input routine...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Just tossing my two bits worth into the bit bucket


    KK
    ·
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-06-26 19:47
    Even though it seems easier to just launch another COG, to just wait for a certain time while waiting for a keypress could easily be done in the same loop on the same COG.· Just a thought, but it would be easier to control your program flow this way, and devoting a whole other COG to just waiting for the keypress isn't necessary.· Just my $0.02 no change required...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-06-26 19:50
    LOL @ Chris...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Just tossing my two bits worth into the bit bucket


    KK
    ·
  • SSteveSSteve Posts: 808
    edited 2006-06-26 20:34
    Chris Savage (Parallax) said...
    Even though it seems easier to just launch another COG, to just wait for a certain time while waiting for a keypress could easily be done in the same loop on the same COG.
    I suggested that because I was under the impression that frequency was critical. Adding other code to the output loop would affect timing, wouldn't it?

    But now looking at OP's other thread (Pulsing a Pin) I'm not sure if we wants constant frequency or just a specific number of pulses.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • NewzedNewzed Posts: 2,503
    edited 2006-06-26 20:47
    I solved my little problem by writing:

    PUB start
    · repeat

    and then after I found the right place to put it I wrote:

    ··· if kb.getkey
    ····· waitcnt(clkfreq/100 + cnt)

    Now the display remains on the screen until I press any key, then it clears the screen and displays the starting menu.· Nice and clean and it works perfectly.

    Sid
  • NewzedNewzed Posts: 2,503
    edited 2006-06-26 21:33
    Just so everyone knows what I am trying to do, I would like to replace the BS2E that controls my SuperMill with the Propeller.·

    there are two big ifs - if i can ever get the program written and, if the Propellor can do it as well as the BS2E.· The BS2E uses five banks, so I may have to use·5 cogs - I just don't know yet.

    I have modified my pulsout.spin to pulsout_B.spin.· The modifications let me select the axis X or Y.· If I select X it lets me set the direction L(eft) or R(ight).· If I select Y it lets me select I(n) or O(ut).

    Then I enter the travel.· For each mil of travel on the X axis I need to generate 51.56 pulses.· For each mil of travel on the Y axis I need to generate 20.3 pulses.

    I have attached a copy of pulsout_B and· of the objects it uses so you will have them handy.···If your Propellor has a keyboard and a TV connected, I would appreciate anyone running the program and giving me their comments.

    I still have to write a sequence to select and control the Z axis, which I think I can do.· I think memory might be a problem.· The BS2E probably uses about 8500 of the 10,000 bytes available in the 5 banks.· If I understand correctly, a long on the Propellor is 4 bytes, but I'm not sure how much memory each cog has.· As you can see, my little old Propellor will be spinning its heart out.· Wish me luck and give me lots of H-E-L-P.

    Sid
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-06-27 00:43
    Steve,

    ·· Perhaps I missed something...This thread is a little hard to follow.· I thought he just wanted to count to a certain point while waiting for a keypress...IN any event it seems you have more of a handle on what he's doing.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
Sign In or Register to comment.