-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLabelMakerCustomImage_Example.ino
More file actions
302 lines (260 loc) · 12.1 KB
/
Copy pathLabelMakerCustomImage_Example.ino
File metadata and controls
302 lines (260 loc) · 12.1 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
//This is the Custom Image hack for the Hack Pack LabelMaker
//////////////////////////////////////////////////
// LIBRARIES //
//////////////////////////////////////////////////
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Stepper.h>
#include <ezButton.h>
#include <Servo.h>
//////////////////////////////////////////////////
// PINS AND PARAMETERS //
//////////////////////////////////////////////////
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16x2 display
ezButton button1(14); //joystick button handler
#define INIT_MSG "Initializing..."
#define MODE_NAME "PLOT CUSTOM IMG "
#define PLOTTING " PLOTTING... "
// Set the grid resolution and desired drawing width (in steps)
// (Change GRID_SIZE here in the Python-generated code to 25, 40, etc.)
#define GRID_SIZE 80
#define DESIRED_WIDTH 1350
// Compute the scale factor so that (GRID_SIZE * customImagescale) equals DESIRED_WIDTH
int customImagescale = DESIRED_WIDTH / GRID_SIZE;
// Calculate the number of bytes in the bitmap
#define BITMAP_SIZE ((GRID_SIZE * GRID_SIZE + 7) / 8)
//////////////////////////////////////////////////
// JOYSTICK, MOTOR, SERVO, ETC. SETUP
//////////////////////////////////////////////////
const int joystickXPin = A2;
const int joystickYPin = A1;
const int joystickButtonThreshold = 200;
int currentCharacter = 0;
int cursorPosition = 0;
int currentPage = 0;
const int charactersPerPage = 16;
const int stepCount = 200;
const int stepsPerRevolution = 2048;
Stepper xStepper(stepsPerRevolution, 6,8,7,9);
Stepper yStepper(stepsPerRevolution, 2,4,3,5);
int xPins[4] = {6, 8, 7, 9};
int yPins[4] = {2, 4, 3, 5};
const int SERVO_PIN = 13;
Servo servo;
int angle = 30;
enum State { MainMenu, Editing, PrintConfirmation, Printing, plotCustomImg };
State currentState = MainMenu;
State prevState = Printing;
enum jState {LEFT, RIGHT, UP, DOWN, MIDDLE, UPRIGHT, UPLEFT, DOWNRIGHT, DOWNLEFT};
jState joyState = MIDDLE;
jState prevJoyState = MIDDLE;
boolean pPenOnPaper = false;
int lineCount = 0;
int xpos = 0;
int ypos = 0;
const int posS = 2;
const int posM = 7;
const int posL = 12;
bool joyUp;
bool joyDown;
bool joyLeft;
bool joyRight;
int button1State;
int joystickX;
int joystickY;
//////////////////////////////////////////////////
// BITMAP IMAGE DATA //
//////////////////////////////////////////////////
// The image is stored as a bit-packed array. Each bit represents a dot (1 = draw, 0 = skip).
// The bits are stored in serpentine order: even rows left-to-right, odd rows right-to-left.
const uint8_t customImage[BITMAP_SIZE] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x0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80, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xF0, 0xFF, 0xFF, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x00, 0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0x3F, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//////////////////////////////////////////////////
// S E T U P //
//////////////////////////////////////////////////
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(INIT_MSG);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
button1.setDebounceTime(50);
servo.attach(SERVO_PIN);
servo.write(angle);
plot(false);
yStepper.setSpeed(10);
xStepper.setSpeed(8);
penUp();
homeYAxis();
ypos = 0;
xpos = 0;
releaseMotors();
lcd.clear();
}
////////////////////////////////////////////////
// L O O P //
////////////////////////////////////////////////
void loop() {
button1.loop();
button1State = button1.getState();
joystickX = analogRead(joystickXPin);
joystickY = analogRead(joystickYPin);
joyUp = joystickY < (512 - joystickButtonThreshold);
joyDown = joystickY > (512 + joystickButtonThreshold);
joyLeft = joystickX < (512 - joystickButtonThreshold);
joyRight = joystickX > (512 + joystickButtonThreshold);
switch (currentState) {
case plotCustomImg:
{
if (prevState != plotCustomImg){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(MODE_NAME);
lcd.setCursor(0,1);
lcd.print(PLOTTING);
cursorPosition = 5;
prevState = plotCustomImg;
}
lcd.setCursor(cursorPosition, 1);
plotcustomImage();
homeYAxis();
releaseMotors();
lcd.clear();
currentState = MainMenu;
prevState = plotCustomImg;
}
break;
case MainMenu:
{
if (prevState != MainMenu){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(MODE_NAME);
lcd.setCursor(0,1);
lcd.print(" START ");
cursorPosition = 5;
prevState = MainMenu;
}
lcd.setCursor(cursorPosition, 1);
lcd.blink();
if (button1.isPressed()) {
lcd.clear();
lcd.noBlink();
currentState = plotCustomImg;
prevState = MainMenu;
}
}
break;
}
}
void line(int newx, int newy, bool drawing) {
if (drawing < 2) {
plot(drawing);
}
int i;
long over = 0;
long dx = newx - xpos;
long dy = newy - ypos;
int dirx = dx > 0 ? -1 : 1;
int diry = dy > 0 ? 1 : -1;
dx = abs(dx);
dy = abs(dy);
if (dx > dy) {
over = dx / 2;
for (i = 0; i < dx; i++) {
xStepper.step(dirx);
over += dy;
if (over >= dx) {
over -= dx;
yStepper.step(diry);
}
}
} else {
over = dy / 2;
for (i = 0; i < dy; i++) {
yStepper.step(diry);
over += dx;
if (over >= dy) {
over -= dy;
xStepper.step(dirx);
}
}
}
xpos = newx;
ypos = newy;
}
void plot(boolean penOnPaper) {
if (penOnPaper)
angle = 80;
else
angle = 25;
servo.write(angle);
if (penOnPaper != pPenOnPaper) delay(50);
pPenOnPaper = penOnPaper;
}
void penUp(){
servo.write(25);
}
void penDown(){
servo.write(80);
}
void releaseMotors() {
for (int i = 0; i < 4; i++) {
digitalWrite(xPins[i], 0);
digitalWrite(yPins[i], 0);
}
plot(false);
}
void homeYAxis(){
yStepper.step(-3000);
}
void plotcustomImage(){
Serial.println("CUSTOM IMG TIME");
// Iterate over each row of the grid
for (int row = 0; row < GRID_SIZE; row++){
if (row % 2 == 0) { // even row: left-to-right
for (int col = 0; col < GRID_SIZE; col++){
int index = row * GRID_SIZE + col;
int byteIndex = index / 8;
int bitIndex = index % 8;
uint8_t byteVal = customImage[byteIndex];
if ((byteVal >> bitIndex) & 1) {
int x_end = row * customImagescale;
int y_end = col * customImagescale * 2.65;
Serial.print("X_goal: ");
Serial.print(x_end);
Serial.print(" Y_goal: ");
Serial.print(y_end);
Serial.println(" Plotting dot");
line(x_end, y_end, 0);
penDown();
delay(80);
penUp();
delay(80);
}
}
} else { // odd row: right-to-left
for (int col = GRID_SIZE - 1; col >= 0; col--){ // columns backwards saves some movement
int index = (row+1) * GRID_SIZE - col - 1; // serpentine order, look at the bits backwards too
int byteIndex = index / 8;
int bitIndex = index % 8;
uint8_t byteVal = customImage[byteIndex];
if ((byteVal >> bitIndex) & 1) {
int x_end = row * customImagescale;
int y_end = col * customImagescale * 2.65;
Serial.print("X_goal: ");
Serial.print(x_end);
Serial.print(" Y_goal: ");
Serial.print(y_end);
Serial.println(" Plotting dot");
line(x_end, y_end, 0);
penDown();
delay(80);
penUp();
delay(80);
}
}
}
}
releaseMotors();
}