SimpleHMI example: PIN numbers

This example shows you a way of defining and changing PIN numbers on SimpleHMI. This can provide better security, and be easier for an Enduser, than the PIN provided by the Bluetooth pairing processes. It also illustrates the use of the Trace feature to trace what is happening in the program if it is run with the SPLat/PC version of SimpleHMI. The program initially offers you a single button choice: Ask for PIN. When that is clicked, the program asks you to input the pin (which defaults to 1234). If you get the PIN right it gives you an option to change the PIN.

(Click here for some tips for working around problems with copy and paste out of Internet Explorer and HTML-help (.chm) files)

;SimpleHMI PIN example. 
;This is the main program. A secondary program forces the PIN back to a known setting
fPIN    mEQU    0,4                     ;PIN is stored as a float in Shadow memory at 0 

#       Open_Serial User(38400,8,N) 
        GoSub           InitializePIN   ;Make sure at least a default PIN is set
        GoSub           HMI_Connect    ;Paint the initial screen
#       HMI ConnectEvent(HMI_Connect)  ;Declare the handler for the Connected event
        LaunchTask      HeartBeat      ;Blink an output to show we are alive
        LaunchTask      Crash          ;Catch an input, crash to get back control of serial port
        RunTasksForever

;Connected event: Paint the screen
HMI_Connect: 
# HMI_Trace Print("Connect") NL()
# HMI Reset() Cls() HideAllButtons()   
# HMI Cursor(1, C-9) Print("PIN number example")
# HMI ButtonEvent(, C-3, 0.3, 6, 0.4, "Ask for PIN", evGetPIN)
        return          

evGetPIN:
# HMI_Trace Print("evGetPin") NL()
# HMI Reset() Cls() HideAllButtons()   
# HMI Cursor(1, C-9) Print("PIN number example")
# HMI IntInputEvent("Enter the PIN", 1000, 99999, evTestPIN) 
        Return
 
fTemp:   defFLOAT 
;Here when enduser enters a PIN (X = True, PIN in W) or Cancels (X = False)
evTestPIN:                            
# HMI_Trace Print("evTestPIN") NL()
       GoIfF            NoPIN                      ;g/ user Cancelled, nothing more to do
       fStore           fTemp                      ;Save enduser's input
       ShadowRead       fPIN,4                     ;Restore the old PIN
# HMI_Trace Print("Entered", f(fTemp,6,0), " PIN is", f(fPIN, 6, 0))  NL()
       fRecallQ         fPIN
       fTestWeqQ
       GoIfT            GotPin
       LaunchTask       PINFailed
       Return

PINFailed:
# HMI_Trace Print("PINFailed") NL()
# HMI Cursor(5, C-8) SetFGColour(255,0,0) Print(" **** FAIL **** ") SetFGColour(255, 255, 255)  
        Pause           100
        GoSub           HMI_Connect   ;Repaint the home screen
        KillTask        
        
NoPIN:
        GoSub           HMI_Connect   ;Repaint the home screen
        Return
        
GotPin:
# HMI_Trace Print("GotPin") NL()
# HMI Reset() Cls() HideAllButtons()   
# HMI Cursor(1, C-9) Print("PIN number example")
# HMI ButtonEvent(, C-5, 0.3, 6, 0.4, "Enter new PIN", evAskNewPin)
# HMI ButtonEvent(, C+5, 0.3, 6, 0.4, "Go back", HMI_Connect)
        return          



evAskNewPin:                            
# HMI_Trace Print("evAskNewPin") NL()
# HMI IntInputEvent("Enter the PIN", 1000, 99999, evGotNewPIN) 
       Return

evGotNewPIN:
;Here when enduser enters a new PIN (X = True, PIN in W) or Cancels (X = False)
# HMI_Trace Print("evGotNewPIN") NL()
       GoIfZ            NoPIN      ;g/ user Cancelled, nothing more to do
       fStore           fPIN 
       ShadowWrite      fPIN,4
# HMI_Trace Print("New PIN:") floatvar(fPIN,6,0)  NL()  
        Return

********************* Minor stuff ***************************************************        
;Blink an output to signal the program is running
HeartBeat:
        On              0
        Pause           1
        Off             0
        Pause           25
        GoTo            HeartBeat        
                
;Deliberately crash the program to get back control of the serial port, so SPLat/PC can connect.
Crash:  WaitOnK         0
Here    GoSub           Here             
              
;Force PIN to be > 1000. Anything less is assumed to be uninitialized.
InitializePIN: 
        ShadowRead      fPIN,4
        fRecallW        fPin
        FLoadQ          1000
        fTestWgeQ       
        RetIfT
        fLoadW          1234
        fStore          fPin
        ShadowWrite     fPIN,4  
        Return