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>
|
8
|
10
|
1
|
11 #include "basedfilelib.h"
|
7
|
12
|
|
13 #ifdef __linux__
|
|
14 #include "linuxconio.h"
|
|
15 #endif
|
|
16
|
|
17 #ifdef _WIN32
|
11
|
18 #include <windows.h>>
|
7
|
19 #include <conio.h>
|
|
20 #endif
|
1
|
21
|
|
22 #define DEEPNESS 5
|
|
23 #define MAXPOINT 4096
|
13
|
24 #define UNITS "G20\n"
|
2
|
25 #define SPEED 20
|
1
|
26
|
18
|
27 #define SBPUP "JZ &ZUP\n"
|
|
28 #define SBPDWN 0.0625
|
|
29
|
|
30
|
1
|
31 typedef struct{
|
6
|
32 unsigned int X;
|
|
33 unsigned int Y;
|
|
34 unsigned int down;
|
1
|
35 }point;
|
|
36
|
4
|
37 point points[MAXPOINT];
|
1
|
38
|
8
|
39 unsigned int X = 100;
|
11
|
40 unsigned int Y = 25;
|
6
|
41 unsigned int step = 5;
|
5
|
42 unsigned int down = 0;
|
|
43 unsigned int numpoints = 0;
|
1
|
44 char action;
|
|
45
|
18
|
46
|
|
47 /*********************************************
|
|
48 * Description - Convert integers to strings, makes
|
|
49 * source code more pretty.
|
|
50 * Author - William King
|
|
51 * Date - Sep 26 2023
|
|
52 * *******************************************/
|
|
53 char * IntToString(int num){
|
|
54 char * s;
|
|
55 sprintf(s,"%d",num);
|
|
56 return s;
|
|
57 }
|
|
58
|
1
|
59 /*********************************************
|
|
60 * Description - Renders the screen
|
|
61 * Author - William King
|
|
62 * Date - Sep 08 2023
|
|
63 * *******************************************/
|
8
|
64 void Render(){
|
7
|
65 clrscr();
|
10
|
66 gotoxy(0,3);
|
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
|
1
|
71 for(int i = 0;i != MAXPOINT;i++){
|
8
|
72 gotoxy(0,50);
|
7
|
73
|
|
74 gotoxy(points[i].X,points[i].Y);
|
|
75
|
5
|
76 if(points[i].down == 0){
|
9
|
77 printf("X");
|
5
|
78 }
|
|
79 else{
|
9
|
80 printf("*");
|
5
|
81 }
|
1
|
82
|
7
|
83
|
1
|
84 }
|
|
85
|
5
|
86 //Render cursor
|
7
|
87
|
|
88 gotoxy(X,Y);
|
|
89
|
5
|
90 puts("&");
|
|
91
|
10
|
92 //Move cursor to top of document, so its not trailing the cadcursor
|
|
93 gotoxy(0,0);
|
|
94
|
5
|
95
|
1
|
96
|
|
97 }
|
|
98
|
|
99 /*********************************************
|
|
100 * Description - This function compiles instructions for CNC machines.
|
|
101 * Author - William King
|
|
102 * Date - Sep 08 2023
|
|
103 * *******************************************/
|
|
104 void Compile(){
|
12
|
105
|
13
|
106 char filename[64];
|
12
|
107 char file[8192];
|
13
|
108 char buffer[64];
|
12
|
109 unsigned int choice;
|
|
110
|
11
|
111 clrscr();
|
18
|
112 puts("Select your format\n1. RAW GCODE\n2. OPENSBP");
|
1
|
113 scanf("%d",&choice);
|
|
114
|
|
115
|
|
116 puts("Enter the filename");
|
|
117 scanf("%s",filename);
|
13
|
118 printf("Compiling %s...\n",filename);
|
1
|
119 if(choice == 1){
|
2
|
120 //Set the miller
|
|
121 strcat(file,UNITS);
|
13
|
122 strcat(file,"G0 X0 Y0 Z0\n");
|
2
|
123 //Meat of instructions
|
|
124 for(int i = 0;i != numpoints;i++){
|
4
|
125 //Compose
|
15
|
126 char instruction[64];
|
13
|
127 strcat(instruction,"G0 X ");
|
14
|
128 //Convert X to string
|
|
129 sprintf(buffer,"%d",points[i].X);
|
8
|
130 strcat(instruction,buffer);
|
13
|
131 strcat(instruction," Y ");
|
11
|
132 //Convert Y to string
|
14
|
133 sprintf(buffer,"%d",points[i].Y);
|
8
|
134 strcat(instruction,buffer);
|
12
|
135 if(points[i].down = 1){
|
13
|
136 strcat(instruction," Z 5\n");
|
12
|
137 }
|
|
138 else{
|
13
|
139 strcat(instruction," Z 0\n");
|
12
|
140 }
|
4
|
141 //Write to string
|
|
142 strcat(file,instruction);
|
2
|
143 }
|
16
|
144
|
2
|
145 //Finish
|
13
|
146 strcat(file,"G0 X0 Y0 Z0\n");
|
2
|
147 WriteFile(filename,file);
|
1
|
148 }
|
18
|
149 else if(choice == 2){
|
|
150 // Setup the miller
|
|
151 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");
|
|
152
|
|
153 for(int i = 0;i != numpoints;i++){
|
|
154
|
|
155 char instruction[64];
|
|
156
|
|
157 strcat(instruction,"J2 ");
|
|
158 strcat(instruction,IntToString(points[i].X));
|
|
159 strcat(instruction," ");
|
|
160 strcat(instruction,IntToString(points[i].Y));
|
|
161 strcat(instruction,"\n");
|
|
162
|
|
163 if(points[i].down == 0){
|
|
164 strcat(instruction,"MZ -0,0625\n");
|
|
165 }
|
|
166 else{
|
|
167 strcat(instruction,"JZ,&ZUP\n");
|
|
168 }
|
|
169
|
|
170
|
|
171 strcat(file,instruction);
|
|
172 }
|
|
173
|
|
174 //Finished, add instructions to wait for the user
|
|
175 strcat(file,"\nJ2,0,0\nSO, 1,0\n'All done. Wait for user.\nPAUSE");
|
|
176
|
|
177 WriteFile(filename,file);
|
|
178
|
|
179 }
|
12
|
180 else{
|
|
181
|
16
|
182 puts("Invalid compiliation format");
|
|
183 scanf("");
|
12
|
184
|
|
185
|
|
186 }
|
1
|
187
|
18
|
188 puts("Finished compiling instructions");
|
16
|
189 scanf("");
|
12
|
190
|
1
|
191 }
|
|
192
|
|
193
|
|
194 /*********************************************
|
|
195 * Description - Main function
|
|
196 * Author - William King
|
|
197 * Date - Sep 08 2023
|
|
198 * *******************************************/
|
|
199 void main(int argc, char* argv[]){
|
9
|
200 clrscr();
|
5
|
201 while(1){
|
8
|
202 Render();
|
9
|
203 switch(getchar()){
|
12
|
204 case 'h':
|
|
205 X -= step;
|
|
206 break;
|
|
207 case 'j':
|
|
208 Y += step;
|
|
209 break;
|
|
210 case 'k':
|
|
211 Y -= step;
|
|
212 break;
|
|
213 case 'l':
|
|
214 X += step;
|
|
215 break;
|
2
|
216
|
12
|
217 case 's':
|
|
218 puts("New stepsize");
|
|
219 scanf("%d",&step);
|
9
|
220
|
12
|
221 break;
|
9
|
222
|
12
|
223 case 'a':
|
|
224 points[numpoints].X = X;
|
|
225 points[numpoints].Y = Y;
|
|
226 points[numpoints].down = down;
|
|
227 numpoints++;
|
|
228 break;
|
|
229 case 'd':
|
|
230 if(down == 1){
|
|
231 down = 0;
|
|
232 }
|
|
233 else{
|
|
234 down = 1;
|
|
235 }
|
|
236 break;
|
9
|
237
|
12
|
238 case 'c':
|
|
239 Compile();
|
|
240 break;
|
14
|
241 case 'q':
|
|
242 clrscr();
|
|
243 exit(0);
|
|
244 break;
|
12
|
245 default:
|
|
246 putchar('\a');
|
|
247 break;
|
8
|
248 }
|
5
|
249 }
|
|
250
|
1
|
251 exit(0);
|
|
252 }
|