Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_dice.c
1 #include <bc_dice.h>
2 
3 #define _BC_DICE_THRESHOLD 0.4f
4 
5 const int8_t _bc_dice_vectors[][3] =
6 {
7  [BC_DICE_FACE_UNKNOWN] = { 0, 0, 0 },
8 
9  [BC_DICE_FACE_1] = { 0, 0, 1 },
10  [BC_DICE_FACE_2] = { 1, 0, 0 },
11  [BC_DICE_FACE_3] = { 0, 1, 0 },
12  [BC_DICE_FACE_4] = { 0, -1, 0 },
13  [BC_DICE_FACE_5] = { -1, 0, 0 },
14  [BC_DICE_FACE_6] = { 0, 0, -1 }
15 };
16 
18 {
19  memset(self, 0, sizeof(*self));
20 
21  self->_face = start;
22 
23  self->_threshold = _BC_DICE_THRESHOLD;
24 }
25 
26 void bc_dice_set_threshold(bc_dice_t *self, float threshold)
27 {
28  self->_threshold = threshold;
29 }
30 
31 void bc_dice_feed_vectors(bc_dice_t *self, float x_axis, float y_axis, float z_axis)
32 {
33  int8_t vector_x = _bc_dice_vectors[self->_face][0];
34  int8_t vector_y = _bc_dice_vectors[self->_face][1];
35  int8_t vector_z = _bc_dice_vectors[self->_face][2];
36 
37  bool update = false;
38 
39  if ((vector_x == 0 && (x_axis < -self->_threshold || x_axis > self->_threshold)) ||
40  (vector_x == 1 && (x_axis < 1.f - self->_threshold)) ||
41  (vector_x == -1 && (x_axis > -1.f + self->_threshold)))
42  {
43  update = true;
44  }
45 
46  if ((vector_y == 0 && (y_axis < -self->_threshold || y_axis > self->_threshold)) ||
47  (vector_y == 1 && (y_axis < 1.f - self->_threshold)) ||
48  (vector_y == -1 && (y_axis > -1.f + self->_threshold)))
49  {
50  update = true;
51  }
52 
53  if ((vector_z == 0 && (z_axis < -self->_threshold || z_axis > self->_threshold)) ||
54  (vector_z == 1 && (z_axis < 1.f - self->_threshold)) ||
55  (vector_z == -1 && (z_axis > -1.f + self->_threshold)))
56  {
57  update = true;
58  }
59 
60  if (update)
61  {
62  for (size_t i = 1; i <= 6; i++)
63  {
64  float delta_x = _bc_dice_vectors[i][0] - x_axis;
65  float delta_y = _bc_dice_vectors[i][1] - y_axis;
66  float delta_z = _bc_dice_vectors[i][2] - z_axis;
67 
68  if (delta_x < 0.f) { delta_x = -delta_x; }
69  if (delta_y < 0.f) { delta_y = -delta_y; }
70  if (delta_z < 0.f) { delta_z = -delta_z; }
71 
72  if (delta_x < 1.f - self->_threshold &&
73  delta_y < 1.f - self->_threshold &&
74  delta_z < 1.f - self->_threshold)
75  {
76  self->_face = i;
77  }
78  }
79  }
80 }
81 
83 {
84  return self->_face;
85 }
Unknown dice face.
Definition: bc_dice.h:15
struct bc_dice_t bc_dice_t
Dice instance.
Definition: bc_dice.h:39
Dice face 5.
Definition: bc_dice.h:30
bc_dice_face_t
Dice faces.
Definition: bc_dice.h:12
bc_dice_face_t bc_dice_get_face(bc_dice_t *self)
Get calculated dice face.
Definition: bc_dice.c:82
Dice face 3.
Definition: bc_dice.h:24
Dice face 1.
Definition: bc_dice.h:18
void bc_dice_init(bc_dice_t *self, bc_dice_face_t start)
Initialize dice.
Definition: bc_dice.c:17
Dice face 6.
Definition: bc_dice.h:33
Dice face 2.
Definition: bc_dice.h:21
void bc_dice_feed_vectors(bc_dice_t *self, float x_axis, float y_axis, float z_axis)
Feed dice with X/Y/Z axis vectors.
Definition: bc_dice.c:31
Dice face 4.
Definition: bc_dice.h:27
void bc_dice_set_threshold(bc_dice_t *self, float threshold)
Set threshold.
Definition: bc_dice.c:26