Firmware SDK
twr_dice.c
1 #include <twr_dice.h>
2 
3 #define _TWR_DICE_THRESHOLD 0.4f
4 
5 const int8_t _twr_dice_vectors[][3] =
6 {
7  [TWR_DICE_FACE_UNKNOWN] = { 0, 0, 0 },
8 
9  [TWR_DICE_FACE_1] = { 0, 0, 1 },
10  [TWR_DICE_FACE_2] = { 1, 0, 0 },
11  [TWR_DICE_FACE_3] = { 0, 1, 0 },
12  [TWR_DICE_FACE_4] = { 0, -1, 0 },
13  [TWR_DICE_FACE_5] = { -1, 0, 0 },
14  [TWR_DICE_FACE_6] = { 0, 0, -1 }
15 };
16 
18 {
19  memset(self, 0, sizeof(*self));
20 
21  self->_face = start;
22 
23  self->_threshold = _TWR_DICE_THRESHOLD;
24 }
25 
26 void twr_dice_set_threshold(twr_dice_t *self, float threshold)
27 {
28  self->_threshold = threshold;
29 }
30 
31 void twr_dice_feed_vectors(twr_dice_t *self, float x_axis, float y_axis, float z_axis)
32 {
33  int8_t vector_x = _twr_dice_vectors[self->_face][0];
34  int8_t vector_y = _twr_dice_vectors[self->_face][1];
35  int8_t vector_z = _twr_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 = _twr_dice_vectors[i][0] - x_axis;
65  float delta_y = _twr_dice_vectors[i][1] - y_axis;
66  float delta_z = _twr_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 }
void twr_dice_set_threshold(twr_dice_t *self, float threshold)
Set threshold.
Definition: twr_dice.c:26
twr_dice_face_t
Dice faces.
Definition: twr_dice.h:13
void twr_dice_init(twr_dice_t *self, twr_dice_face_t start)
Initialize dice.
Definition: twr_dice.c:17
void twr_dice_feed_vectors(twr_dice_t *self, float x_axis, float y_axis, float z_axis)
Feed dice with X/Y/Z axis vectors.
Definition: twr_dice.c:31
twr_dice_face_t twr_dice_get_face(twr_dice_t *self)
Get calculated dice face.
Definition: twr_dice.c:82
struct twr_dice_t twr_dice_t
Dice instance.
Definition: twr_dice.h:39
@ TWR_DICE_FACE_UNKNOWN
Unknown dice face.
Definition: twr_dice.h:15
@ TWR_DICE_FACE_6
Dice face 6.
Definition: twr_dice.h:33
@ TWR_DICE_FACE_1
Dice face 1.
Definition: twr_dice.h:18
@ TWR_DICE_FACE_5
Dice face 5.
Definition: twr_dice.h:30
@ TWR_DICE_FACE_4
Dice face 4.
Definition: twr_dice.h:27
@ TWR_DICE_FACE_3
Dice face 3.
Definition: twr_dice.h:24
@ TWR_DICE_FACE_2
Dice face 2.
Definition: twr_dice.h:21