forked from MarcoAntonini/RepRapLcd4D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialManager.fnc
213 lines (188 loc) · 6.34 KB
/
SerialManager.fnc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
this file is part of ReprapLcd4D Project
Copyright (C) 2012 Marco Antonini
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var cmd_buff[BUFF_LEN];
var buff_index :=0;
var __COMBUFF[COMBUFF_LEN];
//Function
func SerialInit()
com_Init(__COMBUFF, COMBUFF_LEN, 0 );
com_SetBaud(COM0, 11520);
endfunc
func SerialEvent()
var char;
char:=serin();
if(char >= 0 ) // is valid char
cmd_buff[buff_index]:=char;
if(cmd_buff[buff_index] == '\n')
cmd_buff[buff_index]:='\0';
if(WINDOW!=W_SDCARD || SD_READING==FALSE)
parseCmd(cmd_buff,buff_index);
else if(WINDOW==W_SDCARD)
parseSDFile(cmd_buff,buff_index);
endif
buff_index:=0;
else
buff_index++;
endif
if(buff_index >= BUFF_LEN)
buff_index:=BUFF_LEN-1;
endif
endif
endfunc
func parseCmd(var *buffer,var n)
var i;
var cmd_id[3],cmd_val[BUFF_LEN],index_val;
var parsing_val:=FALSE;
var cmd_val_buff[BUFF_LEN];
var p_cmd_val;
for( i:=0; i<n; i++)
if(parsing_val==TRUE) //Cmd Val
cmd_val[i-index_val-1]:=buffer[i];
else if(buffer[i]==':')
parsing_val:=TRUE;
index_val:=i;
else if(i<3) //Cmd Id
cmd_id[i]:=buffer[i];
endif
next
// buff2str(cmd_id,index_val,cmd_id_buff);
// p_cmd_id := str_Ptr(cmd_id_buff);
buff2str(cmd_val,n-index_val-1,cmd_val_buff);
p_cmd_val := str_Ptr(cmd_val_buff);
if( cmd_id[0] == MESSAGE_ID )
updateMessage(p_cmd_val,"","");
to(msg); printBuffer(p_cmd_val);
else if( cmd_id[0] == HOTEND_ID )
if(cmd_id[1]=='0')
if(WINDOW!=W_SDCARD && WINDOW !=W_PRINT_CONFIRM) updateHotEnd0(p_cmd_val);
to(tH0); printBuffer(p_cmd_val);
else if(cmd_id[1]=='1')
if(WINDOW!=W_SDCARD && WINDOW !=W_PRINT_CONFIRM) updateHotEnd1(p_cmd_val);
to(tH1); printBuffer(p_cmd_val);
endif
else if( cmd_id[0] == TARGETT_ID )
if(cmd_id[1]=='0')
if(WINDOW!=W_SDCARD && WINDOW !=W_PRINT_CONFIRM)
updateTHotEnd0(p_cmd_val);
updateHotEnd0(str_Ptr(tH0));
endif
to(ttH0); printBuffer(p_cmd_val);
else if(cmd_id[1]=='1')
if(WINDOW!=W_SDCARD && WINDOW !=W_PRINT_CONFIRM)
updateTHotEnd1(p_cmd_val);
updateHotEnd1(str_Ptr(tH1));
endif
to(ttH1); printBuffer(p_cmd_val);
else if(cmd_id[1]=='B')
if(WINDOW!=W_SDCARD && WINDOW!=W_PRINTING_OPTION && WINDOW !=W_PRINT_CONFIRM)
updateTBed(p_cmd_val);
updateBed(str_Ptr(tB));
endif
to(ttB); printBuffer(p_cmd_val);
endif
else if( cmd_id[0] == BED_ID )
if(WINDOW!=W_SDCARD && WINDOW!=W_PRINTING_OPTION && WINDOW !=W_PRINT_CONFIRM)
updateBed(p_cmd_val);
endif
to(tB); printBuffer(p_cmd_val);
else if( cmd_id[0] == TIME_ID )
updateTime(p_cmd_val);
to(timePrint); printBuffer(p_cmd_val);
else if( cmd_id[0] == ZPOS_ID )
updateZpos(p_cmd_val);
to(zPos); printBuffer(p_cmd_val);
else if( cmd_id[0] == SDPERCENT_ID )
updateSDPerc(p_cmd_val);
to(sdPerc); printBuffer(p_cmd_val);
else if( cmd_id[0] == SOUND_ID )
sound(FINISH);
else if(cmd_id[0] == DEBUG_SHOW_MEM_ID)
to(COM0); print("Memory available = ",mem_Heap(),"\n");
endif
endfunc
//SD_MAX_FILE
func parseSDFile(var *buffer,var n)
var p,i;
var val[BUFF_LEN];
buff2str(buffer,n,val);
p:=str_Ptr(val);
if(file_count>MAX_FILE)
file_count:=MAX_FILE;
return;
endif
if(str_Match(&p,"Begin file list") && FILE_START==FALSE) //FILESTART: avoid it a double malloc
FILE_START:=TRUE;
file_count:=0;
sd_current_page:=0;
filenames := mem_AllocZ(MAX_FILE*(MAX_FILE_NAME+2));
p_filenames := str_Ptr(filenames);
// allocate a buffer for the filenames
if(!filenames)
updateMessage("LCD ERRORR: Out of memory !\n","","");
//Reset...
endif
else if(str_Match(&p,"End file list"))
//for(i:=0; i<file_count; i++) //for DEBUG
// to(COM0); str_Printf(&files[i],"%s\n"); //for DEBUG
//next //for DEBUG
//SerialPrintNumber(file_count); //for DEBUG
SD_READING:=FALSE;
if(file_count>0)
sd_page_count:=file_count/24;
updateButtonFileList();
endif
else if(FILE_START) //FILESTART semaphore
if(str_Find(&p,".G") || str_Find(&p,".g")) //if is GCODE file
files[file_count]:=p_filenames;
file_count++;
for(i:=0; i<n; i++)
str_PutByte(p_filenames,tolower(buffer[i]));
p_filenames++;
next
str_PutByte(p_filenames,'\0');
p_filenames++;
endif
endif
endfunc
func SerialPrintBuffer(var *buffer)
to(COM0); str_Printf(&buffer, "%s");
endfunc
func SerialPrintlnBuffer(var *buffer)
to(COM0); str_Printf(&buffer, "%s\n");
endfunc
func SerialPrintNumber(var numb)
to(COM0); putnum(DEC,numb);
endfunc
func SerialPrintlnNumber(var numb)
to(COM0); putnum(DEC,numb);
to(COM0); print("\n");
endfunc
func printBuffer(var *buffer)
str_Printf (&buffer, "%s");
endfunc
func buff2str(var *buffer,var buffer_count,var *buff_out)
var i:=0;
for(i:=0; i<buffer_count; i++)
if(i==0) to(buff_out);
if(i>0) to(APPEND);
print([CHR]buffer[i]);
next
endfunc
func SerialErrorWatchDog()
if(com_Error()) // if there were low level comms errors,
//SystemReset();
SerialInit();
endif
endfunc