1
|
1 /*********************************************
|
|
2 * Description - Harelet is a CAD and compiler program for CNC milling machines
|
|
3 * Author - William King
|
|
4 * Date - Sep 08 2023
|
|
5 * *******************************************/
|
|
6
|
|
7 #include <stdio.h>
|
|
8 #include <stdlib.h>
|
4
|
9 #include <string.h>
|
19
|
10 #include <assert.h>
|
8
|
11
|
20
|
12 #include "based/basedfilelib.h"
|
|
13 #include "based/basedtermgrx.h"
|
1
|
14
|
|
15 #define DEEPNESS 5
|
|
16 #define MAXPOINT 4096
|
13
|
17 #define UNITS "G20\n"
|
2
|
18 #define SPEED 20
|
1
|
19
|
|
20 typedef struct{
|
6
|
21 unsigned int X;
|
|
22 unsigned int Y;
|
|
23 unsigned int down;
|
1
|
24 }point;
|
|
25
|
4
|
26 point points[MAXPOINT];
|
1
|
27
|
19
|
28 unsigned int X = 5;
|
|
29 unsigned int Y = 5;
|
6
|
30 unsigned int step = 5;
|
5
|
31 unsigned int down = 0;
|
|
32 unsigned int numpoints = 0;
|
1
|
33 char action;
|
|
34
|
18
|
35
|
|
36 /*********************************************
|
19
|
37 * Description - Warn the user about something
|
|
38 * Author - William King
|
|
39 * Date - Sep 27 2023
|
|
40 * *******************************************/
|
|
41 void Warn(char * s){
|
|
42 printf("WARNING:");
|
|
43 puts(s);
|
|
44 putchar('\a');
|
|
45 getchar();
|
|
46 }
|
|
47
|
|
48 /*********************************************
|
|
49 * Description - Convert integers to strings, makes
|
|
50 * source code more pretty.
|
|
51 * Author - William King
|
|
52 * Date - Sep 26 2023
|
|
53 * *******************************************/
|
20
|
54 char * IntToString(unsigned int num){
|
18
|
55 char * s;
|
|
56 sprintf(s,"%d",num);
|
|
57 return s;
|
|
58 }
|
|
59
|
1
|
60 /*********************************************
|
|
61 * Description - Renders the screen
|
|
62 * Author - William King
|
|
63 * Date - Sep 08 2023
|
|
64 * *******************************************/
|
8
|
65 void Render(){
|
19
|
66 ClrScr();
|
6
|
67 puts("HARELET A CAD PROGRAM BY VILYAEM KENYAZ, PEEP SOFTWARE 2023");
|
|
68 printf("Number of Points: %d X: %d Y: %d STEPSIZE: %d DWN?: %d\n",numpoints,X,Y,step,down);
|
|
69
|
5
|
70 //Render points
|
19
|
71 if(numpoints > 0){
|
|
72 for(int i = 0;i != numpoints;i++){
|
|
73 if(points[i].down == 0){
|
|
74 DrawChar(points[i].X,points[i].Y,'U');
|
7
|
75
|
19
|
76 }
|
|
77 else{
|
|
78 DrawChar(points[i].X,points[i].Y,'D');
|
|
79 }
|
5
|
80 }
|
1
|
81 }
|
|
82
|
5
|
83 //Render cursor
|
19
|
84 DrawChar(X,Y,'&');
|
5
|
85
|
19
|
86 Splash();
|
1
|
87
|
|
88 }
|
|
89
|
|
90 /*********************************************
|
|
91 * Description - This function compiles instructions for CNC machines.
|
|
92 * Author - William King
|
|
93 * Date - Sep 08 2023
|
|
94 * *******************************************/
|
|
95 void Compile(){
|
12
|
96
|
13
|
97 char filename[64];
|
12
|
98 char file[8192];
|
13
|
99 char buffer[64];
|
12
|
100 unsigned int choice;
|
|
101
|
18
|
102 puts("Select your format\n1. RAW GCODE\n2. OPENSBP");
|
1
|
103 scanf("%d",&choice);
|
|
104
|
|
105
|
|
106 puts("Enter the filename");
|
|
107 scanf("%s",filename);
|
13
|
108 printf("Compiling %s...\n",filename);
|
1
|
109 if(choice == 1){
|
2
|
110 //Set the miller
|
|
111 strcat(file,UNITS);
|
13
|
112 strcat(file,"G0 X0 Y0 Z0\n");
|
2
|
113 //Meat of instructions
|
|
114 for(int i = 0;i != numpoints;i++){
|
4
|
115 //Compose
|
15
|
116 char instruction[64];
|
13
|
117 strcat(instruction,"G0 X ");
|
19
|
118 strcat(instruction,IntToString(points[i].X));
|
13
|
119 strcat(instruction," Y ");
|
19
|
120 strcat(instruction,IntToString(points[i].Y));
|
|
121 if(points[i].down == 1){
|
13
|
122 strcat(instruction," Z 5\n");
|
12
|
123 }
|
|
124 else{
|
13
|
125 strcat(instruction," Z 0\n");
|
12
|
126 }
|
4
|
127 //Write to string
|
|
128 strcat(file,instruction);
|
2
|
129 }
|
16
|
130
|
2
|
131 //Finish
|
13
|
132 strcat(file,"G0 X0 Y0 Z0\n");
|
2
|
133 WriteFile(filename,file);
|
1
|
134 }
|
18
|
135 else if(choice == 2){
|
19
|
136 // Setup the miller
|
|
137 strcat(file,"'OpenSBP file written by Harelet\n'Harelet, written by William King\n'No responsibilty is taken for any damages to any equipment\n\n'Starting\nSO, 1,1\nPause 2\nSA,\n&ZUP = 0.25\nMH\n\n'Bulk of instructions\n");
|
18
|
138
|
19
|
139 for(int i = 0;i != numpoints;i++){
|
|
140
|
18
|
141 char instruction[64];
|
|
142
|
|
143 strcat(instruction,"J2 ");
|
|
144 strcat(instruction,IntToString(points[i].X));
|
|
145 strcat(instruction," ");
|
|
146 strcat(instruction,IntToString(points[i].Y));
|
|
147 strcat(instruction,"\n");
|
|
148
|
|
149 if(points[i].down == 0){
|
|
150 strcat(instruction,"MZ -0,0625\n");
|
|
151 }
|
|
152 else{
|
|
153 strcat(instruction,"JZ,&ZUP\n");
|
|
154 }
|
|
155
|
|
156 strcat(file,instruction);
|
19
|
157 }
|
18
|
158
|
19
|
159 //Finished, add instructions to wait for the user
|
|
160 strcat(file,"\nJ2,0,0\nSO, 1,0\n'All done. Wait for user.\nPAUSE");
|
18
|
161
|
19
|
162 WriteFile(filename,file);
|
18
|
163
|
|
164 }
|
12
|
165 else{
|
19
|
166 Warn("Invalid compiliation format");
|
12
|
167 }
|
1
|
168
|
18
|
169 puts("Finished compiling instructions");
|
19
|
170 getchar();
|
12
|
171
|
1
|
172 }
|
|
173
|
|
174
|
|
175 /*********************************************
|
|
176 * Description - Main function
|
|
177 * Author - William King
|
|
178 * Date - Sep 08 2023
|
|
179 * *******************************************/
|
|
180 void main(int argc, char* argv[]){
|
5
|
181 while(1){
|
8
|
182 Render();
|
19
|
183 #ifndef DVORAK
|
9
|
184 switch(getchar()){
|
12
|
185 case 'h':
|
19
|
186 if(X - step >= 2){
|
|
187 X -= step;
|
|
188 }
|
12
|
189 break;
|
|
190 case 'j':
|
19
|
191 if(Y + step < RESY - step){
|
|
192 Y += step;
|
|
193 }
|
12
|
194 break;
|
|
195 case 'k':
|
19
|
196 if(Y - step >= 2){
|
|
197 Y -= step;
|
|
198 }
|
12
|
199 break;
|
|
200 case 'l':
|
19
|
201 if(X + step < RESX - step){
|
|
202 X += step;
|
|
203 }
|
12
|
204 break;
|
|
205 case 's':
|
19
|
206 puts("New stepsize:");
|
12
|
207 scanf("%d",&step);
|
|
208 break;
|
9
|
209
|
12
|
210 case 'a':
|
|
211 points[numpoints].X = X;
|
|
212 points[numpoints].Y = Y;
|
|
213 points[numpoints].down = down;
|
|
214 numpoints++;
|
|
215 break;
|
|
216 case 'd':
|
|
217 if(down == 1){
|
|
218 down = 0;
|
|
219 }
|
|
220 else{
|
|
221 down = 1;
|
|
222 }
|
|
223 break;
|
|
224 case 'c':
|
|
225 Compile();
|
|
226 break;
|
14
|
227 case 'q':
|
19
|
228 ClrScr();
|
14
|
229 exit(0);
|
|
230 break;
|
12
|
231 default:
|
|
232 break;
|
8
|
233 }
|
19
|
234 #endif
|
|
235 #ifdef DVORAK
|
|
236 switch(getchar()){
|
|
237 case 'a':
|
|
238 if(X - step >= 2){
|
|
239 X -= step;
|
|
240 }
|
|
241 break;
|
|
242 case 'o':
|
|
243 if(Y + step < RESY - step){
|
|
244 Y += step;
|
|
245 }
|
|
246 break;
|
|
247 case ',':
|
|
248 if(Y - step >= 2){
|
|
249 Y -= step;
|
|
250 }
|
|
251 break;
|
|
252 case 'e':
|
|
253 if(X + step < RESX - step){
|
|
254 X += step;
|
|
255 }
|
|
256 break;
|
|
257 case '.':
|
|
258 puts("New stepsize:");
|
|
259 scanf("%d",&step);
|
|
260 break;
|
|
261
|
|
262 case ' ':
|
|
263 points[numpoints].X = X;
|
|
264 points[numpoints].Y = Y;
|
|
265 points[numpoints].down = down;
|
|
266 numpoints++;
|
|
267 break;
|
|
268 case 'p':
|
|
269 if(down == 1){
|
|
270 down = 0;
|
|
271 }
|
|
272 else{
|
|
273 down = 1;
|
|
274 }
|
|
275 break;
|
|
276 case 'y':
|
|
277 Compile();
|
|
278 break;
|
|
279 case 'q':
|
|
280 ClrScr();
|
|
281 exit(0);
|
|
282 break;
|
|
283 default:
|
|
284 break;
|
|
285 }
|
|
286 #endif
|
5
|
287 }
|
|
288
|
1
|
289 exit(0);
|
|
290 }
|