-
Notifications
You must be signed in to change notification settings - Fork 100
/
hexbright_factory.ino
executable file
·206 lines (183 loc) · 4.82 KB
/
hexbright_factory.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
Factory firmware for HexBright FLEX
v2.4 Dec 6, 2012
*/
#include <math.h>
#include <Wire.h>
// Settings
#define OVERTEMP 340
// Pin assignments
#define DPIN_RLED_SW 2
#define DPIN_GLED 5
#define DPIN_PWR 8
#define DPIN_DRV_MODE 9
#define DPIN_DRV_EN 10
#define APIN_TEMP 0
#define APIN_CHARGE 3
// Modes
#define MODE_OFF 0
#define MODE_LOW 1
#define MODE_MED 2
#define MODE_HIGH 3
#define MODE_BLINKING 4
#define MODE_BLINKING_PREVIEW 5
// State
byte mode = 0;
unsigned long btnTime = 0;
boolean btnDown = false;
void setup()
{
// We just powered on! That means either we got plugged
// into USB, or the user is pressing the power button.
pinMode(DPIN_PWR, INPUT);
digitalWrite(DPIN_PWR, LOW);
// Initialize GPIO
pinMode(DPIN_RLED_SW, INPUT);
pinMode(DPIN_GLED, OUTPUT);
pinMode(DPIN_DRV_MODE, OUTPUT);
pinMode(DPIN_DRV_EN, OUTPUT);
digitalWrite(DPIN_DRV_MODE, LOW);
digitalWrite(DPIN_DRV_EN, LOW);
// Initialize serial busses
Serial.begin(9600);
Wire.begin();
btnTime = millis();
btnDown = digitalRead(DPIN_RLED_SW);
mode = MODE_OFF;
Serial.println("Powered up!");
}
void loop()
{
static unsigned long lastTempTime;
unsigned long time = millis();
// Check the state of the charge controller
int chargeState = analogRead(APIN_CHARGE);
if (chargeState < 128) // Low - charging
{
digitalWrite(DPIN_GLED, (time&0x0100)?LOW:HIGH);
}
else if (chargeState > 768) // High - charged
{
digitalWrite(DPIN_GLED, HIGH);
}
else // Hi-Z - shutdown
{
digitalWrite(DPIN_GLED, LOW);
}
// Check the temperature sensor
if (time-lastTempTime > 1000)
{
lastTempTime = time;
int temperature = analogRead(APIN_TEMP);
Serial.print("Temp: ");
Serial.println(temperature);
if (temperature > OVERTEMP && mode != MODE_OFF)
{
Serial.println("Overheating!");
for (int i = 0; i < 6; i++)
{
digitalWrite(DPIN_DRV_MODE, LOW);
delay(100);
digitalWrite(DPIN_DRV_MODE, HIGH);
delay(100);
}
digitalWrite(DPIN_DRV_MODE, LOW);
mode = MODE_LOW;
}
}
// Do whatever this mode does
switch (mode)
{
case MODE_BLINKING:
case MODE_BLINKING_PREVIEW:
digitalWrite(DPIN_DRV_EN, (time%300)<75);
break;
}
// Periodically pull down the button's pin, since
// in certain hardware revisions it can float.
pinMode(DPIN_RLED_SW, OUTPUT);
pinMode(DPIN_RLED_SW, INPUT);
// Check for mode changes
byte newMode = mode;
byte newBtnDown = digitalRead(DPIN_RLED_SW);
switch (mode)
{
case MODE_OFF:
if (btnDown && !newBtnDown && (time-btnTime)>20)
newMode = MODE_LOW;
if (btnDown && newBtnDown && (time-btnTime)>500)
newMode = MODE_BLINKING_PREVIEW;
break;
case MODE_LOW:
if (btnDown && !newBtnDown && (time-btnTime)>50)
newMode = MODE_MED;
break;
case MODE_MED:
if (btnDown && !newBtnDown && (time-btnTime)>50)
newMode = MODE_HIGH;
break;
case MODE_HIGH:
if (btnDown && !newBtnDown && (time-btnTime)>50)
newMode = MODE_OFF;
break;
case MODE_BLINKING_PREVIEW:
// This mode exists just to ignore this button release.
if (btnDown && !newBtnDown)
newMode = MODE_BLINKING;
break;
case MODE_BLINKING:
if (btnDown && !newBtnDown && (time-btnTime)>50)
newMode = MODE_OFF;
break;
}
// Do the mode transitions
if (newMode != mode)
{
switch (newMode)
{
case MODE_OFF:
Serial.println("Mode = off");
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, LOW);
digitalWrite(DPIN_DRV_MODE, LOW);
digitalWrite(DPIN_DRV_EN, LOW);
break;
case MODE_LOW:
Serial.println("Mode = low");
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, HIGH);
digitalWrite(DPIN_DRV_MODE, LOW);
analogWrite(DPIN_DRV_EN, 64);
break;
case MODE_MED:
Serial.println("Mode = medium");
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, HIGH);
digitalWrite(DPIN_DRV_MODE, LOW);
analogWrite(DPIN_DRV_EN, 255);
break;
case MODE_HIGH:
Serial.println("Mode = high");
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, HIGH);
digitalWrite(DPIN_DRV_MODE, HIGH);
analogWrite(DPIN_DRV_EN, 255);
break;
case MODE_BLINKING:
case MODE_BLINKING_PREVIEW:
Serial.println("Mode = blinking");
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, HIGH);
digitalWrite(DPIN_DRV_MODE, HIGH);
break;
}
mode = newMode;
}
// Remember button state so we can detect transitions
if (newBtnDown != btnDown)
{
btnTime = time;
btnDown = newBtnDown;
delay(50);
}
}