-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBilgePumpController.ino
142 lines (113 loc) · 2.56 KB
/
BilgePumpController.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
142
#include <SPI.h>
#include <Wire.h>
#include <RtcDS3231.h>
#include <avr/pgmspace.h>
#include <TimerOne.h>
#include "BilgePumpController.h"
RtcDS3231 Rtc;
Data data;
volatile bool doRefresh = false;
void refreshDisplay()
{
doRefresh = true;
}
void setup() {
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Timer1.initialize(100000); // 0.10 seconds
Timer1.attachInterrupt(refreshDisplay);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
if (!Rtc.IsDateTimeValid()) {
Rtc.SetDateTime(compiled);
}
if(!Rtc.GetIsRunning()) {
Rtc.SetIsRunning(true);
}
Rtc.Enable32kHzPin(false);
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
pump1 = new Pump(0);
pump2 = new Pump(1);
pumpPin1 = new Pin(pumpSensor1Pin, debounceDelay, handlePump1);
pumpPin2 = new Pin(pumpSensor2Pin, debounceDelay, handlePump2);
buttonPin1 = new Pin(button1Pin, debounceDelay, handleButton1);
buttonPin2 = new Pin(button2Pin, debounceDelay, handleButton2);
pc = new PageController(&Rtc, &getData);
pc->drawCurrentPage();
}
void loop() {
pumpPin1->Read();
pumpPin2->Read();
buttonPin1->Read();
buttonPin2->Read();
if(doRefresh && !isResetting)
{
noInterrupts();
doRefresh = false;
interrupts();
pc->drawCurrentPage();
}
if(isBuzzing) {
digitalWrite(buzzerPin, HIGH);
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
}
if(isResetting) {
int elapsed = (millis() - startResetCounter) / 1000;
int remaining = resetDelay - elapsed;
pc->drawResetWindow(remaining);
if(remaining == 0) {
doReset();
}
}
}
Data *getData() {
data.pump1 = pump1;
data.pump2 = pump2;
data.isBuzzing = isBuzzing;
return &data;
}
void doReset() {
isResetting = false;
pump1->ResetStatistics();
pump2->ResetStatistics();
delay(500);
}
void handleButton1(Event *e) {
if(e->type == UP) {
if(pc->currentPage == 1) {
isBuzzing = false;
} else {
isResetting = false;
}
} else if(e->type == DOWN && pc->currentPage != 1) {
startResetCounter = millis();
isResetting = true;
}
}
void handleButton2(Event *e) {
if(e->type == DOWN) {
pc->drawNextPage();
}
}
void handlePump1(Event *e) {
if(e->type == DOWN) {
pump1->OnStart(e);
isBuzzing = true;
} else {
pump1->OnStop(e);
isBuzzing = false;
}
}
void handlePump2(Event *e) {
if(e->type == DOWN) {
pump2->OnStart(e);
isBuzzing = true;
} else {
pump2->OnStop(e);
isBuzzing = false;
}
}