276 lines
11 KiB
C++
276 lines
11 KiB
C++
#include <unistd.h>
|
|
#include "core_simulation.h"
|
|
#include "app/include/Terrarium.h"
|
|
#include "app/include/TerrariumParameter.h"
|
|
#include "app/include/Menu.h"
|
|
#include "app/include/Tortue.h"
|
|
#include "include/I2CDevices/Keyboard.h"
|
|
|
|
|
|
Terrarium *terrarium = new Terrarium(1);
|
|
TerrariumParameter *temperature = new TerrariumParameter(25,
|
|
20);
|
|
TerrariumParameter *pressure = new TerrariumParameter(3000,
|
|
2990);
|
|
|
|
Menu *menu = new Menu();
|
|
|
|
list<Tortue> * tortues = new list<Tortue>;
|
|
list<Tortue>::iterator iteratorT;
|
|
|
|
string message = "";
|
|
string input = "";
|
|
|
|
|
|
|
|
|
|
// la fonction d'initialisation d'arduino
|
|
void Board::setup(){
|
|
// on configure la vitesse de la liaison
|
|
Serial.begin(9600);
|
|
// on fixe les pin en entree et en sorite en fonction des capteurs/actionneurs mis sur la carte
|
|
pinMode(1, INPUT);
|
|
pinMode(4, INPUT);
|
|
pinMode(5, INPUT);
|
|
pinMode(3, INPUT);
|
|
pinMode(6, INPUT);
|
|
pinMode(7, INPUT);
|
|
|
|
pinMode(0, OUTPUT);
|
|
pinMode(2, OUTPUT);
|
|
|
|
digitalWrite(0,LOW);
|
|
//pinMode(6, OUTPUT);
|
|
tortues->push_back(*(new Tortue("Marvin","03/09/1996","male")));
|
|
tortues->push_back(*(new Tortue("Clara","03/09/2008", "female")));
|
|
tortues->push_back(*(new Tortue("Camille","03/09/2017", "non binary")));
|
|
tortues->push_back(*(new Tortue("Clarisse","03/05/1843", "danseuse")));
|
|
|
|
}
|
|
|
|
// la boucle de controle arduino
|
|
void Board::loop() {
|
|
char buf[150];
|
|
char inputBuf[150];
|
|
static int cpt = 0;
|
|
static int bascule = 0;
|
|
int i = 0;
|
|
std::string toDisplay;
|
|
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
TemperatureManagement();
|
|
PressureManagement();
|
|
cout << tortues->back() << endl;
|
|
|
|
if (menu->getId() == 0 && menu->displayDefault(temperature->getValue(),
|
|
pressure->getValue(),
|
|
terrarium->getRadState(),
|
|
terrarium->getTapState(),
|
|
0).c_str() != NULL) {
|
|
message = menu->displayDefault(temperature->getValue(),
|
|
pressure->getValue(),
|
|
terrarium->getRadState(),
|
|
terrarium->getTapState(),
|
|
Tortue::getNbTortue());
|
|
|
|
} else if (menu->getMessage().c_str() != NULL){
|
|
message = menu->getMessage();
|
|
}
|
|
|
|
int a3 = analogRead(3);
|
|
int a4 = analogRead(4);
|
|
int a6 = analogRead(6);
|
|
int a7 = analogRead(7);
|
|
if ( a3 == HIGH && (terrarium->getPrevState() != a3)) {
|
|
terrarium->setPrevState(HIGH);
|
|
menu->previousChoice();
|
|
} else if (a3 == LOW && (terrarium->getPrevState() != a3)) {
|
|
terrarium->setPrevState(LOW);
|
|
}
|
|
else if (a4 == HIGH && (terrarium->getNextState() != a4)) {
|
|
terrarium->setNextState(HIGH);
|
|
menu->nextChoice();
|
|
} else if (a4 == LOW && (terrarium->getNextState() != a4)) {
|
|
terrarium->setNextState(LOW);
|
|
}
|
|
else if (a6 == HIGH && (terrarium->getOkState() != a6)) {
|
|
terrarium->setOkState(HIGH);
|
|
cout << "OK" << endl;
|
|
menu->menuNext();
|
|
} else if (a6 == LOW && (terrarium->getOkState() != a6)) {
|
|
terrarium->setOkState(LOW);
|
|
cout << "NOK" << endl;
|
|
}
|
|
else if (a7 == HIGH && (terrarium->getBackState() != a7)) {
|
|
terrarium->setBackState(HIGH);
|
|
menu->menuBack();
|
|
} else if (a7 == LOW && (terrarium->getBackState() != a7)) {
|
|
terrarium->setBackState(LOW);
|
|
}
|
|
|
|
// cout << buf << endl;
|
|
//sprintf(buf,"%s",menu.getMessage().c_str());
|
|
|
|
//sprintf(buf,"%f",temperature.getValue());
|
|
|
|
if (menu->getId() == 2){
|
|
iteratorT = tortues->begin();
|
|
for (int j = 0; j < menu->getChoice(); ++j) {
|
|
iteratorT++;
|
|
}
|
|
//cout << iteratorT->getInfo() << endl;
|
|
message += iteratorT->getInfo();
|
|
}
|
|
if (cpt % 2 == 0) {
|
|
strcpy(buf, message.c_str());
|
|
if (buf != NULL) bus.write(1, buf, 150);
|
|
}
|
|
|
|
if (menu->getId() == 5 || menu->getId() == 11
|
|
|| menu->getId() == 12 || menu->getId() == 6
|
|
|| menu->getId() == 7 || menu->getId() == 8
|
|
|| menu->getId() == 9 || menu->getId() == 10){
|
|
Keyboard::saisie = true ;
|
|
sleep(4);
|
|
if ( (&bus!=NULL)&&!(bus.isEmptyRegister(2))) {
|
|
bus.requestFrom(2, inputBuf, 150);
|
|
if (inputBuf[0] != '\0') {
|
|
input = "";
|
|
for (int j = 0; inputBuf[j] != '\0'; ++j) {
|
|
input += inputBuf[j];
|
|
}
|
|
|
|
switch (menu->getId()) {
|
|
case 5 : {
|
|
Tortue *newTortue = new Tortue();
|
|
newTortue->setNom(input);
|
|
tortues->push_back(*newTortue);
|
|
menu->menuNext();
|
|
break;
|
|
}
|
|
case 11 :
|
|
try {
|
|
tortues->back().setNaissance(input);
|
|
menu->menuNext();
|
|
} catch (std::invalid_argument excep) {
|
|
cout << "Dates are not integers" << endl;
|
|
} catch (Tortue::ExceptionDate excep) {
|
|
if (excep.id == 1) cout << "Not yet born ? Time travelling turtles are not allowed in this terrarium";
|
|
else if (excep.id == 2) cout << "The format of the date is not correct";
|
|
else if (excep.id == 3) cout << "It wasn't really born before 1900... You were scammed";
|
|
else if (excep.id == 4) cout << "After December you start over at January";
|
|
else if (excep.id == 5) cout << "Months don't usually last more than 31 days !";
|
|
}
|
|
|
|
break;
|
|
case 12 :
|
|
tortues->back().setSexe(input);
|
|
menu->menuNext();
|
|
break;
|
|
case 6: {
|
|
int length = tortues->size();
|
|
iteratorT = tortues->begin();
|
|
for (int j = 0; j < length; ++j) {
|
|
if (iteratorT->getNom() == input) {
|
|
tortues->erase(iteratorT);
|
|
Tortue::delete1();
|
|
menu->menuNext();
|
|
}
|
|
++iteratorT;
|
|
}
|
|
if (iteratorT == tortues->end()) {
|
|
cout << "not in list" << endl;
|
|
menu->menuBack();
|
|
}
|
|
break;
|
|
}
|
|
case 7:
|
|
try {
|
|
temperature->setThresholdHigh(stoi(input));
|
|
menu->menuBack();
|
|
}
|
|
catch (TerrariumParameter::ThresholdExcep excep) {
|
|
if (excep.id == 1) cout << "value is under the minimum value";
|
|
}
|
|
catch (std::invalid_argument excep) {
|
|
cout << "Not a value" << endl;
|
|
}
|
|
break;
|
|
case 8 :
|
|
try {
|
|
temperature->setThresholdLow(stoi(input));
|
|
menu->menuBack();
|
|
}catch (TerrariumParameter::ThresholdExcep excep) {
|
|
if (excep.id == 2) cout << "value is over the maximum value";
|
|
}
|
|
catch (std::invalid_argument excep) {
|
|
cout << "Not a value" << endl;
|
|
}
|
|
break;
|
|
case 9 :
|
|
try {
|
|
pressure->setThresholdHigh(stoi(input));
|
|
menu->menuBack();
|
|
}catch (TerrariumParameter::ThresholdExcep excep) {
|
|
if (excep.id == 1) cout << "value is under the minimum value";
|
|
}
|
|
catch (std::invalid_argument excep) {
|
|
cout << "Not a value" << endl;
|
|
}
|
|
break;
|
|
case 10 :
|
|
try {
|
|
pressure->setThresholdLow(stoi(input));
|
|
menu->menuBack();
|
|
}catch (TerrariumParameter::ThresholdExcep excep) {
|
|
if (excep.id == 2) cout << "value is over the maximum value";
|
|
}
|
|
catch (std::invalid_argument excep) {
|
|
cout << "Not a value" << endl;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
cpt++;
|
|
sleep(1);
|
|
|
|
|
|
}
|
|
}
|
|
|
|
void Board::TemperatureManagement() {
|
|
char buf[100];
|
|
temperature->setValue(analogRead(1));
|
|
//sprintf(buf, "temperature %f", temperature.getValue());
|
|
//Serial.println(buf);
|
|
|
|
if ((temperature->is2Low()) && (terrarium->getRadState() == LOW)) {
|
|
digitalWrite(0, HIGH);
|
|
terrarium->setRadState(HIGH);
|
|
} else if ((temperature->is2High()) && (terrarium->getRadState() == HIGH)) {
|
|
digitalWrite(0, LOW);
|
|
terrarium->setRadState(LOW);
|
|
}
|
|
}
|
|
|
|
void Board::PressureManagement() {
|
|
char buf[100];
|
|
pressure->setValue(analogRead(5));
|
|
//sprintf(buf, "pressure %f", pressure.getValue());
|
|
//Serial.println(buf);
|
|
|
|
if ((pressure->is2Low()) && (terrarium->getTapState() == LOW)) {
|
|
digitalWrite(2, HIGH);
|
|
terrarium->setTapState(HIGH);
|
|
} else if ((pressure->is2High()) && (terrarium->getTapState() == HIGH)) {
|
|
digitalWrite(2, LOW);
|
|
terrarium->setTapState(LOW);
|
|
}
|
|
}
|