-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
173 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,71 @@ | ||
class Gear { | ||
// constants | ||
|
||
// threshold over which detected motion can cause an action trigger | ||
float TRIGGER_THRESHOLD = 2.5; | ||
|
||
// duration while which another trigger cannot be sent | ||
int TRIGGER_DURATION = 1000; | ||
|
||
|
||
// vars | ||
int type; // 1: small, 2: medium, 3: big | ||
int id = -1; | ||
int triggerEndTime = 0; | ||
|
||
int viewLeftEdge; | ||
int viewWidth; | ||
|
||
Rectangle viewZone; | ||
PImage view; | ||
|
||
float[] flowValues = {0, 0}; | ||
|
||
boolean isTriggered = false; | ||
|
||
|
||
// constructor | ||
Gear(int _id) { | ||
id = _id; | ||
} | ||
|
||
void initView(int viewHeight) { | ||
view = new PImage(viewWidth, viewHeight); | ||
|
||
|
||
// methods definitions | ||
void init(float[] edges, int screenWidth, int screenHeight) { | ||
// init view zone | ||
viewZone = new Rectangle(); | ||
viewZone.x = floor(edges[0] * screenWidth); | ||
viewZone.w = floor(screenWidth * (edges[1] - edges[0])); | ||
viewZone.h = screenHeight; | ||
|
||
// init view | ||
view = new PImage(viewZone.w, viewZone.h); | ||
} | ||
|
||
void analyzeIfTriggered() { | ||
// limit number of triggers | ||
if (isTriggered) { | ||
return; | ||
} | ||
|
||
if (abs(getAverageFlow()) > TRIGGER_THRESHOLD) { | ||
isTriggered = true; | ||
triggerEndTime = millis() + TRIGGER_DURATION; | ||
|
||
println("gear " + id + " triggered (" + getAverageFlow() + ")"); | ||
// TODO: send to arduino (https://github.com/jansensan/kinetic-cabinet/issues/26) | ||
} | ||
} | ||
|
||
float getAverageFlow() { | ||
return (flowValues[0] + flowValues[1]) * 0.5; | ||
} | ||
|
||
void updateFlow(float newFlow) { | ||
flowValues[0] = flowValues[1]; | ||
flowValues[1] = newFlow; | ||
} | ||
|
||
void update() { | ||
if (isTriggered && millis() >= triggerEndTime) { | ||
isTriggered = false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.