Contributed by Charles Linquist.
KBNumberInput.basSince people like INCLUDE files, here is one (KBNumberInput.bas) that some may find useful. It allows keyboard input of numbers, and unlike HSERIN [DEC x], it echoes each character and allows for backspaces to rub out errors.
If the var ALLLOWSIGN is set to 1, it will accept a "-" or a "+" in the first position. You can also set the maximum number of chars that the user is allowed to input. If no input is received within a certain period of time (also user settable), the file exits with a RESULT of 0. The program has defaults, but can be over-ridden by setting any of them before calling the routine.
I have included a little test program (GetNumber.bas) that explains the use.
GetNumber.basCode:;--------------------------------------------------------------------- ; Numeric Input Routine by Charles Linquist ; Set MaxNum to maximum number of digits you will take (default = 5) ; Set KInputTimeout to the number of secs you will wait before giving up. ; If you will accept a sign ("+" or "-") then set AllowSign to 1 in your ; program it will over-ride these values ; ; RESULT is , well, the result. Keyin var byte Maxnum var byte YY var word POSN var byte IsNeg var bit AllowSign var bit TimedOut var bit KInputString var byte [10] KResult Var word KInputTimeout var Word XN var byte BS CON 8 ESC cON 27 LF con 10 SP con 32 CR con 13 MaxNum = 5 KInputTimeout = 10000 ; 10 sec AllowSign = 0 goto OverKInput GetNum: KResult = 0 YY = 1 POSN=0 IsNeg = 0 TimedOut = 0 ArrayWrite KInputString,[Rep $FF\8] ; Fill with $FF GetMoreN: HSERIN KInputTimeout,DeadAtKeyboard,[Keyin] IF Keyin = ESC THEN ZeroNum If Keyin = CR THEN DunInputn If AllowSign then IF Posn = 0 THEN If Keyin = "-" THEN IsNeg = 1 Hserout ["-"] goto GetMoreN endif if Keyin = "+" then Hserout ["+"] goto GetMoreN endif ENDIF endif IF Keyin = BS THEN HSEROUT [BS,SP,BS] IF POSN > 0 THEN POSN = POSN - 1 KInputString [POSN] = $FF GOTO GetMoreN ENDIF ENDIF iF POSN >= MaxNum THEN goto DunInputN IF Keyin < "0" or Keyin > "9" THEN GetMoreN HSEROUT [Keyin] KInputString [POSN] = Keyin - "0" POSN = POSN +1 GOTO GetMoreN DunInputN: For xN = 7 to 0 STEP - 1 IF KInputString[XN]= 255 THEN NoMoreNum KResult = KResult + (KInputString[xN]*YY) YY = YY *10 NoMoreNum: Next XN If Isneg then If Kresult < 32768 then KResult = 0 - KResult else hserout [CR,LF,"Error",CR,LF] goto zeronum endif endif Return DeadAtKeyboard: TimedOut = 1 ZeroNum: KResult = 0 RETURN OverKInput:
Code:'**************************************************************** '* Name : Number Input routine * '* Author : Charles Linquist * '* Notice : 2011 Charles Linquist * '* : All Rights Reserved * '* Date : 9/13/2011 * '* Version : 1.0 * '* Notes : For the forum * '* : * '**************************************************************** DEFINE OSC 40 DEFINE NO_CLRWDT 1 DEFINE _18F8723 1 DEFINE HSER_RCSTA 90H DEFINE HSER_TXSTA 20H DEFINE HSER_CLROERR 1 define HSER_BAUD 9600 TRISC = %10111111 ; Just for the serial port ; The following vars are created by the include and may be changed before using ; the structure GOSUB GetNum ; ; MaxNum - default = 5 ; KInputTimeout - default = 10000 = 10 sec ; AllowSign - default = 0 ("no") ; ; The result is returned in the WORD var KRESULT include "kbnumberinput.bas" topp: hserout [CR,Lf,"Input a number "] gosub getnum hserout [CR,LF,"You entered "] IF isneg = 1 then Hserout [SDEC KResult,CR,LF] ELSE hserout [dec KResult,CR,LF] ENDIF Goto topp



Reply With Quote

Bookmarks