TOUCH MENU FROM FLASH MEMORY

Code Examples ››
Parent Previous Next

DEMO TOUCH MENU FROM FLASH MEMORY


Device = 18F67K22


Declare Xtal = 64

Declare Watchdog = OFF

Declare Warnings = Off

Declare Optimiser_Level = 3


'================ TFT SCREEN ================

$define TFT_IC               ST7789V

$define TFT_COM              SPI_1

$define TftScreenWidth       320

$define TftScreenHeight      240

$define TFT_CS               PORTF.7

$define TFT_RESET            PORTC.7

$define TFT_DC               PORTC.2

$define TFT_MOSI             PORTC.5

$define TFT_MISO             PORTC.4

$define TFT_SCLK             PORTC.3

$define _TFT_MOSI            _PORTC.5

$define _TFT_MISO            _PORTC.4

$define _TFT_SCLK            _PORTC.3


'================ TFT TOUCH ================

$define TOUCH_IC             XPT2046

$define TOUCH_COM            SPI_SW

$define Z_Threshold          600

$define TouchAveragePoints   2

$define TouchMedianDistance  8

$define T_CS                 PORTG.1

$define T_IRQ                PORTA.5

$define T_MOSI               PORTC.5

$define T_MISO               PORTC.4

$define T_SCLK               PORTC.3

$define _T_MOSI              _PORTC.5

$define _T_MISO              _PORTC.4

$define _T_SCLK              _PORTC.3


'================ MCU ================

$define McuType              0


'================ SD CARD ================

$define SDCardUsed           1

$define SDCardFatUsed        1

$define SD_COM               SPI_2

$define SD_CS                PORTG.3

$define SD_SDO               PORTD.4

$define SD_SDI               PORTD.5

$define SD_CLK               PORTD.6

$define _SD_SDO              _PORTD.4

$define _SD_SDI              _PORTD.5

$define _SD_CLK              _PORTD.6


'================ MEMORY ================

$define MemoryType           2

$define MEM_COM              SPI_2

$define MEM_CS               PORTA.2

$define MEM_MOSI             PORTD.4

$define MEM_MISO             PORTD.5

$define MEM_SCLK             PORTD.6

$define _MEM_MOSI            _PORTD.4

$define _MEM_MISO            _PORTD.5

$define _MEM_SCLK            _PORTD.6

$define MEM_PAGE_SIZE        256

$define MEM_SECTOR_SIZE      4096

$define MEM_SECTOR_COUNT     2048

$define MEM_FILE_COUNT       256

$define MEM_WRITE_DELAY      5


'================ FONT ================

$define FontSmooth           0

$define FontMaxSpace         8

$define FontMaxPixels        8

Include                      "TftLib\Fonts\SND\F0_SND_8x8_Basic.inc"

Include                      "TftLib\TFT_Graphic_Lib.inc"


'Generated with Excel table

$define TickMenu_List_Front         0

$define TickMenu_List_Back          1

$define TickMenu_List_Kitchen       2

$define TickMenu_List_Porch         3

$define TickMenu_List_All           4

$define TickMenu_Mark_Empty_Front   5

$define TickMenu_Mark_Empty_Back    6

$define TickMenu_Mark_Empty_Kitchen 7

$define TickMenu_Mark_Empty_Porch   8

$define TickMenu_Mark_Empty_All     9

$define Button_Up_Set               10

$define TickMenu_List_Front_Erase   11

$define TickMenu_List_Back_Erase    12

$define TickMenu_List_Kitchen_Erase 13

$define TickMenu_List_Porch_Erase   14

$define TickMenu_List_All_Erase     15

$define TickMenu_Mark_Check_Front   16

$define TickMenu_Mark_Check_Back    17

$define TickMenu_Mark_Check_Kitchen 18

$define TickMenu_Mark_Check_Porch   19

$define TickMenu_Mark_Check_All     20

$define Button_Down_Set             21

$define Tree_TickMenu_Show          22

$define Tree_Button_Door_Show       23

$define Button_Down_Lock            24

$define Button_Down_Unlock          25

$define Button_Down_Door            26

$define Tree_TickMenu_Hide          27

$define Tree_Button_Door_Hide       28

$define Button_Up_Door              29

$define Button_Up_Lock              30

$define Button_Up_Unlock            31

$define Button_Hide_Lock            32

$define Button_Hide_Unlock          33

$define Button_Hide_Set             34

$define MainScreen                  35

$define Message_Clear               36


Dim sFileName As String * 12

Dim bCounter1 As Byte

Dim bCounter2 As Byte

Dim wBitVariable As Word

Dim ButtonDoor As wBitVariable.0    

Dim ButtonLock As wBitVariable.1    = 1 

Dim ButtonUnlock As wBitVariable.2  = 0

Dim ButtonSet As wBitVariable.3 

Dim TickFront As wBitVariable.4

Dim TickBack As wBitVariable.5

Dim TickKitchen As wBitVariable.6

Dim TickPorch As wBitVariable.7

Dim TickAll As wBitVariable.8

    

Main:

    Clear

    SetCrystal()

    SetPins()

    TftBmpMemSet()

    TftSetSndFont(0,0)

    TftSetUp(0)  

    TftResetScreen(BLACK)

    TransferFilesToFlash()

    TftTouchCalibration(3) 

    'Mask full screen transfer lag by switching off the screen during transfer

    TftDisplayOff()

    TftBmpFromMemIndex(MainScreen, 0, 0)

    TftDisplayOn()

    

    While 1 = 1

        'Touch IRQ triggered

        If T_IRQ = 0 Then

            'if touch ADC values are within valid range

            If TftTouchGetCoordinates(1) = 1 Then

                'check if valid menu has been selected

                ClickMenu(wTouchToPointX, wTouchToPointY)

                'Debounce

                DelayMS 300

            EndIf    

        EndIf  

    Wend  

End  


Proc TransferFilesToFlash()

    Dim wTemp As Dword

    

    'Some delay to avoid accidental FLASH erasure

    TftPrintSndString("File transfer in  ", 1, 5,13,1, GREEN,BLACK,0)

    For bCounter1 = 4 DownTo 0

        TftPrintSndInteger(bCounter1,1, 1, 23,13,1, GREEN,BLACK,0) 

        DelayMS 1000   

    Next

    

    TftPrintSndString("Format memory start", 1, 5,13,1, GREEN,BLACK,0)

    TftBmpMemFormat() 

    TftPrintSndString("Format memory done ", 1, 5,13,1, GREEN,BLACK,0)

    DelayMS 1000

    

    bCounter2 = 0

    'Generate file names as in SD card and transfer

    For bCounter1 = 0 To 36

        sFileName = "P" + Str$(Dec3 bCounter1) + ".BMP"

        TftPrintSndString(sFileName, 1, 5,12,1, GREEN,BLACK,0)

        TftPrintSndString("                ", 1, 14,12,1, GREEN,BLACK,0)

        Inc bCounter2

        Select TftBmpSdToMem(sFileName) 

            Case 0

                Dec bCounter2

            Case 1

                TftPrintSndString("SD NO RESPONSE  ", 1, 14,12,1, GREEN,BLACK,0)

                DelayMS 2000

            Case 2

                TftPrintSndString("NO FAT16/FAT32  ", 1, 14,12,1, GREEN,BLACK,0)

                DelayMS 2000

            Case 3

                TftPrintSndString("FILE NOT FOUND  ", 1, 14,12,1, GREEN,BLACK,0)

                DelayMS 2000

            Case 4

                TftPrintSndString("FILE IN USE     ", 1, 14,12,1, GREEN,BLACK,0)

                DelayMS 2000

            Case 5

                TftPrintSndString("READ/WRITE ERROR", 1, 14,12,1, GREEN,BLACK,0)

                DelayMS 2000

            Case 6

                TftPrintSndString("MEMORY ERROR    ", 1, 14,12,1, GREEN,BLACK,0)

                DelayMS 2000

            Case 7

                TftPrintSndString("NO FREE SPACE   ", 1, 14,12,1, GREEN,BLACK,0)

                DelayMS 2000

            Case Else

                TftPrintSndString("UNKNOWN ERROR   ", 1, 14,12,1, GREEN,BLACK,0)

                DelayMS 2000

        EndSelect

    Next


    TftPrintSndString("Done with " + Str$(Dec3 bCounter2) + " errors", 1, 5,11,1, GREEN,BLACK,0)

    DelayMS 2000

EndProc


Proc ClickMenu(wPointX As Word, wPointY As Word)

    'Button door

    If wPointX > 17 And wPointX < 106 And wPointY > 189 And wPointY < 221 Then

        Toggle ButtonDoor 

        ButtonDoor_Change()

        ExitProc

    EndIf        


    'Button lock

    If wPointX > 127 And wPointX < 216 And wPointY > 189 And wPointY < 221 Then

        If ButtonDoor = 1 Then

            Toggle ButtonLock 

            ButtonLock_Change()

        EndIf

        ExitProc

    EndIf  

    

    'Button unlock

    If wPointX > 127 And wPointX < 216 And wPointY > 145 And wPointY < 177 Then

        If ButtonDoor = 1 Then

            Toggle ButtonUnlock 

            ButtonUnlock_Change()

        EndIf

        ExitProc

    EndIf  

    

    'Tick front

    If wPointX > 233 And wPointX < 265 And wPointY > 194 And wPointY < 219 Then

        If ButtonDoor = 1 Then

            Toggle TickFront  

            TickFront_Change()

        EndIf

        ExitProc

    EndIf 

    

    'Tick back

    If wPointX > 233 And wPointX < 265 And wPointY > 163 And wPointY < 188 Then

        If ButtonDoor = 1 Then

            Toggle TickBack 

            TickBack_Change()

        EndIf

        ExitProc

    EndIf 

     

    'Tick kitchen

    If wPointX > 233 And wPointX < 265 And wPointY > 134 And wPointY < 159 Then

        If ButtonDoor = 1 Then

            Toggle TickKitchen

            TickKitchen_Change()

        EndIf

        ExitProc

    EndIf 

    

    'Tick porch

    If wPointX > 233 And wPointX < 265 And wPointY > 104 And wPointY < 129 Then

        If ButtonDoor = 1 Then

            Toggle TickPorch

            TickPorch_Change()

        EndIf

        ExitProc

    EndIf 

    

    'Tick all

    If wPointX > 233 And wPointX < 265 And wPointY > 74 And wPointY < 99 Then

        If ButtonDoor = 1 Then

            Toggle TickAll

            TickAll_Change()

        EndIf

        ExitProc

    EndIf 

    

    'Button Set

    If wPointX > 233 And wPointX < 318 And wPointY > 12 And wPointY < 45 Then

        If ButtonDoor = 1 Then

            TickButtonSet_Change()

        EndIf

        ExitProc

    EndIf 

EndProc



Proc TickButtonSet_Change()

    TftBmpFromMemIndex(Button_Down_Set, 233, 12)

    DelayMS 1000  

    TftBmpFromMemIndex(Button_Up_Set, 233, 12) 

    If ButtonLock = 1 Then

        TftBmpFromMemIndex(Message_Clear, 59, 221)

        TftPrintSndString("Door LOCK ... Done", 1,10,28,1, CARDINAL, BLACK,1)

    Else

        TftBmpFromMemIndex(Message_Clear, 59, 221)

        TftPrintSndString("Door UNLOCK ... Done", 1,9,28,1, CARDINAL, BLACK,1)

    EndIf

EndProc


Proc TickAll_Change()

    If TickAll = 0 Then

        TftBmpFromMemIndex(TickMenu_Mark_Empty_All, 233, 74)

        wBitVariable = wBitVariable & %1111111100001111

        TickPorch_Change()

        TickKitchen_Change()

        TickBack_Change()

        TickFront_Change()

    Else

        TftBmpFromMemIndex(TickMenu_Mark_Check_All, 233, 74)

        wBitVariable = wBitVariable | %0000000011110000

        TickPorch_Change()

        TickKitchen_Change()

        TickBack_Change()

        TickFront_Change()

    EndIf 

EndProc


Proc TickPorch_Change()

    If TickPorch = 0 Then

        TftBmpFromMemIndex(TickMenu_Mark_Empty_Porch, 233, 104)

        TftBmpFromMemIndex(TickMenu_Mark_Empty_All, 233, 74)

        TickAll = 0

    Else

        TftBmpFromMemIndex(TickMenu_Mark_Check_Porch, 233, 104)

        If (wBitVariable & %0000000011110000) = %11110000 Then

            TftBmpFromMemIndex(TickMenu_Mark_Check_All, 233, 74)

        EndIf

    EndIf 

EndProc


Proc TickKitchen_Change()

    If TickKitchen = 0 Then

        TftBmpFromMemIndex(TickMenu_Mark_Empty_Kitchen, 233, 134)

        TftBmpFromMemIndex(TickMenu_Mark_Empty_All, 233, 74)

        TickAll = 0

    Else

        TftBmpFromMemIndex(TickMenu_Mark_Check_Kitchen, 233, 134)

        If (wBitVariable & %0000000011110000) = %11110000 Then

            TftBmpFromMemIndex(TickMenu_Mark_Check_All, 233, 74)

        EndIf

    EndIf 

EndProc


Proc TickBack_Change()

    If TickBack = 0 Then

        TftBmpFromMemIndex(TickMenu_Mark_Empty_Back, 233, 163)

        TftBmpFromMemIndex(TickMenu_Mark_Empty_All, 233, 74)

        TickAll = 0

    Else

        TftBmpFromMemIndex(TickMenu_Mark_Check_Back, 233, 163)

        If (wBitVariable & %0000000011110000) = %11110000 Then

            TftBmpFromMemIndex(TickMenu_Mark_Check_All, 233, 74)

        EndIf

    EndIf 

EndProc


Proc TickFront_Change()

    If TickFront = 0 Then

        TftBmpFromMemIndex(TickMenu_Mark_Empty_Front, 233, 194)

        TftBmpFromMemIndex(TickMenu_Mark_Empty_All, 233, 74)

        TickAll = 0

    Else

        TftBmpFromMemIndex(TickMenu_Mark_Check_Front, 233, 194)

        If (wBitVariable & %0000000011110000) = %11110000 Then

            TftBmpFromMemIndex(TickMenu_Mark_Check_All, 233, 74)

        EndIf

    EndIf 

EndProc


Proc ButtonUnlock_Change()

    If ButtonUnlock = 0 Then

        ButtonLock = 1 

        TftBmpFromMemIndex(Button_Up_Unlock, 127, 145)

        TftBmpFromMemIndex(Button_Down_Lock, 127, 189)  

        

    Else

        ButtonLock = 0

        TftBmpFromMemIndex(Button_Down_Unlock, 127, 145)

        TftBmpFromMemIndex(Button_Up_Lock, 127, 189)

    EndIf

EndProc


Proc ButtonLock_Change()

    If ButtonLock = 0 Then

        ButtonUnlock = 1 

        TftBmpFromMemIndex(Button_Down_Unlock, 127, 145)

        TftBmpFromMemIndex(Button_Up_Lock, 127, 189)  

    Else

        ButtonUnlock = 0

        TftBmpFromMemIndex(Button_Up_Unlock, 127, 145)

        TftBmpFromMemIndex(Button_Down_Lock, 127, 189)

    EndIf

EndProc


Proc ButtonDoor_Change()

    If ButtonDoor = 0 Then

        TftBmpFromMemIndex(Button_Hide_Set, 233, 12)

        TftBmpFromMemIndex(TickMenu_List_All_Erase, 233, 74)

        TftBmpFromMemIndex(TickMenu_List_Porch_Erase, 233, 104)

        TftBmpFromMemIndex(TickMenu_List_Kitchen_Erase, 233, 134)

        TftBmpFromMemIndex(Button_Hide_Unlock, 127, 145)

        TftBmpFromMemIndex(TickMenu_List_Back_Erase, 233, 163)

        TftBmpFromMemIndex(Tree_TickMenu_Hide, 216, 87)

        TftBmpFromMemIndex(Tree_Button_Door_Hide, 106, 162)

        TftBmpFromMemIndex(Button_Hide_Lock, 127, 189)

        TftBmpFromMemIndex(TickMenu_List_Front_Erase, 233, 194)

        TftBmpFromMemIndex(Button_Up_Door, 17, 189)

        TftBmpFromMemIndex(Message_Clear, 59, 221)

    Else

        TftBmpFromMemIndex(Button_Down_Door, 17, 189)

        TftBmpFromMemIndex(Tree_Button_Door_Show, 106, 162)

        

        If ButtonLock = 0 Then

            TftBmpFromMemIndex(Button_Up_Lock, 127, 189)

        Else

            TftBmpFromMemIndex(Button_Down_Lock, 127, 189)

        EndIf

        

        If ButtonUnlock = 0 Then

            TftBmpFromMemIndex(Button_Up_Unlock, 127, 145)

        Else

            TftBmpFromMemIndex(Button_Down_Unlock, 127, 145)

        EndIf

        

        TftBmpFromMemIndex(Tree_TickMenu_Show, 216, 87)

        

        If TickFront = 0 Then

            TftBmpFromMemIndex(TickMenu_Mark_Empty_Front, 233, 194)

        Else

            TftBmpFromMemIndex(TickMenu_Mark_Check_Front, 233, 194)

        EndIf

        TftBmpFromMemIndex(TickMenu_List_Front, 264, 201)

        

        If TickBack = 0 Then

            TftBmpFromMemIndex(TickMenu_Mark_Empty_Back, 233, 163)

        Else

            TftBmpFromMemIndex(TickMenu_Mark_Check_Back, 233, 163)

        EndIf

        TftBmpFromMemIndex(TickMenu_List_Back, 264, 166)

        

        If TickKitchen = 0 Then

            TftBmpFromMemIndex(TickMenu_Mark_Empty_Kitchen, 233, 134)

        Else

            TftBmpFromMemIndex(TickMenu_Mark_Check_Kitchen, 233, 134)

        EndIf

        TftBmpFromMemIndex(TickMenu_List_Kitchen, 264, 136)

        

        If TickPorch = 0 Then

            TftBmpFromMemIndex(TickMenu_Mark_Empty_Porch, 233, 104)

        Else

            TftBmpFromMemIndex(TickMenu_Mark_Check_Porch, 233, 104)

        EndIf

        TftBmpFromMemIndex(TickMenu_List_Porch, 264, 106)

        

        If TickAll = 0 Then

            TftBmpFromMemIndex(TickMenu_Mark_Empty_All, 233, 74)

        Else

            TftBmpFromMemIndex(TickMenu_Mark_Check_All, 233, 74)

        EndIf

        TftBmpFromMemIndex(TickMenu_List_All, 264, 75)

        

        TftBmpFromMemIndex(Button_Up_Set, 233, 12)

    EndIf     

EndProc


  

'Set Fosc    

Proc SetCrystal()

    Clear 

    OSCTUNE.6 = 1

    DelayMS 100

EndProc   


Proc SetPins()

    'Set all digital

    ANCON0 = 0

    ANCON1 = 0

    ANCON2 = 0


    SSP1STAT.6 = 1

    SSP1CON1 = %00100000

    

    SSP2STAT.6 = 1

    SSP2CON1 = %00100000

EndProc


Config_Start

  RETEN = On        ;Enabled

  INTOSCSEL = Low        ;LF-INTOSC in Low-power mode during Sleep

  SOSCSEL = Dig        ;digital

  XINST = OFF        ;Disabled

  FOSC = EC2        ;HS oscillator (High power, 16 MHz - 25 MHz)

  PLLCFG = On        ;Enabled

  FCMEN = OFF        ;Disabled

  IESO = OFF        ;Disabled

  PWRTEN = OFF        ;Disabled

  BOREN = OFF        ;Disabled in hardware, SBOREN disabled

  BORV = 0        ;3.0V

  BORPWR = High        ;BORMV set to high power level

  WDTEN = OFF        ;WDT disabled in hardware; SWDTEN bit disabled

  WDTPS = 1048576        ;1:1048576

  RTCOSC = SOSCREF        ;RTCC uses SOSC

  CCP2MX = PORTBE        ;RC1

  MSSPMSK = MSK7        ;7 Bit address masking mode

  MCLRE = On        ;MCLR Enabled, RG5 Disabled

  STVREN = On        ;Enabled

  BBSIZ = BB2K        ;2K word Boot Block size

  Debug = OFF        ;Disabled

  Cp0 = OFF        ;Disabled

  CP1 = OFF        ;Disabled

  CP2 = OFF        ;Disabled

  CP3 = OFF        ;Disabled

  CP4 = OFF        ;Disabled

  CP5 = OFF        ;Disabled

  CP6 = OFF        ;Disabled

  CP7 = OFF        ;Disabled

  CPB = OFF        ;Disabled

  CPD = OFF        ;Disabled

  WRT0 = OFF        ;Disabled

  WRT1 = OFF        ;Disabled

  WRT2 = OFF        ;Disabled

  WRT3 = OFF        ;Disabled

  WRT4 = OFF        ;Disabled

  WRT5 = OFF        ;Disabled

  WRT6 = OFF        ;Disabled

  WRT7 = OFF        ;Disabled

  WRTC = OFF        ;Disabled

  WRTB = OFF        ;Disabled

  WRTD = OFF        ;Disabled

  EBRT0 = OFF        ;Disabled

  EBRT1 = OFF        ;Disabled

  EBRT2 = OFF        ;Disabled

  EBRT3 = OFF        ;Disabled

  EBRT4 = OFF        ;Disabled

  EBRT5 = OFF        ;Disabled

  EBRT6 = OFF        ;Disabled

  EBRT7 = OFF        ;Disabled

  EBRTB = OFF        ;Disabled

Config_End  






Created with the Personal Edition of HelpNDoc: News and information about help authoring tools and software