'*****************************************************************
'*  Name    : Matrix Orbital to Keylogic translator              *
'*  Author  : Stijn Coenen (Stynus)                              *
'*  Notice  : Copyright (c) 2010 Stijn Coenen                    * 
'*          : ElektronicaStynus.Be                               *
'*          : All Rights Reserved                                *
'*  Date    : 19/01/2010                                         *
'*  Version : 1.1                                                *
'*  Links   : elektronicastynus.be/Projecten/vfd_controller/     *
'*          : schematheek.net/index.php?p=forum/topic&t=268&n=1  *
'*          : gathering.tweakers.net/forum/list_messages/1383770 *
'*****************************************************************
Device  16F627A
Config  WDT_Off, HS_OSc, PWRTE_ON, lvp_off, MCLRE_OFF
Xtal    = 20
All_Digital = true

Symbol led      = PORTB.7    : TRISB.7 = 0
Symbol VFD_ser  = PORTB.5
Declare Rsout_Pin PORTB.5
Declare Serial_Baud 9600
Declare Rsout_Mode TRUE  

Dim positie     As Byte
Dim Regel       As Byte
Dim verwacht    As Byte
Dim wrap        As Bit
Symbol  VWTekst     = 0
Symbol  VWCommand   = 1
Symbol  VWColom     = 3
Symbol  VWRow       = 4
Symbol  Crap1       = 5
Symbol  Crap2       = 6
Symbol  Crap3       = 7
Symbol  Crap4       = 8
Symbol  Crap5       = 9
Symbol  Crap6       = 10
Symbol  Crap7       = 11
Symbol  Crap8       = 12
Symbol  Crap9       = 13

'Intterupt bij seriŽle ontvangst:
Hserial_Clear   = On            
Hserial_RCSTA   = 144
Hserial_TXSTA   = 36
Hserial_Baud    = 9600			
On_Interrupt GoTo Intteruptroutine
INTCON          = %11000000
PIE1.5          = 1

Dim serdata As Byte

Dim TekstRegel1[21] As Byte
Dim TekstRegel2[21] As Byte

'*****************************************************************
Clear
DelayMS 200
GoTo main
'*****************************************************************
Intteruptroutine:
    HSerIn [serdata]
    High led
     
    If  serdata = 254 Then
        'Aangeven dat er een commando komt     
        verwacht = VWCommand
    EndIf
    
    If verwacht = VWTekst Then
        'Gewone tekst
        If Regel = 1 Then
            'Regel 1
            TekstRegel1[positie - 1] = serdata 
            If positie < 21 Then
                Inc positie
            Else
                If wrap = 1 Then
                    positie = 0
                    Regel = 2
                EndIf  
            EndIf    
        Else     
            'Regel 2
            TekstRegel2[positie - 1] = serdata 
           If positie < 21 - 1 Then
                Inc positie
           Else
                If wrap = 1 Then
                    positie = 0
                    Regel = 1
                EndIf 
           EndIf    
        EndIf
    ElseIf verwacht = VWCommand Then
        'Waarvoor is dit commando?
        Select serdata
            '****************************************************
            Case 67
                'Auto line wrap on
                wrap = 1
                verwacht   = VWTekst
            Case 68
                'Auto line wrap on
                wrap = 1
                verwacht   = VWTekst
            Case 71
                'Set cursor position
                verwacht = VWColom
            Case 72
                'Send cursor hom
                 positie    = 0
                 Regel      = 1
                 verwacht   = VWTekst
            Case 76
                'Cursor left      
                If positie > 0 Then
                    Dec positie 
                Else
                    If wrap = 1 Then
                        positie = 19
                        If  Regel = 1 Then
                            Regel = 2
                        Else
                            Regel = 1
                        EndIf     
                    EndIf              
                EndIf  
                verwacht   = VWTekst    
            Case 88
                'Clear display
                TekstRegel1[0] = " "           
                TekstRegel1[1] = " "           
                TekstRegel1[2] = " "
                TekstRegel1[3] = " "
                TekstRegel1[4] = " "
                TekstRegel1[5] = " "          
                TekstRegel1[6] = " "           
                TekstRegel1[7] = " "
                TekstRegel1[8] = " "
                TekstRegel1[9] = " "
                TekstRegel1[10] = " "         
                TekstRegel1[11] = " "          
                TekstRegel1[12] = " "
                TekstRegel1[13] = " "
                TekstRegel1[14] = " "
                TekstRegel1[15] = " "         
                TekstRegel1[16] = " "           
                TekstRegel1[17] = " "
                TekstRegel1[18] = " "
                TekstRegel1[19] = " "
                TekstRegel1[20] = " "  
                  
                TekstRegel2[0] = " "           
                TekstRegel2[1] = " "           
                TekstRegel2[2] = " "
                TekstRegel2[3] = " "
                TekstRegel2[4] = " "
                TekstRegel2[5] = " "          
                TekstRegel2[6] = " "           
                TekstRegel2[7] = " "
                TekstRegel2[8] = " "
                TekstRegel2[9] = " "
                TekstRegel2[10] = " "         
                TekstRegel2[11] = " "          
                TekstRegel2[12] = " "
                TekstRegel2[13] = " "
                TekstRegel2[14] = " "
                TekstRegel2[15] = " "         
                TekstRegel2[16] = " "           
                TekstRegel2[17] = " "
                TekstRegel2[18] = " "
                TekstRegel2[19] = " "
                TekstRegel2[20] = " " 
            '****************************************************
            'Werken niet:    
            Case 74
                'Turn on underline cursor
                'Kan VFD niet
                verwacht   = VWTekst
            Case 75
                'Turn off underline cursor
                'Kan VFD niet
                verwacht   = VWTekst
            Case 83
                'Turn on block (blinking) cursor
                'Kan VFD niet
                verwacht   = VWTekst
            Case 84
                'Turn off block (blinking) cursor
                'Kan VFD niet
                verwacht   = VWTekst   
            Case 80
                'Set contrast
                verwacht   = Crap4   
            Case 66
                'Backlight on
                verwacht   = Crap4  
            Case 70
                'Backlight off
                verwacht   = VWTekst  
            Case 86
                'General purpose output on
                verwacht   = VWTekst
            Case 87
                'General purpose output off 
                verwacht   = VWTekst
            '****************************************************  
            'Bar Graphs and Special Characters
            'Werken ook niet
            Case 118
                 'Initialize wide vertical bar graph
                 verwacht   = VWTekst   
            Case 115
                'Initialize narrow vertical bar graph
                 verwacht   = VWTekst 
            Case 61
                 'Draw vertical bar graph
                 verwacht   = Crap3 
            Case 104
                 'Initialize horizontal bar graph
                 verwacht   = VWTekst 
            Case 124
                 'Draw horizontal bar graph
                 verwacht   = Crap1 
            Case 78
                'Define custom character
                 verwacht   = Crap5 
            Case 118
                 verwacht   = VWTekst 
            '****************************************************     
            Case 254
                 verwacht   = VWCommand 
            Case Else
                'Fout
        EndSelect          
    '*************************************************************    
    ElseIf verwacht = VWColom Then
        'Kolom nummer instellen
        positie     = serdata
        verwacht    = VWRow       
    ElseIf verwacht = VWRow Then
        'Kolom nummer instellen
        Regel       = serdata
        verwacht    = VWTekst
    ElseIf verwacht = Crap1 Then
        verwacht    = Crap2 
    ElseIf verwacht = Crap2 Then
        verwacht    = Crap3
    ElseIf verwacht = Crap3 Then
        verwacht    = Crap4
    ElseIf verwacht = Crap4 Then
        verwacht    = VWTekst
    ElseIf verwacht = Crap5 Then
        verwacht    = Crap6 
    ElseIf verwacht = Crap6 Then
        verwacht    = Crap7
    ElseIf verwacht = Crap7 Then
        verwacht    = Crap8
    ElseIf verwacht = Crap8 Then
        verwacht    = Crap9
    ElseIf verwacht = Crap9 Then
        verwacht    = Crap1
    '*************************************************************   
    EndIf
    
    Low led
Context Restore
'*****************************************************************
main:
'*****************************************************************
'Opstarten:
    TekstRegel1[0] = " "           
    TekstRegel1[1] = " "           
    TekstRegel1[2] = " "
    TekstRegel1[3] = " "
    TekstRegel1[4] = " "
    TekstRegel1[5] = " "          
    TekstRegel1[6] = " "           
    TekstRegel1[7] = " "
    TekstRegel1[8] = " "
    TekstRegel1[9] = " "
    TekstRegel1[10] = " "         
    TekstRegel1[11] = " "          
    TekstRegel1[12] = " "
    TekstRegel1[13] = " "
    TekstRegel1[14] = " "
    TekstRegel1[15] = " "         
    TekstRegel1[16] = " "           
    TekstRegel1[17] = " "
    TekstRegel1[18] = " "
    TekstRegel1[19] = " "
    TekstRegel1[20] = " "  
    
    TekstRegel2[0] = " "           
    TekstRegel2[1] = " "           
    TekstRegel2[2] = " "
    TekstRegel2[3] = " "
    TekstRegel2[4] = " "
    TekstRegel2[5] = " "          
    TekstRegel2[6] = " "           
    TekstRegel2[7] = " "
    TekstRegel2[8] = " "
    TekstRegel2[9] = " "
    TekstRegel2[10] = " "         
    TekstRegel2[11] = " "          
    TekstRegel2[12] = " "
    TekstRegel2[13] = " "
    TekstRegel2[14] = " "
    TekstRegel2[15] = " "         
    TekstRegel2[16] = " "           
    TekstRegel2[17] = " "
    TekstRegel2[18] = " "
    TekstRegel2[19] = " "
    TekstRegel2[20] = " "    
    wrap = 1
'*****************************************************************
While 1 = 1
    'SEROUT VFD_ser, 9600, [TekstRegel1[20] /20]
    HSerOut[Str TekstRegel1 \20]
    HSerOut["                    "] 'Achter regel 1 whitespace
    HSerOut[Str TekstRegel2 \20]
    HSerOut["                          "] 'Achter regel 2 whitespace
    DelayMS 250
    
Wend
'*****************************************************************
End