Skip to content

Commit b98af12

Browse files
authored
Merge pull request #36 from saxbophone/josh/35-auto-orient
Add a simple auto-orientation feature to the spiral design.
2 parents e6598ba + 5d542aa commit b98af12

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

saxbospiral.png

19 Bytes
Loading

saxbospiral/initialise.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ change_direction(direction_t current, rotation_t turn) {
2424
*/
2525
spiral_t
2626
init_spiral(buffer_t buffer) {
27-
// number of lines is number of bits of the data
28-
size_t line_count = buffer.size * 8;
27+
// number of lines is number of bits of the data, + 1 for the first UP line
28+
size_t line_count = (buffer.size * 8) + 1;
2929
// create spiral_t struct
3030
spiral_t result = {
3131
.size = line_count,
3232
.collides = -1,
3333
// allocate enough memory for a line_t struct for each bit
3434
.lines = calloc(sizeof(line_t), line_count),
3535
};
36-
/*
37-
* first direction is a one-off, pre-prepare the stored direction for this
38-
* if first bit is 0, then first direction is UP, else if 1 then it's RIGHT
39-
*/
40-
direction_t current = ((buffer.bytes[0] & 0b10000000) == 0) ? LEFT : DOWN;
36+
// First line is always an UP line - this is for orientation purposes
37+
direction_t current = UP;
38+
result.lines[0].direction = current;
39+
result.lines[0].length = 0;
4140
/*
4241
* now, iterate over all the bits in the data and convert to directions
4342
* that make the spiral pattern, storing these directions in the result lines
@@ -48,7 +47,7 @@ init_spiral(buffer_t buffer) {
4847
// bit level loop
4948
uint8_t e = 7 - b; // which power of two to use with bit mask
5049
uint8_t bit = (buffer.bytes[s] & (1 << e)) >> e; // the currently accessed bit
51-
size_t index = (s * 8) + b; // line index
50+
size_t index = (s * 8) + b + 1; // line index
5251
rotation_t rotation; // the rotation we're going to make
5352
// set rotation direction based on the current bit
5453
rotation = (bit == 0) ? CLOCKWISE : ANTI_CLOCKWISE;

tests.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,31 @@ test_init_spiral() {
3838
buffer.bytes = malloc(buffer.size);
3939
buffer.bytes = (uint8_t[2]){ 0b01101101, 0b11000111, };
4040
// build expected output struct
41-
spiral_t expected = { .size = 16, };
42-
expected.lines = calloc(sizeof(line_t), 16);
43-
direction_t directions[16] = {
44-
UP, LEFT, DOWN, LEFT, DOWN, RIGHT, DOWN, RIGHT,
45-
UP, LEFT, UP, RIGHT, DOWN, RIGHT, UP, LEFT,
41+
spiral_t expected = { .size = 17, };
42+
expected.lines = calloc(sizeof(line_t), 17);
43+
direction_t directions[17] = {
44+
UP, RIGHT, UP, LEFT, UP, LEFT, DOWN, LEFT,
45+
DOWN, RIGHT, UP, RIGHT, DOWN, LEFT, DOWN, RIGHT, UP
4646
};
47-
for(uint8_t i = 0; i < 16; i++) {
47+
for(uint8_t i = 0; i < 17; i++) {
4848
expected.lines[i].direction = directions[i];
4949
}
5050

5151
// call init_spiral with buffer and store result
5252
spiral_t output = init_spiral(buffer);
5353

54+
if(output.size != expected.size) {
55+
result = false;
56+
}
5457
// compare with expected struct
55-
for(uint8_t i = 0; i < 16; i++) {
58+
for(uint8_t i = 0; i < 17; i++) {
5659
if(output.lines[i].direction != expected.lines[i].direction) {
60+
printf(
61+
"%u: %i != %i\n",
62+
i,
63+
output.lines[i].direction,
64+
expected.lines[i].direction
65+
);
5766
result = false;
5867
}
5968
}

0 commit comments

Comments
 (0)