-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProtonPack.h
129 lines (114 loc) · 3.42 KB
/
ProtonPack.h
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
#ifndef ProtonPack_h
#define ProtonPack_h
#include <stdint.h>
#include "Arduino.h"
int pp_decrement(int current, int max_value);
int pp_mod_increment(int current, int max_value);
template <typename T,unsigned S>
inline unsigned arraysize(const T (&v)[S]) {
return S;
}
template<typename Data>
class Vector {
size_t d_size; // Stores no. of actually stored objects
size_t d_capacity; // Stores allocated capacity
Data *d_data; // Stores data
public:
Vector() : d_size(0), d_capacity(0), d_data(0) {
}; // Default constructor
Vector(Vector const &other) : d_size(other.d_size), d_capacity(other.d_capacity), d_data(0) {
d_data = (Data *) malloc(d_capacity*sizeof(Data));
memcpy(d_data, other.d_data, d_size*sizeof(Data));
}; // Copy constuctor
~Vector() {
free(d_data);
}; // Destructor
Vector &operator=(Vector const &other) {
free(d_data);
d_size = other.d_size;
d_capacity = other.d_capacity;
d_data = (Data *) malloc(d_capacity*sizeof(Data));
memcpy(d_data, other.d_data, d_size*sizeof(Data));
return *this;
}; // Needed for memory management
void push_back(Data const &x) {
if (d_capacity == d_size) resize();
d_data[d_size++] = x;
}; // Adds new value. If needed, allocates more space
size_t size() const {
return d_size;
}; // Size getter
Data const &operator[](size_t idx) const {
return d_data[idx];
}; // Const getter
Data &operator[](size_t idx) {
return d_data[idx];
}; // Changeable getter
private:
void resize() {
d_capacity = d_capacity ? d_capacity*2 : 1;
Data *newdata = (Data *)malloc(d_capacity*sizeof(Data));
memcpy(newdata, d_data, d_size * sizeof(Data));
free(d_data);
d_data = newdata;
};// Allocates double the old space
};
typedef struct _protonpack {
int last_updated;
unsigned long next_call_time;
unsigned long now;
unsigned long started_at;
boolean is_on;
boolean is_initializing;
boolean is_firing;
} Pack;
class ProtonPack;
class PackComponent {
public:
PackComponent(int offset, int num_leds);
void callAgainIn(int num_millis);
void setPack(ProtonPack *pack);
virtual bool isReadyToUpdate();
virtual void onUpdate(Pack pack);
virtual void onActivateButtonPress(Pack pack);
virtual void onFiringStart(Pack pack);
virtual void onFiringStop(Pack pack);
virtual void onPackStartUp(Pack pack);
virtual void onPackShutDown(Pack pack);
virtual void onPackInitStart(Pack pack);
virtual void onPackInitComplete(Pack pack);
virtual void reset(Pack pack);
protected:
ProtonPack *_proton_pack;
void setLed(int ledNumber, int value);
unsigned short _num_leds;
private:
int _offset;
unsigned long _last_updated;
unsigned long _started_at;
unsigned long _next_call_time;
};
class PackListener: public PackComponent {
public:
PackListener();
bool isReadyToUpdate();
};
class ProtonPack {
public:
ProtonPack(int power_switch_id,
int activate_switch_id);
void update();
void initialize();
void reset();
void addComponent(PackComponent *component);
protected:
Pack _pack;
Vector<PackComponent*> _components;
int _init_millis;
int _power_switch_id;
int _activate_switch_id;
int _power_button_state;
int _activate_button_state;
int _num_leds;
};
#endif