forked from dicarlo236/cube-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbed.cpp
319 lines (279 loc) · 7.48 KB
/
mbed.cpp
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
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <unistd.h>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include "mbed.h"
char soln_moves[] = "UDLRFB";
uint8_t board_ids[] = {0,3,4,1,2,5};
sequence_t solve_sequence;
move_t moves[30];
// 0, ... ,5, UDLRFB
// 6, 2
// 7, '
// 8, <space>
// -1 other
int get_char_type(char a)
{
for(int i = 0; i < 6; i++)
if(soln_moves[i] == a)
return i;
if(a == '2')
return 6;
if(a == 39) // '
return 7;
if(a == ' ')
return 8;
printf("[ERROR] unrecognized character in solution %c (decimal %d)\n",a,a);
}
void string_to_sequence(char* string)
{
int move = 0;
for(int i = 0; i < strlen(string); i++)
{
int type = get_char_type(string[i]);
// direction
if(i%3 == 0)
{
if(type>5 || type <0)
{
printf("[ERROR] Could not find direction for move %d! Got %c instead.\n",move,string[i]);
return;
}
moves[move].face = (face_t)type;
}
if(i%3 == 1)
{
if(type == 6)
moves[move].n_turns = 2;
else if(type == 7)
moves[move].n_turns = -1;
else if(type == 8)
moves[move].n_turns = 1;
else
{
printf("[ERROR] Not a valid move modifier for move %d: %c\n",move,string[i]);
return;
}
}
if(i%3 == 2)
{
if(type != 8)
{
printf("[ERROR] Got invalid space character for move %d: %c\n",move,string[i]);
return;
}
move++;
}
}
solve_sequence.n_moves = move;
solve_sequence.moves = moves;
}
sequence_t* get_sequence()
{
return &solve_sequence;
}
void print_sequence()
{
int n_moves = solve_sequence.n_moves;
printf("Sequence with %d moves\n",n_moves);
for(int i = 0; i < n_moves; i++)
printf("\tMove face %c %d turns.\n",soln_moves[solve_sequence.moves[i].face],solve_sequence.moves[i].n_turns);
}
int sequence_to_serial(sequence_t* seq, uint8_t* buffer, int n_bytes)
{
printf("[sequence to serial] sequence with %d moves.\n",seq->n_moves);
if(n_bytes < seq->n_moves)
{
printf("SERIAL BUFFER TOO SMALL!\n");
return 1;
}
// i = 0
buffer[0] = K_START;
// i = 1
buffer[1] = K_MOVES + (seq->n_moves & 0x3f);
// i = 2 .. (n_moves+2)
for(int i = 0; i < seq->n_moves; i++)
buffer[i+2] = (0x7 & (seq->moves[i].n_turns)) + (0x38 & ((uint8_t)board_ids[seq->moves[i].face]<<3));
return seq->n_moves + 2;
}
int serial_to_sequence(sequence_t* seq, uint8_t* buffer, int n_bytes)
{
int start_index = -1;
int n_moves = -1;
for(int i = 0; i < n_bytes; i++)
{
if(buffer[i] == K_START)
{
start_index = i+2;
break;
}
}
if(start_index == -1)
{
printf("FAILED TO FIND START OF MOVE!\n");
return 1;
}
if((buffer[start_index-1] & 0xC0) != K_MOVES)
{
printf("COULD NOT FIND NUMBER OF MOVES!\n");
return 1;
}
n_moves = buffer[start_index-1]&0x3f;
seq->n_moves = n_moves;
if(n_moves < 15)
{
printf("NUMBER OF MOVES LESS THAN 15.\n");
return 1;
}
for(int i = 0; i < n_moves; i++)
{
uint8_t ser_move = buffer[start_index+i];
int8_t face = (face_t)((ser_move>>3) & 0x07);
int8_t n_turns = (int8_t)((ser_move) & 0x07);
// 2's complement negation
if(n_turns > 2) n_turns |= 0xf8;
if(face < 0 || face > 5)
{
printf("GOT INVALID FACE.\n");
return 1;
}
if(!(n_turns == -2 || n_turns == -1 || n_turns == 1 || n_turns == 2))
{
printf("GOT INVALID NUMBER OF TURNS: %d\n",n_turns);
printf("Serial data: " BYTE_TO_BINARY_PATTERN "\n",BYTE_TO_BINARY(ser_move));
return 1;
}
seq->moves[i].n_turns = n_turns;
seq->moves[i].face = (face_t)face;
}
return 0;
}
int states[6] = {0,0,0,0,0,0};
int move_counts[6] = {0,0,0,0,0,0};
int wait_counter[6] = {0,0,0,0,0,0};
//states:
// 0 - start command
// 1 - delay for command
// 2 - wait for and
// 3 - delay for and
// 4 - done.
void reset_mbed()
{
for(int i = 0; i < 6; i++)
{
states[i] = 0;
move_counts[i] = 0;
wait_counter[i] = 0;
}
}
void* run_sequence_2(void* command)
{
mbed_info_t* cmd = (mbed_info_t*)command;
int n_moves = cmd->seq->n_moves;
int n_turns = 0;
if(states[cmd->face] == 0)
{
cmd->set_and(0,cmd->face); // and off
// check if done
if(move_counts[cmd->face] > n_moves)
{
cmd->set_and(1,cmd->face);
states[cmd->face] = 4;
return NULL;
}
// check how many turns needed
if(cmd->face == cmd->seq->moves[move_counts[cmd->face]].face)
{
n_turns = cmd->seq->moves[move_counts[cmd->face]].n_turns;
}
else n_turns = 0;
// increment move counter
move_counts[cmd->face]++;
// rotate if needed
if(n_turns)
cmd->rotate(n_turns,cmd->face);
// wait...
states[cmd->face] = 1;
wait_counter[cmd->face] = 0;
return NULL;
}
else if(states[cmd->face] == 1)
{
//printf("b%d 1\n",cmd->face);
wait_counter[cmd->face]++;
if(wait_counter[cmd->face] < 500) return NULL;
states[cmd->face] = 2;
cmd->set_and(1,cmd->face);
return NULL;
}
else if(states[cmd->face] == 2)
{
//printf("b%d 2\n",cmd->face);
//printf("b 1 %d\n",cmd->face);
if(cmd->get_and())
{
wait_counter[cmd->face] = 0;
states[cmd->face] = 3;
return NULL;
}
}
else if(states[cmd->face] == 3)
{
//printf("b%d 3\n",cmd->face);
if(wait_counter[cmd->face]++ < 100) return NULL;
states[cmd->face] = 0;
cmd->set_and(0,cmd->face);
return NULL;
}
else if(states[cmd->face] == 4)
;
return NULL;
//printf("b%d 4\n",cmd->face);
}
void print_sequence_2(sequence_t* seq, uint8_t* buffer)
{
printf("Sequence of %d moves:\n",seq->n_moves);
for(int i = 0; i < seq->n_moves; i++)
{
printf(" Move %d: face %d, %d turns.\n",i,(int)seq->moves[i].face,seq->moves[i].n_turns);
if(buffer != NULL)
printf(" Buffer: " BYTE_TO_BINARY_PATTERN "\n",BYTE_TO_BINARY(buffer[2+i]));
}
uint8_t buffer_2[300];
sequence_to_serial(seq,buffer_2,seq->n_moves);
for(int i = 0; i < seq->n_moves; i++)
printf("0x%hhx, ",buffer_2[i]);
printf("\n");
}
void make_random_sequence(sequence_t* seq)
{
int n_moves = random(15,18);
seq->n_moves = n_moves;
for(int i = 0; i < n_moves; i++)
{
seq->moves[i].face = (face_t)random(0,5);
int n_turns = random(-2,1);
if(n_turns==0)n_turns=2;
seq->moves[i].n_turns = n_turns;
printf("random move %d: %d turns, face %d\n",i,n_turns,seq->moves[i].face);
}
}
void reset_sequence(sequence_t* seq)
{
seq->n_moves = 0;
for(int i = 0; i < 30; i ++)
{
seq->moves[i].face = (face_t)0;
seq->moves[i].n_turns = 0;
}
}
int random(int min, int max)
{
int rn = (rand() % (max + 1 - min)) + min;
if(rn > max || rn < min)
printf("RANDOM ERROR\n");
return rn;
}