-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interrupt.h
144 lines (114 loc) · 3.88 KB
/
Interrupt.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Interrupt.h -- interrupts must be static on the ATmega328p... ABANDON ALL HOPE YEE WHO ENTER HERE...
* this saves us from 2 or 3 static objects with a horrible tendency to be inserted in RAM in an order we don't have full control over.
* For example the Rain Gauge can wake up the ECU the same as daylight... the systems need each other, but if both are static "who spawns first?"
* Here all "interrupts" are packaged in one ugly static thing so the rest of the system can spawn on an as needed basis.
* Created: 12/27/2019 7:18:16 PM
* Author: Tristan Callahan
*/
#ifndef INTERRUPT_H_
#define INTERRUPT_H_
#include "Arduino.h"
#include "Clock.h"
#include <avr/sleep.h>
namespace Interrupts
{
//sleepMode
static boolean isItBedtime;
//raingauge
static long rainGaugeTicks = 0;
volatile static int rainGaugeSwitchPinState = HIGH;
volatile static int rainGaugeValvePinState = LOW;
static const int rainGaugeValve = 13;
static const int rainGaugeSwitch = 2;
static AutoFarm::TimeStamp rainGaugeValveTimeOpened;
//lightsensor
static const int lightSensorPin = 3;
volatile static boolean isItDaylight = true;
class Interrupt
{
private:
public:
static long getRainInInches()
{
long temp = rainGaugeTicks * .5; //some arbitrary number. Needs calibration with final bucket switch float switch thingy.
rainGaugeTicks = 0;
return temp;
}
static void setRainGaugeValvePinState(int s)
{
if (s == HIGH || s == LOW)
{
rainGaugeValvePinState = s;
}
}
static void setRainGaugeValveOpenTime()
{
rainGaugeValveTimeOpened = AutoFarm::Clock::currentTimeStamp();
}
static AutoFarm::TimeStamp getRainGaugeValveOpenTime()
{
return rainGaugeValveTimeOpened;
}
static int getRainGaugePinState()
{
return rainGaugeSwitchPinState;
}
static int getRainGaugeValveState ()
{
return rainGaugeValvePinState;
}
static bool isItTimeToSleep()
{
return isItBedtime;
}
static void goDark()
{
isItDaylight = false;
isItBedtime = true;
}
static void rainGaugeISR()
{
detachInterrupt(digitalPinToInterrupt(rainGaugeSwitch));
rainGaugeSwitchPinState = HIGH;
}
static void rainGaugeTickProcessed()
{
rainGaugeTicks = rainGaugeTicks + 1;
rainGaugeSwitchPinState = LOW;
attachInterrupt(digitalPinToInterrupt(rainGaugeSwitch), rainGaugeISR, RISING);
}
static void rainTickWakeUp()
{
sleep_disable();
detachInterrupt(digitalPinToInterrupt(rainGaugeSwitch));
detachInterrupt(digitalPinToInterrupt(lightSensorPin));
rainGaugeISR();
//need to handle waking up the light sensor to go dark again but what if it is raining at night.
attachInterrupt(digitalPinToInterrupt( rainGaugeSwitch ), rainGaugeISR, RISING);
attachInterrupt(digitalPinToInterrupt(lightSensorPin), goDark, LOW);
};
static void wakeUp()
{
sleep_disable();
detachInterrupt(digitalPinToInterrupt(rainGaugeSwitch));
detachInterrupt(digitalPinToInterrupt(lightSensorPin));
isItBedtime = false;
isItDaylight = true;
attachInterrupt(digitalPinToInterrupt( rainGaugeSwitch ), rainGaugeISR, RISING);
attachInterrupt(digitalPinToInterrupt(lightSensorPin), goDark, FALLING);
//resume normal operation
};
static void goToSleep()
{
detachInterrupt(digitalPinToInterrupt(rainGaugeSwitch));
detachInterrupt(digitalPinToInterrupt(lightSensorPin));
sleep_enable();
attachInterrupt(digitalPinToInterrupt( rainGaugeSwitch ), rainTickWakeUp, RISING);
attachInterrupt(digitalPinToInterrupt(lightSensorPin), wakeUp, RISING);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_cpu();
};
};
}
#endif /* INTERRUPT_H_ */