Shop OBEX P1 Docs P2 Docs Learn Events
how to let propeller chip record the number in decimal I input through a keyboard? — Parallax Forums

how to let propeller chip record the number in decimal I input through a keyboard?

daniel dingdaniel ding Posts: 52
edited 2011-11-14 05:19 in Propeller 1
HI everyone
I want to use a LCD and a keyboard to do a job.
Now the problem is that how to let the chip record a number in decimal which I input use a keyboard。
for one
I input “1”,“0”,“0” through a keyboard and display "100" on LCD, now I want let chip know I input a decimal 100 but not a "1". "0"."0".
who can give me some advices.
thanks
daniel

Comments

  • Clive WakehamClive Wakeham Posts: 152
    edited 2011-11-12 03:32
    Maybe let the key strokes be stored in an array until the "enter" key is hit.

    What is the value from the keyboard? ASCII?
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-11-12 03:45
    Back in the olden days you could do this with a couple of lines of MBASIC or similar
    a$ = "100"
    a = val(a$)
    

    Things are not quite so simple in Spin because you need to set up some string space in order to store the characters"1" "0" "0"

    Then you need to convert it to a numerical value.

    Fortunately, Kye has done all the hard work with a brilliant library of string routines. The routines you could use are "buildstring" and "decimaltointeger". Have a look at the other routines too as they are very useful.

    {{
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // ASCII0 String Engine
    //
    // Author: Kwabena W. Agyeman
    // Updated: 8/28/2010
    // Designed For: P8X32A
    // Version: 1.2
    //
    // Copyright (c) 2010 Kwabena W. Agyeman
    // See end of file for terms of use.
    //
    // Update History:
    //
    // v1.0 - Original release - 4/10/2009.
    // v1.1 - Made code faster - 8/18/2009.
    // v1.2 - Updated library functions, fixed bugs, and made code more robust against whitespace and capitalization - 7/27/2010.
    //
    // For each included copy of this object only one spin interpreter should access it at a time.
    //
    // Nyamekye,
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }}
    
    VAR
    
      word tokenStringPointer
      byte decimalString[12], hexadecimalString[9], binaryString[33], characterToStringPointer, characterToString[255]
    
    PUB buildString(character) '' 4 Stack longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Builds a string from individual characters. Use "builtString" to get the address of the string.
    '' //
    '' // If the backspace character is put into the string it is automatically evaluated by removing the previous character.
    '' //
    '' // If 254 characters are put into the string all characters excluding backspace that are put into the string are ignored.
    '' //
    '' // Character - The next character to include in the string. Null will be ignored.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      ifnot(characterToStringPointer)
        bytefill(@characterToString, 0, 255)
    
      if(characterToStringPointer and (character == 8))
        characterToString[--characterToStringPointer] := 0
    
      elseif(character and (characterToStringPointer <> 254))
        characterToString[characterToStringPointer++] := character
    
    PUB builtString(resetString) '' 4 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Returns the pointer to the string built from individual characters.
    '' //
    '' // Reset - If true the next call to "buildString" will begin building a new string and the old string will be destroyed.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      characterToStringPointer &= not(resetString)
      return @characterToString
    
    PUB builderNumber '' 3 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Returns the number of characters in the string builder buffer.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      return characterToStringPointer
    
    PUB builderFull '' 3 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Returns true if the string builder buffer is full and false if not.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      return (characterToStringPointer == 254)
    
    PUB stringCompareCS(characters, otherCharacters) '' 5 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Compares two strings case sensitively.
    '' //
    '' // Returns zero if the two strings are equal.
    '' // Returns a positive value if "characters" comes lexicographically after "otherCharacters".
    '' // Returns a negative value if "characters" comes lexicographically before "otherCharacters".
    '' //
    '' // Characters - A pointer to a string of characters.
    '' // OtherCharacters - A pointer to another string of characters.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      repeat
        result := (byte[characters] - byte[otherCharacters++])
      while(byte[characters++] and (not(result)))
    
    PUB stringCompareCI(characters, otherCharacters) '' 9 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Compares two strings case insensitively.
    '' //
    '' // Returns zero if the two strings are equal.
    '' // Returns a positive value if "characters" comes lexicographically after "otherCharacters".
    '' // Returns a negative value if "characters" comes lexicographically before "otherCharacters".
    '' //
    '' // Characters - A pointer to a string of characters.
    '' // OtherCharacters - A pointer to another string of characters.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      repeat
        result := (ignoreCase(byte[characters]) - ignoreCase(byte[otherCharacters++]))
      while(byte[characters++] and (not(result)))
    
    PUB stringCopy(whereToPut, whereToGet) '' 5 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Copies a string from one location to another. This method can corrupt memory.
    '' //
    '' // Returns a pointer to the new string.
    '' //
    '' // WhereToPut - Address of where to put the copied string.
    '' // WhereToGet - Address of where to get the string to copy.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      bytemove(whereToPut, whereToGet, (strsize(whereToGet) + 1))
      return whereToPut
    
    PUB stringConcatenate(whereToPut, whereToGet) '' 5 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Concatenates a string onto the end of another. This method can corrupt memory.
    '' //
    '' // Returns a pointer to the new string.
    '' //
    '' // WhereToPut - Address of the string to concatenate a string to.
    '' // WhereToGet - Address of where to get the string to concatenate.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      bytemove((whereToPut + strsize(whereToPut)), whereToGet, (strsize(whereToGet) + 1))
      return whereToPut
    
    PUB stringToLowerCase(characters) '' 4 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Demotes all upper case characters in the set of ("A","Z") to their lower case equivalents.
    '' //
    '' // Characters - A pointer to a string of characters to convert to lowercase.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      repeat strsize(characters--)
        result := byte[++characters]
        if((result => "A") and (result =< "Z"))
          byte[characters] := (result + 32)
    
    PUB stringToUpperCase(characters) '' 4 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Promotes all lower case characters in the set of ("a","z") to their upper case equivalents.
    '' //
    '' // Characters - A pointer to a string of characters to convert to uppercase.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      repeat strsize(characters--)
        result := byte[++characters]
        if((result => "a") and (result =< "z"))
          byte[characters] := (result - 32)
    
    PUB trimString(characters) '' 8 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Removes white space and new lines arround the outside of string of characters.
    '' //
    '' // Returns a pointer to the trimmed string of characters.
    '' //
    '' // Characters - A pointer to a string of characters to be trimmed.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      result := ignoreSpace(characters)
      characters := (result + ((strsize(result) - 1) #> 0))
    
      repeat
        case byte[characters]
          8 .. 13, 32, 127: byte[characters--] := 0
          other: quit
    
    PUB tokenizeString(characters) '' 8 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Removes white space and new lines arround the inside of a string of characters.
    '' //
    '' // Returns a pointer to the tokenized string of characters, or an empty string when out of tokenized strings of characters.
    '' //
    '' // Characters - A pointer to a string of characters to be tokenized, or null to continue tokenizing a string of characters.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      if(characters)
        tokenStringPointer := characters
    
      result := tokenStringPointer := ignoreSpace(tokenStringPointer)
    
      repeat while(byte[tokenStringPointer])
        case byte[tokenStringPointer++]
          8 .. 13, 32, 127:
            byte[tokenStringPointer - 1] := 0
            quit
    
    PUB findCharacter(stringToSearch, characterToFind) '' 5 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Searches a string of characters for the first occurence of the specified character.
    '' //
    '' // Returns the address of that character if found and zero if not found.
    '' //
    '' // StringToSearch - A pointer to the string of characters to search.
    '' // CharacterToFind - The character to find in the string of characters to search.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      repeat strsize(stringToSearch--)
        if(byte[++stringToSearch] == characterToFind)
          return stringToSearch
    
    PUB replaceCharacter(stringToSearch, characterToReplace, characterToReplaceWith) '' 11 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Replaces the first occurence of the specified character in a string of characters with another character.
    '' //
    '' // Returns the address of the next character after the character replaced on success and zero on failure.
    '' //
    '' // StringToSearch - A pointer to the string of characters to search.
    '' // CharacterToReplace - The character to find in the string of characters to search.
    '' // CharacterToReplaceWith - The character to replace the character found in the string of characters to search.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      result := findCharacter(stringToSearch, characterToReplace)
      if(result)
        byte[result++] := characterToReplaceWith
    
    PUB replaceAllCharacters(stringToSearch, characterToReplace, characterToReplaceWith) '' 17 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Replaces all occurences of the specified character in a string of characters with another character.
    '' //
    '' // StringToSearch - A pointer to the string of characters to search.
    '' // CharacterToReplace - The character to find in the string of characters to search.
    '' // CharacterToReplaceWith - The character to replace the character found in the string of characters to search.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      repeat while(stringToSearch)
        stringToSearch := replaceCharacter(stringToSearch, characterToReplace, characterToReplaceWith)
    
    PUB findString(stringToSearch, stringToFind) | index, size '' 7 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Searches a string of characters for the first occurence of the specified string of characters.
    '' //
    '' // Returns the address of that string of characters if found and zero if not found.
    '' //
    '' // StringToSearch - A pointer to the string of characters to search.
    '' // StringToFind - A pointer to the string of characters to find in the string of characters to search.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      size := strsize(stringToFind)
      if(size--)
    
        repeat strsize(stringToSearch--)
          if(byte[++stringToSearch] == byte[stringToFind])
    
            repeat index from 0 to size
              if(byte[stringToSearch][index] <> byte[stringToFind][index])
                result := true
                quit
    
            ifnot(result~)
              return stringToSearch
    
    PUB replaceString(stringToSearch, stringToReplace, stringToReplaceWith) '' 13 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Replaces the first occurence of the specified string of characters in a string of characters with another string of
    '' // characters. Will not enlarge or shrink a string of characters.
    '' //
    '' // Returns the address of the next character after the string of characters replaced on success and zero on failure.
    '' //
    '' // StringToSearch - A pointer to the string of characters to search.
    '' // StringToReplace - A pointer to the string of characters to find in the string of characters to search.
    '' // StringToReplaceWith - A pointer to the string of characters that will replace the string of characters found in the
    '' //                       string of characters to search.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      result := findString(stringToSearch, stringToReplace)
      if(result)
    
        repeat (strsize(stringToReplaceWith) <# strsize(stringToReplace))
          byte[result++] := byte[stringToReplaceWith++]
    
    PUB replaceAllStrings(stringToSearch, stringToReplace, stringToReplaceWith) '' 19 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Replaces all occurences of the specified string of characters in a string of characters with another string of
    '' // characters. Will not enlarge or shrink a string of characters.
    '' //
    '' // StringToSearch - A pointer to the string of characters to search.
    '' // StringToReplace - A pointer to the string of characters to find in the string of characters to search.
    '' // StringToReplaceWith - A pointer to the string of characters that will replace the string of characters found in the
    '' //                       string of characters to search.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      repeat while(stringToSearch)
        stringToSearch := replaceString(stringToSearch, stringToReplace, stringToReplaceWith)
    
    PUB integerToDecimal(number, length) '' 5 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Converts an integer number to the decimal string of that number padded with zeros.
    '' //
    '' // Returns a pointer to the converted string.
    '' //
    '' // Number - A 32 bit signed integer number to be converted to a string.
    '' // Length - The length of the converted string, "+" or "-" will be concatenated onto the head of converted string.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      length := (10 - ((length <# 10) #> 0))
    
      decimalString := "+"
      if(number < 0)
        decimalString := "-"
    
      if(number == negx)
        bytemove(@decimalString, string("-2147483648KA"), 11)
    
      else
        repeat result from 10 to 1
          decimalString[result] := ((||(number // 10)) + "0")
          number /= 10
    
      decimalString[length] := decimalString
      return @decimalString[length]
    
    PUB integerToHexadecimal(number, length) '' 5 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Converts an integer number to the hexadecimal string of that number padded with zeros.
    '' //
    '' // Returns a pointer to the converted string.
    '' //
    '' // Number - A 32 bit signed integer number to be converted to a string.
    '' // Length - The length of the converted string, negative numbers need a length of 8 for sign extension.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      repeat result from 7 to 0
        hexadecimalString[result] := lookupz((number & $F): "0".."9", "A".."F")
        number >>= 4
    
      return @hexadecimalString[8 - ((length <# 8) #> 0)]
    
    PUB integerToBinary(number, length) '' 5 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Converts an integer number to the binary string of that number padded with zeros.
    '' //
    '' // Returns a pointer to the converted string.
    '' //
    '' // Number - A 32 bit signed integer number to be converted to a string.
    '' // Length - The length of the converted string, negative numbers need a length of 32 for sign extension.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      repeat result from 31 to 0
        binaryString[result] := ((number & 1) + "0")
        number >>= 1
    
      return @binaryString[32 - ((length <# 32) #> 0)]
    
    PUB decimalToInteger(characters) | sign '' 10 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Converts a decimal string into an integer number. Expects a string with only "+-0123456789" characters.
    '' //
    '' // If the string has a "-" sign as its leading character the converted integer returned will be negated.
    '' //
    '' // If the string has a "+" sign as its leading character the converted integer returned will not be negated.
    '' //
    '' // Returns the converted integer. By default the number returned is positive and the "+" sign is unnecessary.
    '' //
    '' // Characters - A pointer to the decimal string to convert. The number returned will be 2's complement compatible.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      characters := checkSign(ignoreSpace(characters), @sign)
    
      repeat (strsize(characters) <# 10)
        ifnot(checkDigit(characters, "0", "9"))
          quit
    
        result := ((result * 10) + (byte[characters++] & $F))
      result *= sign
    
    PUB hexadecimalToInteger(characters) | sign '' 10 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Converts a hexadecimal string into an integer number. Expects a string with only "+-0123456789ABCDEFabdcef" characters.
    '' //
    '' // If the string has a "-" sign as its leading character the converted integer returned will be negated.
    '' //
    '' // If the string has a "+" sign as its leading character the converted integer returned will not be negated.
    '' //
    '' // Returns the converted integer. By default the number returned is positive and the "+" sign is unnecessary.
    '' //
    '' // Characters - A pointer to the hexadecimal string to convert. The number returned will be 2's complement compatible.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      characters := checkSign(ignoreSpace(characters), @sign)
    
      repeat (strsize(characters) <# 8)
        ifnot(checkDigit(characters, "0", "9"))
          ifnot(checkDigit(characters, "A", "F") or checkDigit(characters, "a", "f"))
            quit
    
          result += $90_00_00_00
        result := ((result <- 4) + (byte[characters++] & $F))
      result *= sign
    
    PUB binaryToInteger(characters) | sign '' 10 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Converts a binary string into an integer number. Expects a string with only "+-01" characters.
    '' //
    '' // If the string has a "-" sign as its leading character the converted integer returned will be negated.
    '' //
    '' // If the string has a "+" sign as its leading character the converted integer returned will not be negated.
    '' //
    '' // Returns the converted integer. By default the number returned is positive and the "+" sign is unnecessary.
    '' //
    '' // Characters - A pointer to the binary string to convert. The number returned will be 2's complement compatible.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      characters := checkSign(ignoreSpace(characters), @sign)
    
      repeat (strsize(characters) <# 32)
        ifnot(checkDigit(characters, "0", "1"))
          quit
    
        result := ((result << 1) + (byte[characters++] & 1))
      result *= sign
    
    PRI ignoreCase(character) ' 4 Stack Longs
    
      result := character
      if((character => "a") and (character =< "z"))
        result -= 32
    
    PRI ignoreSpace(characters) ' 4 Stack Longs
    
      result := characters
      repeat strsize(characters--)
        case byte[++characters]
          8 .. 13, 32, 127:
          other: return characters
    
    PRI checkSign(characters, signAddress) ' 5 Stack Longs
    
      if(byte[characters] == "-")
        result := -1
    
      if(byte[characters] == "+")
        result := 1
    
      long[signAddress] := (result + ((not(result)) & 1))
      return (characters + (||result))
    
    PRI checkDigit(characters, low, high) ' 5 Stack Longs
    
      result := byte[characters]
      return ((low =< result) and (result =< high))
    
    ' added commands J Moxham Jan 2010
    
    PUB endsWithString(stringToSearch, stringToFind) '' 12 Stack Longs
    
    '' &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488;
    '' &#9474; Checks if the string of characters ends with the specified characters.                                                   &#9474;
    '' &#9474;                                                                                                                          &#9474;
    '' &#9474; Returns true if yes and false if no.                                                                                     &#9474;
    '' &#9474;                                                                                                                          &#9474;
    '' &#9474; StringToSearch - A pointer to the string of characters to search.                                                        &#9474;                                                           
    '' &#9474; StringToFind - A pointer to the string of characters to find in the string of characters to search.                      &#9474;                                                                           
    '' &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;
    
      return ((stringToSearch + strsize(stringToSearch) - strsize(stringToFind)) == findString(stringToSearch, stringToFind)) 
    
    PUB Left(Source, Destination,Number)   ' returns the left number of characters
       repeat Number
          byte[Destination] := byte[Source]
          Source++
          Destination++
       byte[Destination] :=0 ' add in the zero terminator
         
                                                                          
    PUB Len(Source) ' returns the length of the string
      return strsize(Source)
    
    PUB Mid(Source,Destination,Start,Number) ' returns strings starting at start with number characters
      Start-- ' so starts at the right position
      Source += Start ' position 1 is the first character
      repeat Number
        byte[Destination] := byte[Source]
        Source++
        Destination++
      byte[Destination] :=0 'add in the zero terminator  
    
    PUB Copy(Source,Destination) ' reverse order to stringcopy. Can also use to create strings eg copy(string("test"),@lineoftext1)
      bytemove(Destination, Source, (strsize(Source) + 1))
    
    PUB Str(Destination,Number) | n' convert a number to a string representation. Uses Basic syntax, ie leading character is a space if +ve, and is - if negative  
        n := number ' temp store for at the end when add the + or -
        Destination += 10 
        byte[Destination] := 0 ' terminator
        Destination--
        repeat result from 10 to 1
          byte[Destination] := ((||(number // 10)) + "0")
          number /= 10
          Destination--
    
        destination++    ' points to start again
    
        repeat while byte[destination] == "0"  ' while equal to zero remove this leading zero
          repeat result from 0 to 11 ' include the zero terminator as well
            bytemove(destination+result,destination+result+1,1)' shuffle to left
        repeat result from 11 to 1 
          bytemove(destination+result,destination+result-1,1) ' shuffle once to right - leading space or -
        byte[destination] :=" " ' leading space if positive. Can use trim to remove this
        if (n<0)
          byte[destination] := "-"
    
    PUB Stringfill(Destination, Number,AsciiValue)' fill string with a string, eg 3,65 is"AAA" and 2,32 is "  "
        bytefill(destination,AsciiValue,Number) ' same as Basic "string$" function and can also replicate space$ 
        byte[destination+number] :=0 ' zero terminator
    
    PUB Instr(Source,AsciiValue) | j ' find asciivalue in Source. Returns 0 if not found
        j := 0
        repeat result from 0 to strsize(Source)
          if byte[source+result] == AsciiValue
            j := result + 1 ' basic format where 1 is the last first character of a string
        return j    
    
    'PUB Val(Source)' basic string to decimal value only works for integers not floating point, see DecimalToInteger above        
    
    
    
    
    {{
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //                                                  TERMS OF USE: MIT License
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
    // files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
    // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
    // Software is furnished to do so, subject to the following conditions:
    //
    // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
    // Software.
    //
    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
    // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }}
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-11-12 21:50
    Daniel,

    I haven't been following the Propeller forum very closely the last couple of days. (Don't tell anyone, but I've been learning how to program AVR chips.) (I blame Leon.)

    Parallax Serial Terminal has a great method that will help. You'll need to copy it into your program since it's a private method.
    VAR
      long decValue
    
      byte keyboardArray[32]
    PUB Main
    
      ' use keyboard to fill keyboardArray with the characters you can changed to a decimal value.
    
      decValue := StrToBase(@keyboardArray, 10) ' The "10" is used to tell the method the characters are base ten.
    
      ' do stuff with decValue
    
    PRI 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)).}
      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
           
    
    

    I hope that helps.

    If you're still having problems post the code you have so far.

    Duane

    Edit: What out for the text that wraps around to the left side of the code window.
  • daniel dingdaniel ding Posts: 52
    edited 2011-11-13 00:52
    yes,ascii.Clive
  • daniel dingdaniel ding Posts: 52
    edited 2011-11-13 01:21
    Duane
    thanks for your reply.haha !!:smile:
    I add your program into the keyboard.spin of parallax library, and use a object of keyboard.spin .
    but, it does not work. LCD always display 0,
    sth you should know that I am going to use a keyboard PS2 to chip but not the keyboard of the PC.
    I see the pst.spin in parallax library, but it can not give me any help.
    whatever thanks a lot.
    I will show you my program later.
    best wishes to u my foreign friend.
  • daniel dingdaniel ding Posts: 52
    edited 2011-11-13 02:05
    Duane
    I don't know if you can understand me, I want to input a "x" and "y",and let the value of x and y to be a motor‘s coordinates to go with a platform.
    I got a big proplem in how to get the value of the x and y through a keyboard.
    the codes nest have a lot errors I :frown:think .
    Wish you can understand me clearly.
    Daniel
    con
      _clkmode=xtal1+pll16x
      _xinfreq=5_000_000
    var
      byte key_input
    obj
      key:"keyboard"
      lcd:"lcdroutines4"
    var
     byte keyinput
     byte keyinput2
     byte keyinput3
     byte keyinput4     
    pub main |temp,counter,str,a,b,c,x
      lcd.initialize_lcd
           
       
    
    
      key.start(17,16)
    
    
    
    
           
    repeat
          keyinput:=key.newkey
          
          if keyinput==103
            lcd.print(string("G"))
            lcd.space(1)
            waitcnt(clkfreq+cnt)
            lcd.print(string("Y/N:"))
          elseif keyinput==121
              
              
              lcd.send_char("Y")
              waitcnt(clkfreq+cnt)
              lcd.position(1,1)
              
              lcd.print(string("Ent the Num:"))
          elseif keyinput==49
              lcd.position(2,1)
              lcd.out(keyinput)
              lcd.position(1,1)
              lcd.print(string("Ent the Pos Val:"))
              lcd.position(2,1)
              lcd.print(string("X="))
              keyinput2:=key.getkey
                
                                    lcd.out(keyinput2)
                                   
                                      a:=(keyinput2-48)*100
                                      key.clearkeys
              keyinput3:=key.newkey    
                                    lcd.out(keyinput3)
                                    b:=(keyinput3-48)*10
                                    key.clearkeys
             keyinput4:=key.newkey
                                    lcd.out(keyinput4)
                                    c:=(keyinput4-48)*1
                                    key.clearkeys
    
    
            x:=a+b+c
          elseif keyinput==13
             lcd.clear
            lcd.position(1,1)
            lcd.print(string("Ent the Pos Val:"))   
            lcd.print(string("Y="))
                                    
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-11-13 09:53
    Daniel,

    I'm not sure where the problem is happening.

    If you type "100" into the keyboard, "100" is displayed on the LCD, correct?

    I think your:
    x:= a+b+c
    

    should work.

    Does the LCD object have a "Dec" method?

    Can you use:
    lcd.dec(x)
    

    and have the value of x displayed?

    There are easier ways of changing ASCII characters to decimal values but we wont worry about that right now. I think the way you are doing it should work.

    Duane
  • daniel dingdaniel ding Posts: 52
    edited 2011-11-13 17:45
    Duane
    I have already figure it out, you are really a nice guy, thanks very very much to your help.
    Now I will transfer the value of x and y to another object, but I really have no idear how to do it.
    Daniel from China
  • daniel dingdaniel ding Posts: 52
    edited 2011-11-13 17:47
    Hi
    thanks a lot to your reply, though I can't understand your codes clearly.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-11-13 18:51
    Daniel,

    Don't worry if you don't understand what I post. It seems like you are figuring things out whether or not I help.

    This is the way I'd use the method "StrToBase" in your program. The ASCII characters are stored in the array keyinput and then the entire array is converted to a number with "StrToBase".

    Again, don't worry if you don't see what I'm doing with this code. I just thought I'd show you another way of doing this.
    con
      _clkmode=xtal1+pll16x
      _xinfreq=5_000_000
    var
      byte key_input
    obj
      key:"keyboard"
      lcd:"lcdroutines4"
    var
     byte keyinput[5]
         
    pub main |temp,counter,str,a,b,c,x, localIndex
      lcd.initialize_lcd
      key.start(17,16)
           
      repeat
        bytefill(@keyinput, 0, 5) ' fill array with zeros
        keyinput[0]:=key.newkey
         
        if keyinput[0]==103
          lcd.print(string("G"))
          lcd.space(1)
          waitcnt(clkfreq+cnt)
          lcd.print(string("Y/N:"))
        elseif keyinput[0]==121
            
            
          lcd.send_char("Y")
          waitcnt(clkfreq+cnt)
          lcd.position(1,1)
           
          lcd.print(string("Ent the Num:"))
        elseif keyinput[0] == "1" 
          
          lcd.position(2,1)
          lcd.out(keyinput[0])
          lcd.position(1,1)
          lcd.print(string("Ent the Pos Val:"))
          lcd.position(2,1)
          lcd.print(string("X="))
          repeat localIndex from 0 to 2
            keyinput[localIndex]:=key.getkey
              
            lcd.out(keyinput[localIndex])
            key.clearkeys     
          x := StrToBase(@keyinput, 10)
          ' x should equal the decimal value of the three digits entered
        elseif keyinput==13
           lcd.clear
          lcd.position(1,1)
          lcd.print(string("Ent the Pos Val:"))   
          lcd.print(string("Y="))
    PRI 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)).}
      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
      
    
  • daniel dingdaniel ding Posts: 52
    edited 2011-11-14 05:19
    Duane
    Thanks for your reply soon, I tried your codes and rebuilded my program just now.
    But will you imagine what happened?
    The value of x and y is displayed 0 in LCD.
    Could u tell me why?
    next is the codes!
    con
      _clkmode=xtal1+pll16x
      _xinfreq=5_000_000
    var
      byte key_input
      long stack[100]
    obj
      key:"keyboard"
      lcd:"lcdroutines4"
      lcd2:"chabu1110"
    var
     byte keyinput[5]
    pub main |temp,counter,str
    lcd2.chabu  
      lcd.initialize_lcd
           
       
      key.start(17,16)
    
           
    repeat
          keyinput[0]:=key.newkey
          
          if keyinput[0]==103
            lcd.send_char("G")
            lcd.space(1)
            waitcnt(clkfreq/5+cnt)
            lcd.print(string("Y/N:"))
          elseif keyinput[0]==121
            
              
              
              lcd.send_char("Y")
              waitcnt(clkfreq+cnt)
              lcd.position(1,1)
              
              lcd.print(string("Ent the Num:"))
          elseif keyinput[0]==49
            counter++
            if counter==1
              displayxy
            else
              repeat
                                    lcd.out(key.newkey)
          
    pub displayxy|localindex,x,y 
              
              lcd.out(keyinput)
             keyinput[0]:=key.newkey
            if keyinput[0]==13
             
              lcd.position(1,1)
              lcd.print(string("Ent Pos Val x=:"))
              lcd.position(2,1)
              lcd.print(string("X="))
              repeat localIndex from 0 to 2
                                    keyinput[localIndex]:=key.getkey
              
                                    lcd.out(keyinput[localIndex])
                                    key.clearkeys     
              x := StrToBase(@keyinput, 10)
          '                         x should equal the decimal value of the three digits entered
                           
            keyinput[0]:=key.newkey
           
            if keyinput[0]==13
              
              lcd.clear
              waitcnt(clkfreq/5+cnt)
             
              
              
              lcd.print(string("Ent Pos Val:"))
              
              lcd.position(2,1)
              lcd.print(string("Y="))
              repeat localIndex from 0 to 2
                                    keyinput[localIndex]:=key.getkey
              
                                    lcd.out(keyinput[localIndex])
                                    key.clearkeys     
              y := StrToBase(@keyinput, 10)
          '                         x should equal the decimal value of the three digits entered
            
                        
            keyinput[0]:=key.newkey
            if keyinput[0]==13
              
              lcd.clear
              waitcnt(clkfreq/5+cnt)
              lcd.print(string("G01"))
              lcd.space(1)
              lcd.print(string("x="))
              lcd.print_dec(x)
              lcd.space(1)
              lcd.print(string("Y="))
              lcd.print_dec(y)
              lcd.position(2,1)
              lcd.print(string("Y/N"))
            elseif keyinput[0]==121
              lcd.space(1)
              lcd.send_char("Y")
              run
    pub run
      lcd2.go
             
    PRI 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)).}
      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           
    
Sign In or Register to comment.