Skip to content

Commit 0b2e280

Browse files
committed
Added screen saver to flora_II,cleaned up mess from servo rewrite
That pretty much says it all. Moved the servo stuff to the LC_RCServo library. Wrote in the screen saver with the tap sensor stuff in the Flora_II
1 parent 583a8dd commit 0b2e280

File tree

13 files changed

+265
-536
lines changed

13 files changed

+265
-536
lines changed

flora_II/UI.cpp

Lines changed: 115 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@
66
#include "parameters.h"
77
#include <liveText.h>
88
#include <offscreen.h>
9+
#include "sleepTools.h"
10+
11+
12+
13+
#define OLED_CS 10
14+
#define OLED_RST 6
15+
#define OLED_SDCS 4
16+
#define TAP_PIN 15
17+
#define TAP_SUMTIME 20
18+
#define TAP_NUMDATA 3
19+
#define TAP_VAL 10
20+
#define AWAKE_MS 8000
21+
#define DIMMING_MS 3000
922

10-
#define OLED_CS 10
11-
#define OLED_RST 6
12-
#define OLED_SDCS 4
1323

1424
UI ourDisplay;
1525
blinker idleLight(13,80,2000);
@@ -168,74 +178,81 @@ void logInd::logging(bool onOff) {
168178

169179

170180
UI::UI(void)
171-
: timeObj(500) {
181+
: timeObj(250) {
172182

173183
mHaveScreen = false;
174184
mColorMap = NULL;
185+
mTSensor = NULL;
175186
}
176187

177188

178-
UI::~UI(void) { if (mColorMap) delete(mColorMap); }
189+
UI::~UI(void) {
190+
if (mColorMap) delete(mColorMap);
191+
if (mTSensor) delete(mTSensor);
192+
}
179193

180194

181195
// We can't tell weather we have a screen or not. So we'll look for the SD drive.
182196
void UI::begin(void) {
183197

184-
hookup();
185-
mHaveScreen = SD.begin(OLED_SDCS);
186-
dataLog::begin(mHaveScreen); // Hardware's been checked. Fire the logger up.
187-
if (mHaveScreen) {
188-
mHaveScreen = mHaveScreen && initScreen(ADAFRUIT_684,OLED_CS,OLED_RST,0);
189-
if (mHaveScreen) {
190-
191-
mLastMoist = -1;
192-
mLastLimit = -1;
193-
mLastState = weAre;
198+
hookup();
199+
mHaveScreen = SD.begin(OLED_SDCS);
200+
dataLog::begin(mHaveScreen); // Hardware's been checked. Fire the logger up.
201+
if (mHaveScreen) {
202+
mHaveScreen = initScreen(ADAFRUIT_684,OLED_CS,OLED_RST,0);
203+
if (mHaveScreen) {
204+
mTSensor = new tapSensor(TAP_PIN,TAP_SUMTIME,TAP_NUMDATA);
205+
if (mTSensor) {
206+
mTSensor->begin();
207+
ourSleepMgr.setupSleep(AWAKE_MS,DIMMING_MS);
208+
}
209+
mLastMoist = -1;
210+
mLastLimit = -1;
211+
mLastState = weAre;
194212

195-
screen->fillScreen(&black);
196-
//screen->drawRect(0,0,96,screen->height(),&white);
197-
mColorMap = new colorMultiMap();
198-
mWetColor.setColor(LC_BLUE);
199-
mWetColor.blend(&green,40);
200-
mDryColor.setColor(LC_YELLOW);
201-
mDryColor.blend(&red,40);
202-
mDryColor.blend(&white,40);
213+
screen->fillScreen(&black);
214+
mColorMap = new colorMultiMap();
215+
mWetColor.setColor(LC_BLUE);
216+
mWetColor.blend(&green,40);
217+
mDryColor.setColor(LC_YELLOW);
218+
mDryColor.blend(&red,40);
219+
mDryColor.blend(&white,40);
203220

204-
int yPos = 0;
221+
int yPos = 0;
205222

206-
mMoisture = new percView(5,yPos);
207-
mMoisture->setPercent(mLastMoist);
208-
viewList.addObj(mMoisture);
223+
mMoisture = new percView(5,yPos);
224+
mMoisture->setPercent(mLastMoist);
225+
viewList.addObj(mMoisture);
209226

210-
mLimit = new percView(53,yPos);
211-
mLimit->setPercent(mLastLimit);
212-
viewList.addObj(mLimit);
227+
mLimit = new percView(53,yPos);
228+
mLimit->setPercent(mLastLimit);
229+
viewList.addObj(mLimit);
213230

214-
mSlash = new label(41,yPos,96,16,"/",2);
215-
mSlash->setColors(&white);
216-
viewList.addObj(mSlash);
217-
218-
yPos = yPos + 20;
219-
mKey = new label(9,yPos,96,16,"Reading/Limit",1);
220-
mKey->setColors(&white);
221-
viewList.addObj(mKey);
222-
223-
mLoggingInd = new logInd(0,yPos+10);
224-
viewList.addObj(mLoggingInd);
225-
mLoggingInd->logging(false);
226-
227-
yPos = yPos + 20; //"100/100"
228-
mState = new stateView(0,yPos);
229-
mState->setState(mLastState);
230-
viewList.addObj(mState);
231-
232-
start();
233-
}
234-
}
235-
if (!mHaveScreen) {
236-
pinMode(13, OUTPUT);
237-
idleLight.setOnOff(true); // Start up our running light. Its all we got.
238-
}
231+
mSlash = new label(41,yPos,96,16,"/",2);
232+
mSlash->setColors(&white);
233+
viewList.addObj(mSlash);
234+
235+
yPos = yPos + 20;
236+
mKey = new label(9,yPos,96,16,"Reading/Limit",1);
237+
mKey->setColors(&white);
238+
viewList.addObj(mKey);
239+
240+
mLoggingInd = new logInd(0,yPos+10);
241+
viewList.addObj(mLoggingInd);
242+
mLoggingInd->logging(false);
243+
244+
yPos = yPos + 20; //"100/100"
245+
mState = new stateView(0,yPos);
246+
mState->setState(mLastState);
247+
viewList.addObj(mState);
248+
249+
start();
250+
}
251+
}
252+
if (!mHaveScreen) {
253+
pinMode(13, OUTPUT);
254+
idleLight.setOnOff(true); // Start up our running light. Its all we got.
255+
}
239256
}
240257

241258

@@ -282,31 +299,47 @@ void UI::idle(void) {
282299

283300
bool refresh;
284301
colorObj aColor;
285-
286-
if (mHaveScreen && ding()) {
287-
if (weAre!=sitting) {
288-
mState->setNeedRefresh();
289-
}
290-
refresh = false;
291-
if (ourParamObj.getDryLimit()!=mLastLimit) {
292-
mLimit->setPercent(ourParamObj.getDryLimit());
293-
mLastLimit = ourParamObj.getDryLimit();
294-
refresh = true;
295-
}
296-
if (moisture!=mLastMoist||refresh) {
297-
setColorMap(ourParamObj.getDryLimit());
298-
aColor = mColorMap->Map(moisture);
299-
mMoisture->setColors(&aColor,&black);
300-
mMoisture->setPercent(moisture);
301-
mLastMoist = moisture;
302-
}
303-
if (weAre!=mLastState) {
304-
mState->setState(weAre);
305-
mLastState = weAre;
306-
}
307-
if (isLogging()==mLoggingInd->holding) { // If they match,
308-
mLoggingInd->logging(isLogging()); // fix it.
309-
} // Otherwise, leave it alone!
310-
start();
311-
}
302+
303+
if (mTSensor) { // If we have a tap sensor..
304+
if (mTSensor->getTapVal()>=TAP_VAL) { // If we sense a tap..
305+
ourSleepMgr.wakeup(); // We need to wake up.
306+
}
307+
}
308+
if (mHaveScreen && ding()) { // If we have a screen and its time to check stuff..
309+
if (ourSleepMgr.checkSleep()!=percBlack) { // If we need to change the dimming value of the display..
310+
percBlack = ourSleepMgr.checkSleep(); // Set the new diming value.
311+
mMoisture->setNeedRefresh(); // This is SUCH the sad hack. We need to redraw everthing.
312+
mLimit->setNeedRefresh(); // This is first time coding a screen saver and there is
313+
mSlash->setNeedRefresh(); // no "draw all" method. So we hadrcode for now.
314+
mKey->setNeedRefresh(); //
315+
mLoggingInd->setNeedRefresh(); //
316+
mState->setNeedRefresh(); //
317+
}
318+
if (percBlack!=100) { // If the screen is not off..
319+
if (weAre!=sitting) { // If we are not sitting.. (IE Watering or Soaking)
320+
mState->setNeedRefresh(); // We need to draw the state view, showing the progress bar.
321+
}
322+
refresh = false; // Default to no refresh on these next guys.
323+
if (ourParamObj.getDryLimit()!=mLastLimit) { // If the user changed the dry limit while we weren't looking..
324+
mLimit->setPercent(ourParamObj.getDryLimit()); // Set the display limit to match the new value.
325+
mLastLimit = ourParamObj.getDryLimit(); // Save the new value for later checking.
326+
refresh = true; // And we're going to do some refreshing of the screen.
327+
}
328+
if (moisture!=mLastMoist||refresh) { // If the moisture has changed, or we need to refresh anyway..
329+
setColorMap(ourParamObj.getDryLimit()); // Reset the color map to reflect the new dry limit.
330+
aColor = mColorMap->Map(moisture); // Map our a color for the new moisture value.
331+
mMoisture->setColors(&aColor,&black); // Set up the text colors for the drawing.
332+
mMoisture->setPercent(moisture); // Draw the new percentage value.
333+
mLastMoist = moisture; // Save off the new percentage value for chekcing later on.
334+
}
335+
}
336+
if (weAre!=mLastState) { // If we have changed state..
337+
mState->setState(weAre); // Change the state text.
338+
mLastState = weAre; // Save off the new state for checking later on.
339+
}
340+
if (isLogging()==mLoggingInd->holding) { // If they match.. (Sadly don't know what I'm doing here.)
341+
mLoggingInd->logging(isLogging()); // fix it.
342+
} // Otherwise, leave it alone!
343+
start();
344+
}
312345
};

flora_II/UI.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <liveText.h>
99
#include <colorObj.h>
1010
#include <bitmap.h>
11+
#include "tapSensor.h"
1112

1213
class percView : public label {
1314

@@ -61,19 +62,20 @@ class UI : public dataLog,
6162
virtual void idle(void);
6263
void sensorDeath(void);
6364

64-
bool mHaveScreen;
65-
percView* mLimit;
66-
percView* mMoisture;
67-
label* mSlash;
68-
label* mKey;
69-
stateView* mState;
70-
logInd* mLoggingInd;
71-
float mLastMoist;
72-
int mLastLimit;
73-
weDo mLastState;
74-
colorObj mWetColor;
75-
colorObj mDryColor;
76-
colorMultiMap* mColorMap;
65+
bool mHaveScreen;
66+
percView* mLimit;
67+
percView* mMoisture;
68+
label* mSlash;
69+
label* mKey;
70+
stateView* mState;
71+
logInd* mLoggingInd;
72+
float mLastMoist;
73+
int mLastLimit;
74+
weDo mLastState;
75+
colorObj mWetColor;
76+
colorObj mDryColor;
77+
colorMultiMap* mColorMap;
78+
tapSensor* mTSensor;
7779
};
7880

7981

flora_II/sleepTools.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "sleepTools.h"
2+
3+
4+
sleepMgr::sleepMgr(void) {
5+
6+
awakeTimer = NULL;
7+
rampDownTimer = NULL;
8+
}
9+
10+
11+
sleepMgr::~sleepMgr(void) {
12+
13+
if (awakeTimer) {
14+
delete(awakeTimer);
15+
awakeTimer = NULL;
16+
}
17+
if (rampDownTimer) {
18+
delete(rampDownTimer);
19+
rampDownTimer = NULL;
20+
}
21+
}
22+
23+
// If we want to run a sleep function. This is where you set it up. How long in Ms to stay
24+
// awake, how long in Ms to dim down the screen as a warning.
25+
void sleepMgr::setupSleep(int awakeMS,int rampDownMs) {
26+
27+
shutOffSleep(); // Just in case its already running..
28+
awakeTimer = new timeObj(awakeMS); // Create a timer for awake time.
29+
rampDownTimer = new timeObj(rampDownMs); // Create a timer for going dark.
30+
wakeup(); // Make sure we start it up.
31+
}
32+
33+
34+
// No longer want to use the sleep function? This will shut it down.
35+
void sleepMgr::shutOffSleep(void) {
36+
37+
if (awakeTimer) { // if we have an awake timer..
38+
delete(awakeTimer); // delete the awake timer..
39+
awakeTimer = NULL; // Set it to NULL as a flag.
40+
}
41+
if (rampDownTimer) { // if we have a ramp down timer..
42+
delete(rampDownTimer); // delete the ramp down timer..
43+
rampDownTimer = NULL; // Set it to NULL as a flag.
44+
}
45+
sleepState = awake; // From now on, we are awake.
46+
}
47+
48+
49+
// Meant to be called repeatedly. This keeps track of the state of the sleeping function
50+
// and returns the fraction of "Awake" your screen is.
51+
float sleepMgr::checkSleep(void) {
52+
53+
if (awakeTimer && rampDownTimer) { // If we have both timers..
54+
switch (sleepState) { // Check our sleep sate..
55+
case awake : // If we are awake..
56+
if (awakeTimer->ding()) { // If the awake timer has expired..
57+
rampDownTimer->start(); // We begin the ramp down period
58+
sleepState = rampDown; // And note we are ramping down.
59+
return 100 * (1-rampDownTimer->getFraction()); // rampDown fraction left.
60+
}
61+
return 0; // We are still awake, still on.
62+
case rampDown : // Check our ramp down state..
63+
if (rampDownTimer->ding()) { // If the ramp down timer has expired..
64+
sleepState = sleeping; // We are in the sleeping state.
65+
return 100; // Sleeping would be zero.
66+
}
67+
return 100 * (1-rampDownTimer->getFraction()); // rampDown fraction left.
68+
case sleeping : return 100; // Sleeping would be zero.
69+
}
70+
} else { // Else we DON'T have both timers..
71+
sleepState = awake; // In this case, we are awake.
72+
return 0; // Therefor return 1.
73+
}
74+
return 0; // No way we can get here but.. 1 is default.
75+
}
76+
77+
78+
// When something happens that should wake up the machine, call this to wake it up.
79+
void sleepMgr::wakeup(void) {
80+
81+
if (awakeTimer && rampDownTimer) { // If we have both timers..
82+
awakeTimer->start(); // Start up the awake timer.
83+
}
84+
sleepState = awake; // In any case, we are awake now.
85+
}
86+
87+
sleepMgr ourSleepMgr;

flora_II/sleepTools.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef sleepTools_h
2+
#define sleepTools_h
3+
4+
#include "Arduino.h"
5+
#include "timeObj.h"
6+
7+
8+
9+
class sleepMgr {
10+
11+
public:
12+
enum sleepStates { awake, rampDown, sleeping };
13+
14+
sleepMgr(void);
15+
virtual ~sleepMgr(void);
16+
17+
void setupSleep(int awakeMS,int rampDownMs);
18+
void shutOffSleep(void);
19+
float checkSleep(void);
20+
void wakeup(void);
21+
22+
protected:
23+
timeObj* awakeTimer;
24+
timeObj* rampDownTimer;
25+
sleepStates sleepState;
26+
};
27+
28+
extern sleepMgr ourSleepMgr;
29+
30+
31+
32+
#endif

0 commit comments

Comments
 (0)