$regfile = "m2560def.dat" '8K SRAM | 4K EEPROM
$crystal = 16000000
$hwstack = 256
$swstack = 128
$framesize = 256
$baud = 115200
$baud1 = 9600
Debug On
Config Submode = New
Const Max_usb_in_command_len = 50
Const Max_nex_command_len = 50
'*****************************************************************
'* CONFIG BUFFERED USART FOR DEBUG via USB *
'*****************************************************************
Config Com1 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Echo Off
Config Serialin0 = Buffered , Size = Max_usb_in_command_len 'RX from Terminal
Dim Char As Byte , Cmd_timeout1 As Byte , Got_str1 As Byte
Dim Command1 As String * Max_usb_in_command_len
'Config Serialout0 = Buffered , Size = Max_usb_out_command_len 'TX to the Terminal
Open "COM1:" For Binary As #1
Const Usb = 1
'*****************************************************************
'* CONFIG BUFFERED USART FOR NEXTION *
'*****************************************************************
Config Com2 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Serialin1 = Buffered , Size = Max_nex_command_len 'RX
Dim Cmd_timeout As Byte , Got_str As Byte
'unnecessary
Dim Command As String * Max_nex_command_len
'Config Serialout1 = Buffered , Size = Max_nex_command_len 'TX
Open "COM2:" For Binary As #2
Const Nex = 2
'*****************************************************************
'* TIMER2 FOR TIME BASE *
'*****************************************************************
Config Timer2 = Timer , Prescale = 1024 , Compare_a = Disconnect , Compare_b = Disconnect , Clear_timer = 1
Compare2a = 155 '10ms @16MHz/1024
'BARTek multipurpose
Dim Helpb As Byte , Helpstr As String * 30 , N As Byte , Tempb As Byte , Helpl As Long
Enable Interrupts 'buffered USART
Print #usb , "Program restarted"
'NEXTION RETURN DATA LENGTH
Const Invalid_instruction = &H00 '4
Const Instruction_okay = &H01 '4
Const Invalid_component_id = &H02 '4
Const Invalid_page_id = &H03 '4
Const Invalid_picture_id = &H04 '4
Const Invalid_font_id = &H05 '4
Const Invalid_file_operation = &H06 '4
Const Invalid_crc = &H09 '4
Const Invalid_baud_rate = &H11 '4
Const Invalid_vaweform_id = &H12 '4
Const Invalid_variable_name = &H1A '4
Const Invalid_variable_operation = &H1B '4
Const Assignement_failed = &H1C '4
Const Eeprom_operation_failed = &H1D '4
Const Invalid_parameters_quantity = &H1E '4
Const Io_operation_failed = &H1F '4
Const Escape_char_invalid = &H20 '4
Const Variable_name_to_long = &H23 '4
'return codes valid in all cases
'00 00 00 FF FF FF =>Nextion starup '6
Const Serial_buff_overflow = &H24 '4
Const Touch_event = &H65 '7
' 65 00 01 01 FF FF FF
' | | | |
' | | | event 01=press, 00=release
' | | component ID
' | page number
' touch event
Const Curr_page_num = &H66 '5 second byte is page num
Const Touch_awake_coordinate = &H67 '9
Const Touch_sleep_coordinate = &H68
'first two bytes is X
'next two bytes is Y
'last byte is event 01=press, 00=release
'eg. 67 00 7A 00 1E 01 FF FF FF
Const String_data = &H70 'varied len eg.70 xx xx xx xx xx xx FF FF FF
Const Numeric_data = &H71 '8 eg.71 xx xx xx xx FF FF FF <-4bytes 32bit value, little endian
Const Auto_sleep = &H86 '4
Const Auto_awake = &H87 '4
Const Nextion_ready = &H88 '4
Const Micro_sd_upgrade = &H89 '4
Const Transp_data_finished = &HFD '4
Const Transp_data_ready = &HFE '4
Macro Nexend 'to end a tx sequence--------------------------
Printbin #nex , &HFF ; &HFF ; &HFF
End Macro
Macro Nexq ' to send quote before or after a sentence---------
Printbin &H22
End Macro
Sub Set_return_level()
'how much return will be sent over serial
'LEVEL (2 is default)
'0=no return command will be sent
'1=only confirm succes commands
'2=only if command failed
'3=all return command from &H00 to &H23 will be sent
Print #nex , "bkcmd=1";
Nexend
End Sub
Dim Nex_buff(30) As Byte , Nex_idx As Byte , End_cnt As Byte , Min_len As Byte
Dim New_long As Long At Nex_buff(2) Overlay
Dim New_string As String * 29 At Nex_buff(2) Overlay
'for touch event
Dim Touch_page As Byte At Nex_buff(2) Overlay
Dim Touch_id As Byte At Nex_buff(3) Overlay
Dim Touch_state As Byte At Nex_buff(4) Overlay '1=press , 0=release
'for coordinates
Dim New_x As Word At Nex_buff(2) Overlay
Dim New_y As Word At Nex_buff(4) Overlay
Call Set_return_level
Do
'--[ USART0 COM1 USB ]------------------------
If 0 < Ischarwaiting(#usb) Then
Char = Inkey(#usb)
Cmd_timeout1 = 10 '
Select Case Char
Case 10 : Got_str1 = 1
Case 13
Case Else
Command1 = Command1 + Chr(char)
End Select
If Len(command1) > 49 Then Got_str1 = 1
If Got_str1 = 0 Then Cmd_timeout1 = 50 '50x10ms
'end if Ischarwaiting
End If
'--[ PARSE COM1 USB ]-------------------------
If Got_str1 = 1 Then
If 0 < Instr(command1 , "AT+RST") Then
Print #usb , "Reseting in progress..."
'Waitms 50 'tylko jeśli Serialout bo wyłaczmy przerwania
Disable Interrupts
Config Watchdog = 16
Start Watchdog
Do : Loop
Elseif 0 < Instr(command1 , "AT+ON") Then
Print #nex , "sendxy=1";
Nexend
Elseif 0 < Instr(command1 , "AT+OFF") Then
Print #nex , "sendxy=0";
Nexend
End If
Got_str1 = 0
Command1 = ""
Cmd_timeout1 = 0
End If
'--[ USART1 COM2 nex ]---------------------
If 0 < Ischarwaiting(#nex) Then
Incr Nex_idx
Nex_buff(nex_idx) = Waitkey(#nex)
Cmd_timeout = 10 'unnecessary
If Nex_idx = 1 Then 'check what type of msg we receiving
Select Case Nex_buff(1)
Case 0
'it can be 4bytes Invalid_instruction
'or 6 bytes Nextion_startup (may be after reset)
Min_len = 1
Case Touch_event
Min_len = 4
Case Touch_awake_coordinate '9 bytes
Min_len = 6
Case Touch_sleep_coordinate '9 bytes
Min_len = 6
Case String_data 'it will be string with variable len
Min_len = 2
Case Numeric_data 'there can be FF`s so we must wait for 8 bytes
Min_len = 5
Case Else
Min_len = 1 'rest of return`s have one byte followed three FF`s
End Select
Else
'msg continue
If Nex_buff(nex_idx) = &HFF Then
If Nex_idx > Min_len Then
If End_cnt < 2 Then
Incr End_cnt
Else 'we received third FF
' '-----------------------------[ DEBUG ]----------
' Debug #usb , "Debug=> ";
' For N = 1 To Nex_idx
' Debug #usb , Hex(nex_buff(n)) ; " ";
' Next
' Debug #usb , ""
' '------------------------------------------------
'parse
Select Case Nex_buff(1)
Case Invalid_instruction
If Nex_idx = 4 Then
Debug #usb , "Invalid instruction!"
Elseif Nex_idx = 6 Then
Debug #usb , "Nextion rebooted OK"
End If
Case Instruction_okay
Debug #usb , "Command accepted"
Case Invalid_variable_name
Debug #usb , "Variable or component NAME not recognized!"
Case Touch_event
If Touch_state = 0 Then
Debug #usb , "Relased object " ; Touch_id ; " on page " ; Touch_page
Else
Debug #usb , "Pressed object " ; Touch_id ; " on page " ; Touch_page
End If
Case Touch_awake_coordinate To Touch_sleep_coordinate
Swap New_x 'big endian order received
Swap New_y
Debug #usb , "Coordinates are: X=" ; New_x ; " Y=" ; New_y
Case String_data
Helpb = Nex_idx - 2
Nex_buff(helpb) = 0 'add string terminator
Debug #usb , "New string=> " ; New_string
'You can parse commands/data here
Case Numeric_data
Debug #usb , "New value=> " ; New_long
'If you expect numeric data from Nextion then it will be here
End Select
Nex_idx = 0
End_cnt = 0
End If
End If
End If
End If
End If
'--[ TIMER2 10ms ]----------------------------
If Tifr2.ocf2a = 1 Then
Tifr2.ocf2a = 1
If Cmd_timeout1 > 0 Then
Decr Cmd_timeout1
If Cmd_timeout1 = 0 Then
Command1 = ""
End If
End If
If Cmd_timeout > 0 Then
Decr Cmd_timeout
If Cmd_timeout = 0 Then
Nex_idx = 0
End_cnt = 0
End If
End If
'10ms
End If
Loop
End