@@ -6,10 +6,9 @@ namespace Eloquent {
6
6
/* *
7
7
* Output a robust value when a given number of votes agree
8
8
* @tparam votes
9
- * @tparam quorum
10
9
*/
11
- template <uint8_t votes, uint8_t quorum >
12
- class MajorityVoting {
10
+ template <uint8_t votes>
11
+ class Voting {
13
12
public:
14
13
15
14
/* *
@@ -32,14 +31,16 @@ namespace Eloquent {
32
31
33
32
/* *
34
33
* Test if quorum was achieved
34
+ * @param quorum how many votes should agree
35
+ * @param row how many votes "in row" should agree
35
36
*/
36
- bool hasMajority ( ) {
37
+ bool agree ( uint8_t quorum, uint8_t row = 0 ) {
37
38
_decision = 255 ;
38
39
39
40
if (_idx < votes)
40
41
return false ;
41
42
42
- bool majority = false ;
43
+ bool agree = false ;
43
44
44
45
for (uint8_t i = 0 ; i < votes - quorum; i++) {
45
46
uint8_t vote = _votes[i];
@@ -51,12 +52,32 @@ namespace Eloquent {
51
52
52
53
if (count >= quorum) {
53
54
_decision = vote;
54
- majority = true ;
55
+ agree = true ;
55
56
break ;
56
57
}
57
58
}
58
59
59
- return majority;
60
+ // check votes "in row"
61
+ if (row > 0 ) {
62
+ for (uint8_t i = votes - row; i < votes; i++) {
63
+ if (_votes[i] != _decision) {
64
+ _decision = 255 ;
65
+ agree = false ;
66
+ break ;
67
+ }
68
+ }
69
+ }
70
+
71
+ return agree;
72
+ }
73
+
74
+ /* *
75
+ * Test if quorum was achieved
76
+ * @param quorum how many votes should agree (in percent)
77
+ * @param row how many votes "in row" should agree
78
+ */
79
+ bool agree (float quorum, uint8_t row = 0 ) {
80
+ return agree ((uint8_t ) round (votes * quorum), row);
60
81
}
61
82
62
83
/* *
0 commit comments