Shop OBEX P1 Docs P2 Docs Learn Events
Trying to write a times table tester — Parallax Forums

Trying to write a times table tester

NurbitNurbit Posts: 53
edited 2012-08-14 12:13 in Propeller 1
I've finally found time to sit down and get my head around the prop.

I've been working on some code for testing times tables

Here is what I've got so far.....
CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

OBJ

  text : "tv_text"            'tv output object
  kb   : "keyboard"           'keyboard input object
  rnd  : "SL32_INTEngine_2"   'random numbers
VAR

  byte x[3]
  byte y[3]
  byte z[10]
PUB Main

rnd.random(cnt,1,9)

text.start(12)                 'start the TV object on pin 12
kb.start(26,27)               'start the keyvoard object on pins 26 and 27

text.str(string("      Nurbit's Times Table Tester",13,13))       'print to screen (13 adds CR)
text.str(string("   Which Table would you like? (1-9)"))   'ask which table   

kb.getkey

if kb.keystate("1") == TRUE
    text.out($00)
    x :=1

elseif kb.keystate("2") == TRUE
    text.out($00)
    x:=2

elseif kb.keystate("3") ==TRUE
    text.out($00)
    x:=3

elseif kb.keystate("4") ==TRUE
    text.out($00)
    x:=4

elseif kb.keystate("5") ==TRUE
    text.out($00)
    x:=5

elseif kb.keystate("6") ==TRUE
    text.out($00)
    x:=6

elseif kb.keystate("7") ==TRUE
    text.out($00)
    x:=7

elseif kb.keystate("8") ==TRUE
    text.out($00)
    x:=8

elseif kb.keystate("9") ==TRUE
    text.out($00)
    x:=9

y:= rnd.random(cnt,1,9)


text.out($00)
text.str(string("What is "))
text.dec(x)
text.str(string(" times "))
text.dec(y)

kb.getkey


I'm stuck because the key input system only accepts one key so far so I can't go above 9 and I'd like to go to 12.
Also it stops me entering the correct answer because it only waits for a keypress and not a string of presses.

Does anyone have any life changing information they could share with me?

Comments

  • SarielSariel Posts: 182
    edited 2012-08-14 04:09
    you are going to need to declare a byte buffer that is as long as the input you desire + 1 for null termination. Take the keys that are input, and keep filling the buffer until a CR (dec 13) is found, then run a string converter to change that from ASCII string to a decimal number. once that is done, then you can then operate on the decimal number any way that you choose. If I get some time, I will mock up some code for ya.
  • SarielSariel Posts: 182
    edited 2012-08-14 05:45
    Here is a little program that you type the character in, it stores it in a buffer until a CR is found. once that condition is met, then it takes that string and converts it to a decimal. Finally, it displays the decimal variable on the screen to prove that the conversion from a string was a success. I did not have a chance to fully debug it, so when you first load the program, the first conversion wont display anything but a "0" for me... but all other values entered in after that worked fine.

    Anyhow, it should give you a solid idea of the concept. Good Luck!
    CON
    '' Clock settings
      _CLKMODE      = XTAL1 +PLL16X 
      _XINFREQ      = 5_000_000
    
    '************** Connections 
    {
    '' This is My setup
      TV            = 0
      KeySig        = 24
      KeyClk        = 25 
    }
    '' This is Nurbit's setup
      TV            = 12
      KeySig        = 26
      KeyClk        = 27 
    '************** ASCII Conversion chart
      BKSP          = 8             ' Backspace                                     
      CR            = 13            ' carriage return   
      SPC           = 32            ' Space
      USCORE        = 95            ' Underscore  
    
    '************** Buffer size information
      MaxBufferSize = 64            ' alter this number to make your buffer bigger/smaller for your needs   
      
    VAR
      Byte  InputBuffer[MaxBufferSize]                      ' input buffer  
      Long  Decimal                                         ' Decimal decoded string                
    
    OBJ
      text          : "tv_text"                             'tv output object
      key           : "keyboard"                            'keyboard input object
    '  rnd           : "SL32_INTEngine_2"                    'random numbers
                                                                                               
    DAT
                                                         
    OverFlow_               Byte    "Buffer Overflow Error",CR, 0
    FoundVal_               Byte    "Found value = ",0
      
    PUB Init
    '' Init the drivers here
    '  rnd.random(cnt,1,9)          'Start the RNG
      TEXT.start(TV)                'start the TV object on pin 12     
      KEY.start(KeySig,KeyClk)      'start the keyvoard object on pins 26 and 27    
      TEXT.out(USCORE)
      Main
    PUB Main | char, pos
    '' Main method for accepting input from the user, storing it as a string, then coverting it to decimal values.
    '' Char is the character found at the keyboard. (ASCII value)
    '' Pos is the position in the buffer where it should be stored.   
    
      repeat
        char := KEY.getkey                                  ' Get the key
         
        if char == CR                                       ' if a CR is found
          TEXT.out(BKSP)                                    ' go over the previous cursor 
          TEXT.out(SPC)                                     ' White out the cursor 
          TEXT.out(CR)                                      ' go to new line     
          InputBuffer[Pos] := 0                             ' Null terminate the string
          Pos := 0                                          ' reset the pointer
          Decimal := StrToBase(@InputBuffer, 10)            ' convert the string to a base 10 decimal           
          ManipulateHere                                    ' Go to the method that does the work on the decimal value
        elseif char > 31 and char < 126                     ' If the character is standard ASCII number/letter/symbol
          InputBuffer[Pos++] := char                        ' store the character in the buffer, post increment pointer
          TEXT.out(BKSP)                                    ' delete the previous cursor
          TEXT.out(char)                                    ' Display the character
          TEXT.out(USCORE)                                  ' Display the cursor
        if Pos == (MaxBufferSize-1)                         ' if pointer is at final null termination             
          TEXT.str(@OverFlow_)                              ' display on the screen the buffer has overflowed
          bytemove(@InputBuffer, 0, MaxBufferSize)          ' Zero Fill the buffer                                       
                   
             
    PUB StrToBase(stringptr, base) : value | chr, index
    {Converts a zero terminated string representation of a number to a value in the designated base.
    Ignores all non-digit characters (except negative (-) when base is decimal (10)).}
    '' This method taken from one of the many string manipulator methods in the obex. Cannot remember what one tho.
      value := index := 0
      repeat until ((chr := byte[stringptr][index++]) == 0)
        chr := -15 + --chr & 011111 + 39*(chr > 56)                              'Make "0"-"9","A"-"F","a"-"f" be 0 - 15, others out of range     
        if (chr > -1) and (chr < base)                                              'Accumulate valid values into result; ignore others
          value := value * base + chr                                                  
      if (base == 10) and (byte[stringptr] == "-")                                  'If decimal, address negative sign; ignore otherwise
        value := - value
        
    PUB ManipulateHere 
    '' Put your code here for manipulating the decimal value. For now, it just reports to user the converted decimal number.
                                                                  
      TEXT.str(@FoundVal_)        
      TEXT.dec(Decimal)
      TEXT.out(CR)            
      TEXT.out(USCORE)                                  ' Display the cursor
                  
    
  • NurbitNurbit Posts: 53
    edited 2012-08-14 12:13
    Thanks Sariel

    Wow, that's really well commented, even I should be able to do something with that

    I'll have a play with it :)
Sign In or Register to comment.