-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtajima.c
202 lines (160 loc) · 4.53 KB
/
tajima.c
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
/*
tajima.c - plugin for parsing Tajima DST format file read from SD card.
Part of grblHAL
Copyright (c) 2023 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "embroidery.h"
#if EMBROIDERY_ENABLE
typedef struct {
uint8_t b0;
uint8_t b1;
uint8_t b2;
} stitch_data_t;
static int32_t first_color = -1;
static const char *get_thread_color (embroidery_thread_color_t color)
{
return "None";
}
static inline int16_t get_x (uint8_t b2, uint8_t b1, uint8_t b0)
{
int16_t x = 0;
if(b2 & bit(2))
x += 81;
if(b2 & bit(3))
x -= 81;
if(b1 & bit(2))
x += 27;
if(b1 & bit(3))
x -= 27;
if(b0 & bit(2))
x += 9;
if(b0 & bit(3))
x -= 9;
if(b1 & bit(0))
x += 3;
if(b1 & bit(1))
x -= 3;
if(b0 & bit(0))
x += 1;
if(b0 & bit(1))
x -= 1;
return x;
}
static inline int16_t get_y (uint8_t b2, uint8_t b1, uint8_t b0)
{
int16_t y = 0;
if(b2 & bit(5))
y += 81;
if(b2 & bit(4))
y -= 81;
if(b1 & bit(5))
y += 27;
if(b1 & bit(4))
y -= 27;
if(b0 & bit(5))
y += 9;
if(b0 & bit(4))
y -= 9;
if(b1 & bit(7))
y += 3;
if(b1 & bit(6))
y -= 3;
if(b0 & bit(7))
y += 1;
if(b0 & bit(6))
y -= 1;
return y;
}
static bool get_stitch (stitch_t *stitch, vfs_file_t *file)
{
bool sm = false;
int16_t dx = 0, dy = 0;
stitch_data_t sd;
if(first_color != -1) {
stitch->type = Stitch_Stop;
stitch->color = (embroidery_thread_color_t)first_color;
first_color = -1;
return true;
}
if(vfs_read(&sd, sizeof(stitch_data_t), 1, file) == sizeof(stitch_data_t)) {
if((sd.b2 & 0b11110011) == 0b11110011)
return false;
dx = get_x(sd.b2, sd.b1, sd.b0);
dy = get_y(sd.b2, sd.b1, sd.b0);
if((sd.b2 & 0b11000011) == 0b11000011)
stitch->type = Stitch_Stop;
else if((sd.b2 & 0b01000011) == 0b01000011)
sm = !sm;
else if((sd.b2 & 0b10000011) == 0b10000011)
stitch->type = sm ? Stitch_SequinEject : Stitch_Jump;
else
stitch->type = Stitch_Normal;
stitch->target.x = (float)dx / 10.0f;
stitch->target.y = (float)dy / 10.0f;
} else
return false;
return true;
}
static bool read_meta (char *buf, vfs_file_t *file)
{
char c;
*buf = '\0';
while(vfs_read(&c, 1, 1, file) == 1) {
if(c == ASCII_EOF)
return false;
if(c == ASCII_CR || c == ASCII_LF) {
*buf = '\0';
break;
}
*buf++ = c;
}
return true;
}
bool tajima_open_file (vfs_file_t *file, embroidery_t *api)
{
bool ok = false;
static char buf[21];
if(vfs_read(buf, 3, 1, file) == 3 && !strncmp(buf, "LA:", 3)) {
char meta[20];
float value;
uint_fast8_t idx;
read_meta(buf, file); // Name
while(read_meta(meta, file)) {
idx = 3;
strcaps(meta);
if(read_float(meta, &idx, &value)) {
if(!strncmp(meta, "ST:", 3))
api->stitches = value;
else if(!strncmp(meta, "CO:", 3))
api->color_changes = value;
else if(!strncmp(meta, "+X:", 3))
api->max.x = value / 10.0f;
else if(!strncmp(meta, "-X:", 3))
api->min.x = -value / 10.0f;
else if(!strncmp(meta, "+Y:", 3))
api->max.y = value / 10.0f;
else if(!strncmp(meta, "-Y:", 3))
api->min.y = -value / 10.0f;
}
}
api->name = buf;
api->get_stitch = get_stitch;
api->get_thread_color = get_thread_color;
// first_color = pec_1.palette_index[0];
vfs_seek(file, 512);
ok = true;
} else
vfs_seek(file, 0);
return ok;
}
#endif // EMBROIDERY_ENABLE