-
Notifications
You must be signed in to change notification settings - Fork 1
/
EggEsp.ino
197 lines (163 loc) · 3.35 KB
/
EggEsp.ino
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
#include <ESP8266WiFi.h>
#include <Servo.h>
#include "./config.h"
#include "./Stepper.hpp"
#include "./Draw2D.hpp"
#ifdef DEBUG
#define LOG(msg) Serial.print(msg)
#define LOGLN(msg) Serial.println(msg)
#else
#define LOG(msg)
#define LOGLN(msg)
#endif
#define OP_MOVE 1
#define OP_MOVE_REL 2
#define OP_LINE 3
#define OP_LINE_REL 4
struct header
{
uint16_t instruction_count;
//...
} __attribute__((packed));
struct instruction
{
uint8_t op;
uint8_t spacing1; //reserved
int16_t x;
int16_t y;
uint16_t spacing2; //reserved
} __attribute__((packed));
void togglePen(bool);
#define PEN_PIN D10
#define PEN_DRAWING 120
#define PEN_MOVING 160
bool penStatus;
Servo pen;
Stepper x(D0, D2, D1, D3);
Stepper y(D5, D7, D6, D8);
Draw2D draw(x, y, 0, 256, togglePen);
struct instruction *instructions = NULL;
uint16_t instruction_index;
uint16_t instruction_count = 0;
WiFiServer server(1337);
void togglePen(bool status)
{
if(status != penStatus)
{
if(penStatus)
{
pen.write(PEN_MOVING);
}
else
{
uint8_t angle = PEN_MOVING;
while(angle > PEN_DRAWING)
{
angle -= 2;
pen.write(angle);
delay(50);
}
}
penStatus = status;
}
}
void setup()
{
#ifdef DEBUG
Serial.begin(115200);
#endif
LOGLN("Initializing servo");
pen.attach(PEN_PIN, 1000, 2000);
pen.write(PEN_MOVING);
penStatus = false;
LOGLN("Initializing steppers");
x.init();
x.setSpeed(100);
y.init();
y.setSpeed(100);
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_MODE);
#if WIFI_MODE == WIFI_AP
LOGLN("Initializing WiFi AP");
WiFi.softAP(WIFI_SSID, WIFI_PSK, WIFI_AP_CHANNEL, false);
#elif WIFI_MODE == WIFI_STA
LOGLN("Initializing WiFi Station");
WiFi.begin(WIFI_SSID, WIFI_PSK);
#endif
server.begin();
delay(1000);
togglePen(true);
delay(1000);
togglePen(false);
}
void loop()
{
for(; instruction_index < instruction_count; instruction_index++)
{
int16_t x = instructions[instruction_index].x;
int16_t y = instructions[instruction_index].y;
LOG("Instruction: op = ");
LOG(instructions[instruction_index].op);
LOG(" x = ");
LOG(x);
LOG(" y = ");
LOGLN(y);
switch(instructions[instruction_index].op)
{
case OP_MOVE:
draw.moveTo(x, y);
break;
case OP_MOVE_REL:
draw.move(x, y);
break;
case OP_LINE:
draw.lineTo(x, y);
break;
case OP_LINE_REL:
draw.line(x, y);
break;
}
}
if(instructions != NULL)
{
togglePen(false);
free(instructions);
instructions = NULL;
}
WiFiClient input = server.available();
if(input)
{
LOGLN("Client connected");
struct header header;
if(input.readBytes((char *)&header, sizeof(struct header)) == sizeof(struct header))
{
LOG("Received header with instruction count ");
LOGLN(header.instruction_count);
size_t size = sizeof(struct instruction) * header.instruction_count;
instructions = (struct instruction *)malloc(size);
if(instructions != NULL)
{
instruction_index = 0;
instruction_count = header.instruction_count;
LOG("Transferring ");
LOG(size);
LOGLN(" bytes...");
input.write((uint8_t)0);
input.setTimeout(5000);
if(input.readBytes((char *)instructions, size) != size)
{
LOGLN("Transmission error!");
free(instructions);
instructions = NULL;
instruction_count = 0;
}
else
{
LOGLN("Successfully recived instructions");
}
}
}
input.stop();
}
}