-
Notifications
You must be signed in to change notification settings - Fork 0
/
HalosController.ino
109 lines (84 loc) · 2.14 KB
/
HalosController.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
#include <EEPROM.h>
#include <Adafruit_NeoPixel.h>
#include <TimerOne.h>
#include "Patterns.h"
// Address in EEPROM where to store the selected pattern index
#define PATTERN_IDX_EEPROM_ADDR 67
// Variable that holds the index of the currently selected pattern
volatile uint8_t patternIdx = 0;
// Variable to turn frame on and off (off = paused)
volatile uint8_t isPaused = 0;
// Variable to remember last button interrupt time
volatile unsigned long lastButtonIntr = 0;
// Object for the fan frame, which is compatible with NeoPixel
Adafruit_NeoPixel frame;
// Arduino setup and loop functions
void setup() {
Serial.begin(115200);
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
frame = Adafruit_NeoPixel(30, 6, NEO_GRB + NEO_KHZ800);
frame.begin();
frame.show();
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT);
attachInterrupt(digitalPinToInterrupt(2), buttonIntr, CHANGE);
Timer1.initialize(2000000L);
patternIdx = EEPROM.read(PATTERN_IDX_EEPROM_ADDR);
if (patternIdx >= numPatterns) {
patternIdx = 0;
}
}
void loop() {
if (!isPaused) {
if (isSleeping()) {
breathe();
} else {
patternFunctions[patternIdx]();
}
}
}
// Interrupt service routines for button and timer
void buttonIntr() {
delay(2);
unsigned long t = millis() - lastButtonIntr;
if (isButtonDown()) {
if (t > 2) {
Timer1.start();
Timer1.attachInterrupt(timerIntr);
}
} else {
if ((t > 2) && (t < 2000)) {
buttonPress();
}
}
lastButtonIntr = millis();
}
void timerIntr() {
unsigned long t = millis() - lastButtonIntr;
if (t > 2) {
Timer1.stop();
Timer1.detachInterrupt();
if (isButtonDown()) {
buttonLongPress();
}
}
}
void buttonPress() {
patternIdx = (patternIdx+1) % numPatterns;
EEPROM.update(PATTERN_IDX_EEPROM_ADDR, patternIdx);
}
void buttonLongPress() {
isPaused ^= 1;
if (isPaused) {
clear();
}
}
// Helper functions to read pins
uint8_t isButtonDown() {
return digitalRead(2) == 0;
}
uint8_t isSleeping() {
return digitalRead(3) == 0;
}