-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
343 lines (256 loc) · 7.25 KB
/
Copy pathmain.c
File metadata and controls
343 lines (256 loc) · 7.25 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <locale.h>
#include "filereader.h"
#define KEY_1 49
#define KEY_2 50
#define KEY_3 51
#define KEY_4 52
#define KEY_5 53
#define KEY_6 54
#define KEY_7 55
#define KEY_8 56
#define KEY_9 57
Artwork** read_artworks(void);
void draw_artworks(Artwork** artworks, Motor* motorX, Motor* motorY,
Stylus* stylus, Servo* servo);
void move_pen(Motor* motorX, Motor* motorY);
void pen_menu(void);
void main_menu(void);
void draw_menu(void);
void initialise(void);
void draw_square(Motor* motorX, Motor* motorY);
int main(int argc, char* argv[]) {
// Ncurses Setup
setlocale(LC_ALL, "");
initscr();
keypad(stdscr, TRUE);
noecho();
cbreak();
nodelay(stdscr, TRUE);
Motor motorX = { .dirPin = MOTOR_X_DIR, .step = MOTOR_X_STEP,
.enable = ENABLE_X, .logic = LOGIC_X };
Motor motorY = { .dirPin = MOTOR_Y_DIR, .step = MOTOR_Y_STEP,
.enable = ENABLE_Y, .logic = LOGIC_Y };
Stylus stylus = { .currentX = 0, .currentY = 0, .state = 0 };
Servo servo = { .state = 0 };
// RPi Pins Setup
initialise();
// Read-in art files
Artwork** artworks = read_artworks();
main_menu();
int input = 0;
while (1) {
input = getch();
switch(input) {
case KEY_1:
draw_artworks(artworks, &motorX, &motorY, &stylus, &servo);
main_menu();
break;
case KEY_2:
move_pen(&motorX, &motorY);
main_menu();
break;
case KEY_3:
exit(0);
}
}
endwin();
return 0;
}
void draw_artworks(Artwork** artworks, Motor* motorX, Motor* motorY,
Stylus* stylus, Servo* servo) {
draw_menu();
int input = 0;
while (1) {
input = getch();
switch(input) {
case KEY_1:
printw(" \r");
printw("Moving the stylus......\r");
//sketch_artwork(artworks..., motorX, motorY, stylus, servo);
break;
case KEY_2:
printw(" \r");
printw("Drawing a line......\r");
//sketch_artwork(artworks...., motorX, motorY, stylus, servo);
break;
case KEY_3:
printw(" \r");
printw("Drawing a border......\r");
//sketch_artworks(artworks[5], motorX, motorY, stylus, servo);
draw_square(motorX, motorY);
break;
case KEY_4:
printw(" \r");
printw("Drawing the Basic Artwork......\r");
sketch_artwork(artworks[0], motorX, motorY, stylus, servo);
break;
case KEY_5:
printw(" \r");
printw("Drawing the Beginner Artwork.......\r");
//sketch_artwork(artworks[1], motorX, motorY, stylus, servo);
break;
case KEY_6:
printw(" \r");
printw("Drawing the Apprentice Artwork......\r");
//sketch_artwork(artworks[2], motorX, motorY, stylus, servo);
break;
case KEY_7:
printw(" \r");
printw("Drawing the Journeyman Artwork.......\r");
//sketch_artwork(artworks[3], motorX, motorY, stylus, servo);
break;
case KEY_8:
printw(" \r");
printw("Drawing the Master Artwork...........\r");
//sketch_artwork(artworks[4], motorX, motorY, stylus, servo);
break;
case KEY_9:
return;
// else keep looping
}
}
}
void draw_square(Motor* motorX, Motor* motorY) {
digitalWrite(motorX->dirPin, 1);
digitalWrite(motorX->enable, LOW);
for (int i = 0; i < 7000; ++i) {
digitalWrite(motorX->step, HIGH);
delayMicroseconds(100);
digitalWrite(motorX->step, LOW);
delayMicroseconds(1400);
}
digitalWrite(motorX->enable, HIGH);
delay(1);
digitalWrite(motorY->dirPin, 1);
digitalWrite(motorY->enable, LOW);
for (int i = 0; i < 7000; ++i) {
digitalWrite(motorY->step, HIGH);
delayMicroseconds(100);
digitalWrite(motorY->step, LOW);
delayMicroseconds(1400);
}
digitalWrite(motorY->enable, HIGH);
delay(1);
digitalWrite(motorX->dirPin, 0);
digitalWrite(motorX->enable, LOW);
for (int i = 0; i < 7000; ++i) {
digitalWrite(motorX->step, HIGH);
delayMicroseconds(100);
digitalWrite(motorX->step, LOW);
delayMicroseconds(1400);
}
digitalWrite(motorX->enable, HIGH);
delay(1);
digitalWrite(motorY->dirPin, 0);
digitalWrite(motorY->enable, LOW);
for (int i = 0; i < 7000; ++i) {
digitalWrite(motorY->step, HIGH);
delayMicroseconds(100);
digitalWrite(motorY->step, LOW);
delayMicroseconds(1400);
}
digitalWrite(motorY->enable, HIGH);
}
void move_pen(Motor* motorX, Motor* motorY) {
pen_menu();
int input = 0;
while (1) {
input = getch();
switch(input) {
case KEY_UP:
printw("Moving Stylus Forwards.....\r");
drive_motor(motorY, 1, FORWARD, STEPS_PER_REV);
flushinp();
break;
case KEY_RIGHT:
printw("Moving Stylus to the right......\r");
drive_motor(motorX, 1, FORWARD, STEPS_PER_REV);
flushinp();
break;
case KEY_LEFT:
printw("Moving Stylus to the left......\r");
drive_motor(motorX, 1, BACKWARD, STEPS_PER_REV);
flushinp();
break;
case KEY_DOWN:
printw("Moving Stylus Backwards.....\r");
drive_motor(motorY, 1, BACKWARD, STEPS_PER_REV);
flushinp();
break;
case KEY_5:
return;
default:
digitalWrite(motorX->enable, HIGH);
digitalWrite(motorY->enable, HIGH);
}
}
}
void initialise() {
wiringPiSetup();
// Set up pins
pinMode(MOTOR_X_STEP, OUTPUT);
pinMode(MOTOR_Y_STEP, OUTPUT);
pinMode(MOTOR_X_DIR, OUTPUT);
pinMode(MOTOR_Y_DIR, OUTPUT);
//pinMode(LOGIC_X, OUTPUT);
pinMode(ENABLE_X, OUTPUT);
//pinMode(LOGIC_Y, OUTPUT);
pinMode(ENABLE_Y, OUTPUT);
//digitalWrite(LOGIC_X, HIGH);
digitalWrite(ENABLE_X, HIGH);
//digitalWrite(LOGIC_Y, HIGH);
digitalWrite(ENABLE_Y, HIGH);
// Motor pins
// Servo pins
// Limit switches
}
void main_menu() {
clear();
printw("Welcome to microangelo's amazing artbot!\n");
printw("Select an action from the below menu\n");
printw("[1] - Draw art\n");
printw("[2] - Move pen\n");
printw("[3] - Exit\n");
refresh();
}
void draw_menu() {
clear();
printw("Lets draw some art!\n");
printw("Select an action from the below menu\n");
printw("[1] - Show that we can move the stylus\n");
printw("[2] - Draw a line\n");
printw("[3] - Draw a border\n");
printw("[4] - Draw the Basic Artwork\n");
printw("[5] - Draw the Beginner Artwork\n");
printw("[6] - Draw Apprentice Artwork\n");
printw("[7] - Draw Journeyman Artwork\n");
printw("[8] - Draw Master Artwork\n");
printw("[9] - Back\n");
refresh();
}
void pen_menu() {
clear();
printw("Pen Menu\n");
printw("[\u2190 ] - Move Pen Left\n");
printw("[\u2192 ] - Move Pen Right\n");
printw("[\u2191 ] - Move Pen Forward\n");
printw("[\u2193 ] - Move Pen Backward\n");
printw("[5] - Back\n");
refresh();
}
Artwork** read_artworks() {
Artwork** artworks = malloc(sizeof(Artwork*) * 7);
artworks[0] = filereader("artwork-files/basic.txt");
artworks[1] = filereader("artwork-files/beginner.txt");
artworks[2] = filereader("artwork-files/apprentice.txt");
artworks[3] = filereader("artwork-files/journeyman.txt");
artworks[4] = filereader("artwork-files/master.txt");
artworks[5] = filereader("artwork-files/square.txt");
artworks[6] = '\0';
return artworks;
}