-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdva_array.ino
141 lines (122 loc) · 2.82 KB
/
dva_array.ino
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
/* dva_array file
* Maintains a array of
* the recieved DVA
* in rx mode.
*--------------------------*/
#include "DVA.h"
//Private vars
Dva dva_array[ARRAY_MAX_LEN];
int current_target;
int current_len;
/* array_seutp function
* Used to setup the array struct
*---------------------------------
* returns: void
* ------------------------------*/
void array_setup(){
memset(dva_array, 0, ARRAY_MAX_LEN * sizeof(Dva));
current_target = 0;
current_len = 0;
}
/* array_push function
* Adds or updates a dva on
* the dva_array
*---------------------------------
* @dva_object: the dva we want
* to insert or update
* --------------------------------
* returns: boolean:
* true->updated/inserted
* false->not inserted/found
* && max_len reached
* ------------------------------*/
boolean array_push(Dva dva_object){
boolean pushed = false;
boolean updated = false;
int i = 0;
if(current_len != 0){
while(i < current_len && i < ARRAY_MAX_LEN && !pushed){
if((dva_object.id).equals(dva_array[i].id)){
dva_array[i] = dva_object;
updated = true;
}
i++;
}
if(!pushed && i < ARRAY_MAX_LEN){
current_len += 1;
dva_array[i] = dva_object;
pushed = true;
}
}
else{
current_len = 1;
current_target = 0;
dva_array[0] = dva_object;
pushed = true;
}
return pushed || updated;
}
/* array_pop function
* Deletes a certain DVA from
* the array
*---------------------------------
* returns: boolean:
* true->deleted
* false->out of range/!valid
* ------------------------------*/
boolean array_pop(){
if(current_target > current_len || current_target > ARRAY_MAX_LEN || current_len < 1){
return false;
}
else{
dva_array[current_target] = dva_array[current_len - 1];
current_len -= 1;
}
return true;
}
/* array_read function
* Gives the information of
* the DVA we are actually
* targeting to
*---------------------------------
* returns: struct dva: the
* object we should read
* ------------------------------*/
Dva array_read(){
Dva dva_object;
if(current_len > 0){
dva_object = dva_array[current_target];
}
return dva_object;
}
/* array_switch_target function
* Switch between targets
*---------------------------------
* returns: boolean:
* true->switched
* false->array empty
* ------------------------------*/
boolean array_switch_target(){
boolean switched = false;
if(current_len > 0 && current_target < current_len){
if((current_target + 1) == current_len){
current_target = 0;
}
else{
current_target += 1;
}
switched = true;
}
return switched;
}
/* array_is_empty function
* Let the program know if
* the array is empty or not
*---------------------------------
* returns: boolean:
* true->array empty
* false->array !empty
* ------------------------------*/
boolean array_is_empty(){
return current_len == 0;
}