-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwaitForCatArrival.cpp
51 lines (46 loc) · 1.78 KB
/
waitForCatArrival.cpp
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
#include "pt.h"
#include "waitForCatArrival.h"
#include "hardwareConsts.h"
#include "deadSimpleTimer.h"
#include "log4arduino.h"
#include "inMemoryStore.h"
#include "HX711.h"
#include "loadCells.h"
#include "foodMotor.h"
#include "CheapStepper.h"
#include "putFoodIntoBowl.h"
#define CAT_PRESENT_MINIMUM_READING 1000
int WaitForCatArrival::protothread(struct pt *pt, bool *catArrived, double catFoodStartAmount)
{
static struct pt PT_GetCatWeight;
static struct pt PT_GetFoodAmount;
static DeadSimpleTimer::deadTimer catArrivalTimeout;
static double *catWeight = new double;
static bool timerExpired = false;
static double *currentFoodAmount = new double;
PT_BEGIN(pt);
*currentFoodAmount = catFoodStartAmount;
timerExpired = false;
LOG("Waiting for cat arrival %ld ms. Cat food start amount: %f", WaitForCatArrival::catArrivalTimeoutInMs, catFoodStartAmount);
DeadSimpleTimer::setMs(&catArrivalTimeout, WaitForCatArrival::catArrivalTimeoutInMs);
PT_SPAWN(pt, &PT_GetCatWeight, LoadCells::protothreadGetCatWeight(&PT_GetCatWeight, catWeight));
while (*catWeight < CAT_PRESENT_MINIMUM_READING &&
fabs(catFoodStartAmount - *currentFoodAmount) < PutFoodIntoBowl::CatArrivedFoodAmountDiff &&
!timerExpired)
{
timerExpired = DeadSimpleTimer::expired(&catArrivalTimeout);
PT_SPAWN(pt, &PT_GetCatWeight, LoadCells::protothreadGetCatWeight(&PT_GetCatWeight, catWeight));
PT_SPAWN(pt, &PT_GetFoodAmount, LoadCells::protothreadGetFoodAmount(&PT_GetFoodAmount, currentFoodAmount, 14, 3, true));
}
if (timerExpired)
{
LOG("Timer expired. Cat didn't arrive...");
*catArrived = false;
}
else
{
LOG("Cat arrived! Bon Appetit :)");
*catArrived = true;
}
PT_END(pt);
}